R语言与统计分析第五章习题答案.doc
#5.1x<-c(3,5,7,9,11,13,15,17,19,21)y<-c(21,16,15,26,22,14,21,22,18,25)e=sum(x*y)/sum(y) #样本期望d=(sum(x*x*y)/sum(y)-e2 #样本方差a=(8*e+sqrt(64*e2-4*4*(4*e2-12*d)/8 #估计结果b=(8*e-sqrt(64*e2-4*4*(4*e2-12*d)/8ab#5.2x<-c(0,1,2,3,4,5,6)y<-c(17,20,10,2,1,0,0)e=2.718281828459 f<-function()(e(-50*)*50)/(210*62*24) #似然函数optimize(f,c(0,2),maximum=TRUE)#5.3x<-c(482,493,457,471,510,446,435,418,394,469) #0.95置信区间t.test(x)$conf.intchisq.var.test<-function(x,var,alpha,alternative="two.sided") options(digits=4) result<-list() n<-length(x) v<-var(x) result$var<-v chi2<-(n-1)*v/var result$chi2<-chi2 p<-pchisq(chi2,n-1) result$p.value<-p if(alternative="less") result$p.value<-pchaisq(chi2,n-1,lower.tail=F) else if (alternative="two.sider") result$p.value<-2*min(pchaisq(chi2,n-1),pchaisq(chi2,n-1,lower.tail=F) result$conf.int<-c( (n-1)*v/qchisq(alpha/2,df=n-1,lower.tail=F), (n-1)*v/qchisq(alpha/2,df=n-1,lower.tail=T) ) resultchisq.var.test(x,var(x),0.90,alternative="two.side")$conf.int #0.90置信区间#5.4x<-c(25,28,23,26,29,22)y<-c(28,23,30,35,21,27)chisq.var.test(x,var(x),0.95,alternative="two.side")$conf.int #卷烟A方差0.95置信区间chisq.var.test(y,var(y),0.95,alternative="two.side")$conf.int #卷烟B方差0.95置信区间#方法二 两方差比0.95的置信区间var.test(x,y)two.sample.ci<-function(x,y,conf.level=0.95,sigma1,sigama2) #样本方差作为整体方差 options(digits=4) m=length(x) n=length(y) xbar=mean(x)-mean(y) alpha=1-conf.level zstar=qnorm(1-alpha/2)*(sigma1/m+sigma2/n)(1/2) xbar+c(-zstar,+zstar)sigma1<-var(x)sigma2<-var(y)two.sample.ci(x,y,conf.level=0.95,sigma1,sigma2)#5.5x<-c(628,583,510,554,612,523,530,615,573,603,334,564)y<-c(535,433,398,470,567,480,498,560,503,426,338,547)two.sample.ci<-function(x,y,conf.level=0.95,sigma1,sigama2) options(digits=4) m=length(x) n=length(y) xbar=mean(x)-mean(y) alpha=1-conf.level zstar=qnorm(1-alpha/2)*(sigma1/m+sigma2/n)(1/2) xbar+c(-zstar,+zstar)sigma1=2140sigma2=3250a<-two.sample.ci(x,y,conf.level=0.95,sigma1,sigma2)b<-two.sample.ci(x,y,conf.level=0.90,sigma1,sigma2)a2 #置信水平为0.95的置信上限b1 #置信水平为0.90的置信下限#5.6x<-c(15.2,14.5,15.5,14.8,15.1,15.6,14.7)y<-c(15.2,15.0,14.8,15.2,15.0,14.9,15.1,14.8,15.3)var.test(x,y) #x方差与y方差的比值极大,说明x方差大于y方差#5.7prop.test(224,400,conf.level=0.99,correct=TRUE)#5.8size.norm2<-function(s,alpha,d,m) t0<-qt(alpha/2,m,lower.tail=FALSE) n0<-(t0*s/d)2 t1<-qt(alpha/2,n0,lower.tail=FALSE) n1<-(t1*s/d)2 while(abs(n1-n0)>0.5) n0<-(qt(alpha/2,n1,lower.tail=FALSE)*s/d)2 n1<-(qt(alpha/2,n0,lower.tail=FALSE)*s/d)2 n1size.norm2(10,0.05,2,100)#5.8size.bin<-function(d,p,conf.level=0.95) alpha=1-conf.level (qnorm(1-alpha/2)/d)2*p*(1-p)size.bin(0.01,0.05,0.90)3 0 ,.0( (*/ / ( ( )0 ( .00,0,( )/ (<) = (- ) >0 *( . , -)*0- = ( 0), . 0 . , .方于大明极的差与#) . . . ., . . . .下下 0平 上信 为水置 00 . ,( . = + ( /) + ( = . ) = ( ) 0 . , 0 , , 0 , , (. 0 , ( - / +/ () ( - ) ( = = 方体差样 , = . ( <. , .区置的比方二区置 方卷 " ,.) ,( 区置 差烟 .$ = ,.0 , ,0 , (.区置.# . ) . 0,( ( = )() ,/ ) < $ )= , , * " = , - $ " "= (- . ( /) - - $ ( < -( < ( )" , - . 区置 0 , , , . ,0, 函然 0 ( 00, (./) * * -*结估 ) (- +*方样 ) /* 期样) /) , , , , , ,(