统计建模与R软件第六章习题(共10页).doc
精选优质文档-倾情为你奉上第六章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(y1+x)> summary(lm.sol)Call:lm(formula = y 1 + x)Residuals: Min 1Q Median 3Q Max -128.591 -70.978 -3.727 49.263 167.228 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 140.95 125.11 1.127 0.293 x 364.18 19.26 18.908 6.33e-08 *-Signif. codes: 0 * 0.001 * 0.01 * 0.05 . 0.1 1 Residual standard error: 96.42 on 8 degrees of freedomMultiple R-Squared: 0.9781, Adjusted R-squared: 0.9754 F-statistic: 357.5 on 1 and 8 DF, 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 fit lwr upr1, 2690.227 2454.971 2925.484Y(7)= 2690.227, 2454.971,2925.4842> 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(yx1+x2+x3,data=out)> summary(lm.sol)Call:lm(formula = y x1 + x2 + x3, data = out)Residuals: Min 1Q Median 3Q Max -27.575 -11.160 -2.799 11.574 48.808 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 44.9290 18.3408 2.450 0.02806 * x1 1.8033 0.5290 3.409 0.00424 *x2 -0.1337 0.4440 -0.301 0.76771 x3 0.1668 0.1141 1.462 0.16573 -Signif. codes: 0 * 0.001 * 0.01 * 0.05 . 0.1 1 Residual standard error: 19.93 on 14 degrees of freedomMultiple R-Squared: 0.551, Adjusted R-squared: 0.4547 F-statistic: 5.726 on 3 and 14 DF, p-value: 0. 回归方程为 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: Min 1Q Median 3Q Max -9.84130 -2.33691 -0.02137 1.05921 17.83201 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -1.4519 1.8353 -0.791 0.436 x 1.5578 0.2807 5.549 7.93e-06 *-Signif. codes: 0 * 0.001 * 0.01 * 0.05 . 0.1 1 Residual standard error: 5.168 on 26 degrees of freedomMultiple R-Squared: 0.5422, Adjusted R-squared: 0.5246 F-statistic: 30.8 on 1 and 26 DF, 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.resy.fit)> y.rst<-rstandard(lm.sol)> plot(y.rsty.fit)>普通残差标准化残差d(4)> lm.new<-update(lm.data3,sqrt(.).);coef(lm.new)(Intercept) x 0. 0. > plot(x,y)> lines(x,y=0.+0.*x2+0.*x)> y.res<-resid(lm.new);y.fit<-predict(lm.new)> plot(y.resy.fit)> y.rst<-rstandard(lm.new)> plot(y.rsty.fit)4> lm.sol<-lm(YX1+X2,data=toothpaste)> summary(lm.sol)Call:lm(formula = Y X1 + X2, data = toothpaste)Residuals: Min 1Q Median 3Q Max -0. -0. -0. 0. 0. Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 4.4075 0.7223 6.102 1.62e-06 *X1 1.5883 0.2994 5.304 1.35e-05 *X2 0.5635 0.1191 4.733 6.25e-05 *-Signif. codes: 0 * 0.001 * 0.01 * 0.05 . 0.1 1 Residual standard error: 0.2383 on 27 degrees of freedomMultiple R-squared: 0.886, Adjusted R-squared: 0.8776 F-statistic: 105 on 2 and 27 DF, p-value: 1.845e-13 > source("Reg_Diag.R");Reg_Diag(lm.sol) residual s1 standard s2 student s3 hat_matrix s4 DFFITS1 -0. -0. -0. 0. -0.2 -0. -0. -0. 0. -0.3 0. 0. 0. 0. 0.4 -0. -0. -0. 0. -0.5 0. * 2. * 2. * 0. 0.6 -0. -0. -0. 0. -0.7 0. 1. 1. 0. 0.8 0. 2. * 2. * 0. * 1.9 -0. -0. -0. 0. -0.10 -0. -0. -0. 0. -0.11 -0. -2. * -2. * 0. -0.12 0. 0. 0. 0. 0.13 0. 0. 0. 0. 0.14 -0. -0. -0. 0. -0.15 0. 0. 0. 0. 0.16 0. 0. 0. 0. 0.17 0. 0. 0. 0. 0.18 -0. -0. -0. 0. -0.19 -0. -0. -0. 0. -0.20 -0. -0. -0. 0. -0.21 -0. -0. -0. 0. -0.22 -0. -1. -1. 0. -0.23 -0. -1. -1. 0. -0.24 -0. -0. -0. 0. -0.25 -0. -1. -1. 0. -0.26 0. 0. 0. 0. 0.27 0. 1. 1. 0. 0.28 0. 0. 0. 0. 0.29 0. 0. 0. 0. 0.30 0. 0. 0. 0. 0. s5 cooks_distance s6 COVRATIO s71 2.e-03 1. 2 2.e-03 1. 3 5.e-03 1. 4 4.e-05 1. 5 * 2.e-01 0. *6 2.e-03 1. 7 4.e-02 0. 8 * 4.e-01 * 0. 9 1.e-03 1. 10 3.e-04 1. 11 5.e-02 0. 12 1.e-03 1. 13 5.e-03 1. 14 1.e-02 1. 15 3.e-03 1. 16 7.e-03 1. 17 2.e-03 1. 18 1.e-02 1. 19 5.e-03 1. 20 2.e-03 1. 21 2.e-02 1. 22 9.e-02 0. 23 3.e-02 0. 24 1.e-04 1. 25 5.e-02 1. 26 4.e-04 1. 27 9.e-02 1. 28 3.e-04 1. 29 1.e-02 1. 30 2.e-02 1. > 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(YX1+X2,data=toothpaste)> summary(lm.sol)Call:lm(formula = Y X1 + X2, data = toothpaste)Residuals: Min 1Q Median 3Q Max -0.37130 -0.10114 0.03066 0.10016 0.30162 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 4.0759 0.6267 6.504 1.00e-06 *X1 1.5276 0.2354 6.489 1.04e-06 *X2 0.6138 0.1027 5.974 3.63e-06 *-Signif. codes: 0 * 0.001 * 0.01 * 0.05 . 0.1 1 Residual standard error: 0.1767 on 24 degrees of freedomMultiple R-squared: 0.9378, Adjusted R-squared: 0.9327 F-statistic: 181 on 2 and 24 DF, p-value: 3.33e-15 5XX<-cor(cement1:4)kappa(XX,exact=TRUE)1 1376.881 > eigen(XX)$values1 2. 1. 0. 0.$vectors ,1 ,2 ,3 ,41, -0. 0. 0. 0.2, -0. -0. -0. 0.3, 0. -0. 0. 0.4, 0. 0. -0. 0.删去了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(cement1:2)> kappa(XX,exact=TRUE)1 1.复共线性消失了专心-专注-专业