非参数统计(第二版)习题R程序 下载本文

内容发布更新时间 : 2024/12/25 16:28:03星期一 下面是文章的全部内容请认真阅读。

P37.例2.1

build.price<-c(36,32,31,25,28,36,40,32,41,26,35,35,32,87,33,35);build.price

hist(build.price,freq=FALSE)#直方图 lines(density(build.price),col=\连线 #方法一:m<-mean(build.price);m#均值 D<-var(build.price)#方差 SD<-sd(build.price)#标准差S

t=(m-37)/(SD/sqrt(length(build.price)));t#t统计量计算检验统计量 t=

[1] -0.1412332

#方法二:t.test(build.price-37)#课本第38页 例2.2

binom.test(sum(build.price<37),length(build.price),0.5)#课本40页 例2.3

P<-2*(1-pnorm(1.96,0,1));P [1] 0.04999579

P1<-2*(1-pnorm(0.7906,0,1));P1 [1] 0.4291774 >例2.4

> p<-2*(pnorm(-1.96,0,1));p [1] 0.04999579 >

> p1<-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);ss t<-0 t1<-0

for(i in 1:length(ss)){

if (ss[i]<0) t<-t+1#求小于80的个数 else t1<-t1+1求大于80的个数 } t;t1 >t;t1 [1] 13 [1] 15

binom.test(sum(scores<80),length(scores),0.75) p-value = 0.001436<0.01 Cox-Staut趋势存在性检验P47 例2.6

year<-1971:2002;year length(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=0 for (i in 1:5){

if (rain[i]

sum(dbinom(k,5,0.5))#=0.1875 y<-6/(2^5);y#=0.1875

#(2)该地区前32年降雨量是否变化? t=0

for (i in 1:16){

if (rain[i]

k1<-0:min(t,16-t)-1

sum(dbinom(k1,16,0.5))#=0.0002593994 pbinom(max(k1),16,0.5)#= 0.0002593994 y1<-(1+16)/(2^16);y1#=0.0002593994 plot(year,rain)

abline(v=(1971+2002)/2,col=2) lines(year,rain) anova(lm(rain~(year))) 随机游程检验(P50) 例2.8

client<-c(\\\n<-length(client);n n1<-sum(client==\n0<-n-n1;n0 t1<-0

for (i in 1:(length(client)-1)){ if (client[i]==client[i+1]) t1<-t1 else t1<-t1+1 }

R<-t1+1;R#=12

#find rejection region(不写)

rl<-1+2*n1*n0/(n1+n0)*(1-1.96/sqrt(n1+n0));rl ru<-2*n1*n0/(n1+n0)*(1+1.96/sqrt(n1+n0));ru#=15.33476(课本为ru=17)

例2.9

shuju39<-data.frame(read.table (\attach(shuju39) sum.a=0 sum.b=0 sum.c=0

for (i in 1:length(id)){

if (pinzhong[i]==\else if (pinzhong[i]==\sum.b+chanliang[i]

elsefuhao<-sum.c<-sum.c+chanliang[i] }

sum.a;sum.b;sum.c ma<-sum.a/4 mb<-sum.b/4 mc<-sum.c/4 ma;mb;mc

fuhao<-rep(\for (i in 1:length(id)){

if (pinzhong[i]==\fuhao[i]<-\

else if (pinzhong[i]==\fuhao[i]<-\

else if (pinzhong[i]==\fuhao[i]<-\elsefuhao[i]<-\} fuhao

#利用上题编程解决检验的随机性 n<-length(fuhao);n n1<-sum(fuhao==\n0<-n-n1;n0 t1<-0

for (i in 1:(length(fuhao)-1)){ if (fuhao[i]==fuhao[i+1]) t1<-t1 else t1<-t1+1 }

R<-t1+1;R

#find rejection region

rl<-1+2*n1*n0/(n1+n0)*(1-1.96/sqrt(n1+n0));rl ru<-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)));run1 y=factor(run1)

runs.test(y)# 错误: 没有\这个函数

Wilcoxon符号秩检验 W+在零假设下的精确分布

#下面的函数dwilxonfun用来计算W+分布密度函数,即P(W+=x)的一个参考程序! dwilxonfun=function(N){

a=c(1,1) #when n=1 frequency of W+=1 or o n=1

pp=NULL #distribute of all size from 2 to N aa=NULL #frequency of all size from 2 to N for (i in 2:N){ t=c(rep(0,i),a) a=c(a,rep(0,i))+t

p=a/(2^i) #density of Wilcox distribut when size=N

} p }

N=19 #sample size of expected distribution of W+ y<-dwilxonfun(N);y

#计算P(W+=x)中的x取值的R参考程序!! dwilxonfun=function(N){

a=c(1,1) #when n=1 frequency of W+=1 or o n=1

pp=NULL #distribute of all size from 2 to N aa=NULL #frequency of all size from 2 to N for (i in 2:N){ t=c(rep(0,i),a) a=c(a,rep(0,i))+t

p=a/(2^i) #density of Wilcox distribut when size=N } a }

N=19 #sample size of expected distribution of W+ y<-dwilxonfun(N);length(y)-1 hist(y,freq=FALSE) lines(density(y),col=\例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(ceo>320);ceo.num n=length(ceo)

binom.test(ceo.num,n,0.5)