《統計建模與R軟件》薛毅原書課后習題答案_第1頁
《統計建模與R軟件》薛毅原書課后習題答案_第2頁
《統計建模與R軟件》薛毅原書課后習題答案_第3頁
《統計建模與R軟件》薛毅原書課后習題答案_第4頁
《統計建模與R軟件》薛毅原書課后習題答案_第5頁
已閱讀5頁,還剩45頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、第二章答案:x-c(1,2,3)y-c(4,5,6)e-c(1,1,1)z=2*x+y+ez1=crossprod(x,y)#z1為x1與x2的內積或者x%*%yz2=tcrossprod(x,y)#z1為x1與x2的外積或者x%o%yz;z1;z2要點:基本的列表賦值方法,內積和外積概念。內積為標量,外積為矩陣。A-matrix(1:20,c(4,5);AB-matrix(1:20,nrow=4,byrow=TRUE);BC=A+B;C#不存在AB這種寫法E=A*B;EF-A1:3,1:3;FH-matrix(c(1,2,4,5),nrow=1);H#H起過渡作用,不規則的數組下標G-B,H

2、;G要點:矩陣賦值方法。默認是byrow=FALSE數據按列放置。取出部分數據的方法。可以用數組作為數組的下標取出數組元素。x-c(rep(1,times=5),rep(2,times=3),rep(3,times=4),rep(4,times=2);x#或者省略times=,如下面的形式x-c(rep(1,5),rep(2,3),rep(3,4),rep(4,2);x要點:rep()的使用方法。rep(a,b)即將a重復b次n-5;H-array(0,dim=c(n,n)for(iin1:n)for(jin1:n)Hi,j-1/(i+j-1);HG-solve(H);G#求H的逆矩陣ev-e

3、igen(H);ev#求H的特征值和特征向量要點:數組初始化;for循環的使用待解決:如何將很長的命令(如for循環)用幾行打出來再執行?每次想換行的時候一按回車就執行了還沒打完的命令.StudentData-data.frame(name=c(zhangsan,lisi,wangwu,zhaoliu,dingyi),sex=c(F,M,F,M,F),age=c(14,15,16,14,15),height=c(156,165,157,162,159),weight=c(42,49,41.5,52,45.5);StudentData要點:數據框的使用待解決:SSH登陸linux服務器中文顯示亂

4、碼。此處用英文代替。write.table(StudentData,file=studentdata.txt)#把數據框StudentData在工作目錄里輸出,輸出的文件名為studentdata.txt.StudentData_a-read.table(studentdata.txt);StudentData_a#以數據框的形式讀取文檔,存入數據框StudentData_a中。write.csv(StudentData_a,studentdata.csv)#把數據框StudentData_a在工作目錄里輸出,輸出的文件名為studentdata.csv可用Excel打開.要點:讀寫文件。re

5、ad.table(file)write.table(Rdata,file)read.csv(file)write.csv(Rdata,file)外部文件,不論是待讀入或是要寫出的,命令中都得加雙引號。Fun-function(n)if(n=0)list(fail=pleaseinputaintegerabove0!)elserepeatif(n=1)breakelseif(n%2=0)n-n/2elsen-3*n+1list(sucess!)在linux下新建一個R文件,輸入上述代碼,保存為2.7.R然后在當前目錄下進入R環境,輸入source(2.7.R),即打開了這個程序腳本。然后就可以執

6、行函數了。輸入Fun(67),顯示sucess!輸入Fun(-1),顯示$fail1pleaseinputaintegerabove0!待解決:source(*.R)是可以理解為載入這個R文件吧?如何在R環境下關閉R文件呢?OK,自己寫的第一個R程序新建txt文件如下:編寫一個函數(程序名為)描述樣本的各種描述性統計量。data_outline-function(x)n-length(x)m-mean(x)v-var(x)s-sd(x)me-median(x)cv-100*s/mcss-sum(x-m)2)uss-sum(x2)R-max(x)-min(x)R1-quantile(x,3/4)

