STAT6038代写-UESTION 2
时间:2022-11-04
Regression Modelling
STAT2008/4038/6038
R Output for Final Examination, Semester 2 2022
PRINTOUT FOR QUESTION 2
c(summary(Effort), sd(Effort))
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 5.00000 26.25000 50.50000 50.40000 75.75000 97.00000 28.45476
c(summary(Reward), sd(Reward))
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 2.828172 29.886097 52.460727 48.655263 66.437648 95.839993 24.361503
cov(Reward, Effort)
## [1] 633.1174
1
names(bird)
## [1] "name" "extinct" "nest.pair" "size" "mig.status"
levels(bird$size)
## [1] "S" "L"
levels(bird$mig.status)
## [1] "R" "M"
bird.lm <- lm(extinct ~ nest.pair + size + mig.status, data = bird)
summary(bird.lm)
## Call:
## lm(formula = extinct ~ nest.pair + size + mig.status, data = bird)
##
## Residuals:
## Min 1Q Median 3Q Max
## -14.056 -5.578 -2.485 2.942 48.313
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.9062 3.2696 -0.277 0.782898
## nest.pair 2.2534 0.6361 3.543 0.000921 ***
## sizeL 6.1217 2.9401 2.082 0.042923 *
## mig.statusM -4.9136 3.2467 -1.513 0.137017
## ---
## Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
vcov(bird.lm) # Variance-covariance matrix
## (Intercept) nest.pair sizeL mig.statusM
## (Intercept) 10.690611 -1.50199817 -4.43874764 -4.4050888
## nest.pair -1.501998 0.40457802 0.06605802 0.3360600
## sizeL -4.438748 0.06605802 8.64447634 0.2193219
## mig.statusM -4.405089 0.33606004 0.21932190 10.5409041
anova(bird.lm)
## Analysis of Variance Table
##
## Response: extinct
## Df Sum Sq Mean Sq F value Pr(>F)
## nest.pair 1 1534.4 1534.42 14.2451 0.0004581 ***
## size 1 482.9 482.95 4.4835 0.0396600 *
## mig.status 1 246.7 246.71 2.2904 0.1370167
## Residuals 46 4954.9 107.72
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
par(mfrow = c(2, 2))
plot(bird.lm, which = 1)
plot(bird.lm, which = 2)
plot(hatvalues(bird.lm), type = "h")
2
PR I N T OU T FOR QU EST I ON 5
abline(h = 8/length(bird$name), col = "red")
plot(cooks.distance(bird.lm), type = "h")
0 5 10 15 20 25

20
0
20
40
Fitted values
R
es
id
ua
ls
Residuals vs Fitted
49
5
8
−2 −1 0 1 2

