原文地址:统计建模与R软件第五章习题答案(假设检验)
作者:蘓木柒
Ex5.1> x<-c(220, 188, 162, 230, 145, 160, 238, 188, 247, 113, 126, 245, 164, 231, 256, 183, 190, 158, 224, 175)> t.test(x,mu=225) One Sample t-testdata: xt = -3.4783, df = 19, p-value = 0.002516alternative hypothesis: true mean is not equal to 22595 percent confidence interval: 172.3827 211.9173sample estimates:mean of x 192.15原假设:油漆工人的血小板计数与正常成年男子无差异。备择假设:油漆工人的血小板计数与正常成年男子有差异。p值小于0.05,拒绝原假设,认为油漆工人的血小板计数与正常成年男子有差异。上述检验是双边检验。也可采用单边检验。 备择假设:油漆工人的血小板计数小于正常成年男子。> t.test(x,mu=225,alternative="less") One Sample t-testdata: xt = -3.4783, df = 19, p-value = 0.001258alternative hypothesis: true mean is less than 22595 percent confidence interval: -Inf 208.4806sample estimates:mean of x 192.15同样可得出油漆工人的血小板计数小于正常成年男子的结论。Ex5.2> pnorm(1000,mean(x),sd(x))[1] 0.5087941> x [1] 1067 919 1196 785 1126 936 918 1156 920 948> pnorm(1000,mean(x),sd(x))[1] 0.5087941x<=1000的概率为0.509,故x大于1000的概率为0.491.要点:pnorm计算正态分布的分布函数。在R软件中,计算值均为下分位点。Ex5.3> A<-c(113,120,138,120,100,118,138,123)> B<-c(138,116,125,136,110,132,130,110)> t.test(A,B,paired=TRUE) Paired t-testdata: A and Bt = -0.6513, df = 7, p-value = 0.5357alternative hypothesis: true difference in means is not equal to 095 percent confidence interval: -15.62889 8.87889sample estimates:mean of the differences -3.375p值大于0.05,接受原假设,两种方法治疗无差异。Ex5.4(1)正态性W检验:>x<-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)>y<-c(3.7,6.5,5,5.2,0.8,0.2,0.6,3.4,6.6,-1.1,6,3.8,2,1.6,2,2.2,1.2,3.1,1.7,-2) > shapiro.test(x) Shapiro-Wilk normality testdata: xW = 0.9699, p-value = 0.7527> shapiro.test(y) Shapiro-Wilk normality testdata: yW = 0.971, p-value = 0.7754ks检验:> ks.test(x,"pnorm",mean(x),sd(x)) One-sample Kolmogorov-Smirnov testdata: xD = 0.1065, p-value = 0.977alternative hypothesis: two-sidedWarning message:In ks.test(x, "pnorm", mean(x), sd(x)) : cannot compute correct p-values with ties> ks.test(y,"pnorm",mean(y),sd(y)) One-sample Kolmogorov-Smirnov testdata: yD = 0.1197, p-value = 0.9368alternative hypothesis: two-sidedWarning message:In ks.test(y, "pnorm", mean(y), sd(y)) : cannot compute correct p-values with tiespearson拟合优度检验,以x为例。> sort(x) [1] -5.6 -1.6 -1.4 -0.7 -0.5 0.4 0.7 1.7 2.0 2.5 2.5 2.8 3.0 3.5 4.0[16] 4.5 4.6 5.8 6.0 7.1> x1<-table(cut(x,br=c(-6,-3,0,3,6,9)))> p<-pnorm(c(-3,0,3,6,9),mean(x),sd(x))> p[1] 0.04894712 0.24990009 0.62002288 0.90075856 0.98828138> p<-c(p[1],p[2]-p[1],p[3]-p[2],p[4]-p[3],1-p[4]);p[1] 0.04894712 0.20095298 0.37012278 0.28073568 0.09924144> chisq.test(x1,p=p) Chi-squared test for given probabilitiesdata: x1X-squared = 0.5639, df = 4, p-value = 0.967Warning message:In chisq.test(x1, p = p) : Chi-squared approximation may be incorrectp值为0.967,接受原假设,x符合正态分布。(2)方差相同模型t检验:> t.test(x,y,var.equal=TRUE) Two Sample t-testdata: x and yt = -0.6419, df = 38, p-value = 0.5248alternative hypothesis: true difference in means is not equal to 095 percent confidence interval: -2.326179 1.206179sample estimates:mean of x mean of y 2.065 2.625方差不同模型t检验:> t.test(x,y) Welch Two Sample t-testdata: x and yt = -0.6419, df = 36.086, p-value = 0.525alternative hypothesis: true difference in means is not equal to 095 percent confidence interval: -2.32926 1.20926sample estimates:mean of x mean of y 2.065 2.625配对t检验:> t.test(x,y,paired=TRUE) Paired t-testdata: x and yt = -0.6464, df = 19, p-value = 0.5257alternative hypothesis: true difference in means is not equal to 095 percent confidence interval: -2.373146 1.253146sample estimates:mean of the differences -0.56三种检验的结果都显示两组数据均值无差异。(3)方差检验:> var.test(x,y) F test to compare two variancesdata: x and yF = 1.5984, num df = 19, denom df = 19, p-value = 0.3153alternative hypothesis: true ratio of variances is not equal to 195 percent confidence interval: 0.6326505 4.0381795sample estimates:ratio of variances 1.598361接受原假设,两组数据方差相同。Ex5.5> a <- c(126,125,136,128,123,138,142,116,110,108,115,140)> b <- c(162,172,177,170,175,152,157,159,160,162)正态性检验,采用ks检验:> ks.test(a,"pnorm",mean(a),sd(a)) One-sample Kolmogorov-Smirnov testdata: aD = 0.1464, p-value = 0.9266alternative hypothesis: two-sided> ks.test(b,"pnorm",mean(b),sd(b)) One-sample Kolmogorov-Smirnov testdata: bD = 0.2222, p-value = 0.707alternative hypothesis: two-sidedWarning message:In ks.test(b, "pnorm", mean(b), sd(b)) : cannot compute correct p-values with tiesa和b都服从正态分布。方差齐性检验:> var.test(a,b) F test to compare two variancesdata: a and bF = 1.9646, num df = 11, denom df = 9, p-value = 0.3200alternative hypothesis: true ratio of variances is not equal to 195 percent confidence interval: 0.5021943 7.0488630sample estimates:ratio of variances 1.964622可认为a和b的方差相同。选用方差相同模型t检验:> t.test(a,b,var.equal=TRUE) Two Sample t-testdata: a and bt = -8.8148, df = 20, p-value = 2.524e-08alternative hypothesis: true difference in means is not equal to 095 percent confidence interval: -48.24975 -29.78358sample estimates:mean of x mean of y 125.5833 164.6000可认为两者有差别。Ex5.6二项分布总体的假设检验:> binom.test(57,400,p=0.147) Exact binomial testdata: 57 and 400number of successes = 57, number of trials = 400, p-value = 0.8876alternative hypothesis: true probability of success is not equal to 0.14795 percent confidence interval: 0.1097477 0.1806511sample estimates:probability of success 0.1425P 值>0.05,故接受原假设,表示调查结果支持该市老年人口的看法Ex5.7二项分布总体的假设检验:> binom.test(178,328,p=0.5,alternative="greater") Exact binomial testdata: 178 and 328number of successes = 178, number of trials = 328, p-value = 0.06794alternative hypothesis: true probability of success is greater than 0.595 percent confidence interval: 0.4957616 1.0000000sample estimates:probability of success 0.5426829不能认为这种处理能增加母鸡的比例。Ex5.8利用pearson卡方检验是否符合特定分布:>chisq.test(c(315,101,108,32),p=c(9,3,3,1)/16) Chi-squared test for given probabilitiesdata: c(315, 101, 108, 32)X-squared = 0.47, df = 3, p-value = 0.9254接受原假设,符合自由组合定律。Ex5.9利用pearson卡方检验是否符合泊松分布:> n<-length(z)> y<-c(92,68,28,11,1,0)> x<-0:5> q<-ppois(x,mean(rep(x,y)));n<-length(y)> p[1]<-q[1];p[n]=1-q[n-1]>chisq.test(y,p=p) Chi-squared test for given probabilitiesdata: yX-squared = 2.1596, df = 5, p-value = 0.8267Warning message:In chisq.test(y, p = p) : Chi-squared approximation may be incorrect重新分组,合并频数小于5的组:> z<-c(92,68,28,12)> n<-length(z);p<-p[1:n-1];p[n]<-1-q[n-1]> chisq.test(z,p=p) Chi-squared test for given probabilitiesdata: zX-squared = 0.9113, df = 3, p-value = 0.8227可认为数据服从泊松分布。Ex5.10ks检验 两个分布是否相同:> x<-c(2.36,3.14,752,3.48,2.76,5.43,6.54,7.41)> y<-c(4.38,4.25,6.53,3.28,7.21,6.55)> ks.test(x,y) Two-sample Kolmogorov-Smirnov testdata: x and yD = 0.375, p-value = 0.6374alternative hypothesis: two-sidedEx5.11列联数据的独立性检验:> x <- c(358,2492,229,2745)> dim(x)<-c(2,2)>chisq.test(x) Pearson's Chi-squared test with Yates' continuity correctiondata: xX-squared = 37.4143, df = 1, p-value = 9.552e-10P 值<0.05 ,拒绝原假设,有影响。Ex5.12列联数据的独立性检验:> y [,1] [,2] [,3][1,] 45 12 10[2,] 46 20 28[3,] 28 23 30[4,] 11 12 35> chisq.test(y) Pearson's Chi-squared testdata: yX-squared = 40.401, df = 6, p-value = 3.799e-07P 值<0.05 ,拒绝原假设,不独立,有关系。Ex5.13因有的格子的频数小于5,故采用fiser确切概率法检验独立性。>fisher.test(x) Fisher's Exact Test for Count Datadata: xp-value = 0.6372alternative hypothesis: true odds ratio is not equal to 195 percent confidence interval: 0.04624382 5.13272210sample estimates:odds ratio 0.521271p值大于0.05,两变量独立,两种工艺对产品的质量没有影响。Ex5.14由于是在相同个体上的两次试验,故采用McNemar检验。>mcnemar.test(x) McNemar's Chi-squared testdata: xMcNemar's chi-squared = 2.8561, df = 3, p-value = 0.4144p值大于0.05,不能认定两种方法测定结果不同。Ex5.15符号检验:H0:中位数>=14.6;H1: 中位数<14.6> x<-c(13.32,13.06,14.02,11.86,13.58,13.77,13.51,14.42,14.44,15.43)>binom.test(sum(x)>14.6,length(x),al="l") Exact binomial testdata: sum(x) > 14.6 and length(x)number of successes = 1, number of trials = 10, p-value = 0.01074alternative hypothesis: true probability of success is less than 0.595 percent confidence interval: 0.0000000 0.3941633sample estimates:probability of success 0.1拒绝原假设,中位数小于14.6Wilcoxon符号秩检验:>wilcox.test(x,mu=14.6,al="l",exact=F) Wilcoxon signed rank test with continuity correctiondata: xV = 4.5, p-value = 0.01087alternative hypothesis: true location is less than 14.6拒绝原假设,中位数小于14.6Ex5.16符号检验法:> x<-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)> y<-c(37,41,23.4,17,31.5,40,31,36,5.7,11.5,21,6.1,26.5,21.3,44.5,28,22.6,20,11,22.3)> binom.test(sum(x>y),length(x)) Exact binomial testdata: sum(x > y) and length(x)number of successes = 14, number of trials = 20, p-value = 0.1153alternative hypothesis: true probability of success is not equal to 0.595 percent confidence interval: 0.4572108 0.8810684sample estimates:probability of success 0.7接受原假设,无差别。Wilcoxon符号秩检验:> wilcox.test(x,y,paired=TRUE,exact=FALSE) Wilcoxon signed rank test with continuity correctiondata: x and yV = 136, p-value = 0.005191alternative hypothesis: true location shift is not equal to 0拒绝原假设,有差别。Wilcoxon秩和检验:> wilcox.test(x,y,exact=FALSE) Wilcoxon rank sum test with continuity correctiondata: x and yW = 274.5, p-value = 0.04524alternative hypothesis: true location shift is not equal to 0拒绝原假设,有差别。正态性检验:> ks.test(x,"pnorm",mean(x),sd(x)) One-sample Kolmogorov-Smirnov testdata: xD = 0.1407, p-value = 0.8235alternative hypothesis: two-sidedWarning message:In ks.test(x, "pnorm", mean(x), sd(x)) : cannot compute correct p-values with ties> ks.test(y,"pnorm",mean(y),sd(y)) One-sample Kolmogorov-Smirnov testdata: yD = 0.1014, p-value = 0.973alternative hypothesis: two-sided两组数据均服从正态分布。方差齐性检验:> var.test(x,y) F test to compare two variancesdata: x and yF = 1.1406, num df = 19, denom df = 19, p-value = 0.7772alternative hypothesis: true ratio of variances is not equal to 195 percent confidence interval: 0.4514788 2.8817689sample estimates:ratio of variances 1.140639可认为两组数据方差相同。综上,该数据可做t检验。t检验:> t.test(x,y,var.equal=TRUE) Two Sample t-testdata: x and yt = 2.2428, df = 38, p-value = 0.03082alternative hypothesis: true difference in means is not equal to 095 percent confidence interval: 0.812553 15.877447sample estimates:mean of x mean of y 33.215 24.870拒绝原假设,有差别。综上所述,Wilcoxon符号秩检验的差异检出能力最强,符号检验的差异检出最弱。Ex5.17spearman秩相关检验:> x<-c(24,17,20,41,52,23,46,18,15,20)> y<-c(8,1,4,7,9,5,10,3,2,6)> cor.test(x,y,method="spearman",exact=F) Spearman's rank correlation rhodata: x and yS = 9.5282, p-value = 4.536e-05alternative hypothesis: true rho is not equal to 0sample estimates: rho0.9422536kendall秩相关检验:> cor.test(x,y,method="kendall",exact=F) Kendall's rank correlation taudata: x and yz = 3.2329, p-value = 0.001225alternative hypothesis: true tau is not equal to 0sample estimates: tau0.8090398二者有关系,呈正相关。Ex5.18> x<-rep(1:5,c(0,1,9,7,3));y<-rep(1:5,c(2,2,11,4,1))> wilcox.test(x,y,exact=F) Wilcoxon rank sum test with continuity correctiondata: x and yW = 266, p-value = 0.05509alternative hypothesis: true location shift is not equal to 0p值大于0.05,不能拒绝原假设,尚不能认为新方法的疗效显著优于原疗法。