7、-quantile(x,1/4)sm-s/sqrt(n)g1-n/(n-1)*(n-2)*sum(x-m)A3)/sA3g2-(n*(n+1)/(n-1)*(n-2)*(n-3)*sum(x-m)A4)/sA4-(3*(n-1)A2)/(n-2)*(n-3)data.frame(N=n,Mean=m,Var=v,std_dev=s,Median=me,std_mean=sm,CV=cv,CSS=css,USS=uss,R=R,R1=R1,Skewness=g1,Kurtosis=g2,s=1)進入R,source(data_outline.R)#將程序調入內存serumdata

8、-scan(3.1.txt);serumdata#將數據讀入向量serumdata。data_outline(serumdata)結果如下:NMeanVarstd_devMedianstd_meanCVCSSUSSR110073.69615.416753.92641773.50.39264175.3278571526.258544636.320R1SkewnessKurtosis要點:read.table()用于讀表格形式的文件。上述形式的數據由于第七行缺幾個數據,故用read.table()不能讀入。scan()可以直接讀純文本文件。scan()和matrix()連用還可以將數據存放成矩陣形

9、式。X-matrix(scan(3.1.txt,0),ncol=10,byrow=TRUE)#將上述數據放置成10*10的矩陣。scan()還可以從屏幕上直接輸入數據。Yhist(serumdata,freq=FALSE,col=purple,border=red,density=3,angle=60,main=paste(thehistogramofserumdata),xlab=age,ylab=frequency)#直方圖。col是填充顏色。默認空白。border是邊框的顏色,默認前景色。density是在圖上畫條紋陰影,默認不畫。angle是條紋陰影的傾斜角度(逆時針方向),默認45度

10、。main,xlab,ylab是標題,x和y坐標軸名稱。lines(density(serumdata),col=blue)#密度估計曲線。xboxplot(serumdata,col=lightblue,notch=T)#作箱線圖。notch表示帶有缺口。fivenum(serumdata)#五數總結shapiro.test(serumdata)#正態性Shapori-Wilk檢驗方法Shapiro-Wilknormalitytestdata:serumdata結論:p值,可認為來自正態分布的總體。ks.test(serumdata,pnorm,mean(serumdata),sd(seru

11、mdata)#Kolmogrov-SmirnoV檢驗,正態性One-sampleKolmogorov-Smirnovtestdata:serumdataalternativehypothesis:two-sidedWarningmessage:Inks.test(serumdata,pnorm,mean(serumdata),sd(serumdata):cannotcomputecorrectp-valueswithties結論:p值,可認為來自正態分布的總體。注意,這里的警告信息,是因為數據中有重復的數值,ks檢驗要求待檢數據時連續的,不允許重復值。y-c(2,4,3,2,4,7,7,2,2

12、,5,4,5,6,8,5,10,7,12,12,6,6,7,11,6,6,7,9,5,5,10,6,3,10)#輸入數據f-factor(c(rep(1,11),rep(2,10),rep(3,12)#因子分類plot(f,y,col=lightgreen)#plot(生成箱線圖x-c(2,4,3,2,4,7,7,2,2,5,4)y-c(5,6,8,5,10,7,12,12,6,6)z-c(7,11,6,6,7,9,5,5,10,6,3,10)boxplot(x,y,z,names=c(1,2,3),col=c(5,6,7)#boxplot()生成箱線圖結論:第2和第3組沒有顯著差異。第1組合

13、其他兩組有顯著差異。數據太多,懶得錄入。離散圖應該用plot即可。studatanames(studata)attach(studata)#將數據框調入內存plot(weightheight,col=red)#體重對于身高的散點圖coplot(weightheight|sex,col=blue)#不同性別,體重與身高的散點圖coplot(weightheight|age,col=blue)#不同年齡,體重與身高的散點圖coplot(weightheight|age+sex,col=blue)#不同年齡和性別,體重與身高的散點圖xy-seq(-1,7,0.05)f-function(x,y)xA

14、4-2*xA2*y+xA2-2*x*y+2*yA2+4.5*x-4*y+4zt.test(x,alternative=less,mu=72)#t.test()做單樣本正態分布單側區間估計OneSamplet-testdata:xalternativehypothesis:truemeanislessthan7295percentconfidenceinterval:sampleestimates:meanofxp值小于,拒絕原假設,平均脈搏低于常人。要點:t.test()函數的用法。本例為單樣本;可做雙邊和單側檢驗。xv-c(140,137,136,140,145,148,140,135,14

