程序代写案例-STAT2008
时间:2021-02-19
STAT2008 REGRESSION MODELLING
R Output for the 2017 Final Examination
Page 1 of 20
Question 1
> library(UsingR)
> library(faraway)
>
> help(cheddar)
> attach(cheddar)
>
> summary(cheddar)
taste Acetic H2S Lactic
Min. : 0.70 Min. :4.477 Min. : 2.996 Min. :0.860
1st Qu.:13.55 1st Qu.:5.237 1st Qu.: 3.978 1st Qu.:1.250
Median :20.95 Median :5.425 Median : 5.329 Median :1.450
Mean :24.53 Mean :5.498 Mean : 5.942 Mean :1.442
3rd Qu.:36.70 3rd Qu.:5.883 3rd Qu.: 7.575 3rd Qu.:1.667
Max. :57.20 Max. :6.458 Max. :10.199 Max. :2.010
>
> cor(cheddar)
taste Acetic H2S Lactic
taste 1.0000000 0.5495393 0.7557523 0.7042362
Acetic 0.5495393 1.0000000 0.6179559 0.6037826
H2S 0.7557523 0.6179559 1.0000000 0.6448123
Lactic 0.7042362 0.6037826 0.6448123 1.0000000
>
> plot(cheddar)

STAT2008 REGRESSION MODELLING
R Output for the 2017 Final Examination
Page 2 of 20
Question 1 continued
> summary(cheddar.lm)

Call:
lm(formula = taste ~ H2S + Lactic)

Residuals:
Min 1Q Median 3Q Max
-17.343 -6.530 -1.164 4.844 25.618

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -27.592 8.982 -3.072 0.00481 **
H2S 3.946 1.136 3.475 0.00174 **
Lactic 19.887 7.959 2.499 0.01885 *
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 9.942 on 27 degrees of freedom
Multiple R-squared: 0.6517, Adjusted R-squared: 0.6259
F-statistic: 25.26 on 2 and 27 DF, p-value: 6.551e-07

>
> plot(cheddar.lm, which=c(1,2,4))

STAT2008 REGRESSION MODELLING
R Output for the 2017 Final Examination
Page 3 of 20
Question 1 continued


STAT2008 REGRESSION MODELLING
R Output for the 2017 Final Examination
Page 4 of 20
Question 1 continued
> cbind(hii=hatvalues(cheddar.lm), ti=rstudent(cheddar.lm), DFFITS=dffits(cheddar.lm),
COVRATIO=covratio(cheddar.lm))
hii ti DFFITS COVRATIO
1 0.16055269 1.15041237 0.50311309 1.1494533
2 0.05816258 -0.18691381 -0.04644888 1.1842565
3 0.05474793 1.46948593 0.35365147 0.9325540
4 0.08418765 1.04377653 0.31646702 1.0811434
5 0.10993173 -0.15909118 -0.05591084 1.2545277
6 0.21752949 0.20314321 0.10710935 1.4244164
7 0.19919204 0.53253269 0.26559388 1.3536575
8 0.07932505 -1.90436363 -0.55898723 0.8221281
9 0.06773616 0.49753633 0.13411139 1.1675796
10 0.11507987 0.07327532 0.02642441 1.2647309
11 0.06453556 0.49658794 0.13043137 1.1637103
12 0.11200441 1.74947037 0.62132490 0.9031599
13 0.10737069 -0.48392942 -0.16783762 1.2212857
14 0.04255541 0.83151141 0.17530276 1.0810929
15 0.03834518 2.98866976 0.59679303 0.4801714
16 0.13557969 -0.42061322 -0.16657805 1.2694363
17 0.07055189 0.51291069 0.14131345 1.1690398
18 0.06195838 -1.50769212 -0.38748209 0.9284259
19 0.05153776 -1.16303642 -0.27111033 1.0140815
20 0.15120439 -0.96523689 -0.40739393 1.1871285
21 0.06664757 -0.08261418 -0.02207618 1.1989018
22 0.03863636 -0.43011462 -0.08622605 1.1403704
23 0.17622646 -0.60416330 -0.27943833 1.3037655
24 0.19140592 0.44851508 0.21821743 1.3533150
25 0.07673890 0.39982180 0.11526886 1.1908598
26 0.13605080 -0.27079011 -0.10745812 1.2853273
27 0.06299378 -0.78277052 -0.20296074 1.1145012
28 0.06475665 -1.26220126 -0.33213028 1.0017578
29 0.15624474 -0.74572696 -0.32090338 1.2456083
30 0.04821029 -1.10319703 -0.24828612 1.0257167
>
> dfbetas(cheddar.lm)
(Intercept) H2S Lactic
1 0.4811881189 -0.025353883 -0.325413289
2 0.0050935953 0.028521553 -0.026317517
3 -0.0734147314 -0.186837458 0.210953564
4 -0.2005979232 -0.013544907 0.196447544
5 -0.0516418001 0.001776529 0.034500947
6 0.0629737292 0.085252473 -0.092777315
7 0.0931507811 0.235950450 -0.194446572
8 0.3194683253 -0.112639847 -0.241099206
9 0.0598127714 -0.082693326 0.016691188
10 -0.0050951615 -0.021277671 0.018747330
11 -0.0578075841 -0.051332423 0.090248058
12 -0.4412151544 -0.021931464 0.411800257
13 -0.1306430438 0.071353862 0.045496982
14 0.1009420291 -0.034759048 -0.034021275
15 -0.0180293815 0.159359222 0.008418083
16 0.0552392628 -0.118835068 0.013576993
17 0.1059522789 -0.045909248 -0.040562386
18 0.0060543584 0.259376544 -0.202194302
19 0.0962191535 0.084209897 -0.159298496
20 0.2988241399 -0.077295941 -0.218666237
21 -0.0183087878 -0.003191663 0.013735751
22 -0.0447616300 0.010805953 0.016009652
23 -0.0436345950 -0.251625015 0.162873522
24 -0.1439021726 0.096316150 0.070390476
25 0.0423720868 -0.079912289 0.025840926
26 -0.0008375856 0.093315603 -0.062623524
27 0.1060876444 0.021801131 -0.119193700
28 -0.1853243688 0.173258991 0.005476049
29 -0.2216048754 -0.220622636 0.279703575
30 -0.1687739108 0.037089185 0.077622179
>
STAT2008 REGRESSION MODELLING
R Output for the 2017 Final Examination
Page 5 of 20
Question 1 continued
> cheddar.lm2 <- lm(taste ~ Acetic + H2S + Lactic)
>
> anova(cheddar.lm2)
Analysis of Variance Table

