统计建模与R软件第六章习题.docx
《统计建模与R软件第六章习题.docx》由会员分享,可在线阅读,更多相关《统计建模与R软件第六章习题.docx(13页珍藏版)》请在冰点文库上搜索。
统计建模与R软件第六章习题
第六章
1.
a.
>x<-c(5.1,3.5,7.1,6.2,8.8,7.8,4.5,5.6,8.0,6.4)
>y<-c(1907,1287,2700,2373,3260,3000,1947,2273,3113,2493)
>plot(x,y)
X与Y线性相关
b.
>x<-c(5.1,3.5,7.1,6.2,8.8,7.8,4.5,5.6,8.0,6.4)
>y<-c(1907,1287,2700,2373,3260,3000,1947,2273,3113,2493)
>lm.sol<-lm(y~1+x)
>summary(lm.sol)
Call:
lm(formula=y~1+x)
Residuals:
Min1QMedian3QMax
-128.591-70.978-3.72749.263167.228
Coefficients:
EstimateStd.ErrortvaluePr(>|t|)
(Intercept)140.95125.111.1270.293
x364.1819.2618.9086.33e-08***
---
Signif.codes:
0‘***’0.001‘**’0.01‘*’0.05‘.’0.1‘’1
Residualstandarderror:
96.42on8degreesoffreedom
MultipleR-Squared:
0.9781,AdjustedR-squared:
0.9754
F-statistic:
357.5on1and8DF,p-value:
6.33e-08
回归方程为Y=140.95+364.18X,极为显著
d.
>new<-data.frame(x=7)
>lm.pred<-predict(lm.sol,new,interval="prediction",level=0.95)
>lm.pred
fitlwrupr
[1,]2690.2272454.9712925.484
Y(7)=2690.227,[2454.971,2925.484]
2.
>out<-data.frame(
+x1<-c(0.4,0.4,3.1,0.6,4.7,1.7,9.4,10.1,11.6,12.6,10.9,23.1,23.1,21.6,23.1,1.9,26.8,29.9),
+x2<-c(52,34,19,34,24,65,44,31,29,58,37,46,50,44,56,36,58,51),
+x3<-c(158,163,37,157,59,123,46,117,173,112,111,114,134,73,168,143,202,124),
+y<-c(64,60,71,61,54,77,81,93,93,51,76,96,77,93,95,54,168,99)
+)
>lm.sol<-lm(y~x1+x2+x3,data=out)
>summary(lm.sol)
Call:
lm(formula=y~x1+x2+x3,data=out)
Residuals:
Min1QMedian3QMax
-27.575-11.160-2.79911.57448.808
Coefficients:
EstimateStd.ErrortvaluePr(>|t|)
(Intercept)44.929018.34082.4500.02806*
x11.80330.52903.4090.00424**
x2-0.13370.4440-0.3010.76771
x30.16680.11411.4620.16573
---
Signif.codes:
0‘***’0.001‘**’0.01‘*’0.05‘.’0.1‘’1
Residualstandarderror:
19.93on14degreesoffreedom
MultipleR-Squared:
0.551,AdjustedR-squared:
0.4547
F-statistic:
5.726on3and14DF,p-value:
0.009004
回归方程为y=44.9290+1.8033x1-0.1337x2+0.1668x3
由计算结果可以得到,回归系数与回归方程的检验都是显著的
3.
a.
>x<-c(1,1,1,1,2,2,2,3,3,3,4,4,4,5,6,6,6,7,7,7,8,8,8,9,11,12,12,12)
>y<-c(0.6,1.6,0.5,1.2,2.0,1.3,2.5,2.2,2.4,1.2,3.5,4.1,5.1,5.7,3.4,9.7,8.6,4.0,5.5,10.5,17.5,13.4,4.5,
+30.4,12.4,13.4,26.2,7.4)
>lm.sol<-lm(y~1+x)
>summary(lm.sol)
Call:
lm(formula=y~1+x)
Residuals:
Min1QMedian3QMax
-9.84130-2.33691-0.021371.0592117.83201
Coefficients:
EstimateStd.ErrortvaluePr(>|t|)
(Intercept)-1.45191.8353-0.7910.436
x1.55780.28075.5497.93e-06***
---
Signif.codes:
0‘***’0.001‘**’0.01‘*’0.05‘.’0.1‘’1
Residualstandarderror:
5.168on26degreesoffreedom
MultipleR-Squared:
0.5422,AdjustedR-squared:
0.5246
F-statistic:
30.8on1and26DF,p-value:
7.931e-06
线性回归方程为y=-1.4519+1.5578x,并且未通过t检验和F检验
>plot(x,y)
>abline(-1.4519,1.5578)
>
c.
>x<-c(1,1,1,1,2,2,2,3,3,3,4,4,4,5,6,6,6,7,7,7,8,8,8,9,11,12,12,12)
>y<-c(0.6,1.6,0.5,1.2,2.0,1.3,2.5,2.2,2.4,1.2,3.5,4.1,5.1,5.7,3.4,9.7,8.6,4.0,5.5,10.5,17.5,13.4,4.5,
+30.4,12.4,13.4,26.2,7.4)
>y.res<-resid(lm.sol);y.fit<-predict(lm.sol)
>plot(y.res~y.fit)
>y.rst<-rstandard(lm.sol)
>plot(y.rst~y.fit)
>
普通残差
标准化残差
d.
(4)
>lm.new<-update(lm.data3,sqrt(.)~.);coef(lm.new)
(Intercept)x
0.76650180.2913620
>plot(x,y)
>lines(x,y=0.5875222+0.08489065*x^2+0.4466549*x)
>y.res<-resid(lm.new);y.fit<-predict(lm.new)
>plot(y.res~y.fit)
>y.rst<-rstandard(lm.new)
>plot(y.rst~y.fit)
4.
>lm.sol<-lm(Y~X1+X2,data=toothpaste)
>summary(lm.sol)
Call:
lm(formula=Y~X1+X2,data=toothpaste)
Residuals:
Min1QMedian3QMax
-0.497785-0.120312-0.0086720.1108440.581059
Coefficients:
EstimateStd.ErrortvaluePr(>|t|)
(Intercept)4.40750.72236.1021.62e-06***
X11.58830.29945.3041.35e-05***
X20.56350.11914.7336.25e-05***
---
Signif.codes:
0‘***’0.001‘**’0.01‘*’0.05‘.’0.1‘’1
Residualstandarderror:
0.2383on27degreesoffreedom
MultipleR-squared:
0.886,AdjustedR-squared:
0.8776
F-statistic:
105on2and27DF,p-value:
1.845e-13
>source("Reg_Diag.R");Reg_Diag(lm.sol)
residuals1standards2students3hat_matrixs4DFFITS
1-0.047231639-0.21248023-0.208682840.13012303-0.08071140
2-0.098070223-0.42151698-0.415005220.04704358-0.09220771
30.0742886240.334921160.329345250.133859550.12947381
4-0.006645926-0.03003380-0.029472870.13797634-0.01179139
50.581059204*2.55701395*2.88236603*0.090917190.91152886
6-0.107785364-0.46031439-0.453492580.03475104-0.08604674
70.3007583501.315324561.334189930.079553820.39223739
80.4248103602.05723960*2.19842345*0.24932956*1.26699112
9-0.027532493-0.12804079-0.125685450.18600211-0.06008033
10-0.026629932-0.11546376-0.113333350.06356511-0.02952761
11-0.497785364-2.12587089*-2.28622467*0.03475104-0.43379359
120.0619137820.265403050.260782200.041942150.05456415
130.1128163440.489680550.482674930.065569080.12785881
14-0.150249714-0.65395500-0.646873880.07069034-0.17841010
150.1049270890.451122920.444367840.047611070.09935493
160.1543413750.663194900.656164010.046520750.14493729
170.0576395410.253600110.249156420.090566330.07862668
18-0.146012230-0.64156026-0.634421690.08813106-0.19723152
19-0.124487198-0.53776150-0.530557950.05659354-0.12994717
20-0.040713930-0.18584177-0.182484540.15505538-0.07817275
21-0.199843357-0.88485598-0.881185860.10202733-0.29702583
22-0.359558498-1.59386063-1.643282650.10408399-0.56010648
23-0.387785364-1.65609855-1.714554460.03475104-0.32532355
24-0.010697936-0.04979066-0.048862150.18729463-0.02345680
25-0.283315771-1.25181018-1.265687820.09823492-0.41774652
260.0178556550.082305630.080777200.171444880.03674433
270.2792860701.274822111.290430740.155055380.55279486
280.0224835010.098647050.096820470.085489850.02960262
290.1748951000.765136250.759108250.080172070.22411038
300.1472699420.662814700.655781620.130893830.25449685
s5cooks_distances6COVRATIOs7
12.251190e-031.2809524
22.923723e-031.1521157
35.778631e-031.2769056
44.812656e-051.2989982
5*2.179654e-010.5361673*
62.542824e-031.1330964
74.984335e-020.9974548
8*4.685683e-01*0.8945237
91.248734e-031.3732722
103.016556e-041.1941261
115.423517e-020.6696819
121.027897e-031.1597812
135.608624e-031.1668135
141.084362e-021.1487064
153.391268e-031.1494743
167.153137e-031.1180504
172.134879e-031.2226245
181.326021e-021.1728006
195.782640e-031.1493238
202.112633e-031.3203083
212.965359e-021.1417402
229.837757e-020.9293112
233.291392e-020.8413370
241.904437e-041.3775853
255.690208e-021.0379534
264.672410e-041.3505881
279.941147e-021.1001732
283.032306e-041.2232439
291.700877e-021.1399973
302.205512e-021.2266609
>toothpaste<-data.frame(
+X1=c(-0.05,0.25,0.60,0,0.20,0.15,-0.15,0.15,
+0.10,0.40,0.45,0.35,0.30,0.50,0.50,0.40,-0.05,
+-0.05,-0.10,0.20,0.10,0.50,0.60,-0.05,0,0.05,0.55),
+X2=c(5.50,6.75,7.25,5.50,6.50,6.75,5.25,6.00,
+6.25,7.00,6.90,6.80,6.80,7.10,7.00,6.80,6.50,
+6.25,6.00,6.50,7.00,6.80,6.80,6.50,5.75,5.80,6.80),
+Y=c(7.38,8.51,9.52,7.50,8.28,8.75,7.10,8.00,
+8.15,9.10,8.86,8.90,8.87,9.26,9.00,8.75,7.95,
+7.65,7.27,8.00,8.50,8.75,9.21,8.27,7.67,7.93,9.26)
+)
>
>lm.sol<-lm(Y~X1+X2,data=toothpaste)
>summary(lm.sol)
Call:
lm(formula=Y~X1+X2,data=toothpaste)
Residuals:
Min1QMedian3QMax
-0.37130-0.101140.030660.100160.30162
Coefficients:
EstimateStd.ErrortvaluePr(>|t|)
(Intercept)4.07590.62676.5041.00e-06***
X11.52760.23546.4891.04e-06***
X20.61380.10275.9743.63e-06***
---
Signif.codes:
0‘***’0.001‘**’0.01‘*’0.05‘.’0.1‘’1
Residualstandarderror:
0.1767on24degreesoffreedom
MultipleR-squared:
0.9378,AdjustedR-squared:
0.9327
F-statistic:
181on2and24DF,p-value:
3.33e-15
5.
XX<-cor(cement[1:
4])
kappa(XX,exact=TRUE)
[1]1376.881
>eigen(XX)
$values
[1]2.2357040351.5760660700.1866061490.001623746
$vectors
[,1][,2][,3][,4]
[1,]-0.47595520.50897940.67550020.2410522
[2,]-0.5638702-0.4139315-0.31442040.6417561
[3,]0.3940665-0.60496910.63769110.2684661
[4,]0.54793120.4512351-0.19542100.6767340
删去了X3,X4
>cement<-data.frame(
+X1=c(7,1,11,11,7,11,3,1,2,21,1,11,10),
+X2=c(26,29,56,31,52,55,71,31,54,47,40,66,68),
+Y=c(78.5,74.3,104.3,87.6,95.9,109.2,102.7,72.5,
+93.1,115.9,83.8,113.3,109.4)
+)
>XX<-cor(cement[1:
2])
>kappa(XX,exact=TRUE)
[1]1.592620
复共线性消失了