15、4,141);x1140137136140145148140135144141yv-c(135,118,115,140,128,131,130,115,131,125);y1135118115140128131130115131125t.test(x,y,varequal=TRUE)TwoSamplet-testdata:xandyalternativehypothesis:truedifferenceinmeansisnotequalto095percentconfidenceinterval:sampleestimates:meanofxmeanofy140.6期望差的95%置信區間為7.

16、5362620.06374。要點:t.test()可做兩正態樣本均值差估計。此例認為兩樣本方差相等。ps:我怎么覺得這題應該用配對t檢驗?xv-c(0.143,0.142,0.143,0.137)yv-c(0.140,0.142,0.136,0.138,0.140)t.test(x,y,varequal=TRUE)TwoSamplet-testdata:xandyalternativehypothesis:truedifferenceinmeansisnotequalto095percentconfidenceinterval:-0.001996351sampleestimates:meano

17、fxmeanofy0.14125期望差的95%的區間估計為-0.001996351接var.test(x,y)Ftesttocomparetwovariancesdata:xandyalternativehypothesis:trueratioofvariancesisnotequalto195percentconfidenceinterval:sampleestimates:ratioofvariances要點:可做兩樣本方差比的估計。基于此結果可認為方差不等。因此,在中,計算期望差時應該采取方差不等的參數。t.test(x,y)WelchTwoSamplet-testdata:xandya

18、lternativehypothesis:truedifferenceinmeansisnotequalto095percentconfidenceinterval:sampleestimates:meanofxmeanofy140.6期望差的95%置信區間為7.35971320.240287。要點:t.test(x,y,var.equal=TRUE)做方差相等的兩正態樣本的均值差估計t.test(x,y)做方差不等的兩正態樣本的均值差估計xmean(x)-tmp;mean(x)+tmp平均呼喚次數為的置信區間為1.49,2,32xv-c(1067,919,1196,785,1126,936,

19、918,1156,920,948)t.test(x,alternative=greater)OneSamplet-testdata:xalternativehypothesis:truemeanisgreaterthan095percentconfidenceinterval:920.8443Infsampleestimates:meanofx燈泡平均壽命置信度95%的單側置信下限為920.8443要點:t.test()做單側置信區間估計xv-c(220,188,162,230,145,160,238,188,247,113,126,245,164,231,256,183,190,158,22

20、4,175)t.test(x,mu=225)OneSamplet-testdata:xalternativehypothesis:truemeanisnotequalto22595percentconfidenceinterval:sampleestimates:上述檢驗是雙邊檢驗。也可米用單邊檢驗。備擇假設:油漆工人的血小板計數小于正常年男子。t.test(x,mu=225,alternative=less)OneSamplet-testdata:xalternativehypothesis:truemeanislessthan22595percentconfidenceinterval:s