Response: taste
Df Sum Sq Mean Sq F value Pr(>F)
Acetic 1 2314.14 2314.14 22.5481 6.528e-05 ***
H2S 1 2147.02 2147.02 20.9197 0.0001035 ***
Lactic 1 533.32 533.32 5.1964 0.0310795 *
Residuals 26 2668.41 102.63
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
>
> summary(cheddar.lm2)

Call:
lm(formula = taste ~ Acetic + H2S + Lactic)

Residuals:
Min 1Q Median 3Q Max
-17.390 -6.612 -1.009 4.908 25.449

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -28.8768 19.7354 -1.463 0.15540
Acetic 0.3277 4.4598 0.073 0.94198
H2S 3.9118 1.2484 3.133 0.00425 **
Lactic 19.6705 8.6291 2.280 0.03108 *
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 10.13 on 26 degrees of freedom
Multiple R-squared: 0.6518, Adjusted R-squared: 0.6116
F-statistic: 16.22 on 3 and 26 DF, p-value: 3.81e-06

>
STAT2008 REGRESSION MODELLING
R Output for the 2017 Final Examination
Page 6 of 20
Question 2
> attach(kid.weights)
> help(kid.weights)
> summary(kid.weights)
age weight height gender
Min. : 3.00 Min. : 10.00 Min. :12.00 F:129
1st Qu.: 12.25 1st Qu.: 22.00 1st Qu.:28.00 M:121
Median : 39.00 Median : 32.00 Median :36.00
Mean : 47.95 Mean : 38.38 Mean :36.52
3rd Qu.: 69.75 3rd Qu.: 45.00 3rd Qu.:43.00
Max. :144.00 Max. :150.00 Max. :67.00
>
> # pairs(kid.weights)
>
> log_weight <- log(weight)
> # pairs(cbind(age, height, log_weight))
>
> log_age <- log(age)
> pairs(cbind(log_age, height, log_weight))

> # growth.lm <- lm(log_weight ~ height)
> # plot(growth.lm, which=c(1,2,4))
>
> # anova(growth.lm)
>
> # growth.lm1 <- lm(log_weight ~ height + log_age)
> # plot(growth.lm1, which=c(1,2,4))
> # anova(growth.lm1)
> # vif(growth.lm1)
>
> log_age2 <- log_age^2
> # growth.lm2 <- lm(log_weight ~ height + log_age + log_age2)
> # plot(growth.lm2, which=c(1,2,4))
> # anova(growth.lm2)
> # vif(growth.lm2)
>
> boy <- ifelse(gender=="M",1,0)
> growth.lm3 <- lm(log_weight ~ boy*(height + log_age + log_age2))
> plot(growth.lm3, which=c(1,2,4))
STAT2008 REGRESSION MODELLING
R Output for the 2017 Final Examination
Page 7 of 20
Question 2 continued



STAT2008 REGRESSION MODELLING
R Output for the 2017 Final Examination
Page 8 of 20
> anova(growth.lm3)
Analysis of Variance Table

Response: log_weight
Df Sum Sq Mean Sq F value Pr(>F)
boy 1 0.169 0.169 4.7657 0.02999 *
height 1 60.139 60.139 1693.5977 < 2.2e-16 ***
log_age 1 9.532 9.532 268.4233 < 2.2e-16 ***
log_age2 1 0.619 0.619 17.4430 4.135e-05 ***
boy:height 1 0.314 0.314 8.8428 0.00324 **
boy:log_age 1 0.143 0.143 4.0289 0.04584 *
boy:log_age2 1 0.003 0.003 0.0796 0.77804
Residuals 242 8.593 0.036
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
>
> summary(growth.lm3)

Call:
lm(formula = log_weight ~ boy * (height + log_age + log_age2))

