alt text

5.2 重要的参数检验

实际问题中大多数随机变量服从或近似服从正态分布,按总体的个数,可分为单个正态总体和两个正态总体的参数检验

5.2.1 正态总体均值的假设检验

1、单个总体的均值检验

在假设数据总体服从正态分布时,实验观测值的均值mu与零假设的均值mu0是否有差异,可以使用t.test()来检验此类零假设的有效性

例5.2 某种元件的寿命X(以小时计)服从正态分布N(mu,sigma^2),其中mu和sigma均未知,现测得16只元件的寿命,问是否有理由认为元件的平均寿命大于225小时?

  • 原假设 H0:mu<=mu0=225
  • 备择假设 H1: mu> mu0=225
X <- c(159, 280, 101, 212, 224, 379, 179, 264, 222, 362, 168, 250, 149, 260, 
    485, 170)
t.test(X, alternative = "greater", mu = 225)
## 
##  One Sample t-test
## 
## data:  X
## t = 0.6685, df = 15, p-value = 0.257
## alternative hypothesis: true mean is greater than 225
## 95 percent confidence interval:
##  198.2321      Inf
## sample estimates:
## mean of x 
##     241.5

由于p-value=0.257>0.05,不能拒绝原假设,因此认为平均寿命不大于225小时

2、两个总体的均值检验

例5.3 在平炉上进行一项实验以确定改变操作方法的建议是否会增加钢的得率,实验是在同一个平炉上进行的,每炼一炉炼钢时除操作方法外,其他条件都尽可能的相同。先用标准方法炼一炉,然后用新方法炼一炉,以后交替进行,各炼了10炉,其得率为

标准方法 78.1 72.4 76.2 74.3 77.4 78.4 76.0 75.5 76.7 77.3

新方法 79.1 81.0 77.3 79.1 80.0 79.1 79.1 77.3 80.2 82.1

设这两样本相互独立,且分别来自正态总体N(mu1,sigma^2)N(mu2,sigma^2),其中mu1,mu2,sigma未知,问新的操作能否提高得率?

根据题意,选择t-检验法,方差相等的情况

  • H0:mu1>=mu2
  • H1:mu1<mu2
X <- c(78.1, 72.4, 76.2, 74.3, 77.4, 78.4, 76, 75.5, 76.7, 77.3)
Y <- c(79.1, 81, 77.3, 79.1, 80, 79.1, 79.1, 77.3, 80.2, 82.1)
t.test(X, Y, var.equal = TRUE, alternative = "less")
## 
##  Two Sample t-test
## 
## data:  X and Y
## t = -4.2957, df = 18, p-value = 0.0002176
## alternative hypothesis: true difference in means is less than 0
## 95 percent confidence interval:
##       -Inf -1.908255
## sample estimates:
## mean of x mean of y 
##     76.23     79.43
# t.test(X,Y,var.equal = FALSE,alternative = 'less')

p-value = 0.0002176<0.05,故拒绝原假设,即认为新的操作方法能够提高得率

3、成对数据的t-检验

对例5.3的数据作成对t-检验:

  • H0:mu1>=mu2
  • H1:mu1<mu2
X <- c(78.1, 72.4, 76.2, 74.3, 77.4, 78.4, 76, 75.5, 76.7, 77.3)
Y <- c(79.1, 81, 77.3, 79.1, 80, 79.1, 79.1, 77.3, 80.2, 82.1)
t.test(X - Y, alternative = "less")
## 
##  One Sample t-test
## 
## data:  X - Y
## t = -4.2018, df = 9, p-value = 0.00115
## alternative hypothesis: true mean is less than 0
## 95 percent confidence interval:
##       -Inf -1.803943
## sample estimates:
## mean of x 
##      -3.2
# 等价于t.test(X,Y,alternative = 'less',paired=TRUE)

p-value = 0.00115<0.05,故拒绝原假设,即认为新方法优于标准方法

5.2.2 正态总体方差的假设检验

1、单个总体的方差检验

例5.4 从小学五年级男学生中抽取20名,测量其身高(单位:厘米),其数据如下,以alpha=0.05,做假设检验:

  • (1)H0:mu=149,H1:mu<>149
  • (2)H0:sigma^2=75,sigma^2<>75
X <- c(136, 144, 143, 157, 137, 159, 135, 158, 147, 165, 158, 142, 159, 150, 
    156, 152, 140, 149, 148, 155)