21、ampleestimates:meanofx同樣可得出油漆工人的血小板計數小于正常成年男子的結論。pnorm(1000,mean(x),sd(x)x11067919119678511269369181156920948pnorm(1000,mean(x),sd(x)xv=1000的概率為0.509,故x大于1000的概率為0.491.要點:pnorm計算正態分布的分布函數。在R軟件中,計算值均為下分位點。Av-c(113,120,138,120,100,118,138,123)Bv-c(138,116,125,136,110,132,130,110)t.test(A,B,paired=TRUE

22、)Pairedt-testdata:AandBalternativehypothesis:truedifferenceinmeansisnotequalto095percentconfidenceinterval:-15.62889sampleestimates:meanofthedifferencesp值大于,接受原假設,兩種方法治療無差異。(1)正態性W檢驗:xv-c(-0.7,-5.6,2,2.8,0.7,3.5,4,5.8,7.1,-0.5,2.5,-1.6,1.7,3,0.4,4.5,4.6,2.5,6,-1.4)yv-c(3.7,6.5,5,5.2,0.8,0.2,0.6,3.4,

23、6.6,-1.1,6,3.8,2,1.6,2,2.2,1.2,3.1,1.7,-2)shapiro.test(x)Shapiro-Wilknormalitytestdata:xshapiro.test(y)Shapiro-Wilknormalitytestdata:yks檢驗:ks.test(x,pnorm,mean(x),sd(x)One-sampleKolmogorov-Smirnovtest(2)data:xalternativehypothesis:two-sidedWarningmessage:Inks.test(x,pnorm,mean(x),sd(x):cannotcompute

24、correctp-valueswithtiesks.test(y,pnorm,mean(y),sd(y)One-sampleKolmogorov-Smirnovtestdata:yalternativehypothesis:two-sidedWarningmessage:Inks.test(y,pnorm,mean(y),sd(y):cannotcomputecorrectp-valueswithtiespearson擬合優度檢驗,以x為例。sort(x)1-5.6-1.6-1.4-0.7-1.72.03.03.56.0 x1v-tab

25、le(cut(x,br=c(-6,-3,0,3,6,9)pv-pnorm(c(-3,0,3,6,9),mean(x),sd(x)ppchisq.test(x1,p=p)Chi-squaredtestforgivenprobabilitiesdata:x1Warningmessage:Inchisq.test(x1,p=p):Chi-squaredapproximationmaybeincorrectp值為,接受原假設,x符合正態分布。方差相同模型t檢驗:t.test(x,y,varequal=TRUE)TwoSamplet-testdata:xandyalternativehypothesis

26、:truedifferenceinmeansisnotequalto095percentconfidenceinterval:-2.326179sampleestimates:meanofxmeanofy2.065方差不同模型t檢驗:t.test(x,y)WelchTwoSamplet-testdata:xandyalternativehypothesis:truedifferenceinmeansisnotequalto095percentconfidenceinterval:-2.32926sampleestimates:meanofxmeanofy2.065配對t檢驗:t.test(x,

27、y,paired=TRUE)Pairedt-testdata:xandyalternativehypothesis:truedifferenceinmeansisnotequalto095percentconfidenceinterval:-2.373146sampleestimates:meanofthedifferences三種檢驗的結果都顯示兩組數據均值無差異。(3)方差檢驗:var.test(x,y)Ftesttocomparetwovariancesdata:xandyalternativehypothesis:trueratioofvariancesisnotequalto195p

28、ercentconfidenceinterval:sampleestimates:ratioofvariances接受原假設,兩組數據方差相同。a-c(126,125,136,128,123,138,142,116,110,108,115,140)bks.test(b,pnorm,mean(b),sd(b)One-sampleKolmogorov-Smirnovtestdata:balternativehypothesis:two-sidedWarningmessage:Inks.test(b,pnorm,mean(b),sd(b):cannotcomputecorrectp-valueswi

29、thtiesa和b都服從正態分布。方差齊性檢驗:var.test(a,b)Ftesttocomparetwovariancesdata:aandbalternativehypothesis:trueratioofvariancesisnotequalto195percentconfidenceinterval:sampleestimates:ratioofvariances可認為a和b的方差相冋。選用方差相同模型t檢驗:t.test(a,b,var.equal=TRUE)TwoSamplet-testdata:aandbalternativehypothesis:truedifferencei

30、nmeansisnotequalto095percentconfidenceinterval:sampleestimates:meanofxmeanofy125.5833可認為兩者有差別。二項分布總體的假設檢驗:binom.test(57,400,p=0.147)Exactbinomialtestdata:57and40095percentconfidenceinterval:sampleestimates:probabilityofsuccessP值,故接受原假設,表示調查結果支持該市老年人口的看法二項分布總體的假設檢驗:binom.test(178,328,p=0.5,alternativ

31、e=greater)Exactbinomialtestdata:178and32895percentconfideneeinterval:sampleestimates:probabilityofsuccess不能認為這種處理能增加母雞的比例。利用pearson卡方檢驗是否符合特定分布:chisqtest(c(315,101,108,32),p=c(9,3,3,1)/16)Chi-squaredtestforgivenprobabilitiesdata:c(315,101,108,32)接受原假設,符合自由組合定律。利用pearson卡方檢驗是否符合泊松分布:nv-length(z)yv-c(

32、92,68,28,11,1,0)xv-0:5qv-ppois(x,mean(rep(x,y);nv-length(y)p1v-q1;pn=1-qn-1chisqtest(y,p=p)Chi-squaredtestforgivenprobabilitiesdata:yWarningmessage:Inchisq.test(y,p=p):Chi-squaredapproximationmaybeincorrect重新分組,合并頻數小于5的組:zv-c(92,68,28,12)nv-length(z);pv-p1:n-1;pnv-1-qn-1chisq.test(z,p=p)Chi-squaredt

33、estforgivenprobabilitiesdata:z可認為數據服從泊松分布。ks檢驗兩個分布是否相同:xv-c(2.36,3.14,752,3.48,2.76,5.43,6.54,7.41)yv-c(4.38,4.25,6.53,3.28,7.21,6.55)ks.test(x,y)Two-sampleKolmogorov-Smirnovtestdata:xandyalternativehypothesis:two-sided列聯數據的獨立性檢驗:xv-c(358,2492,229,2745)dim(x)v-c(2,2)chisq.test(x)PearsonsChi-squaredt

34、estwithYatescontinuitycorrectiondata:xP值0.05,拒絕原假設,有影響。列聯數據的獨立性檢驗:TOC o 1-5 h zy,1,2,31,4512102,4620283,2823304,111235chisq.test(y)PearsonsChi-squaredtestdata:yP值0.05,拒絕原假設,不獨立,有關系。因有的格子的頻數小于5,故采用fiser確切概率法檢驗獨立性。fisher.test(x)FishersExactTestforCountDatadata:xalternativehypothesis:trueoddsratioisnot

35、equalto195percentconfideneeinterval:sampleestimates:oddsratiop值大于,兩變量獨立,兩種工藝對產品的質量沒有影響。由于是在相同個體上的兩次試驗,故采用McNemar檢驗。mcnemar.test(x)McNemarsChi-squaredtestdata:xH1:中位數xv-c(13.32,13.06,14.02,11.86,13.58,13.77,13.51,14.42,14.44,15.43)binomtest(sum(x)146,length(x),al=l)Exactbinomialtestdata:sum(x)14.6and

36、length(x)95percentconfidenceinterval:sampleestimates:probabilityofsuccess拒絕原假設,中位數小于Wilcoxon符號秩檢驗:wilcox.test(x,mu=146,al=l,exact=F)data:xandyWilcoxonsignedranktestwithcontinuitycorrectiondata:x拒絕原假設,中位數小于符號檢驗法:xv-c(48,33,37.5,48,42.5,40,42,36,11.3,22,36,27.3,14.2,32.1,52,38,17.3,20,21,46.1)yy),leng

37、th(x)Exactbinomialtestdata:sum(xy)andlength(x)95percentconfidenceinterval:sampleestimates:probabilityofsuccess接受原假設,無差別。Wilcoxon符號秩檢驗:wilcox.test(x,y,paired=TRUE,exact=FALSE)Wilcoxonsignedranktestwithcontinuitycorrectiondata:xandyalternativehypothesis:truelocationshiftisnotequalto0拒絕原假設,有差別。Wilcoxon

38、秩和檢驗:wilcox.test(x,y,exact=FALSE)Wilcoxonranksumtestwithcontinuitycorrectionalternativehypothesis:truelocationshiftisnotequalto0One-sampleKolmogorov-Smirnovtestdata:xalternativehypothesis:two-sidedWarningmessage:Inks.test(x,pnorm,mean(x),sd(x):cannotcomputecorrectp-valueswithtiesks.test(y,pnorm,mean

39、(y),sd(y)One-sampleKolmogorov-Smirnovtestdata:yalternativehypothesis:two-sided兩組數據均服從正態分布。方差齊性檢驗:var.test(x,y)Ftesttocomparetwovariancesdata:xandyalternativehypothesis:trueratioofvariancesisnotequalto195percentconfidenceinterval:sampleestimates:ratioofvariances可認為兩組數據方差相同。綜上,該數據可做t檢驗。t檢驗:t.test(x,y,

40、varequal=TRUE)TwoSamplet-testalternativehypothesis:truedifferenceinmeansisnotequalto095percentconfidenceinterval:sampleestimates:meanofxmeanofy33.215拒絕原假設,有差別。綜上所述,Wilcoxon符號秩檢驗的差異檢出能力最強,符號檢驗的差異檢出最弱。spearman秩相關檢驗:xv-c(24,17,20,41,52,23,46,18,15,20)yv-c(8,1,4,7,9,5,10,3,2,6)cor.test(x,y,method=spearm

41、an,exact=F)Spearmansrankcorrelationrhodata:xandyalternativehypothesis:truerhoisnotequalto0sampleestimates:rhokendall秩相關檢驗:cor.test(x,y,method=kendall,exact=F)Kendallsrankcorrelationtaudata:xandyalternativehypothesis:truetauisnotequalto0sampleestimates:tau二者有關系,呈正相關。xv-rep(1:5,c(0,1,9,7,3);yv-rep(1:5

42、,c(2,2,11,4,1)wilcox.test(x,y,exact=F)Wilcoxonranksumtestwithcontinuitycorrectionalternativehypothesis:truelocationshiftisnotequalto0p值大于,不能拒絕原假設,尚不能認為新方法的療效顯著優于原療法。(1)x-c(5.1,3.5,7.1,6.2,8.8,7.8,4.5,5.6,8.0,6.4)yCJ由此判斷,Y和X有線性關系。lm.solv-lm(y1+x)summary(lm.sol)Call:lm(formula=y1+x)Residuals:Min1QMedi

43、an3QMax-128.591-70.978-3.72749.263Coefficients:EstimateStd.ErrortvaluePr(|t|)(Intercept)140.95125.111.1270.293x364.1819.2618.9086.33e-08*Signif.codes:0*0.001*0.01*0.05.0.11Residualstandarderror:96.42on8degreesoffreedomlm.predv-predict(lm.sol,new,interval=prediction)fitIwrupr故Y(7)=2690.227,2454.971,2

44、925.484(1)phov-data.frame(x1v-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),x2v-c(52,34,19,34,24,65,44,31,29,58,37,46,50,44,56,36,58,51),x3v-c(158,163,37,157,59,123,46,117,173,112,111,114,134,73,168,143,202,124),yv-c(64,60,71,61,54,77,81,93,93,51,76,96,77,93,95

45、,54,168,99)Im.solv-lm(yx1+x2+x3,data=pho)summary(lm.sol)Call:lm(formula=yx1+x2+x3,data=pho)Residuals:Min1QMedian3QMax-27.575-11.160-2.79911.574Coefficients:EstimateStd.ErrortvaluePr(|t|)(Intercept)44.929018.34082.4500.02806*x11.80330.52903.4090.00424*x2-0.13370.4440-0.3010.76771x30.16680.11411.4620.

46、16573Signif.codes:0*0.001*0.01*0.05.0.11Residualstandarderror:19.93on14degreesoffreedomlmstepv-step(lmsol)Start:yx1+x2+x3DfSumofSqRSSAIC-x2136.05599.4vnone5563.4-x31849.86413.1-x114617.810181.2Step:yx1+x3DfSumofSqRSSAICvnone5599.4-x31833.26432.6-x115169.510768.9summary(lm.step)Call:lm(formula=yx1+x3

47、,data=pho)Residuals:Min1QMedian3QMax-29.713-11.324-2.95311.286Coefficients:EstimateStd.ErrortvaluePr(|t|)(Intercept)41.479413.88342.9880.00920*x11.73740.46693.7210.00205*x30.15480.10361.4940.15592Signif.codes:0*0.001*0.01*0.05.0.11Residualstandarderror:19.32on15degreesoffreedomMultipleR-squared:0.54

48、81,F-statistic:9.095on2and15DF,x3仍不夠顯著。再用drop1函數做逐步回歸。drop1(lm.step)SingletermdeletionsModel:yx1+x3DfSumofSqRSSAICvnone5599.4x115169.510768.9x31833.26432.6可以考慮再去掉x3.lmopt|t|)(Intercept)59.25907.42007.9865.67e-07*x11.84340.47893.8490.00142*-Signif.codes:0*0.001*0.01*0.05.0.11Residualstandarderror:20.

49、05on16degreesoffreedomMultipleR-squared:0.4808,F-statistic:14.82on1and16DF,皆顯著。xv-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)yv-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)plot(x,y)lm.solv-lm(y1+x)summary

50、(lm.sol)Call:lm(formula=y1+x)Residuals:Min1QMedian3QMax-9.8413-2.3369-0.0214Coefficients:EstimateStd.ErrortvaluePr(|t|)(Intercept)-1.45191.8353-0.7910.436x1.55780.28075.5497.93e-06*Signif.codes:0*0.001*0.01*0.05.0.11Residualstandarderror:5.168on26degreesoffreedomoooooooooooos-o80801510plot(y.rsty.fi

51、t)匚、J+0ydCM1015y+fit殘差并非是等方差的。修正模型,對相應變量Y做開方。Im.newv-update(lm.sol,sqrt(.).)summary(lm.new)Call:lm(formula=sqrt(y)x)Residuals:Min1QMedian3QMax-1.54255-0.45280-0.011770.34925Coefficients:EstimateStd.ErrortvaluePr(|t|)(Intercept)0.766500.255922.9950.00596*x0.291360.039147.4446.64e-08*Signif.codes:0*0.

52、001*0.01*0.05.0.11Residualstandarderror:0.7206on26degreesoffreedom:l+8.CMtoothpastev-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

53、,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.solv-lm(YX1+X2,data=toothpaste);summary(lm.sol)Call:lm(formula=YX1+X2,data=toothp

54、aste)Residuals:Min1QMedian3QMax-0.37130-0.101140.030660.10016Coefficients: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.11Residualstandarderror:0.1767on24degreesoffreedomMultipleR-squared

55、:0.9378,F-statistic:181on2and24DF,回歸診斷:influence.measures(lm.sol)Influeneemeasuresoflm(formula=YX1+X2,data=toothpaste):dfb.1dfb.X1dfb.X2dffitcov.rcook.dhatinf0.009080.00260-0.008470.01211.3665.11e-050.16810.062770.04467-0.06785-0.12441.1595.32e-030.0537-0.028090.077240.025400.18581.2831.19e-020.1386

56、0.116880.05055-0.110780.14041.3776.83e-030.1843*0.011670.01887-0.01766-0.10371.1413.69e-030.0384-0.43010-0.428810.457740.60610.8141.11e-010.09360.078400.01534-0.072840.10821.4814.07e-030.2364*0.015770.00913-0.014850.02081.2371.50e-040.08230.01127-0.02714-0.003640.10711.1563.95e-030.0466-0.078300.001

57、710.080520.18901.1551.22e-020.07260.00301-0.09652-0.00365-0.22811.1271.76e-020.0735-0.031140.018480.034590.15421.1328.12e-030.0514-0.09236-0.038010.099400.22011.0711.62e-020.0522-0.026500.034340.026060.11791.2354.81e-030.09560.00968-0.11445-0.00857-0.25451.1502.19e-020.0910-0.00285-0.061850.00098-0.

58、16081.1468.83e-030.05940.072010.09744-0.07796-0.10991.3644.19e-030.17310.151320.30204-0.17755-0.39071.0875.04e-020.10850.074890.47472-0.12980-0.75790.7311.66e-010.10920.052490.08484-0.07940-0.46600.6256.11e-020.0384*0.075570.07284-0.07861-0.08801.4712.69e-030.2304*-0.17959-0.390160.18241-0.54940.912

59、9.41e-020.10220.060260.10607-0.062070.12511.3745.42e-030.1804-0.54830-0.741970.593580.83710.9142.13e-010.17310.085410.01624-0.077750.13141.2495.97e-030.10690.325560.11734-0.302000.44801.0186.49e-020.10330.172430.32754-0.176760.41271.1485.66e-020.1369source(Reg_Diag.R);Reg_Diag(lm.sol)#薛毅老師自己寫的程序resi

60、duals1standards2students3hatmatrixs4DFFITSs50.004438430.027538650.026959250.168118190.01211949-0.09114255-0.53021138-0.522114690.05369239-0.124367270.077268870.471128630.463356660.138573530.185843100.048056650.301110620.295329120.184276630.14036860-0.09130271-0.52689847-0.518814060.03838430-0.103654

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論