http://www.r-exercises.com/2017/01/15/multiple-regression-part-1-solutions/
https://www.r-bloggers.com/multiple-regression-part-1/
In the exercises below nosotros comprehend some cloth on multiple regression inward R.
Answers to the exercises are available here.
If you lot obtained a dissimilar (correct) respond than those listed on the solutions page, delight experience gratuitous to postal service your respond equally a comment on that page.
We volition travel using the dataset
state.x77
, which is component subdivision of the state
datasets available in R
. (Additional information well-nigh the dataset tin travel obtained yesteryear running help(state.x77)
.) Exercise 1
a. Load the
b. Convert the
c. Rename the
state
datasets.b. Convert the
state.x77
dataset to a dataframe.c. Rename the
Life Exp
variable to Life.Exp
, and HS Grad
to HS.Grad
. (This avoids problems amongst referring to these variables when specifying a model.) Exercise 2
Suppose nosotros wanted to travel inward all the variables inward a first-order linear regression model with
Suppose nosotros wanted to travel inward all the variables inward a first-order linear regression model with
Life Expectancy
as the subject variable. Fit this model. Exercise 3
Suppose nosotros wanted to take away the
Income
, Illiteracy
, and Area
variables from the model inward Exercise 2. Use the update
function to check this model. Learn more about multiple linear regression inward the online course Linear regression inward R for Data Scientists. In this course of report you lot volition larn how to:
- Model basic as well as complex existent basis work using linear regression
- Understand when models are performing poorly as well as right it
- Design complex models for hierarchical data
- And much more
Exercise 4
Let’s assume that nosotros bring settled on a model that has
Let’s assume that nosotros bring settled on a model that has
HS.Grad
and Murder
as predictors. Fit this model. Exercise 5
Add an interaction term to the model inward Exercise iv (3 dissimilar ways).
Add an interaction term to the model inward Exercise iv (3 dissimilar ways).
Exercise 6
For this as well as the remaining exercises inward this educate nosotros volition utilization the model from Exercise 4.
For this as well as the remaining exercises inward this educate nosotros volition utilization the model from Exercise 4.
Obtain 95% confidence intervals for the coefficients of the ii predictor variables.
Exercise 7
Predict the Life Expectancy for a country where 55% of the population are High School graduates, as well as the murder charge per unit of measurement is 8 per 100,000.
Predict the Life Expectancy for a country where 55% of the population are High School graduates, as well as the murder charge per unit of measurement is 8 per 100,000.
Exercise 8
Obtain a 98% confidence interval for the hateful Life Expectancy inward a country where 55% of the population are High School graduates, as well as the murder charge per unit of measurement is 8 per 100,000.
Exercise 9
Obtain a 98% confidence interval for the Life Expectancy of a individual living inward a country where 55% of the population are High School graduates, as well as the murder charge per unit of measurement is 8 per 100,000.
Exercise 10
Since our model solely has ii predictor variables, nosotros tin generate a 3D plot of our information as well as the fitted regression plane. Create this plot.
_________________________________________
Below are the solutions to these exercises on Multiple Regression (part 1).
#################### # # # 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 '.' agency 'all' summary(model)
## ## Call: ## lm(formula = Life.Exp ., information = 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 measure error: 0.7448 on 42 degrees of liberty ## Multiple R-squared: 0.7362, Adjusted R-squared: 0.6922 ## F-statistic: 16.74 on vii as well as 42 DF, p-value: 2.534e-10
#################### # # # Exercise iii # # # #################### model2 <- update(model, . . -Income -Illiteracy -Area) #the '.' agency 'same equally inward master copy model' summary(model2)
## ## Call: ## lm(formula = Life.Exp Population + Murder + HS.Grad + Frost, ## information = 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 measure error: 0.7197 on 45 degrees of liberty ## Multiple R-squared: 0.736, Adjusted R-squared: 0.7126 ## F-statistic: 31.37 on iv as well as 45 DF, p-value: 1.696e-12
#################### # # # Exercise iv # # # #################### model3 <- lm(Life.Exp HS.Grad + Murder, data=state77) summary(model3)
## ## Call: ## lm(formula = Life.Exp HS.Grad + Murder, information = 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 measure error: 0.7959 on 47 degrees of liberty ## Multiple R-squared: 0.6628, Adjusted R-squared: 0.6485 ## F-statistic: 46.2 on 2 as well as 47 DF, p-value: 8.016e-12
#################### # # # Exercise v # # # #################### 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, information = 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 measure error: 0.7948 on 46 degrees of liberty ## Multiple R-squared: 0.6709, Adjusted R-squared: 0.6495 ## F-statistic: 31.26 on iii as well as 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, information = 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 measure error: 0.7948 on 46 degrees of liberty ## Multiple R-squared: 0.6709, Adjusted R-squared: 0.6495 ## F-statistic: 31.26 on iii as well as 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, information = 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 measure error: 0.7948 on 46 degrees of liberty ## Multiple R-squared: 0.6709, Adjusted R-squared: 0.6495 ## F-statistic: 31.26 on iii as well as 46 DF, p-value: 3.592e-11
#################### # # # Exercise vi # # # #################### 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 vii # # # #################### 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)
## check lwr upr ## 1 70.81416 70.52183 71.1065
#################### # # # Exercise nine # # # #################### predict(model3,data.frame(HS.Grad=55,Murder=8),interval="prediction",level=0.98)
## check lwr upr ## 1 70.81416 68.87527 72.75306
#################### # # # Exercise 10 # # # #################### library(rgl)