Create an R notebook and include all the code and interpretations/explanations needed to answer the questions below.
Dataset
The dataset you are going to use for this assignment is called Hitters_Fixed, which is a cleaned version of the Hitters dataset. First, you are going to retrieve the Hitters dataset; then, you will clean it to get the Hitters_Fixed dataset. Take the following steps to do so:
Load the “ISLR” package in R. The “ISLR” contains the Hitters dataset.
Remove the missing data from the Hitters dataset and save the results into Hitters_Fixed. Run the following line of code to do so:
library(ISLR)
Hitters <- na.omit(Hitters)
Hitters_Fixed <- na.omit(Hitters)
str(Hitters_Fixed)
## 'data.frame': 263 obs. of 20 variables:
## $ AtBat : int 315 479 496 321 594 185 298 323 401 574 ...
## $ Hits : int 81 130 141 87 169 37 73 81 92 159 ...
## $ HmRun : int 7 18 20 10 4 1 0 6 17 21 ...
## $ Runs : int 24 66 65 39 74 23 24 26 49 107 ...
## $ RBI : int 38 72 78 42 51 8 24 32 66 75 ...
## $ Walks : int 39 76 37 30 35 21 7 8 65 59 ...
## $ Years : int 14 3 11 2 11 2 3 2 13 10 ...
## $ CAtBat : int 3449 1624 5628 396 4408 214 509 341 5206 4631 ...
## $ CHits : int 835 457 1575 101 1133 42 108 86 1332 1300 ...
## $ CHmRun : int 69 63 225 12 19 1 0 6 253 90 ...
## $ CRuns : int 321 224 828 48 501 30 41 32 784 702 ...
## $ CRBI : int 414 266 838 46 336 9 37 34 890 504 ...
## $ CWalks : int 375 263 354 33 194 24 12 8 866 488 ...
## $ League : Factor w/ 2 levels "A","N": 2 1 2 2 1 2 1 2 1 1 ...
## $ Division : Factor w/ 2 levels "E","W": 2 2 1 1 2 1 2 2 1 1 ...
## $ PutOuts : int 632 880 200 805 282 76 121 143 0 238 ...
## $ Assists : int 43 82 11 40 421 127 283 290 0 445 ...
## $ Errors : int 10 14 3 4 25 7 9 19 0 22 ...
## $ Salary : num 475 480 500 91.5 750 ...
## $ NewLeague: Factor w/ 2 levels "A","N": 2 1 2 2 1 1 1 2 1 1 ...
## - attr(*, "na.action")= 'omit' Named int [1:59] 1 16 19 23 31 33 37 39 40 42 ...
## ..- attr(*, "names")= chr [1:59] "-Andy Allanson" "-Billy Beane" "-Bruce Bochte" "-Bob Boone" ...
That is all. Now you can use the Hitters_Fixed dataset to do this assignment!
Note: If you want more info about the variables in the Hitters_Fixed dataset, you can get it using this link:
https://docs.google.com/document/d/1qKeEoWVnAkrlPDwuq7Uz65ybOzrXpBC2pplRbabcgJ0/edit?usp=sharing
Question 1 (60 points)
Interpret the value of the Residual Standard Error that you obtained.
Note: Interpreting the value of RSE means commenting on what the value means. It is not whether the value is low or high and whether the equation is good. It is simply interpreting the meaning of its value.
salary_hy_lr = lm(Salary ~ Hits + Years, data = Hitters_Fixed)
summary(salary_hy_lr)
##
## Call:
## lm(formula = Salary ~ Hits + Years, data = Hitters_Fixed)
##
## Residuals:
## Min 1Q Median 3Q Max
## -838.06 -211.89 -43.40 76.07 2248.37
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -199.2510 67.4690 -2.953 0.00343 **
## Hits 4.3124 0.5013 8.603 7.46e-16 ***
## Years 36.9501 4.7187 7.831 1.24e-13 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 366.1 on 260 degrees of freedom
## Multiple R-squared: 0.3465, Adjusted R-squared: 0.3415
## F-statistic: 68.94 on 2 and 260 DF, p-value: < 2.2e-16
The Residual Standard Error of 366.1 provides a measure of the average deviation of actual salaries from the salaries predicted by the model
The Equation for the model is: Predicted Salary = -199.25 + (4.31 x Hits) + (36.95 x Years)
summary(salary_hy_lr)$adj.r.squared
## [1] 0.3415173
The Adjusted R-square of 0.3415 means that the model explains about 34.15% of the variability in Salary. This is less than 50%, indicating that the model does not explain half or more of the variation in Salary.
Ho: The residuals follow a Normal distribution Ha: The residuals do NOT follow a Normal distribution
shapiro.test(residuals(salary_hy_lr))
##
## Shapiro-Wilk normality test
##
## data: residuals(salary_hy_lr)
## W = 0.84691, p-value = 1.975e-15
PV = 1.975e-15, which is a lot smaller than alpha (0.05). Thus, we reject Ho and support Ha. The data is giving us evidence that the residuals do NOT follow a Normal distribution. Assumption 3 is not satisfied.
plot(predict (salary_hy_lr), residuals (salary_hy_lr))
abline(h=0, col = "red")
The graphs clearly shows some outliers and the residuals getting spread as the predicted value increase. Therefore, the variability is not the same for all values. Assumption 4 is not satisfied
salary_hyr_lr = lm(Salary ~ Hits + Years + RBI, data = Hitters_Fixed)
summary(salary_hyr_lr)
##
## Call:
## lm(formula = Salary ~ Hits + Years + RBI, data = Hitters_Fixed)
##
## Residuals:
## Min 1Q Median 3Q Max
## -724.40 -190.04 -45.45 85.01 2248.38
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -194.5418 67.1578 -2.897 0.004092 **
## Hits 3.0545 0.8183 3.733 0.000233 ***
## Years 35.2177 4.7782 7.371 2.28e-12 ***
## RBI 2.7890 1.4385 1.939 0.053618 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 364.1 on 259 degrees of freedom
## Multiple R-squared: 0.3559, Adjusted R-squared: 0.3484
## F-statistic: 47.7 on 3 and 259 DF, p-value: < 2.2e-16
d1) Write the equation.
The Equation for the model is: Predicted Salary = -194.54 + (3.05 x Hits) + (35.21 x Years) + (2.78 x RBI)
d2) Use Adjusted R Squared to decide whether this equation is better than the one you obtained in part a.
summary(salary_hy_lr)$adj.r.squared
## [1] 0.3415173
summary(salary_hyr_lr)$adj.r.squared
## [1] 0.3484308
( summary(salary_hyr_lr)$adj.r.squared - summary(salary_hy_lr)$adj.r.squared) / summary(lm(salary_hy_lr))$adj.r.squared
## [1] 0.0202436
Even though, the model including the RBI has a greater Adj. R Square (0.3484 > 0.3415), it is not practical significant because the increase is about ~2%
Question 2 (40 points) Apply Linear Regression to predict “Salary” based on these predictors: Hits, League, and Division. Show all your work.
salary_hld_lr = lm(Salary ~ Hits + League + Division, data = Hitters_Fixed)
summary(salary_hld_lr)
##
## Call:
## lm(formula = Salary ~ Hits + League + Division, data = Hitters_Fixed)
##
## Residuals:
## Min 1Q Median 3Q Max
## -844.52 -264.61 -59.75 169.95 1958.07
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 118.1472 78.3232 1.508 0.13266
## Hits 4.3350 0.5574 7.778 1.75e-13 ***
## LeagueN 46.7805 50.1133 0.933 0.35143
## DivisionW -140.7468 49.6199 -2.837 0.00492 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 400.8 on 259 degrees of freedom
## Multiple R-squared: 0.2196, Adjusted R-squared: 0.2105
## F-statistic: 24.29 on 3 and 259 DF, p-value: 6.985e-14
Note: Do not forget that the question is asking you to write the equation.
The only statistically significant predictors are Hits(1.75e-13 < 0.05) and Division (0.004 < 0.05), because they are less than alpha. Therefore, the ecuation will be:
Predicted Salary = 118.1472 + 4.335 X Hits - 140.7468 x Division
Hitters_Fixed$Division[1]
## [1] W
## Levels: E W
as.numeric(Hitters_Fixed$Division[1])
## [1] 2
We now know that West = 2
hits_value = 120
div_value = 2
predicted_salary = 118.1472 + 4.335*hits_value - 140.7468*div_value
predicted_salary
## [1] 356.8536
We predict that a player with 120 hits and in the West Division in 1986 will have a salary of 356.85K$
salary_hldn_lr = lm(Salary ~ Hits + League + Division + NewLeague, data = Hitters_Fixed)
summary(salary_hldn_lr)
##
## Call:
## lm(formula = Salary ~ Hits + League + Division + NewLeague, data = Hitters_Fixed)
##
## Residuals:
## Min 1Q Median 3Q Max
## -843.75 -265.76 -59.39 171.08 1961.07
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 118.317 78.469 1.508 0.13283
## Hits 4.345 0.560 7.760 1.99e-13 ***
## LeagueN 67.617 99.084 0.682 0.49559
## DivisionW -140.672 49.711 -2.830 0.00502 **
## NewLeagueN -24.012 98.445 -0.244 0.80749
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 401.6 on 258 degrees of freedom
## Multiple R-squared: 0.2197, Adjusted R-squared: 0.2076
## F-statistic: 18.17 on 4 and 258 DF, p-value: 3.683e-13
summary(salary_hld_lr)$sigma
## [1] 400.8302
summary(salary_hldn_lr)$sigma
## [1] 401.5599
The model including the NewLeague predictor does not give any improvement, because it has a greater RSE value (401 > 400)