
 问题导向:在样本较小时(频数小于4时)用Fisher精确检验来做独立性检验
 H0:变量独立,对产品质量无影响; H1: 变量间有关系,对产品质量有影响
> x=matrix(c(3,6,4,4),nrow=2)
> fisher.test(x)
        Fisher's Exact Test for Count Data
data:  x
p-value = 0.6372
alternative hypothesis: true odds ratio is not equal to 1
95 percent confidence interval:
 0.04624382 5.13272210
sample estimates:
odds ratio 
  0.521271 
fisher.test()函数格式:
fisher.test(x,y,alternative='',conr.int=T(给出区间估计),conf.level=0.95),x,y为矩阵
结果:p-value = 0.6372>0.05,不能拒绝原假设,两变量独立,两种工艺对产品的质量没有影响

 问题导向:因为是在相同个体上的两次检验,所以用McNemar检验
 H0:结果相同; H1: 结果不同
> x=matrix(c(58, 1, 8, 2, 42, 9, 3, 7, 17),nrow=3)
> mcnemar.test(x)
        McNemar's Chi-squared test
data:  x
McNemar's chi-squared = 2.8561, df = 3, p-value = 0.4144
结果:p-value = 0.4144>0.05,不能拒绝原假设,两种方法测定结果相同

 问题导向:观察数据,中位数应该比14.6小,所以原假设>14.6,数据中大于14.6的有只有一个,小于14.6的有9个,选符号的秩小的1放在函数中,程序语言即sum(x)>14.6
 H0:中位数>14.6 H1:中位数<14.6
 (1)符号检验:
> x=c(13.32,13.06,14.02,11.86,13.58,13.77,13.51,14.42,14.44,15.43)
> binom.test(sum(x)>14.6,length(x),al="l")  #alternative='less'备择假设
        Exact binomial test
data:  sum(x) > 14.6 and length(x)
number of successes = 1, number of trials = 10, p-value = 0.01074
alternative hypothesis: true probability of success is less than 0.5
95 percent confidence interval:
 0.0000000 0.3941633
sample estimates:
probability of success 
                   0.1 
结果:p-value = 0.01074<0.05,拒绝原假设,中位数<14.6
 (2)Wilcoxon符号秩检验:
> x=c(13.32,13.06,14.02,11.86,13.58,13.77,13.51,14.42,14.44,15.43)
> wilcox.test(x,mu=14.6,al="l",conf.int=T)
        Wilcoxon signed rank test with continuity correction
data:  x
V = 4.5, p-value = 0.01087
alternative hypothesis: true location is less than 14.6
95 percent confidence interval:
     -Inf 14.37497
sample estimates:
(pseudo)median 
      13.74995 
Warning messages:
1: In wilcox.test.default(x, mu = 14.6, al = "l", conf.int = T) :
  cannot compute exact p-value with ties
2: In wilcox.test.default(x, mu = 14.6, al = "l", conf.int = T) :
  cannot compute exact confidence interval with ties
结果:p-value = 0.01087<0.05,拒绝原假设,认为中位数<14.6
 
 H0:无显著差异 H1:有显著差异
 (1)符号检验法:
> x=c(48,33,37.5,48,42.5,40,42,36,11.3,22,36,27.3,14.2,32.1,52,38,17.3,20,21,46.1)
> y=c(37,41,23.4,17,31.5,40,31,36,5.7,11.5,21,6.1,26.5,21.3,44.5,28,22.6,20,11,22.3)
> binom.test(sum(x>y),length(x))
        Exact binomial test
data:  sum(x > y) and length(x)
number of successes = 14, number of trials = 20, p-value = 0.1153
alternative hypothesis: true probability of success is not equal to 0.5
95 percent confidence interval:
 0.4572108 0.8810684
sample estimates:
probability of success 
                   0.7 
结果:p-value = 0.1153>0.05,接受原假设,无显著差异
 (2)Wilcoxon符号秩检验:
> x=c(48,33,37.5,48,42.5,40,42,36,11.3,22,36,27.3,14.2,32.1,52,38,17.3,20,21,46.1)
> y=c(37,41,23.4,17,31.5,40,31,36,5.7,11.5,21,6.1,26.5,21.3,44.5,28,22.6,20,11,22.3)
> wilcox.test(x,y,paired=TRUE)  #成对数据
        
        Wilcoxon signed rank test with continuity correction
