P37.例2.1build.price-c(36,32,31,25,28,36,40,32,41,26,35,35,32,87,33,35);build.pricehist(build.price,freq=FALSE)#直方图lines(density(build.price),col=red)#连线#方法一:m-mean(build.price);m#均值D-var(build.price)#方差SD-sd(build.price)#标准差St=(m-37)/(SD/sqrt(length(build.price)));t#t统计量计算检验统计量t=[1]-0.1412332#方法二:t.test(build.price-37)#课本第38页例2.2binom.test(sum(build.price37),length(build.price),0.5)#课本40页例2.3P-2*(1-pnorm(1.96,0,1));P[1]0.04999579P1-2*(1-pnorm(0.7906,0,1));P1[1]0.4291774例2.4p-2*(pnorm(-1.96,0,1));p[1]0.04999579p1-2*(pnorm(-0.9487,0,1));p1[1]0.3427732例2.5(P45)scores-c(95,89,68,90,88,60,81,67,60,60,60,63,60,92,60,88,88,87,60,73,60,97,91,60,83,87,81,90);length(scores)#输入向量求长度ss-c(scores-80);sst-0t1-0for(iin1:length(ss)){if(ss[i]0)t-t+1#求小于80的个数elset1-t1+1求大于80的个数}t;t1t;t1[1]13[1]15binom.test(sum(scores80),length(scores),0.75)p-value=0.0014360.01Cox-Staut趋势存在性检验P47例2.6year-1971:2002;yearlength(year)rain-c(206,223,235,264,229,217,188,204,182,230,223,227,242,238,207,208,216,233,233,274,234,227,221,214,226,228,235,237,243,240,231,210)length(rain)#(1)该地区前10年降雨量是否变化?t1=0for(iin1:5){if(rain[i]rain[i+5])t1-t1+1}t1k-0:t1-1sum(dbinom(k,5,0.5))#=0.1875y-6/(2^5);y#=0.1875#(2)该地区前32年降雨量是否变化?t=0for(iin1:16){if(rain[i]rain[i+16])t-t+1}tk1-0:min(t,16-t)-1sum(dbinom(k1,16,0.5))#=0.0002593994pbinom(max(k1),16,0.5)#=0.0002593994y1-(1+16)/(2^16);y1#=0.0002593994plot(year,rain)abline(v=(1971+2002)/2,col=2)lines(year,rain)anova(lm(rain~(year)))随机游程检验(P50)例2.8client-c(F,M,M,M,M,M,F,M,M,F,M,M,M,M,F,M,F,M,M,M,F,F,F,M,M,M);clientn-length(client);nn1-sum(client==M);n1n0-n-n1;n0t1-0for(iin1:(length(client)-1)){if(client[i]==client[i+1])t1-t1elset1-t1+1}R-t1+1;R#=12#findrejectionregion(不写)rl-1+2*n1*n0/(n1+n0)*(1-1.96/sqrt(n1+n0));rlru-2*n1*n0/(n1+n0)*(1+1.96/sqrt(n1+n0));ru#=15.33476(课本为ru=17)例2.9shuju39-data.frame(read.table(SHUJU39.txt,header=TRUE));shuju39attach(shuju39)sum.a=0sum.b=0sum.c=0for(iin1:length(id)){if(pinzhong[i]==A)sum.a-sum.a+chanliang[i]elseif(pinzhong[i]==B)sum.b-sum.b+chanliang[i]elsefuhao-sum.c-sum.c+chanliang[i]}sum.a;sum.b;sum.cma-sum.a/4mb-sum.b/4mc-sum.c/4ma;mb;mcfuhao-rep(a,12);fuhaofor(iin1:length(id)){if(pinzhong[i]==A&((chanliang[i]-ma)0))fuhao[i]-+elseif(pinzhong[i]==B&((chanliang[i]-mb)0))fuhao[i]-+elseif(pinzhong[i]==C&((chanliang[i]-mc)0))fuhao[i]-+elsefuhao[i]--}fuhao#利用上题编程解决检验的随机性n-length(fuhao);nn1-sum(fuhao==+);n1n0-n-n1;n0t1-0for(iin1:(length(fuhao)-1)){if(fuhao[i]==fuhao[i+1])t1-t1elset1-t1+1}R-t1+1;R#findrejectionregionrl-1+2*n1*n0/(n1+n0)*(1-1.96/sqrt(n1+n0));rlru-2*n1*n0/(n1+n0)*(1+1.96/sqrt(n1+n0));ru例2.10(P52)library(quadprog)#不存在叫‘quadprog’这个名字的程辑包library(zoo)#不存在叫‘zoo’这个名字的程辑包library(tseries)#不存在叫‘tseries’这个名字的程辑包run1=factor(c(1,1,1,0,rep(1,7),0,1,1,0,0,rep(1,6),0,rep(1,4),0,rep(1,5),rep(0,4),rep(1,13)));run1y=factor(run1)runs.test(y)#错误:没有runs.test这个函数Wilcoxon符号秩检验W+在零假设下的精确分布#下面的函数dwilxonfun用来计算W+分布密度函数,即P(W+=x)的一个参考程序!dwilxonfun=function(N){a=c(1,1)#whenn=1frequencyofW+=1oron=1pp=NULL#distributeofallsizefrom2toNaa=NULL#frequencyofallsizefrom2toNfor(iin2:N){t=c(rep(0,i),a)a=c(a,rep(0,i))+tp=a/(2^i)#densityofWilcoxdistributwhensize=N}p}N=19#samplesizeofexpecteddistributionofW+y-dwilxonfun(N);y#计算P(W+=x)中的x取值的R参考程序!!dwilxonfun=function(N){a=c(1,1)#whenn=1frequencyofW+=1oron=1pp=NULL#distributeofallsizefrom2toNaa=NULL#frequencyofallsizefrom2toNfor(iin2:N){t=c(rep(0,i),a)a=c(a,rep(0,i))+tp=a/(2^i)#densityofWilcoxdistributwhensize=N}a}N=19#samplesizeofexpecteddistributionofW+y-dwilxonfun(N);length(y)-1hist(y,freq=FALSE)lines(density(y),col=red)例2.12(P59)ceo-c(310,350,370,377,389,400,415,425,440,295,325,296,250,340,298,365,375,360,385);length(ceo)#方法一wilcox.test(ceo-320)#方法二ceo.num-sum(ceo320);ceo.numn=length(ceo)binom.test(ceo.num,n,0.5)例2.13(P61)a-c(62,70,74,75,77,80,83,85,88)walsh-NULLfor(iin1:(length(a)-1)){for(jin(i+1):length(a)){walsh-c(walsh,(a[i]+a[j])/2)}}walsh=c(walsh,a)NW=length(walsh);NWmedian(walsh)2.5单组数据的位置参数置信区间估计(P61)例2.14‘stu-c(82,53,70,73,103,71,69,80,54,38,87,91,62,75,65,77);stualpha=0.05rstu-sort(stu);rstuconff-NULL;conffn=length(stu);nfor(iin1:(n-1)){for(jin(i+1):n){conf=pbinom(j,n,0.5)-pbinom(i,n,0.5)if(conf1-alpha){conff-c(conff,i,j,conf)}}}confflength(conff)min-103-38;minc-seq(1,(length(conff)-1),3);cfor(iinc){col-c(rstu[conff[i]],rstu[conff[i+1]],conff[i+2])min1-rstu[conff[i+1]]-rstu[conff[i]]if(min1min){min-min1;l-i}print(col)}col1-c(rstu[conff[l]],rstu[conff[l+1]],conff[l+2]);col1min例2.14“stu-c(82,53,70,73,103,71,69,80,54,38,87,91,62,75,65,77);stualpha=0.05n=length(stu);nconf=pbinom(n,n,0.5)-pbinom(0,n,0.5);conffor(kin1:n){conf=pbinom(n-k,n,0.5)-pbinom(k,n,0.5)if(conf1-alpha){loc=k-1;break}}print(loc)(剩余的例题参考程序在课本)3.6正态记分检验例2.18baby1-c(4,6,9,15,31,33,36,65,77,88)baby=(baby1-34);babybaby.mean=mean(baby);baby.mean例2.18qiuzhi-function(x){n=length(x)a=rep(2,n)for(iin1:n){a[i]=sum(x=x[i])}a}fuhao-function(x,y){n=length(x)sgn=rep(2,n)for(iin1:n){if(x[i]y)sgn[i]=1elseif(x[i]==y)sgn