1
0
1
2
3
4
5
Theoretical Quantiles
St
an
da
rd
ize
d
re
sid
ua
ls
Normal Q−Q
49
5
37
0 10 20 30 40 50
0.
05
0.
10
0.
15
0.
20
0.
25
Index
ha
tv
a
lu
es
(bi
rd.
lm
)
0 10 20 30 40 50
0.
00
0.
05
0.
10
0.
15
0.
20
0.
25
0.
30
0.
35
Index
co
o
ks
.
di
st
an
ce
(bi
rd.
lm
)
cbind(dffits(bird.lm), dfbetas(bird.lm)[, 2:4])
## nest.pair sizeL mig.statusM
## 1 0.158479248 -0.058558514 -0.069108602 0.097519683
## 2 0.106531771 -0.015893473 0.068144741 -0.044526900
## 3 -0.227400617 -0.084424830 -0.109505637 -0.166017788
## 4 -0.009803451 0.005946269 0.005125687 0.004319411
## 5 0.670816690 -0.384797862 0.344796479 -0.281391735
## 6 -0.057027630 0.006099956 0.035975322 0.025375671
## 7 0.157416517 -0.058165832 -0.068645173 0.096865734
## 8 0.554734574 0.362372808 -0.252097995 -0.121651626
## 9 -0.112045810 -0.002557150 -0.056602362 -0.081202042
3
## 10 -0.199511930 -0.081025299 -0.121776354 0.059403702
## 11 -0.226324663 -0.007504613 -0.147800813 0.088839434
## 12 -0.069260179 0.039729436 -0.035599391 0.029053007
## 13 -0.025683188 0.013569405 0.014231861 0.011585430
## 14 -0.085561401 0.018348475 -0.053859727 0.036259301
## 15 0.146522780 -0.029765359 -0.066363447 0.099445876
## 16 -0.172616336 -0.046182807 -0.110113422 0.058705582
## 17 -0.104662867 0.056581756 -0.055430748 0.044264531
## 18 0.540748846 0.430227829 0.228860696 -0.060415019
## 19 -0.137696397 0.053921760 -0.080733146 0.059219726
## 20 -0.140050962 -0.030285614 0.085167659 0.053872865
## 21 0.243404038 0.117689747 0.143122486 -0.065676988
## 22 0.129431473 -0.042192434 -0.057194031 0.082081009
## 23 -0.009803451 0.005946269 0.005125687 0.004319411
## 24 -0.259010562 -0.070567545 -0.165037566 0.087739094
## 25 -0.050054522 0.019921618 0.029664098 0.022988688
## 26 -0.167711792 0.092961358 -0.087761999 0.070705198
## 27 -0.107451499 0.002190768 0.067830835 0.046554481
## 28 -0.392707923 -0.257599488 -0.158639710 -0.255693470
## 29 -0.035696116 0.011856144 -0.016567435 -0.022353007
## 30 -0.007402054 -0.001454069 0.003252686 -0.005512499
## 31 -0.094631138 0.020293462 -0.059569002 0.040102884
## 32 -0.098692356 0.020731085 -0.047946839 -0.066214842
## 33 -0.009803451 0.005946269 0.005125687 0.004319411
## 34 -0.318090926 -0.259013572 0.107302924 0.037253110
## 35 -0.169320991 -0.080372769 0.091151023 0.051008140
## 36 -0.058720002 0.020742571 0.035378704 0.026999040
## 37 1.202715763 1.096932725 -0.272316000 -0.033591763
## 38 0.150843266 -0.055736998 -0.065778753 0.092820906
## 39 -0.031152205 0.015345362 0.017640104 0.014155588
## 40 -0.032579967 0.008316963 -0.015598132 -0.021364587
## 41 -0.376677429 -0.186928466 -0.219898328 0.099783106
## 42 0.215588002 0.059696522 0.106619300 0.159116207
## 43 -0.063233586 0.020410040 0.038466595 0.029058426
## 44 -0.059852021 -0.018500143 0.025259222 -0.044387757
## 45 -0.029464756 0.011077293 0.017610878 0.013543899
## 46 -0.149330574 -0.083247898 0.075247037 0.039765309
## 47 -0.144737703 -0.037637165 0.086813494 0.053975803
## 48 -0.104738677 0.041015553 -0.061409616 0.045045447
## 49 1.616461338 -0.505786783 0.983506786 -0.693579346
## 50 -0.110853903 -0.045285964 0.044462311 -0.080920828
cbind(rstandard(bird.lm), rstudent(bird.lm))
## [,1] [,2]
## 1 0.48644645 0.48237223
## 2 0.46399389 0.46000048
## 3 -0.67235260 -0.66829616
## 4 -0.03477943 -0.03439977
## 5 2.25922219 2.36987389
## 6 -0.25650766 -0.25388585
## 7 0.48320105 0.47913753
## 8 1.79952466 1.84602051
## 9 -0.36078519 -0.35734802
4
## 10 -0.79572094 -0.79249738
## 11 -0.98858938 -0.98834022
## 12 -0.24722304 -0.24468367
## 13 -0.09777917 -0.09672057
## 14 -0.36818292 -0.36469671
## 15 0.47649181 0.47245149
## 16 -0.72903825 -0.72527256
## 17 -0.38413384 -0.38054638
## 18 1.35254678 1.36518597
## 19 -0.55519224 -0.55097348
## 20 -0.61589519 -0.61169119
## 21 0.92496606 0.92348518
## 22 0.40524674 0.40153509
## 23 -0.03477943 -0.03439977
## 24 -1.08450031 -1.08662906
## 25 -0.20686947 -0.20470378
## 26 -0.60702066 -0.60280554
## 27 -0.48523633 -0.48116606
## 28 -0.91157315 -0.90986588
## 29 -0.10792307 -0.10675706
## 30 -0.02416786 -0.02390388
## 31 -0.40707742 -0.40335554
## 32 -0.31019521 -0.30712638
## 33 -0.03477943 -0.03439977
## 34 -0.78870274 -0.78541136
## 35 -0.66671856 -0.66264124
## 36 -0.24771679 -0.24517300
## 37 1.86404796 1.91751830
## 38 0.46312011 0.45913016
## 39 -0.12176477 -0.12045338
## 40 -0.10124404 -0.10014867
## 41 -1.40137235 -1.41662565
## 42 0.66227036 0.65817754
## 43 -0.27002298 -0.26728373
## 44 -0.18889644 -0.18690444
## 45 -0.12311276 -0.12178730
## 46 -0.55321318 -0.54899628
## 47 -0.62902220 -0.62484052
## 48 -0.42290415 -0.41909763
## 49 4.78905369 6.68928292
## 50 -0.33402512 -0.33077585
bird$size <- relevel(bird$size, ref = "L")
bird$mig.status <- relevel(bird$mig.status, ref = "M")
bird.lm2 <- lm(extinct ~ nest.pair + size + mig.status, data = bird)
coef(bird.lm2)
(Intercept) nest.pair sizeS mig.statusR
? ? ? ?
5
model1 = lm(eval ~ students + beauty + gender + native + tenure +
allstudents, data = TeachingRatings)
library(mixlm)
## The last column contains the p-value for that entering predictor
forward(model1, alpha = 1)
## Forward selection, alpha-to-enter: 1
##
## Full model: eval ~ students + beauty + gender + native + tenure + allstudents
##
##
## Step RSS AIC R2pred Cp F value Pr(>F)
## beauty 1 137.16 -559.29 0.027290 42.224 17.0847 4.247e-05 ***
## gender 2 132.81 -572.20 0.053996 28.339 15.0555 0.0001196 ***
## native 3 129.90 -580.45 0.071108 19.718 10.2697 0.0014466 **
## tenure 4 127.83 -585.89 0.082202 14.145 7.4244 0.0066809 **
## allstudents 5 127.61 -584.68 0.079747 15.348 0.7811 0.3772633
## students 6 124.78 -593.07 0.098556 7.000 10.3482 0.0013882 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Call:
## lm(formula = eval ~ beauty + gender + native + tenure + allstudents +
## students, data = TeachingRatings)
##
## Coefficients:
## (Intercept) beauty gender(female) native(no) tenure(no)
## 3.870220 0.138432 -0.112019 -0.154035 0.075163
## allstudents students
## -0.004674 0.007507
## The last column contains the p-value for that deleted predictor
backward(model1, alpha = 0)
## Backward elimination, alpha-to-remove: 0
##
## Full model: eval ~ students + beauty + gender + native + tenure + allstudents
##
##
## Step RSS AIC R2pred Cp F value Pr(>F)
## tenure 1 126.49 -588.78 0.0897079 11.244 6.2438 0.0128136 *
## students 2 129.43 -580.15 0.0701913 19.974 10.6080 0.0012098 **
## allstudents 3 129.90 -580.45 0.0711075 19.718 1.6892 0.1943571
## native 4 132.81 -572.20 0.0539960 28.339 10.2697 0.0014466 **
## gender 5 137.16 -559.29 0.0272904 42.224 15.0555 0.0001196 ***
## beauty 6 142.24 -544.44 -0.0043337 58.800 17.0847 4.247e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Call:
## lm(formula = eval ~ 1, data = TeachingRatings)
PRINTOUT FOR QUESTION 6
6
##
## Coefficients:
## (Intercept)
## 3.998
library(olsrr)
best = ols_step_all_possible(model1)
print(data.frame(best$n, best$predictors, best$adjr, best$cp, best$sbc), row.names = FALSE)
## best.n best.predictors best.adjr best.cp best.sbc
## 1 beauty 0.0336440983 42.22414 769.0594
## 1 gender 0.0202774906 49.15706 775.4197
## 1 native 0.0178779272 50.40165 776.5523
## 1 tenure 0.0146443234 52.07884 778.0742
## 1 students -0.0009085838 60.14573 785.3252
## 1 allstudents -0.0021676828 60.79880 785.9072
## 2 beauty gender 0.0622357175 28.33931 760.2862
## 2 beauty native 0.0521928323 33.53700 765.2182
## 2 beauty tenure 0.0474712403 35.98066 767.5190
## 2 gender tenure 0.0381260335 40.81726 772.0393
## 2 gender native 0.0380771936 40.84254 772.0628
## 2 beauty allstudents 0.0319514458 44.01292 775.0020
## 2 students beauty 0.0316618174 44.16281 775.1405
## 2 native tenure 0.0282227116 45.94272 776.7819
## 2 students allstudents 0.0201501065 50.12069 780.6122
## 2 students gender 0.0185623521 50.94243 781.3619
## 2 gender allstudents 0.0185566784 50.94537 781.3645
## 2 students native 0.0163209829 52.10245 782.4180
## 2 native allstudents 0.0159263309 52.30671 782.6037
## 2 students tenure 0.0154464153 52.55509 782.8295
## 2 tenure allstudents 0.0128486287 53.89957 784.0495
## 3 beauty gender native 0.0807597580 19.71796 756.1789
## 3 beauty gender tenure 0.0796320200 20.30035 756.7466
## 3 beauty gender allstudents 0.0622358241 29.28417 765.4162
## 3 beauty native tenure 0.0617701053 29.52468 765.6461
## 3 students beauty gender 0.0602983185 30.28475 766.3718
## 3 gender native tenure 0.0512302405 34.96772 770.8183
## 3 beauty native allstudents 0.0512173950 34.97436 770.8246
## 3 students beauty native 0.0501292320 35.53631 771.3553
## 3 students beauty allstudents 0.0468556298 37.22688 772.9482
## 3 students beauty tenure 0.0462588429 37.53507 773.2380
## 3 students gender allstudents 0.0454619356 37.94661 773.6247
## 3 beauty tenure allstudents 0.0453966335 37.98034 773.6564
## 3 students native allstudents 0.0387933179 41.39045 776.8481
## 3 students gender tenure 0.0376017064 42.00583 777.4217
## 3 gender native allstudents 0.0370458501 42.29289 777.6891
## 3 students gender native 0.0360600738 42.80196 778.1628
## 3 gender tenure allstudents 0.0360304497 42.81726 778.1770
## 3 students tenure allstudents 0.0340559093 43.83696 779.1245
## 3 students native tenure 0.0278419293 47.04601 782.0934
## 3 native tenure allstudents 0.0261301726 47.93001 782.9080
## 4 beauty gender native tenure 0.0934482578 14.14535 754.8714
## 4 beauty gender native allstudents 0.0821379980 19.97351 760.6121
## 4 students beauty gender allstudents 0.0806589726 20.73565 761.3576
7
## 4 students beauty gender native 0.0792562431 21.45848 762.0635
## 4 beauty gender tenure allstudents 0.0782574480 21.97316 762.5655
## 4 students beauty gender tenure 0.0777005852 22.26011 762.8451
## 4 students beauty native allstudents 0.0664864512 28.03874 768.4407
## 4 students gender native allstudents 0.0645897854 29.01609 769.3804
## 4 students gender tenure allstudents 0.0618615178 30.42197 770.7289
## 4 students beauty native tenure 0.0599779827 31.39255 771.6575
## 4 beauty native tenure allstudents 0.0599499691 31.40699 771.6713
## 4 students beauty tenure allstudents 0.0595644814 31.60563 771.8612
## 4 students gender native tenure 0.0499025857 36.58439 776.5937
## 4 gender native tenure allstudents 0.0493380576 36.87529 776.8687
## 4 students native tenure allstudents 0.0479876826 37.57114 777.5259
## 5 students beauty gender native allstudents 0.1009973714 11.24382 756.1254
## 5 students beauty gender tenure allstudents 0.0959388802 13.84476 758.7233
## 5 beauty gender native tenure allstudents 0.0930148078 15.34825 760.2184
## 5 students beauty gender native tenure 0.0914833819 16.13567 760.9995
## 5 students gender native tenure allstudents 0.0758806913 24.15816 768.8835
## 5 students beauty native tenure allstudents 0.0745627972 24.83579 769.5433
## 6 students beauty gender native tenure allstudents 0.1111959008 7.00000 755.9665
model2 = lm(eval ~ beauty + gender + native, data = TeachingRatings)
summary(model2)
##
## Call:
## lm(formula = eval ~ beauty + gender + native, data = TeachingRatings)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.89116 -0.34453 0.03677 0.39207 1.01321
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3.83661 0.05201 73.772 < 2e-16 ***
## beauty 0.14959 0.03164 4.729 3.01e-06 ***
## gender(female) -0.09870 0.02524 -3.911 0.000106 ***
## native(no) -0.16620 0.05186 -3.205 0.001447 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## s: 0.532 on 459 degrees of freedom
## Multiple R-squared: 0.08673,
## Adjusted R-squared: 0.08076
## F-statistic: 14.53 on 3 and 459 DF, p-value: 4.676e-09
vcov(model2)
## (Intercept) beauty gender(female) native(no)
## (Intercept) 2.704690e-03 -3.009121e-05 9.750756e-05 2.363984e-03
## beauty -3.009121e-05 1.000825e-03 -1.003486e-04 -1.623281e-05
## gender(female) 9.750756e-05 -1.003486e-04 6.369098e-04 -3.313079e-06
## native(no) 2.363984e-03 -1.623281e-05 -3.313079e-06 2.689844e-03
model3 = lm(eval ~ beauty + gender + native + gender*native, data = TeachingRatings)
summary(model3)
8
##
## Call:
## lm(formula = eval ~ beauty + gender + native + gender * native,
## data = TeachingRatings)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.89701 -0.34549 0.02755 0.38939 1.01544
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3.84467 0.05238 73.399 < 2e-16 ***
## beauty 0.15081 0.03163 4.768 2.51e-06 ***
## gender(female) -0.04167 0.05240 -0.795 0.42689
## native(no) -0.15687 0.05238 -2.995 0.00289 **
## gender(female):native(no) 0.06505 0.05240 1.241 0.21507
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## s: 0.5317 on 458 degrees of freedom
## Multiple R-squared: 0.08979,
## Adjusted R-squared: 0.08184
## F-statistic: 11.3 on 4 and 458 DF, p-value: 9.482e-09
vcov(model3)
## (Intercept) beauty gender(female)
## (Intercept) 2.743709e-03 -2.368752e-05 3.958122e-04
## beauty -2.368752e-05 1.000607e-03 -5.520243e-05
## gender(female) 3.958122e-04 -5.520243e-05 2.746194e-03
## native(no) 2.410054e-03 -8.842176e-06 3.421243e-04
## gender(female):native(no) 3.404205e-04 5.136540e-05 2.407011e-03
## native(no) gender(female):native(no)
## (Intercept) 2.410054e-03 0.0003404205
## beauty -8.842176e-06 0.0000513654
## gender(female) 3.421243e-04 0.0024070110
## native(no) 2.743227e-03 0.0003940515
## gender(female):native(no) 3.940515e-04 0.0027457854
anova(lm(eval ~ beauty + native + gender + gender*native, data = TeachingRatings))
## Analysis of Variance Table
##
## Response: eval
## Df Sum Sq Mean Sq F value Pr(>F)
## beauty 1 5.083 5.0830 17.9816 2.7e-05 ***
## native 1 2.924 2.9244 10.3455 0.0013898 **
## gender 1 4.329 4.3287 15.3133 0.0001049 ***
## native:gender 1 0.436 0.4357 1.5412 0.2150686
## Residuals 458 129.467 0.2827
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
9
essay、essay代写