Say something continuing link to previous part https://rpubs.com/nurfnick/715604

Now I can knit

model = lm(W ~ PTS, data = data)
summary(model)
## 
## Call:
## lm(formula = W ~ PTS, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -0.8066 -0.2762  0.2136  0.2136  0.2339 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -0.25414    0.17004  -1.495    0.146    
## PTS          0.51013    0.04319  11.811 2.16e-12 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.3183 on 28 degrees of freedom
##   (1 observation deleted due to missingness)
## Multiple R-squared:  0.8328, Adjusted R-squared:  0.8269 
## F-statistic: 139.5 on 1 and 28 DF,  p-value: 2.163e-12
plot(model)

predict(model)
##         1         2         3         4         5         6         7         8 
## 2.8066298 2.8066298 2.8066298 2.8066298 2.2965009 2.2965009 2.2965009 1.7863720 
##         9        10        11        12        13        14        15        16 
## 1.7863720 1.7863720 1.7863720 1.7863720 1.7863720 1.7863720 1.7863720 1.7863720 
##        17        18        19        20        21        22        23        24 
## 1.7863720 1.7863720 1.2762431 1.2762431 1.2762431 1.2762431 1.2762431 0.7661142 
##        25        26        27        28        29        30 
## 0.7661142 0.7661142 0.7661142 0.7661142 0.7661142 0.2559853
ylist = c(6,10,11,15)
point <- data.frame(PTS = ylist)
predict(model, point, interval = 'prediction')
##        fit      lwr      upr
## 1 2.806630 2.113392 3.499867
## 2 4.847145 3.981220 5.713071
## 3 5.357274 4.431923 6.282625
## 4 7.397790 6.198372 8.597208
residuals(model)
##          1          2          3          4          5          6          7 
##  0.1933702  0.1933702  0.1933702 -0.8066298 -0.2965009 -0.2965009 -0.2965009 
##          8          9         10         11         12         13         14 
##  0.2136280  0.2136280  0.2136280  0.2136280  0.2136280  0.2136280  0.2136280 
##         15         16         17         18         19         20         21 
##  0.2136280  0.2136280  0.2136280  0.2136280 -0.2762431 -0.2762431 -0.2762431 
##         22         23         24         25         26         27         28 
## -0.2762431 -0.2762431  0.2338858  0.2338858  0.2338858  0.2338858 -0.7661142 
##         29         30 
##  0.2338858 -0.2559853
3-2.806630
## [1] 0.19337
hist(residuals(model))

plot(data$PTS,data$W)
abline(model)

cor(data$PTS,data$W, use = "complete.obs")
## [1] 0.9126024
cor.test(data$PTS,data$W, use = "complete.obs")
## 
##  Pearson's product-moment correlation
## 
## data:  data$PTS and data$W
## t = 11.811, df = 28, p-value = 2.163e-12
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.8228826 0.9579232
## sample estimates:
##       cor 
## 0.9126024
data[which((data$PPO > data$PPOA)),"GP"]
##  [1] 3 3 3 3 3 2 3 3 4 3 4 4 3 3
data[which((data$PPO > data$PPOA)),"MPPThanKills"] = TRUE
data[which(!(data$PPO > data$PPOA)),"MPPThanKills"] = FALSE
data$MPPThanKills
##  [1] FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE  TRUE FALSE  TRUE
## [13]  TRUE  TRUE  TRUE FALSE  TRUE  TRUE FALSE FALSE FALSE  TRUE FALSE  TRUE
## [25]  TRUE  TRUE FALSE FALSE  TRUE FALSE    NA
fit <- lm(W ~ PTS + MPPThanKills, data)
fit
## 
## Call:
## lm(formula = W ~ PTS + MPPThanKills, data = data)
## 
## Coefficients:
##      (Intercept)               PTS  MPPThanKillsTRUE  
##          -0.4629            0.5303            0.2877
summary(fit)
## 
## Call:
## lm(formula = W ~ PTS + MPPThanKills, data = data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.71867 -0.12788  0.05413  0.11466  0.40239 
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)       -0.4629     0.1725  -2.684   0.0123 *  
## PTS                0.5303     0.0398  13.322 2.19e-13 ***
## MPPThanKillsTRUE   0.2877     0.1073   2.681   0.0124 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.288 on 27 degrees of freedom
##   (1 observation deleted due to missingness)
## Multiple R-squared:  0.868,  Adjusted R-squared:  0.8582 
## F-statistic: 88.76 on 2 and 27 DF,  p-value: 1.345e-12
y <- data.frame(PTS = 10, MPPThanKills = TRUE)
predict(fit, y)
##        1 
## 5.127451
plot(data$PTS,data$W)
abline(fit, col = "Red")
## Warning in abline(fit, col = "Red"): only using the first two of 3 regression
## coefficients
abline(model, col = "Blue")

library(ggplot2)

ggplot(data = data, aes(x = PTS, y = W, color = MPPThanKills)) +
  geom_jitter() +
  geom_smooth(method="lm")
## `geom_smooth()` using formula 'y ~ x'
## Warning: Removed 1 rows containing non-finite values (stat_smooth).
## Warning: Removed 1 rows containing missing values (geom_point).