Residuals:
Min 1Q Median 3Q Max
-0.41286 -0.12511 -0.04004 0.11195 0.61353

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 2.274074 0.164990 13.783 < 2e-16 ***
boy -0.175592 0.256450 -0.685 0.49419
height 0.006419 0.003254 1.973 0.04965 *
log_age 0.122682 0.095786 1.281 0.20149
log_age2 0.044346 0.016929 2.620 0.00936 **
boy:height ? 0.005029 2.535 0.01188 *
boy:log_age -0.118486 0.142527 -0.831 0.40661
boy:log_age2 0.007300 0.025871 0.282 0.77804
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: ? on ? degrees of freedom
Multiple R-squared: ?, Adjusted R-squared: ?
F-statistic: ? on ? and ? DF, p-value: < 2.2e-16

>
> vif(growth.lm3)
boy height log_age log_age2 boy:height boy:log_age boy:log_age2
115.635862 8.506479 79.005792 97.722824 71.804918 511.926528 313.935801
>
> range(rstandard(growth.lm3))
[1] -2.211860 3.328865
>
> rstandard(growth.lm3)[c(228,9,158)]
228 9 158
3.328865 3.230874 2.856645
>
> range(rstudent(growth.lm3))
[1] -2.229941 3.400754
>
> rstudent(growth.lm3)[c(228,9,158)]
228 9 158
3.400754 3.296063 2.900052
>
STAT2008 REGRESSION MODELLING
R Output for the 2017 Final Examination
Page 9 of 20
Question 3
> cement
x1 x2 x3 x4 y
1 7 26 6 60 78.5
2 1 29 15 52 74.3
3 11 56 8 20 104.3
4 11 31 8 47 87.6
5 7 52 6 33 95.9
6 11 55 9 22 109.2
7 3 71 17 6 102.7
8 1 31 22 44 72.5
9 2 54 18 22 93.1
10 21 47 4 26 115.9
11 1 40 23 34 83.8
12 11 66 9 12 113.3
13 10 68 8 12 109.4
> attach(cement)
>
> x1 + x2 + x3 + x4
[1] 99 97 95 97 98 97 97 98 96 98 98 98 98
> x1 + x3
[1] 13 16 19 19 13 20 20 23 20 25 24 20 18
> x2 + x4
[1] 86 81 76 78 85 77 77 75 76 73 74 78 80
>
> pairs(cement)


STAT2008 REGRESSION MODELLING
R Output for the 2017 Final Examination
Page 10 of 20
Question 3 continued
> cor(cement)
x1 x2 x3 x4 y
x1 1.0000000 0.2285795 -0.8241338 -0.2454451 0.7307175
x2 0.2285795 1.0000000 -0.1392424 -0.9729550 0.8162526
x3 -0.8241338 -0.1392424 1.0000000 0.0295370 -0.5346707
x4 -0.2454451 -0.9729550 0.0295370 1.0000000 -0.8213050
y 0.7307175 0.8162526 -0.5346707 -0.8213050 1.0000000
>
> cement_all.lm <- lm(y ~ x1 + x2 + x3 + x4)
> anova(cement_all.lm)
Analysis of Variance Table
Response: y
Df Sum Sq Mean Sq F value Pr(>F)
x1 1 1450.08 1450.08 242.3679 2.888e-07 ***
x2 1 1207.78 1207.78 201.8705 5.863e-07 ***
x3 1 9.79 9.79 1.6370 0.2366
x4 1 0.25 0.25 0.0413 0.8441
Residuals 8 47.86 5.98
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
>
> cement_all.lm2 <- lm(y ~ x4 + x1 + x2 + x3)
> anova(cement_all.lm2)
Analysis of Variance Table
Response: y
Df Sum Sq Mean Sq F value Pr(>F)
x4 1 1831.90 1831.90 306.1859 1.161e-07 ***
x1 1 809.10 809.10 135.2350 2.722e-06 ***
x2 1 26.79 26.79 4.4776 0.06724 .
x3 1 0.11 0.11 0.0182 0.89592
Residuals 8 47.86 5.98
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
>
> summary(cement_all.lm)
Call:
lm(formula = y ~ x1 + x2 + x3 + x4)
Residuals:
Min 1Q Median 3Q Max
-3.1750 -1.6709 0.2508 1.3783 3.9254
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 62.4054 70.0710 0.891 0.3991
x1 1.5511 0.7448 2.083 0.0708 .
x2 0.5102 0.7238 0.705 0.5009
x3 0.1019 0.7547 0.135 0.8959
x4 -0.1441 0.7091 -0.203 0.8441
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 2.446 on 8 degrees of freedom
Multiple R-squared: 0.9824, Adjusted R-squared: 0.9736
F-statistic: 111.5 on 4 and 8 DF, p-value: 4.756e-07
> vif(cement_all.lm)
x1 x2 x3 x4
38.49621 254.42317 46.86839 282.51286
>
> plot(cement_all.lm, which=c(1,2,4))
STAT2008 REGRESSION MODELLING
R Output for the 2017 Final Examination
Page 11 of 20
Question 3 continued



STAT2008 REGRESSION MODELLING
R Output for the 2017 Final Examination
Page 12 of 20
Question 3 continued
> cement.lm <- lm(y ~ x4 + x1)
> anova(cement.lm)
Analysis of Variance Table

Response: y
Df Sum Sq Mean Sq F value Pr(>F)
x4 1 1831.90 1831.90 245.03 2.319e-08 ***
x1 1 809.10 809.10 108.22 1.105e-06 ***
Residuals 10 74.76 7.48
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
>
> summary(cement.lm)

Call:
lm(formula = y ~ x4 + x1)

