非参数统计:方法与应用
(全书例题R语言实现)
第二章
2.1
例2.1
a <- c(88,12)
b <- c(0.95,0.05)
chisq.test(a,p = b) $p.value p = 0.001318969
例2.2
a <- c(380,69,43,8)
b <- c(0.8,0.12,0.07,0.01)
chisq.test(a, p = b) $p.value p = 0.1127
例2.4(续2.2)
a <- c(380,69,51)
b <- c(0.8,0.12,0.08)
chisq.test(a, p = b) p . v a l u e p = 0.06805 例 2.4 a < − c ( 3 , 8 , 10 , 7 , 2 ) b < − c ( 0.1174 , 0.2355 , 0.328 , 0.2437 , 0.0754 ) c h i s q . t e s t ( a , p = b ) p.value p = 0.06805 例2.4 a <- c(3,8,10,7,2) b <- c(0.1174,0.2355,0.328,0.2437,0.0754) chisq.test(a, p = b) p.valuep=0.06805例2.4a<−c(3,8,10,7,2)b<−c(0.1174,0.2355,0.328,0.2437,0.0754)chisq.test(a,p=b)p.value p = 0.9929581
2.2
例题2.6 (1)
x = c(-5,-3,-1,0,1,2,4,7,8)
y = c(1,1,2,1,5,5,3,1,1)
mu = mean(rep(x,y))
z = (x-mu)/3
f0 = pnorm(z, 0, 1)
n = length(x)
sn = NULL
for(i in 1:n){
sn[i] <- sum(y[1:i])
}
Sn <- sn/20
D = max(abs(Sn-f0))
例题2.8
a <-c(11,11,8,9,7,9,12)
b=rep(1/7,7)
a=a/sum(a)
ks.test(jitter(a),jitter(b))
2.3
例题2.7
binom.test(9,10,alternative = ‘greater’,conf.level = 0.9)
例题2.8
binom.test(10,12,alternative = ‘greater’,conf.level = 0.9)
例题2.9
binom.test(12,14,alternative = ‘two.sided’,conf.level = 0.9)
例题2.10
binom.test(1,8,alternative = ‘two.sided’,conf.level = 0.95)
例题2.11
binom.test(5,6,alternative = ‘greater’,conf.level = 0.9)
例题2.12
binom.test(2,13,alternative = ‘less’,conf.level = 0.9)
例题2.14
binom.test(7,18,p=0.25,alternative = ‘greater’,conf.level = 0.9)
例题2.15
p1 <- pbinom(3,13,0.75,lower.tail = T)
p1
例题2.16
p2 <- pbinom(3,8,0.5,lower.tail = T)
p2
2.4
例题2.17
x1 <- c(24.3,25.8,25.4,24.8,25.2,25.1,25,25.5)
d <- abs(x1-25)
x2 <- x1[-which(d==0)]
wilcox.test(x2,alternative = ‘greater’,mu=25)
例题2.18
x1 <- c(42,51,31,61,44,55,48)
x2 <- c(38,53,36,52,33,49,36)
wilcox.test(x1-x2,alternative = ‘less’,mu=0)
例题2.21
y1 <- c(14,12,18,7,11,9,16,15,13,11,18,8,13,10,14,16,
15,12,17,7,11,9,16,15)
y2 <- c(10,4,14,6,9,6,12,12,10,5,15,6,9,6,11,12,11,6,
14,5,10,4,13,10)
wilcox.test(y1-y2,alternative = ‘two.sided’,exact = F,correct = T,mu=3)
2.5
例题2.19
library(tseries)
y1 <- c(1,1,0,1,0,0,1,1,1,0)
runs.test(factor(y1),alternative = ‘two.sided’)
例题2.20
y1 <- c(0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1)
第三章
3.1
例题3.1
x1 <- c(2,2,2,2,3,3,3,3,3,3,2,3,3,2,3)
x2 <- c(3,4,2,3,2,3,4,4,2,4,4,3,4,4,4)
n1 <- sum(x1-x2>0)
n2 <- sum(x1-x2<0)
n=n1+n2
binom.test(n1,n,alternative = ‘less’)
例题3.2
x <- c(2,3,3,3,3,3,3,3,2,3,2,2,5,2,5,3,1)
y <- c(4,4,5,5,3,2,5,3,1,5,5,5,4,5,5,5,5)
n1 <- sum(x-y>0)
n2 <- sum(x-y<0)
n=n1+n2
binom.test(n1,n,alternative = ‘less’)
3.2
例题3.3
x <- c(78,70,67,81,76,72,85,83)
y <- c(62,58,63,77,80,73,82,78)
wilcox.test(x,y,paired = T,alternative = ‘two.sided’,
exact = T,correct = F)
x <- c(78,70,67,81,76,72,85,83)
y <- c(62,58,63,77,80,73,82,78)
wilcox.test(x,y,paired = T,alternative = ‘greater’,
exact = T,correct = F)
第四章
4.1
例题4.1
x = c(0.94,1.56,1.15)
y = c(1.20,1.63,2.26,1.87,2.20,1.30)
wilcox.test(x,y,paired = F,alternative = ‘less’)
例题4.2
x <- c(83,82,84,96,90,64,91,71,75,72)
y <- c(42,61,52,78,69,81,75,78,78,65)
wilcox.test(x,y,paired = F,alternative = ‘greater’)
4.2
例题4.3
library(tseries)
y <- c(0,0,0,1,0,0,1,1,1,0,
0,0,0,0,1,1,1,1,1,1)
y <- as.factor(y)
runs.test(y)
4.3
例题4.4
f1 <- c(60,21,11,4,4)
f2 <- c(130,50,10,6,4)
f <- f1+f2
n1 <- sum(f1);n2 <- sum(f2);n <- sum(f)
e1 <- f*(n1/n);e2 <- f-e1
Q <- sum((f1-e1)2/e1)+sum((f2-e2)2/e2)
qchisq(0.05,df=4,lower.tail = F)
pchisq(Q,df=4,lower.tail =