# 使用均值检验
t.test(X, mu = 149, var.equal = TRUE)  #认为方差已知,做均值检验,p-value = 0.8025>0.05,无法拒绝原假设,认为mu=149
## 
##  One Sample t-test
## 
## data:  X
## t = 0.2536, df = 19, p-value = 0.8025
## alternative hypothesis: true mean is not equal to 149
## 95 percent confidence interval:
##  145.3736 153.6264
## sample estimates:
## mean of x 
##     149.5
t.test(X, mu = 149, alternative = "two.sided", var.equal = FALSE)  #认为sigma未知,做均值检验
## 
##  One Sample t-test
## 
## data:  X
## t = 0.2536, df = 19, p-value = 0.8025
## alternative hypothesis: true mean is not equal to 149
## 95 percent confidence interval:
##  145.3736 153.6264
## sample estimates:
## mean of x 
##     149.5
# R没有单个总体的方差假设检验函数

2、两个总体的方差检验

X1...Xn1来自总体X~N(mu1,sigma1^2)的样本,Y1...Yn1来自总体Y~N(mu2,sigma2^2)的样本,且两样本独立,其检验问题为

  • 双边检验: H0: sigma1^2 = sigma2^2,H1: sigma1^2 != sigma2^2
  • 单边检验1:H0: sigma1^2 <= sigma2^2,H1: sigma1^2 > sigma2^2
  • 单边检验2:H0: sigma1^2 >= sigma2^2,H1: sigma1^2 < sigma2^2