Residuals:
Min 1Q Median 3Q Max
-5.0234 -1.4737 0.1371 1.7305 3.7701

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 103.09738 2.12398 48.54 3.32e-13 ***
x4 -0.61395 0.04864 -12.62 1.81e-07 ***
x1 1.43996 0.13842 10.40 1.11e-06 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 2.734 on 10 degrees of freedom
Multiple R-squared: 0.9725, Adjusted R-squared: 0.967
F-statistic: 176.6 on 2 and 10 DF, p-value: 1.581e-08

> vif(cement.lm)
x4 x1
1.064105 1.064105
>
> # plot(cement.lm, which=c(1,2,4))
STAT2008 REGRESSION MODELLING
R Output for the 2017 Final Examination
Page 13 of 20
Question 4
> temp <- mammalsleep[,c("brain","body","lifespan","gestation","danger","sleep")]
> temp$danger <- ifelse(temp$danger<4,exp(0),exp(1))
> temp <- temp[!is.na(temp$sleep),]
> temp <- temp[!is.na(temp$lifespan),]
> temp <- temp[!is.na(temp$gestation),]
> pairs(temp)


STAT2008 REGRESSION MODELLING
R Output for the 2017 Final Examination
Page 14 of 20
Question 4 continued
> msleep <- log(temp)
> names(msleep) <- c("log_brain","log_body","log_lifespn","log_gestn","danger",
"log_sleep")
> pairs(msleep)

