##question2
library(MASS)
##
## Attaching package: 'MASS'
## The following object is masked from
'package:olsrr':
##
## cement
body = read.csv("body.csv", header = T)
attach(body)
chest_diam1 = chest_diam
chest_depth1 = chest_depth
ankle_diam1 = ankle_diam
waist_girth1 = waist_girth
wrist_girth1 = wrist_girth
wrist_diam1 = wrist_diam
age1 = age
height1 = height
gender1= gender
weight1 = weight
detach(body)
bd = data.frame(chest_diam =
chest_diam1,
chest_depth
= chest_depth1,
ankle_diam =
ankle_diam1,
waist_girth
= waist_girth1,
wrist_girth
= wrist_girth1,
wrist_diam =
wrist_diam1,
age
= age1,
height
= height1,
gender
= gender1,
weight
= weight1)
attach(bd)
bd.lm = lm(weight ~ ., data =
bd)
plot(bd.lm)
body.lm = lm(weight
~ chest_diam + chest_depth
+ ankle_diam + waist_girth
+
wrist_girth
+ wrist_diam + age
+ height + gender, data = body
)
summary(body.lm)
##
## Call:
## lm(formula = weight ~ chest_diam + chest_depth + ankle_diam
+
## waist_girth + wrist_girth +
wrist_diam + age + height + gender,
## data = body)
##
## Residuals:
## Min 1Q Median 3Q Max
##
-10.5003 -1.7345 0.0929 1.4414 6.8888
##
## Coefficients:
## Estimate
Std. Error t value Pr(>|t|)
## (Intercept) -119.33349 6.83670
-17.455 < 2e-16 ***
##
chest_diam 1.39294 0.21325 6.532
2.91e-09 ***
##
chest_depth 0.59732 0.20463 2.919 0.00436
**
##
ankle_diam 1.26351 0.44921 2.813 0.00594
**
##
waist_girth 0.64234 0.05178 12.404 <
2e-16 ***
##
wrist_girth 0.80607 0.42221 1.909 0.05916
.
##
wrist_diam 0.08803 0.55213 0.159 0.87366
##
age -0.14840 0.03199 -4.639
1.08e-05 ***
##
height 0.38080 0.04747 8.022
2.28e-12 ***
##
gender -7.64330 1.12288 -6.807
8.02e-10 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.'
0.1 ' ' 1
##
## Residual standard error: 2.75 on 98 degrees of freedom
## Multiple R-squared: 0.9678, Adjusted
R-squared: 0.9648
## F-statistic: 327.1 on 9 and 98 DF, p-value: <
2.2e-16
step_forward = stepAIC(body.lm, direction = "forward")
## Start: AIC=227.98
## weight ~ chest_diam + chest_depth + ankle_diam + waist_girth
+
## wrist_girth + wrist_diam + age +
height + gender
step_forward$anova
## Stepwise Model Path
## Analysis of Deviance Table
##
## Initial Model:
## weight ~ chest_diam + chest_depth + ankle_diam + waist_girth
+
## wrist_girth + wrist_diam + age +
height + gender
##
## Final Model:
## weight ~ chest_diam + chest_depth + ankle_diam + waist_girth
+
## wrist_girth + wrist_diam + age +
height + gender
##
##
## Step Df Deviance Resid. Df Resid.
Dev AIC
##
1 98 740.9312
227.9839
step_backward = stepAIC(body.lm, direction = "backward")
## Start: AIC=227.98
## weight ~ chest_diam + chest_depth + ankle_diam + waist_girth
+
## wrist_girth + wrist_diam + age +
height + gender
##
## Df
Sum of
Sq RSS AIC
## -
wrist_diam 1 0.19 741.12
226.01
##
<none> 740.93
227.98
## -
wrist_girth 1 27.56 768.49
229.93
## -
ankle_diam 1 59.81 800.75
234.37
## -
chest_depth 1 64.42 805.35
234.99
## -
age 1 162.71 903.64
247.42
## - chest_diam 1 322.59
1063.52 265.02
## -
gender 1 350.30
1091.23 267.80
## -
height 1 486.54
1227.47 280.50
## - waist_girth 1 1163.30 1904.23
327.93
##
## Step: AIC=226.01
## weight ~ chest_diam + chest_depth + ankle_diam + waist_girth
+
## wrist_girth + age + height +
gender
##
## Df
Sum of
Sq RSS AIC
##
<none> 741.12
226.01
## -
wrist_girth 1 36.54 777.66
229.21
## -
chest_depth 1 64.37 805.49
233.01
## -
ankle_diam 1 65.56 806.68
233.17
## -
age 1 163.73 904.85
245.57
## - chest_diam 1 328.81
1069.93 263.67
## -
gender 1 351.14
1092.26 265.90
## -
height 1 486.38
1227.50 278.51
## - waist_girth 1 1215.83 1956.95
328.88
step_backward$anova
## Stepwise Model Path
## Analysis of Deviance Table
##
## Initial Model:
## weight ~ chest_diam + chest_depth + ankle_diam + waist_girth
+
## wrist_girth + wrist_diam + age +
height + gender
##
## Final Model:
## weight ~ chest_diam + chest_depth + ankle_diam + waist_girth
+
## wrist_girth + age + height +
gender
##
##
## Step
Df Deviance Resid. Df Resid.
Dev AIC
##
1 98 740.9312
227.9839
## 2 - wrist_diam 1
0.1921794 99 741.1234
226.0119
detach(bd)
Finally, as you did in problem 1, fit all possible models, and find the highest adjusted (no need to report the model)- check models wioth 5, 6, and 7 variables.
Get Answers For Free
Most questions answered within 1 hours.