Below are the solutions to these exercises on Multiple Regression (part 1).
Learn more about multiple linear regression in the online course Linear regression in R for Data Scientists. In this course you will learn how to:
- Model basic and complex real world problem using linear regression
- Understand when models are performing poorly and correct it
- Design complex models for hierarchical data
- And much more
#################### # # # Exercise 1 # # # #################### #a. data(state) #b. state77 <- as.data.frame(state.x77) #c. names(state77)[4] <- "Life.Exp" names(state77)[6] <- "HS.Grad" #################### # # # Exercise 2 # # # #################### model <- lm(Life.Exp ~ ., data=state77) #the '.' means 'all' summary(model)
## ## Call: ## lm(formula = Life.Exp ~ ., data = state77) ## ## Residuals: ## Min 1Q Median 3Q Max ## -1.48895 -0.51232 -0.02747 0.57002 1.49447 ## ## Coefficients: ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) 7.094e+01 1.748e+00 40.586 < 2e-16 *** ## Population 5.180e-05 2.919e-05 1.775 0.0832 . ## Income -2.180e-05 2.444e-04 -0.089 0.9293 ## Illiteracy 3.382e-02 3.663e-01 0.092 0.9269 ## Murder -3.011e-01 4.662e-02 -6.459 8.68e-08 *** ## HS.Grad 4.893e-02 2.332e-02 2.098 0.0420 * ## Frost -5.735e-03 3.143e-03 -1.825 0.0752 . ## Area -7.383e-08 1.668e-06 -0.044 0.9649 ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## Residual standard error: 0.7448 on 42 degrees of freedom ## Multiple R-squared: 0.7362, Adjusted R-squared: 0.6922 ## F-statistic: 16.74 on 7 and 42 DF, p-value: 2.534e-10
#################### # # # Exercise 3 # # # #################### model2 <- update(model, . ~ . -Income -Illiteracy -Area) #the '.' means 'same as in original model' summary(model2)
## ## Call: ## lm(formula = Life.Exp ~ Population + Murder + HS.Grad + Frost, ## data = state77) ## ## Residuals: ## Min 1Q Median 3Q Max ## -1.47095 -0.53464 -0.03701 0.57621 1.50683 ## ## Coefficients: ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) 7.103e+01 9.529e-01 74.542 < 2e-16 *** ## Population 5.014e-05 2.512e-05 1.996 0.05201 . ## Murder -3.001e-01 3.661e-02 -8.199 1.77e-10 *** ## HS.Grad 4.658e-02 1.483e-02 3.142 0.00297 ** ## Frost -5.943e-03 2.421e-03 -2.455 0.01802 * ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## Residual standard error: 0.7197 on 45 degrees of freedom ## Multiple R-squared: 0.736, Adjusted R-squared: 0.7126 ## F-statistic: 31.37 on 4 and 45 DF, p-value: 1.696e-12
#################### # # # Exercise 4 # # # #################### model3 <- lm(Life.Exp ~ HS.Grad + Murder, data=state77) summary(model3)
## ## Call: ## lm(formula = Life.Exp ~ HS.Grad + Murder, data = state77) ## ## Residuals: ## Min 1Q Median 3Q Max ## -1.66758 -0.41801 0.05602 0.55913 2.05625 ## ## Coefficients: ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) 70.29708 1.01567 69.213 < 2e-16 *** ## HS.Grad 0.04389 0.01613 2.721 0.00909 ** ## Murder -0.23709 0.03529 -6.719 2.18e-08 *** ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## Residual standard error: 0.7959 on 47 degrees of freedom ## Multiple R-squared: 0.6628, Adjusted R-squared: 0.6485 ## F-statistic: 46.2 on 2 and 47 DF, p-value: 8.016e-12
#################### # # # Exercise 5 # # # #################### model4 <- lm(Life.Exp ~ HS.Grad + Murder + HS.Grad:Murder, data=state77) summary(model4)
## ## Call: ## lm(formula = Life.Exp ~ HS.Grad + Murder + HS.Grad:Murder, data = state77) ## ## Residuals: ## Min 1Q Median 3Q Max ## -1.66077 -0.43846 0.06362 0.52665 1.99416 ## ## Coefficients: ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) 67.831203 2.530131 26.809 <2e-16 *** ## HS.Grad 0.089368 0.045684 1.956 0.0565 . ## Murder 0.023510 0.247487 0.095 0.9247 ## HS.Grad:Murder -0.004959 0.004661 -1.064 0.2930 ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## Residual standard error: 0.7948 on 46 degrees of freedom ## Multiple R-squared: 0.6709, Adjusted R-squared: 0.6495 ## F-statistic: 31.26 on 3 and 46 DF, p-value: 3.592e-11
model4 <- lm(Life.Exp ~ HS.Grad*Murder, data=state77) summary(model4)
## ## Call: ## lm(formula = Life.Exp ~ HS.Grad * Murder, data = state77) ## ## Residuals: ## Min 1Q Median 3Q Max ## -1.66077 -0.43846 0.06362 0.52665 1.99416 ## ## Coefficients: ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) 67.831203 2.530131 26.809 <2e-16 *** ## HS.Grad 0.089368 0.045684 1.956 0.0565 . ## Murder 0.023510 0.247487 0.095 0.9247 ## HS.Grad:Murder -0.004959 0.004661 -1.064 0.2930 ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## Residual standard error: 0.7948 on 46 degrees of freedom ## Multiple R-squared: 0.6709, Adjusted R-squared: 0.6495 ## F-statistic: 31.26 on 3 and 46 DF, p-value: 3.592e-11
model4 <- lm(Life.Exp ~ (HS.Grad+Murder)^2, data=state77) summary(model4)
## ## Call: ## lm(formula = Life.Exp ~ (HS.Grad + Murder)^2, data = state77) ## ## Residuals: ## Min 1Q Median 3Q Max ## -1.66077 -0.43846 0.06362 0.52665 1.99416 ## ## Coefficients: ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) 67.831203 2.530131 26.809 <2e-16 *** ## HS.Grad 0.089368 0.045684 1.956 0.0565 . ## Murder 0.023510 0.247487 0.095 0.9247 ## HS.Grad:Murder -0.004959 0.004661 -1.064 0.2930 ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## Residual standard error: 0.7948 on 46 degrees of freedom ## Multiple R-squared: 0.6709, Adjusted R-squared: 0.6495 ## F-statistic: 31.26 on 3 and 46 DF, p-value: 3.592e-11
#################### # # # Exercise 6 # # # #################### confint(model3, level=0.95)
## 2.5 % 97.5 % ## (Intercept) 68.25382379 72.34034424 ## HS.Grad 0.01144419 0.07633041 ## Murder -0.30807483 -0.16610536
#################### # # # Exercise 7 # # # #################### predict(model3,data.frame(HS.Grad=55,Murder=8))
## 1 ## 70.81416
#################### # # # Exercise 8 # # # #################### predict(model3,data.frame(HS.Grad=55,Murder=8),interval="confidence",level=0.98)
## fit lwr upr ## 1 70.81416 70.52183 71.1065
#################### # # # Exercise 9 # # # #################### predict(model3,data.frame(HS.Grad=55,Murder=8),interval="prediction",level=0.98)
## fit lwr upr ## 1 70.81416 68.87527 72.75306
#################### # # # Exercise 10 # # # #################### library(rgl)
Hello I tried to plot the data i.e exercise 10 but i had the following error. How do i fix it please
> plotdat plotdat$pred1 with(data,plot3d(HS.Grad,Murder,Life.Exp, col=”blue”, size=1, type=”s”))
Error in eval(expr, envir, enclos) : could not find function “plot3d”
> with(plotdat,surface3d(unique(HS.Grad),unique(Murder),pred1,
+ alpha=0.5,front=”line”, back=”line”))
Error in eval(expr, envir, enclos) : could not find function “surface3d”