data:  x and y
V = 136, p-value = 0.005191
alternative hypothesis: true location shift is not equal to 0
Warning messages:
1: In wilcox.test.default(x, y, paired = TRUE) :
  cannot compute exact p-value with ties
2: In wilcox.test.default(x, y, paired = TRUE) :
  cannot compute exact p-value with zeroes
结果:p-value = 0.005191<0.05,拒绝原假设,有显著差异*
 (3正态性检验:
 H0: 数据均服从正态分布 H1:数据不服从正态分布
对x进行正态性检验
> ks.test(x,"pnorm",mean(x),sd(x))
        One-sample Kolmogorov-Smirnov test
data:  x
D = 0.14067, p-value = 0.8235
alternative hypothesis: two-sided
Warning message:
In ks.test(x, "pnorm", mean(x), sd(x)) :
  ties should not be present for the Kolmogorov-Smirnov test
对y进行正态性检验
> ks.test(y,"pnorm",mean(y),sd(y))
        One-sample Kolmogorov-Smirnov test
data:  y
D = 0.10142, p-value = 0.973
alternative hypothesis: two-sided
结果:x,y的p值都大于0.05,不能拒绝原假设,认为数据均服从正态分布
 (4)方差齐性检验:
 H0:方差相同 H1:方差不同
> var.test(x,y)
        F test to compare two variances
data:  x and y
F = 1.1406, num df = 19, denom df = 19, p-value = 0.7772
alternative hypothesis: true ratio of variances is not equal to 1
95 percent confidence interval:
 0.4514788 2.8817689
sample estimates:
ratio of variances 
          1.140639 
结果:p-value = 0.7772>0.05,不能拒绝原假设,认为两组数据方差相同
 (5)t检验:
 H0:两组数据均值相同 H1:两组数据均值不同
> t.test(x,y,var.equal=TRUE)  #方差相同的情况
        Two Sample t-test
data:  x and y
t = 2.2428, df = 38, p-value = 0.03082
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
  0.8125529 15.8774471
sample estimates:
mean of x mean of y 
   33.215    24.870 
结果: p-value = 0.03082<0.05,拒绝原假设,认为两组数据均值不同
 (6)综上所述,Wilcoxon符号秩检验的差异检出能力最强,符号检验的差异检出最弱。
 
 H0:相互独立,不相关 H1:有关系
(1)spearman秩相关检验:
> x=c(24,17,20,41,52,23,46,18,15,20)
> y=c(8,1,4,7,9,5,10,3,2,6)
> cor.test(x,y,method="spearman")
        Spearman's rank correlation rho
data:  x and y
S = 9.5282, p-value = 4.536e-05
alternative hypothesis: true rho is not equal to 0
sample estimates:
      rho 
0.9422536 
Warning message:
In cor.test.default(x, y, method = "spearman") :
  Cannot compute exact p-value with ties
结果:p-value = 4.536e-05<0.05,拒绝原假设,认为学习等级与学习成绩有关系
 (2)kendall秩相关检验:
> cor.test(x,y,method="kendall")
        Kendall's rank correlation tau
data:  x and y
z = 3.2329, p-value = 0.001225
alternative hypothesis: true tau is not equal to 0
sample estimates:
      tau 
0.8090398 
Warning message:
In cor.test.default(x, y, method = "kendall") :
  Cannot compute exact p-value with ties
结果:p-value = 0.001225<0.05,拒绝原假设,认为学习等级与学习成绩有关系,且因为sample estimates:
 tau
 0.8090398 ,呈正相关
 
 H0:两种疗法无差别 H1:新疗法优于原疗法
 差:1,较差:2,一般:3, 较好:4, 好:5
> x=rep(1:5,c(0,1,9,7,3))
> y=rep(1:5,c(2,2,11,4,1))
> wilcox.test(x,y)
        Wilcoxon rank sum test with continuity correction
data:  x and y
W = 266, p-value = 0.05509
alternative hypothesis: true location shift is not equal to 0
Warning message:
In wilcox.test.default(x, y) : cannot compute exact p-value with ties
> 
结果:p-value = 0.05509>0.05,不能拒绝原假设,不能认为新方法的疗效显著优于原疗法