例5.5 对例5.3中的数据作方差的假设检验:

  • H0: sigma1^2 = sigma2^2
  • H1: sigma1^2 != sigma2^2`
X <- c(78.1, 72.4, 76.2, 74.3, 77.4, 78.4, 76, 75.5, 76.7, 77.3)
Y <- c(79.1, 81, 77.3, 79.1, 80, 79.1, 79.1, 77.3, 80.2, 82.1)
var.test(X, Y)
## 
##  F test to compare two variances
## 
## data:  X and Y
## F = 1.4945, num df = 9, denom df = 9, p-value = 0.559
## alternative hypothesis: true ratio of variances is not equal to 1
## 95 percent confidence interval:
##  0.3712079 6.0167710
## sample estimates:
## ratio of variances 
##           1.494481

p-value = 0.559<0.05,无法拒绝原假设,即认为X与Y的方差相等

5.2.3 二项分布总体的假设检验

使用binom.test(x, n, p=0.5,alternative=c("two.sided","less", "greater"),conf.level=0.95),x为指定成功的次数,n为实验的总次数,p为每次实验成功的概率。

例5.6 有一批蔬菜种子的发芽率p0=0.85,现随机抽取500粒,用种衣剂进行浸泡种处理,结果有445粒发芽,试检验种衣剂对种子发芽率有无效果,检验的问题为:

  • H0:p =p0=0.85
  • H1:p!=p0
binom.test(445, 500, p = 0.85)
## 
##  Exact binomial test
## 
## data:  445 and 500
## number of successes = 445, number of trials = 500, p-value =
## 0.01207
## alternative hypothesis: true probability of success is not equal to 0.85
## 95 percent confidence interval:
##  0.8592342 0.9160509
## sample estimates:
## probability of success 
##                   0.89

p-value =0.01207<0.05,故拒绝原假设,即认为种衣剂对种子发芽率有显著效果

例:某产品的优质品率一直保持在40%, 近期技监部门抽查了12件产品, 其中优质品为5件, 问在α=0.05水平上能否认为其优质品率仍保持在40%?

binom.test(7, 12, p = 0.4)
## 
##  Exact binomial test
## 
## data:  7 and 12
## number of successes = 7, number of trials = 12, p-value = 0.2417
## alternative hypothesis: true probability of success is not equal to 0.4
## 95 percent confidence interval:
##  0.2766697 0.8483478
## sample estimates:
## probability of success 
##              0.5833333

p-value = 0.2417,无法拒绝原假设,认为该产品的优质品率仍保持在40%。

prop.test(7, 12, p = 0.4, correct = TRUE)
## Warning in prop.test(7, 12, p = 0.4, correct = TRUE): Chi-squared
## approximation may be incorrect
## 
##  1-sample proportions test with continuity correction
## 
## data:  7 out of 12, null probability 0.4
## X-squared = 1.0035, df = 1, p-value = 0.3165
## alternative hypothesis: true p is not equal to 0.4
## 95 percent confidence interval:
##  0.2859928 0.8350075
## sample estimates:
##         p 
## 0.5833333

当样本容量较小而做近似检验时, R输出的结果会有警告信息pwarning messageq:Chi-squared近似算法有可能不准,在R中当样本容量大于20时不会出现这样的警告信息,样本较小时,应使用binom.test()。通常,我们一般在样本容量大于30时做大样本近似,应用prop.test()。

例:某大学随机调查120名男同学,发现有35人喜欢看武侠小说,问可否认为该大学有四分之一的男同学喜欢看武侠小说?p取α=0.05q

prop.test(35, 120, p = 0.25, correct = TRUE)
## 
##  1-sample proportions test with continuity correction
## 
## data:  35 out of 120, null probability 0.25
## X-squared = 0.9, df = 1, p-value = 0.3428
## alternative hypothesis: true p is not equal to 0.25
## 95 percent confidence interval:
##  0.2141070 0.3828007
## sample estimates:
##         p 
## 0.2916667

p-value = 0.3428>0.05,无法拒绝原假设,故接收原假设,认为该大学有四分之一的男同学喜欢看武侠小说。

例5.7 据以往经验,新生儿染色体异常一般为1%,某医院观察了当地400名新生儿,只有一例染色体异常,问该地区新生儿染色体异常是否低于一般水平,检验的问题为:

  • H0:p>=0.01
  • H1:p< 0.01
binom.test(c(1, 399), p = 0.01, alternative = "less")
## 
##  Exact binomial test
## 
## data:  c(1, 399)
## number of successes = 1, number of trials = 400, p-value = 0.09048
## alternative hypothesis: true probability of success is less than 0.01
## 95 percent confidence interval:
##  0.0000000 0.0118043
## sample estimates:
## probability of success 
##                 0.0025
binom.test(1, 400, p = 0.01, alternative = "less")
## 
##  Exact binomial test
## 
## data:  1 and 400
## number of successes = 1, number of trials = 400, p-value = 0.09048
## alternative hypothesis: true probability of success is less than 0.01
## 95 percent confidence interval:
##  0.0000000 0.0118043
## sample estimates:
## probability of success 
##                 0.0025

p-value =0.09048>0.05,故无法拒绝原假设,不能认为该地区新生儿染色体异常低于一般水平

两样本比率的检验

例:某高校随机抽取了102个男学生与135个女学生调查家中有无计算机, 调查结果为23个男学生与25个女学家中有计算机,问在α=0.05水平上,能否认为男、女学生家中拥有计算机的比率一致。

success <- c(23, 25)
total <- c(102, 135)
prop.test(success, total)
## 
##  2-sample test for equality of proportions with continuity
##  correction
## 
## data:  success out of total
## X-squared = 0.3615, df = 1, p-value = 0.5477
## alternative hypothesis: two.sided
## 95 percent confidence interval:
##  -0.07256476  0.15317478
## sample estimates:
##    prop 1    prop 2 
## 0.2254902 0.1851852

p-value = 0.5477>0.05,无法拒绝原假设,故认为认为该大学的男、女学生家中拥有计算机的比率一致。

5.3 若干重要的非参数检验

在统计推断中,若推定或假定了总体分布的具体形式(如正态分布),只是其中含有若干未知参数,要给基于来自总体分布对参数做出估计或者进行某种形式的假设检验,这类推断方法称为参数方法。

但在许多实际问题中,人们往往对总体的分布知之甚少,很难对总体的分布形式做出正确的假定,最多只能对总体的分布做出诸如连续型分布、关于某点对称分布等一般性的假定。这种不假定总体分布的具体形式,尽量从数据本身来获得所需要的信息的统计方法成为非参数方法。

5.3.1 Pearson拟合优度卡方检验

1、理论分布完全已知的情况

原假设

  • H0:X具有分布F

例5.8 某消费者协会为了确定市场上消费者对5中品牌啤酒的喜好情况,随机抽取了1000名啤酒爱好者作为样本进行如下实验:没人得到5种品牌的啤酒各一瓶,但未标明牌子,这5种啤酒分别写着A、B、C、D、E字母的5张纸片随机的顺序送给每一个人。试根据这些数据判断消费者对这5种品牌啤酒的爱好有无明显差异?即检验数据是否服从均匀分布。

X <- c(210, 312, 170, 85, 223)
chisq.test(X)
## 
##  Chi-squared test for given probabilities
## 
## data:  X
## X-squared = 136.49, df = 4, p-value < 2.2e-16

p-value < 2.2e-16<0.05,拒绝原假设,以此认为消费者对这5种啤酒的喜好是有明显差异的。

例5.9 用Pearson拟合优度卡方检验检验学生成绩是否服从正态分布

X <- c(25, 45, 50, 54, 55, 61, 64, 68, 72, 75, 75, 78, 79, 81, 83, 84, 84, 84, 
    85, 86, 86, 86, 87, 89, 89, 89, 90, 91, 91, 92, 100)
A <- table(cut(X, br = c(0, 69, 79, 89, 100)))  #分组计数
A
## 
##   (0,69]  (69,79]  (79,89] (89,100] 
##        8        5       13        5
p <- pnorm(c(70, 80, 90, 100), mean(X), sd(X))
p
## [1] 0.3439036 0.5781068 0.7869659 0.9184654
p <- c(p[1], p[2] - p[1], p[3] - p[2], 1 - p[3])
p
## [1] 0.3439036 0.2342032 0.2088591 0.2130341
chisq.test(A, p = p)
## 
##  Chi-squared test for given probabilities
## 
## data:  A
## X-squared = 8.334, df = 3, p-value = 0.03959

p-value = 0.03959<0.05,拒绝原假设,即认为学生成绩不服从正态分布。

例5.10 大麦的杂交后代关于芒性的比例应是无芒:长芒:短芒=9:3:4.实际观测值为335:125:160,试检验观测值是否符合理论假设?

chisq.test(c(335, 125, 160), p = c(9, 3, 4)/16)
## 
##  Chi-squared test for given probabilities
## 
## data:  c(335, 125, 160)
## X-squared = 1.362, df = 2, p-value = 0.5061

p-value = 0.5061>0.05,无法拒绝原假设,即认为大麦芒性的比例符合9:3:4的比例。

例5.11 为研究电话总机在某段时间内接到的呼叫次数是够服从Poisson分布,现收集了42个数据,通过对数据的分析,问能否确认在某段时间内接到的呼叫次数服从Poission分布(alpha=0.1)?

X <- 0:6
Y <- c(7, 10, 12, 8, 3, 2, 0)
q <- ppois(X, mean(rep(X, Y)))
n <- length(X)
p[1] <- q[1]
p[n] <- 1 - q[n - 1]
for (i in 2:(n - 1)) p[i] <- q[i] - q[i - 1]
chisq.test(Y, p = p)
## Warning in chisq.test(Y, p = p): Chi-squared approximation may be
## incorrect
## 
##  Chi-squared test for given probabilities
## 
## data:  Y
## X-squared = 1.5057, df = 6, p-value = 0.9591

p-value = 0.9591,警告信息是由于Pearson卡方检验要求在分组后每组的频数至少要大于等于5。

而后三组的频数均小于5,解决的方法是把后三组合并为一组。

Z <- c(7, 10, 12, 8, 5)
n <- length(Z)
p <- p[1:n - 1]
p[n] <- 1 - q[n - 1]
chisq.test(Z, p = p)
## 
##  Chi-squared test for given probabilities
## 
## data:  Z
## X-squared = 0.5389, df = 4, p-value = 0.9696

p-value = 0.9696>0.1,无法拒绝原假设,因此认为服从Poisson分布。

5.3.2 Kolmogorov-Smirnov检验

1、单样本检验 R软件中使用ks.test()函数

原假设:

  • H0:X具有分布F

例5.12 对一台设备进行寿命检验,记录10次无故障工作时间,并按从小到大的次序排列,试用ks.test检验方法检验此设备无故障工作时间的分布是否服从lambda=1/1500的指数分布?

X <- c(420, 500, 920, 1380, 1510, 1650, 1760, 2100, 2300, 2350)
ks.test(X, "pexp", 1/1500)
## 
##  One-sample Kolmogorov-Smirnov test
## 
## data:  X
## D = 0.3015, p-value = 0.2654
## alternative hypothesis: two-sided

p-value = 0.2654>0.05,无法拒绝原假设,因此认为此设备的无故障工作时间的分布服从lambda=1/1500的指数分布。

2、双样本检验

原假设:

  • H0:F(x)=G(x)

例5.13 假定从分布函数为未知的F(x)和G(x)的总体中分别抽出25个和20个观测值的随机样本,现检验F(x)和G(x)是否相同。

X <- c(0.61, 0.29, 0.06, 0.59, -1.73, -0.74, 0.51, -0.56, 0.39, 1.64, 0.05, 
    -0.06, 0.64, -0.82, 0.37, 1.77, 1.09, -1.28, 2.36, 1.31, 1.05, -0.32, -0.4, 
    1.06, -2.47)
Y <- c(2.2, 1.66, 1.38, 0.2, 0.36, 0, 0.96, 1.56, 0.44, 1.5, -0.3, 0.66, 2.31, 
    3.29, -0.27, -0.37, 0.38, 0.7, 0.52, -0.71)
ks.test(X, Y)
## 
##  Two-sample Kolmogorov-Smirnov test
## 
## data:  X and Y
## D = 0.23, p-value = 0.5286
## alternative hypothesis: two-sided

p-value = 0.5286>0.05,无法拒绝原假设,因此认为检验F(x)和G(x)两个分布函数相同。

5.3.3 列联表数据的独立性检验

设两个变量X,Y均为离散型变量,检验原假设:

  • H0:X与Y独立

1、Pearson卡方检验,可使用函数chisq.test(),要求是:所有单元频数不为0,且频数>=5

例5.14 为了研究吸烟是否与患肺癌有关,对63位肺癌症患者及43位非肺癌症患者(对照组)调查了其中的吸烟人数,得到2X2列联表。,检验吸烟是否与患肺癌有关。

x <- c(60, 3, 32, 11)
dim(x) <- c(2, 2)
x
##      [,1] [,2]
## [1,]   60   32
## [2,]    3   11
chisq.test(x, correct = FALSE)  #不带连续校正,p-value = 0.00188
## 
##  Pearson's Chi-squared test
## 
## data:  x
## X-squared = 9.6636, df = 1, p-value = 0.00188
chisq.test(x, correct = TRUE)  #带连续校正
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  x
## X-squared = 7.9327, df = 1, p-value = 0.004855

p-value = 0.004855<0.05,拒绝原假设,认为吸烟与患肺癌有关。

例5.15 检验工作满意度是否与年收入有关

alt text

x <- c(20, 24, 80, 82, 22, 38, 104, 125, 13, 28, 81, 113, 7, 18, 54, 92)
dim(x) <- c(4, 4)
x
##      [,1] [,2] [,3] [,4]
## [1,]   20   22   13    7
## [2,]   24   38   28   18
## [3,]   80  104   81   54
## [4,]   82  125  113   92
chisq.test(x)
## 
##  Pearson's Chi-squared test
## 
## data:  x
## X-squared = 11.9886, df = 9, p-value = 0.214

p-value=0.214>0.05,无法拒绝原假设,即认为工作满意度与年收入无关

2、Fisher精确的独立检验

如果样本较小时(单元的期望频数小于5),需要用Fisher精确检验来作独立性检验

alt text

有一个单元频数小于5,应该用Fisher精确概率检验fisher.test()

x <- c(4, 5, 18, 6)
dim(x) <- c(2, 2)
x
##      [,1] [,2]
## [1,]    4   18
## [2,]    5    6
fisher.test(x)
## 
##  Fisher's Exact Test for Count Data
## 
## data:  x
## p-value = 0.121
## alternative hypothesis: true odds ratio is not equal to 1
## 95 percent confidence interval:
##  0.03974151 1.76726409
## sample estimates:
## odds ratio 
##  0.2791061

p-value = 0.121>0.05,且区间估计包含1,因此无法拒绝原假设,说明两个变量是独立的,即认为两组新生儿的HBV总体感染率无差别。

用Fisher精确检验对例5.14的数据作检验

x <- c(60, 3, 32, 11)
dim(x) <- c(2, 2)
x
##      [,1] [,2]
## [1,]   60   32
## [2,]    3   11
fisher.test(x)
## 
##  Fisher's Exact Test for Count Data
## 
## data:  x
## p-value = 0.00282
## alternative hypothesis: true odds ratio is not equal to 1
## 95 percent confidence interval:
##   1.626301 40.358904
## sample estimates:
## odds ratio 
##    6.74691

p-value = 0.00282<0.05,拒绝原假设,即认为吸烟与肺癌有关,由于赔率比大于1,因此还是正相关的,即吸烟越多,患肺癌的可能性就越大。

3、McNemar检验

McNemar检验是在相同个体上的两次检验,检验两无数据的两个相关分布的频数比变化的显著性。

alt text

例5.17 某胸科医院同时用甲乙两种方法测定202分痰标本中的抗酸杆菌,结果如表5.19所示,问甲乙两法的检出率有误差别?

alt text

X <- c(49, 21, 25, 107)
dim(X) <- c(2, 2)
X
##      [,1] [,2]
## [1,]   49   25
## [2,]   21  107
mcnemar.test(X, correct = FALSE)
## 
##  McNemar's Chi-squared test
## 
## data:  X
## McNemar's chi-squared = 0.3478, df = 1, p-value = 0.5553

p-value = 0.5553>0.05,无法拒绝原假设,不能认定两种检测方法的检出率有差异。

5.3.4 符号检验

1、检验一个样本是否来自某个总体

假设某个总体的中位数为M0,如果样本中位数M=M0,我们就接受样本来自某个总体的假设,原假设:H0:M=M0

例5.18 联合国人员在世界上66个大城市的生活花费指数,北京为99,假设这个样本是从世界许多大城市中随机抽样得到的,试用符号检验分析,北京是在中位数之上还是中位数之下。

样本的中位数M作为城市生活水平的中间值,因此需要检验:H0:M>=99,H1:M<99

X <- c(66, 75, 78, 80, 81, 81, 82, 83, 83, 83, 83, 84, 85, 85, 86, 86, 86, 86, 
    87, 87, 88, 88, 88, 88, 88, 89, 89, 89, 89, 90, 90, 91, 91, 91, 91, 92, 
    93, 93, 96, 96, 96, 97, 99, 100, 101, 102, 103, 103, 104, 104, 104, 105, 
    106, 109, 109, 110, 110, 110, 111, 113, 115, 116, 117, 118, 155, 192)
binom.test(sum(X > 99), length(X), alternative = "less")
## 
##  Exact binomial test
## 
## data:  sum(X > 99) and length(X)
## number of successes = 23, number of trials = 66, p-value =
## 0.009329
## alternative hypothesis: true probability of success is less than 0.5
## 95 percent confidence interval:
##  0.0000000 0.4563087
## sample estimates:
## probability of success 
##              0.3484848

由于-value = 0.009329<0.05,因此拒绝原假设,即认为北京的生活水平高于世界中间水平

2、用成对样本来检验两个总体是否存在显著差异

例5.19 用两种不同的饲料养猪,其增重情况如表5.10所示,试分析两种饲料养猪有无显著差异

x <- c(25, 30, 28, 23, 27, 35, 30, 28, 32, 29, 30, 30, 31, 16)
y <- c(19, 32, 21, 19, 25, 31, 31, 26, 30, 25, 28, 31, 25, 25)
binom.test(sum(x < y), length(x))
## 
##  Exact binomial test
## 
## data:  sum(x < y) and length(x)
## number of successes = 4, number of trials = 14, p-value = 0.1796
## alternative hypothesis: true probability of success is not equal to 0.5
## 95 percent confidence interval:
##  0.08388932 0.58103526
## sample estimates:
## probability of success 
##              0.2857143

p-value = 0.1796>0.05,无法拒绝原假设,即认为两种饲料养猪无显著差异

例5.20 某饮料店为了了解顾客对饮料的爱好情况,进一步改进他们的工作,对顾客喜欢咖啡还是喜欢奶茶,或者两者都喜欢进行了调查,该店某日随机地抽取了13名顾客进行调查,顾客喜欢咖啡超过奶茶用正号表示,喜欢奶茶超过咖啡用负号表示,两者同样爱好用0表示,试分析顾客喜欢咖啡还是喜欢奶茶。

检验如下假设:

  • H0:顾客喜欢咖啡等于喜欢奶茶
  • H1:顾客喜欢咖啡超过奶茶
binom.test(3, 12, p = 1/2, alternative = "less", conf.level = 0.1)  #p-value = 0.073<0.1,因此拒绝原假设,认为喜欢咖啡的人超过喜欢奶茶的人
## 
##  Exact binomial test
## 
## data:  3 and 12
## number of successes = 3, number of trials = 12, p-value = 0.073
## alternative hypothesis: true probability of success is less than 0.5
## 10 percent confidence interval:
##  0.0000000 0.1541877
## sample estimates:
## probability of success 
##                   0.25
binom.test(3, 12, p = 1/2, alternative = "less", conf.level = 0.05)
## 
##  Exact binomial test
## 
## data:  3 and 12
## number of successes = 3, number of trials = 12, p-value = 0.073
## alternative hypothesis: true probability of success is less than 0.5
## 5 percent confidence interval:
##  0.0000000 0.1228507
## sample estimates:
## probability of success 
##                   0.25

p-value = 0.073<0.05,因此无法拒绝原假设,认为喜欢咖啡的人和喜欢奶茶的人一样多。

5.3.5 秩统计量

秩统计量是在非参数检验中有广泛应用的统计量,它的一个重要特性是分布无关性。在R软件中rank()函数可以计算秩统计量

5.3.6 秩相关检验

1.Spearman秩相关检验,应用cor.test()函数

例5.22 一项有6个人参加表演的竞赛,有两人进行评定,试用秩相关检验方法检验这两个评定员对等级评定有无相关关系。

x <- c(1, 2, 3, 4, 5, 6)
y <- c(6, 5, 4, 3, 2, 1)
cor.test(x, y, method = "spearman")
## 
##  Spearman's rank correlation rho
## 
## data:  x and y
## S = 70, p-value = 0.002778
## alternative hypothesis: true rho is not equal to 0
## sample estimates:
## rho 
##  -1

p-value = 0.002778<0.05,拒绝原假设,认为变量x与y相关。由于计算出来的rho=-1,表示这两个量是完全负相关的,即两人的结论有关系,但完全相反。

2.Kendall相关检验

例5.23 某幼儿园对9对双胞胎的智力进行检验,并按百分制打分。试用kendall相关检验方法检验双胞胎的智力是否相关。

X <- c(86, 77, 68, 91, 70, 71, 85, 87, 63)
Y <- c(88, 76, 64, 96, 65, 80, 81, 72, 60)
cor.test(X, Y, method = "kendall")
## 
##  Kendall's rank correlation tau
## 
## data:  X and Y
## T = 31, p-value = 0.005886
## alternative hypothesis: true tau is not equal to 0
## sample estimates:
##       tau 
## 0.7222222

p-value = 0.005886<0.05,拒绝原假设,认为双胞胎的智力是相关的,而且是正相关的

5.3.7 Wilcoxon秩检验

1.对来自一个总体样本的检验

例5.24 假定某电池厂宣称该厂生产的某种型号电池寿命的中位数为140安培小时,为了检验该厂生产的电池是否符合其规定的标准,现从新近生产的一批电池中抽取20个随机样本,并对这个20个电池的寿命进行了测试,试用Wilcoxon符号秩检验分析该厂生产的电池是否符合其标准。

假设如下:

  • H0:电池中位数M>=140安培小时
  • H1:电池中位数M<140安培小时
X <- c(137, 140, 138.3, 139, 144.3, 139.1, 141.7, 137.3, 133.5, 138.2, 141.1, 
    139.2, 136.5, 136.5, 135.6, 138, 140.9, 140.6, 136.3, 134.1)
wilcox.test(X, mu = 140, alternative = "less", exact = FALSE, correct = FALSE, 
    conf.int = TRUE)
## 
##  Wilcoxon signed rank test
## 
## data:  X
## V = 34, p-value = 0.007034
## alternative hypothesis: true location is less than 140
## 95 percent confidence interval:
##   -Inf 139.2
## sample estimates:
## (pseudo)median 
##          138.2

p-value=0.007034<0.05,故拒绝原建设,认为中位数达不到140

alt text alt text

作如下假设:

  • H0:新复合肥的产量与原肥料的产量相同
  • H1:新复合肥的产量高于原肥料的产量
x <- c(459, 367, 303, 392, 310, 342, 421, 446, 430, 412)
y <- c(414, 306, 321, 443, 281, 301, 353, 391, 405, 390)
wilcox.test(x, y, alternative = "greater", paired = TRUE)
## 
##  Wilcoxon signed rank test
## 
## data:  x and y
## V = 47, p-value = 0.02441
## alternative hypothesis: true location shift is greater than 0

p-value = 0.02441<0.05,拒绝原假设,认为新复合肥能够显著提高小麦的产量。

2.非成对样本的秩次和检验

alt text

作如下假设:

  • H0:两组工人血铅无差异
  • H1:铅作业组血铅高于非铅作业组
x <- c(24, 26, 29, 34, 43, 58, 63, 72, 87, 101)
y <- c(82, 87, 97, 121, 164, 208, 213)
wilcox.test(x, y, alternative = "less", exact = FALSE, correct = FALSE)  #不采用连续修正
## 
##  Wilcoxon rank sum test
## 
## data:  x and y
## W = 4.5, p-value = 0.001449
## alternative hypothesis: true location shift is less than 0
wilcox.test(x, y, alternative = "less", exact = FALSE, correct = TRUE)  #采用连续修正
## 
##  Wilcoxon rank sum test with continuity correction
## 
## data:  x and y
## W = 4.5, p-value = 0.001698
## alternative hypothesis: true location shift is less than 0

P-值均小于0.05,故拒绝原假设,认为铅作业组工人血铅高于非铅作业组工人。

alt text

第一种情况

x <- c(3, 5, 7, 9, 10)
y <- c(1, 2, 4, 6, 8)
wilcox.test(x, y, alternative = "greater")
## 
##  Wilcoxon rank sum test
## 
## data:  x and y
## W = 19, p-value = 0.1111
## alternative hypothesis: true location shift is greater than 0

p-value = 0.1111>0.05,无法拒绝原假设,认为新的教学效果并不显著优于原方法。

第二种情况

x <- c(4, 6, 7, 9, 10)
y <- c(1, 2, 3, 5, 8)
wilcox.test(x, y, alternative = "greater")
## 
##  Wilcoxon rank sum test
## 
## data:  x and y
## W = 21, p-value = 0.04762
## alternative hypothesis: true location shift is greater than 0

p-value = 0.04762<0.05,拒绝原假设,认为新的教学效果显著优于原方法。

alt text

x <- rep(1:4, c(62, 41, 14, 11))
y <- rep(1:4, c(20, 37, 16, 15))
wilcox.test(x, y, exact = FALSE)
## 
##  Wilcoxon rank sum test with continuity correction
## 
## data:  x and y
## W = 3994, p-value = 0.0001242
## alternative hypothesis: true location shift is not equal to 0

p-value = 0.0001242<0.05,拒绝原假设,认为该药物对两型慢性支气管炎的治疗是不相同的。

3.mood检验 Mood检验是用来检验两样本尺度参数之间关系的一种非参数方法

例:两个村农民的月收入分别为(单位: 元) A村:321, 266, 256, 388, 330, 329, 303, 334, 299, 221, 365, 250, 258, 342,343, 298, 238, 317, 354;B村:488, 598, 507, 428, 807, 342, 512, 350, 672, 589, 665, 549, 451, 481,514, 391,366,468。问两个村农民的月收入的内部差异是否相同?(α=0.05)

A <- c(321, 266, 256, 388, 330, 329, 303, 334, 299, 221, 365, 250, 258, 342, 
    343, 298, 238, 317, 354)
B <- c(488, 598, 507, 428, 807, 342, 512, 350, 672, 589, 665, 549, 451, 481, 
    514, 391, 366, 468)
diff <- median(B) - median(A)
A <- A + diff
mood.test(A, B)
## 
##  Mood two-sample test of scale
## 
## data:  A and B
## Z = -2.4846, p-value = 0.01297
## alternative hypothesis: two.sided

p-value = 0.01297<0.05,拒绝原假设,认为这两个村的内部差异是不同的。

mood检验需要的假定之一是要两样本的中位数相同,故在做检验时要先消除两样本之间中位数的差异,接着才可以做mood检验。

5.3.8 多总体的比较与检验

1.位置参数的Kruskal-Wallis秩和检验

例:下面的数据是游泳、打篮球、骑自行车等三种不同的运动在30分钟内消耗的热量(单位:卡路里). 这些数据是否说明这三种运动消耗的热量全相等?(α =0.05)游泳: 306, 385, 300, 319, 320;打篮球: 311, 364, 315, 338, 398;骑自行车: 289, 198, 201, 302, 289。

x <- list(swim = c(306, 385, 300, 319, 320), basketball = c(311, 364, 315, 338, 
    398), bicycle = c(289, 198, 201, 302, 289))
kruskal.test(x)
## 
##  Kruskal-Wallis rank sum test
## 
## data:  x
## Kruskal-Wallis chi-squared = 9.1564, df = 2, p-value = 0.01027

p-value = 0.01027<0.05,拒绝原假设,认为为这三种运动消耗的热量不全相等。

2.尺度参数的Ansari-Bradley检验

例:两个工人加工的零件尺寸(各10个)为(单位:mm):工人A: 18.0, 17.1, 16.4, 16.9, 16.9, 16.7, 16.7, 17.2, 17.5, 16.9;工人B: 17.0, 16.9, 17.0, 16.9, 17.2, 17.1,16.8,17.1,17.1,17.2。这个结果能否说明两个工人的水平(加工精度)一致?(α=0.05)

worker.a <- c(18, 17.1, 16.4, 16.9, 16.9, 16.7, 16.7, 17.2, 17.5, 16.9)
worker.b <- c(17, 16.9, 17, 16.9, 17.2, 17.1, 16.8, 17.1, 17.1, 17.2)
ansari.test(worker.a, worker.b)
## Warning in ansari.test.default(worker.a, worker.b): cannot compute exact
## p-value with ties
## 
##  Ansari-Bradley test
## 
## data:  worker.a and worker.b
## AB = 41.5, p-value = 0.04232
## alternative hypothesis: true ratio of scales is not equal to 1

因为p值=0.04232<0.05, 故拒绝原假设,认为这两个工人的水平不一样。

3.尺度参数的的Fligner-Killeen检验

例:三名不同的运动员A、B、C同时在同一条件下进行打靶比赛,各打10发子弹,他们打中的环数如下:A:8,7,9,10,9,6,5,8,10,5;B:8,7,9,6,8,9,10,7,8,9;C:10,10,9,6,8,3,5,6,7,4。问这三名运动员的稳定性是否一样?

x <- list(A = c(8, 7, 9, 10, 9, 6, 5, 8, 10, 5), B = c(8, 7, 9, 6, 8, 9, 10, 
    7, 8, 9), C = c(10, 10, 9, 6, 8, 3, 5, 6, 7, 4))
fligner.test(x)
## 
##  Fligner-Killeen test of homogeneity of variances
## 
## data:  x
## Fligner-Killeen:med chi-squared = 5.1905, df = 2, p-value =
## 0.07463

因为p值=0.07463>0.05,故接受原假设,认为这三名运动员的稳定性相同。