>
> cor(msleep)
log_brain log_body log_lifespn log_gestn danger log_sleep
log_brain 1.0000000 0.9587409 0.8044310 0.7924933 0.3218992 -0.6204359
log_body 0.9587409 1.0000000 0.7300478 0.7352547 0.3455494 -0.6349638
log_lifespn 0.8044310 0.7300478 1.0000000 0.6698078 0.2926964 -0.4360847
log_gestn 0.7924933 0.7352547 0.6698078 1.0000000 0.3715447 -0.6685500
danger 0.3218992 0.3455494 0.2926964 0.3715447 1.0000000 -0.5509559
log_sleep -0.6204359 -0.6349638 -0.4360847 -0.6685500 -0.5509559 1.0000000
>
> attach(msleep)
>
STAT2008 REGRESSION MODELLING
R Output for the 2017 Final Examination
Page 15 of 20
Question 4 continued
> species_name <- row.names(msleep)
> data.frame(species_name, msleep, row.names=1:51)
species_name log_brain log_body log_lifespn log_gestn danger log_sleep
1 African.elephant 8.6503245 8.80297346 3.6532523 6.469250 0 1.1939225
2 African.giant.pouched.rat 1.8870696 0.00000000 1.5040774 3.737670 0 2.1162555
3 Arctic.Fox 3.7954892 1.21935391 2.6390573 4.094345 0 2.5257286
4 Asian.elephant 8.4344635 7.84267147 4.2341065 6.436150 1 1.3609766
5 Baboon 5.1901752 2.35612586 3.2958369 5.192957 1 2.2823824
6 Big.brown.bat -1.2039728 -3.77226106 2.9444390 3.555348 0 2.9806186
7 Brazilian.tapir 5.1298987 5.07517382 3.4144426 5.971262 1 1.8245493
8 Cat 3.2425924 1.19392247 3.3322045 4.143135 0 2.6741486
9 Chimpanzee 6.0867747 3.95431592 3.9120230 5.438079 0 2.2721259
10 Chinchilla 1.8562980 -0.85566611 1.9459101 4.718499 1 2.5257286
11 Cow 6.0473722 6.14203741 3.4011974 5.638355 1 1.3609766
12 Donkey 6.0378709 5.23164323 3.6888795 5.899897 1 1.1314021
13 Eastern.American.mole 0.1823216 -2.59026717 1.2527630 3.737670 0 2.1282317
14 Echidna 3.2188758 1.09861229 3.9120230 3.332205 0 2.1517622
15 European.hedgehog 1.2527630 -0.24207156 1.7917595 3.737670 0 2.3702437
16 Galago 1.6094379 -1.60943791 2.3418058 4.787492 0 2.3702437
17 Goat 4.7449321 3.31998733 2.9957323 4.997212 1 1.3350011
18 Golden.hamster 0.0000000 -2.12026354 1.3609766 2.772589 0 2.6672282
19 Gorilla 6.0063532 5.33271879 3.6712245 5.529429 0 2.4849066
20 Gray.seal 5.7838252 4.44265126 3.7135721 5.736572 0 1.8245493
21 Gray.wolf 4.7833164 3.59264385 2.7850112 4.143135 0 2.5649494
22 Ground.squirrel 1.3862944 -2.29263476 2.1972246 3.332205 0 2.6246686
23 Guinea.pig 1.7047481 0.03922071 2.0281482 4.219508 1 2.1041342
24 Horse 6.4846352 6.25575004 3.8286414 5.817111 1 1.0647107
25 Jaguar 5.0562458 4.60517019 3.1090610 4.605170 0 2.3795461
26 Lesser.short-tailed.shrew -1.9661129 -5.29831737 0.9555114 3.068053 1 2.2082744
27 Little.brown.bat -1.3862944 -4.60517019 3.1780538 3.912023 0 2.9907197
28 Man 7.1853870 4.12713439 4.6051702 5.587249 0 2.0794415
29 Mouse -0.9162907 -3.77226106 1.1631508 2.944439 0 2.5802168
30 Musk.shrew -1.1086626 -3.03655427 0.6931472 3.401197 0 2.5494452
31 N.American.opossum 1.8405496 0.53062825 1.6094379 2.484907 0 2.9652731
32 Nine-banded.armadillo 2.3795461 1.25276297 1.8718022 4.787492 0 2.8564702
33 Owl.monkey 2.7408400 -0.73396918 2.4849066 4.941642 0 2.8332133
34 Patas.monkey 4.7449321 2.30258509 3.0056826 5.135798 1 2.3887628
35 Phanlanger 2.4336134 0.48242615 2.5649494 2.833213 0 2.6173958
36 Pig 5.1929569 5.25749537 3.2958369 4.744932 1 2.1282317
37 Rabbit 2.4932055 0.91629073 2.8903718 3.433987 1 2.1282317
38 Raccoon 3.6686767 1.45582042 2.6173958 4.143135 0 2.5257286
39 Rat 0.6418539 -1.27296568 1.5475625 3.044522 0 2.5802168
40 Red.fox 3.9199912 1.44338333 2.2823824 3.951244 0 2.2823824
41 Rhesus.monkey 5.1873858 1.91692261 3.3672958 5.099866 0 2.2617631
42 Rock.hyrax.Hetero 2.5095993 -0.28768207 1.9459101 5.416100 0 1.8870696
43 Rock.hyrax.Procavia 3.0445224 1.28093385 1.7917595 5.416100 0 1.6863990
44 Roe.deer 4.5870062 2.69665216 2.8332133 5.010635 1 0.9555114
45 Sheep 5.1647860 4.01638302 2.9957323 5.017280 1 1.3350011
46 Slow.loris 2.5257286 0.33647224 2.5416020 4.499810 0 2.3978953
47 Tenrec 0.9555114 -0.10536052 1.5040774 4.094345 0 2.5877640
48 Tree.hyrax 2.5095993 0.69314718 2.0149030 5.298317 0 1.6863990
49 Tree.shrew 0.9162907 -2.26336438 0.8329091 3.828641 0 2.7600099
50 Vervet 4.0604430 1.43270073 3.1780538 5.347108 1 2.3321439
51 Water.opossum 1.3609766 1.25276297 1.0986123 2.639057 0 2.9652731
>
STAT2008 REGRESSION MODELLING
R Output for the 2017 Final Examination
Page 16 of 20
Question 4 continued
> msleep.lm <- step(lm(log_sleep ~ 1), scope=list(upper=as.formula("log_sleep ~
log_brain + log_body + log_lifespn + log_gestn + danger")), direction="both")
Start: AIC=-62.17
log_sleep ~ 1

Df Sum of Sq RSS AIC
+ log_gestn 1 6.4770 8.0142 -90.381
+ log_body 1 5.8426 8.6487 -86.495
+ log_brain 1 5.5783 8.9130 -84.960
+ danger 1 4.3988 10.0924 -78.622
+ log_lifespn 1 2.7558 11.7354 -70.930
14.4912 -62.172

Step: AIC=-90.38
log_sleep ~ log_gestn

Df Sum of Sq RSS AIC
+ danger 1 1.5390 6.4752 -99.256
+ log_body 1 0.6487 7.3655 -92.686
+ log_brain 1 0.3199 7.6943 -90.458
8.0142 -90.381
+ log_lifespn 1 0.0036 8.0106 -88.404
- log_gestn 1 6.4770 14.4912 -62.172

Step: AIC=-99.26
log_sleep ~ log_gestn + danger

Df Sum of Sq RSS AIC
+ log_body 1 0.4452 6.0301 -100.888
+ log_brain 1 0.2561 6.2191 -99.314
6.4752 -99.256
+ log_lifespn 1 0.0194 6.4558 -97.409
- danger 1 1.5390 8.0142 -90.381
- log_gestn 1 3.6172 10.0924 -78.622

Step: AIC=-100.89
log_sleep ~ log_gestn + danger + log_body

Df Sum of Sq RSS AIC
+ log_lifespn 1 0.26183 5.7682 -101.152
6.0301 -100.888
+ log_brain 1 0.06129 5.9688 -99.409
- log_body 1 0.44515 6.4752 -99.256
- log_gestn 1 0.80970 6.8398 -96.463
- danger 1 1.33543 7.3655 -92.686

Step: AIC=-101.15
log_sleep ~ log_gestn + danger + log_body + log_lifespn

Df Sum of Sq RSS AIC
5.7682 -101.152
- log_lifespn 1 0.26183 6.0301 -100.888
+ log_brain 1 0.00000 5.7682 -99.152
- log_body 1 0.68760 6.4558 -97.409
- log_gestn 1 1.01476 6.7830 -94.888
- danger 1 1.34786 7.1161 -92.443
>
> plot(msleep.lm, which=c(1,2,4))
STAT2008 REGRESSION MODELLING
R Output for the 2017 Final Examination
Page 17 of 20
Question 4 continued



STAT2008 REGRESSION MODELLING
R Output for the 2017 Final Examination
Page 18 of 20
Question 4 continued
> anova(msleep.lm)
Analysis of Variance Table

Response: log_sleep
Df Sum Sq Mean Sq F value Pr(>F)
log_gestn 1 6.4770 6.4770 51.6520 4.781e-09 ***
danger 1 1.5390 1.5390 12.2732 0.001035 **
log_body 1 0.4452 0.4452 3.5499 0.065876 .
log_lifespn 1 0.2618 0.2618 2.0880 0.155236
Residuals 46 5.7682 0.1254
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
>
> summary(msleep.lm)

Call:
lm(formula = log_sleep ~ log_gestn + danger + log_body + log_lifespn)

Residuals:
Min 1Q Median 3Q Max
-0.82494 -0.25085 -0.04913 0.24363 0.66865

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 3.07444 0.31460 9.772 8.42e-13 ***
log_gestn -0.21364 0.07510 -2.845 0.00661 **
danger -0.37992 0.11588 -3.279 0.00199 **
log_body -0.06212 0.02653 -2.342 0.02358 *
log_lifespn 0.11434 0.07913 1.445 0.15524
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.3541 on 46 degrees of freedom
Multiple R-squared: 0.6019, Adjusted R-squared: 0.5673
F-statistic: 17.39 on 4 and 46 DF, p-value: 9.336e-09

> vif(msleep.lm)
log_gestn danger log_body log_lifespn
2.446765 1.175837 2.827213 2.333961
STAT2008 REGRESSION MODELLING
R Output for the 2017 Final Examination
Page 19 of 20
Statistical Tables
> # Selected quantiles of the F distribution:
> NumeratorDF <- c(1:10)
> DenominatorDF <- c(1:50,60,70,80,90,100)
> Fquantiles0.95 <- matrix(0,ncol=length(NumeratorDF),nrow=length(DenominatorDF))
> for(i in 1:length(NumeratorDF)){for(j in 1:length(DenominatorDF)){
+ Fquantiles0.95[j,i]=round(qf(0.95,i,DenominatorDF[j]),3)}}
> dimnames(Fquantiles0.95) <- list(DenominatorDF, NumeratorDF)
> Fquantiles0.95
1 2 3 4 5 6 7 8 9 10
1 161.448 199.500 215.707 224.583 230.162 233.986 236.768 238.883 240.543 241.882
2 18.513 19.000 19.164 19.247 19.296 19.330 19.353 19.371 19.385 19.396
3 10.128 9.552 9.277 9.117 9.013 8.941 8.887 8.845 8.812 8.786
4 7.709 6.944 6.591 6.388 6.256 6.163 6.094 6.041 5.999 5.964
5 6.608 5.786 5.409 5.192 5.050 4.950 4.876 4.818 4.772 4.735
6 5.987 5.143 4.757 4.534 4.387 4.284 4.207 4.147 4.099 4.060
7 5.591 4.737 4.347 4.120 3.972 3.866 3.787 3.726 3.677 3.637
8 5.318 4.459 4.066 3.838 3.687 3.581 3.500 3.438 3.388 3.347
9 5.117 4.256 3.863 3.633 3.482 3.374 3.293 3.230 3.179 3.137
10 4.965 4.103 3.708 3.478 3.326 3.217 3.135 3.072 3.020 2.978
11 4.844 3.982 3.587 3.357 3.204 3.095 3.012 2.948 2.896 2.854
12 4.747 3.885 3.490 3.259 3.106 2.996 2.913 2.849 2.796 2.753
13 4.667 3.806 3.411 3.179 3.025 2.915 2.832 2.767 2.714 2.671
14 4.600 3.739 3.344 3.112 2.958 2.848 2.764 2.699 2.646 2.602
15 4.543 3.682 3.287 3.056 2.901 2.790 2.707 2.641 2.588 2.544
16 4.494 3.634 3.239 3.007 2.852 2.741 2.657 2.591 2.538 2.494
17 4.451 3.592 3.197 2.965 2.810 2.699 2.614 2.548 2.494 2.450
18 4.414 3.555 3.160 2.928 2.773 2.661 2.577 2.510 2.456 2.412
19 4.381 3.522 3.127 2.895 2.740 2.628 2.544 2.477 2.423 2.378
20 4.351 3.493 3.098 2.866 2.711 2.599 2.514 2.447 2.393 2.348
21 4.325 3.467 3.072 2.840 2.685 2.573 2.488 2.420 2.366 2.321
22 4.301 3.443 3.049 2.817 2.661 2.549 2.464 2.397 2.342 2.297
23 4.279 3.422 3.028 2.796 2.640 2.528 2.442 2.375 2.320 2.275
24 4.260 3.403 3.009 2.776 2.621 2.508 2.423 2.355 2.300 2.255
25 4.242 3.385 2.991 2.759 2.603 2.490 2.405 2.337 2.282 2.236
26 4.225 3.369 2.975 2.743 2.587 2.474 2.388 2.321 2.265 2.220
27 4.210 3.354 2.960 2.728 2.572 2.459 2.373 2.305 2.250 2.204
28 4.196 3.340 2.947 2.714 2.558 2.445 2.359 2.291 2.236 2.190
29 4.183 3.328 2.934 2.701 2.545 2.432 2.346 2.278 2.223 2.177
30 4.171 3.316 2.922 2.690 2.534 2.421 2.334 2.266 2.211 2.165
31 4.160 3.305 2.911 2.679 2.523 2.409 2.323 2.255 2.199 2.153
32 4.149 3.295 2.901 2.668 2.512 2.399 2.313 2.244 2.189 2.142
33 4.139 3.285 2.892 2.659 2.503 2.389 2.303 2.235 2.179 2.133
34 4.130 3.276 2.883 2.650 2.494 2.380 2.294 2.225 2.170 2.123
35 4.121 3.267 2.874 2.641 2.485 2.372 2.285 2.217 2.161 2.114
36 4.113 3.259 2.866 2.634 2.477 2.364 2.277 2.209 2.153 2.106
37 4.105 3.252 2.859 2.626 2.470 2.356 2.270 2.201 2.145 2.098
38 4.098 3.245 2.852 2.619 2.463 2.349 2.262 2.194 2.138 2.091
39 4.091 3.238 2.845 2.612 2.456 2.342 2.255 2.187 2.131 2.084
40 4.085 3.232 2.839 2.606 2.449 2.336 2.249 2.180 2.124 2.077
41 4.079 3.226 2.833 2.600 2.443 2.330 2.243 2.174 2.118 2.071
42 4.073 3.220 2.827 2.594 2.438 2.324 2.237 2.168 2.112 2.065
43 4.067 3.214 2.822 2.589 2.432 2.318 2.232 2.163 2.106 2.059
44 4.062 3.209 2.816 2.584 2.427 2.313 2.226 2.157 2.101 2.054
45 4.057 3.204 2.812 2.579 2.422 2.308 2.221 2.152 2.096 2.049
46 4.052 3.200 2.807 2.574 2.417 2.304 2.216 2.147 2.091 2.044
47 4.047 3.195 2.802 2.570 2.413 2.299 2.212 2.143 2.086 2.039
48 4.043 3.191 2.798 2.565 2.409 2.295 2.207 2.138 2.082 2.035
49 4.038 3.187 2.794 2.561 2.404 2.290 2.203 2.134 2.077 2.030
50 4.034 3.183 2.790 2.557 2.400 2.286 2.199 2.130 2.073 2.026
60 4.001 3.150 2.758 2.525 2.368 2.254 2.167 2.097 2.040 1.993
70 3.978 3.128 2.736 2.503 2.346 2.231 2.143 2.074 2.017 1.969
80 3.960 3.111 2.719 2.486 2.329 2.214 2.126 2.056 1.999 1.951
90 3.947 3.098 2.706 2.473 2.316 2.201 2.113 2.043 1.986 1.938
100 3.936 3.087 2.696 2.463 2.305 2.191 2.103 2.032 1.975 1.927
> # Columns are numerator df, rows are denominator df
>
STAT2008 REGRESSION MODELLING
R Output for the 2017 Final Examination
Page 20 of 20
Statistical Tables continued
> # Selected quantiles of Student's t distribution:
> DF <- c(1:100, 150, 200, 250, 300, 400, 500, 750, 1000, 10000, 1000000)
> tquantiles0.025 <- round(qt(0.025,DF),4)
> tquantiles0.05 <- round(qt(0.05,DF),4)
> tquantiles0.95 <- round(qt(0.95,DF),4)
> tquantiles0.975 <- round(qt(0.975,DF),4)
> tquantiles <- rbind(tquantiles0.025, tquantiles0.05, tquantiles0.95, tquantiles0.975)
> dimnames(tquantiles) <- list(c("t 0.025", "t 0.05 ", "t 0.95 ", "t 0.975"), DF)
> tquantiles
1 2 3 4 5 6 7 8 9 10
t 0.025 -12.7062 -4.3027 -3.1824 -2.7764 -2.5706 -2.4469 -2.3646 -2.3060 -2.2622 -2.2281
t 0.05 -6.3138 -2.9200 -2.3534 -2.1318 -2.0150 -1.9432 -1.8946 -1.8595 -1.8331 -1.8125
t 0.95 6.3138 2.9200 2.3534 2.1318 2.0150 1.9432 1.8946 1.8595 1.8331 1.8125
t 0.975 12.7062 4.3027 3.1824 2.7764 2.5706 2.4469 2.3646 2.3060 2.2622 2.2281
11 12 13 14 15 16 17 18 19 20
t 0.025 -2.2010 -2.1788 -2.1604 -2.1448 -2.1314 -2.1199 -2.1098 -2.1009 -2.0930 -2.0860
t 0.05 -1.7959 -1.7823 -1.7709 -1.7613 -1.7531 -1.7459 -1.7396 -1.7341 -1.7291 -1.7247
t 0.95 1.7959 1.7823 1.7709 1.7613 1.7531 1.7459 1.7396 1.7341 1.7291 1.7247
t 0.975 2.2010 2.1788 2.1604 2.1448 2.1314 2.1199 2.1098 2.1009 2.0930 2.0860
21 22 23 24 25 26 27 28 29 30
t 0.025 -2.0796 -2.0739 -2.0687 -2.0639 -2.0595 -2.0555 -2.0518 -2.0484 -2.0452 -2.0423
t 0.05 -1.7207 -1.7171 -1.7139 -1.7109 -1.7081 -1.7056 -1.7033 -1.7011 -1.6991 -1.6973
t 0.95 1.7207 1.7171 1.7139 1.7109 1.7081 1.7056 1.7033 1.7011 1.6991 1.6973
t 0.975 2.0796 2.0739 2.0687 2.0639 2.0595 2.0555 2.0518 2.0484 2.0452 2.0423
31 32 33 34 35 36 37 38 39 40
t 0.025 -2.0395 -2.0369 -2.0345 -2.0322 -2.0301 -2.0281 -2.0262 -2.0244 -2.0227 -2.0211
t 0.05 -1.6955 -1.6939 -1.6924 -1.6909 -1.6896 -1.6883 -1.6871 -1.6860 -1.6849 -1.6839
t 0.95 1.6955 1.6939 1.6924 1.6909 1.6896 1.6883 1.6871 1.6860 1.6849 1.6839
t 0.975 2.0395 2.0369 2.0345 2.0322 2.0301 2.0281 2.0262 2.0244 2.0227 2.0211
41 42 43 44 45 46 47 48 49 50
t 0.025 -2.0195 -2.0181 -2.0167 -2.0154 -2.0141 -2.0129 -2.0117 -2.0106 -2.0096 -2.0086
t 0.05 -1.6829 -1.6820 -1.6811 -1.6802 -1.6794 -1.6787 -1.6779 -1.6772 -1.6766 -1.6759
t 0.95 1.6829 1.6820 1.6811 1.6802 1.6794 1.6787 1.6779 1.6772 1.6766 1.6759
t 0.975 2.0195 2.0181 2.0167 2.0154 2.0141 2.0129 2.0117 2.0106 2.0096 2.0086
51 52 53 54 55 56 57 58 59 60
t 0.025 -2.0076 -2.0066 -2.0057 -2.0049 -2.0040 -2.0032 -2.0025 -2.0017 -2.0010 -2.0003
t 0.05 -1.6753 -1.6747 -1.6741 -1.6736 -1.6730 -1.6725 -1.6720 -1.6716 -1.6711 -1.6706
t 0.95 1.6753 1.6747 1.6741 1.6736 1.6730 1.6725 1.6720 1.6716 1.6711 1.6706
t 0.975 2.0076 2.0066 2.0057 2.0049 2.0040 2.0032 2.0025 2.0017 2.0010 2.0003
61 62 63 64 65 66 67 68 69 70
t 0.025 -1.9996 -1.9990 -1.9983 -1.9977 -1.9971 -1.9966 -1.9960 -1.9955 -1.9949 -1.9944
t 0.05 -1.6702 -1.6698 -1.6694 -1.6690 -1.6686 -1.6683 -1.6679 -1.6676 -1.6672 -1.6669
t 0.95 1.6702 1.6698 1.6694 1.6690 1.6686 1.6683 1.6679 1.6676 1.6672 1.6669
t 0.975 1.9996 1.9990 1.9983 1.9977 1.9971 1.9966 1.9960 1.9955 1.9949 1.9944
71 72 73 74 75 76 77 78 79 80
t 0.025 -1.9939 -1.9935 -1.9930 -1.9925 -1.9921 -1.9917 -1.9913 -1.9908 -1.9905 -1.9901
t 0.05 -1.6666 -1.6663 -1.6660 -1.6657 -1.6654 -1.6652 -1.6649 -1.6646 -1.6644 -1.6641
t 0.95 1.6666 1.6663 1.6660 1.6657 1.6654 1.6652 1.6649 1.6646 1.6644 1.6641
t 0.975 1.9939 1.9935 1.9930 1.9925 1.9921 1.9917 1.9913 1.9908 1.9905 1.9901
81 82 83 84 85 86 87 88 89 90
t 0.025 -1.9897 -1.9893 -1.9890 -1.9886 -1.9883 -1.9879 -1.9876 -1.9873 -1.9870 -1.9867
t 0.05 -1.6639 -1.6636 -1.6634 -1.6632 -1.6630 -1.6628 -1.6626 -1.6624 -1.6622 -1.6620
t 0.95 1.6639 1.6636 1.6634 1.6632 1.6630 1.6628 1.6626 1.6624 1.6622 1.6620
t 0.975 1.9897 1.9893 1.9890 1.9886 1.9883 1.9879 1.9876 1.9873 1.9870 1.9867
91 92 93 94 95 96 97 98 99 100
t 0.025 -1.9864 -1.9861 -1.9858 -1.9855 -1.9853 -1.9850 -1.9847 -1.9845 -1.9842 -1.9840
t 0.05 -1.6618 -1.6616 -1.6614 -1.6612 -1.6611 -1.6609 -1.6607 -1.6606 -1.6604 -1.6602
t 0.95 1.6618 1.6616 1.6614 1.6612 1.6611 1.6609 1.6607 1.6606 1.6604 1.6602
t 0.975 1.9864 1.9861 1.9858 1.9855 1.9853 1.9850 1.9847 1.9845 1.9842 1.9840
150 200 250 300 400 500 750 1000 10000 1e+06
t 0.025 -1.9759 -1.9719 -1.9695 -1.9679 -1.9659 -1.9647 -1.9631 -1.9623 -1.9602 -1.9600
t 0.05 -1.6551 -1.6525 -1.6510 -1.6499 -1.6487 -1.6479 -1.6469 -1.6464 -1.6450 -1.6449
t 0.95 1.6551 1.6525 1.6510 1.6499 1.6487 1.6479 1.6469 1.6464 1.6450 1.6449
t 0.975 1.9759 1.9719 1.9695 1.9679 1.9659 1.9647 1.9631 1.9623 1.9602 1.9600
>

End of R Output


essay、essay代写