一.t检验
1.单样本t检验
代码语言:text
复制> daily.intake<-c(1,2,3,4,5)
> t.test(daily.intake,mu=10)#mu为已知总体均数
One Sample t-test
data: daily.intake
t = -9.8995, df = 4, p-value = 0.0005844#p值<0.05,认为daily.intake与总体均数有差异
alternative hypothesis: true mean is not equal to 10
95 percent confidence interval:#样本均数的置信区间
1.036757 4.963243
sample estimates:
mean of x
3
2.两独立样本均数比较的t检验
代码语言:text
复制install.packages("ISwR")
library(ISwR)
attach(energy)
energy#瘦子与胖子24小时消耗的能量
> t.test(expend~stature)
Welch Two Sample t-test
data: expend by stature
t = -3.8555, df = 15.919, p-value = 0.001411
alternative hypothesis: true difference in means between group lean and group obese is not equal to 0
95 percent confidence interval:#瘦子与胖子消耗能量差值的可信区间
-3.459167 -1.004081
sample estimates:
mean in group lean mean in group obese
8.066154 10.297778
t.test(expend~stature,var.equal=T)#假定两样本方差齐,其结果与上相同
如何判断方差齐不齐?——方差齐性检验
代码语言:text
复制> var.test(expend~stature)
F test to compare two variances
data: expend by stature
F = 0.78445, num df = 12, denom df = 8, p-value = 0.6797#p值大于0.05,认为方差齐,读取以上两种结果均可,第一种结果更保守
alternative hypothesis: true ratio of variances is not equal to 1
95 percent confidence interval:
0.1867876 2.7547991
sample estimates:
ratio of variances
0.784446
3.配对样本t检验
代码语言:text
复制attach(intake)#载入数据集
intake#同一个人绝经前与绝经后的能量摄入
> t.test(pre,post,paired=T)
Paired t-test
data: pre and post
t = 11.941, df = 10, p-value = 3.059e-07
alternative hypothesis: true mean difference is not equal to 0
95 percent confidence interval:
1074.072 1566.838
sample estimates:
mean difference
1320.455
二.非参数秩和检验
1.单样本秩和检验
代码语言:javascript
复制> daily.intake<-c(1,2,3,4,5)
> wilcox.test(daily.intake,mu=10)
Wilcoxon signed rank exact test
data: daily.intake
V = 0, p-value = 0.0625
alternative hypothesis: true location is not equal to 10
2.两样本wilcoxon秩和检验
代码语言:text
复制library(ISwR)
attach(energy)
energy#瘦子与胖子24小时消耗的能量
> wilcox.test(expend~stature)
Wilcoxon rank sum test with continuity correction
data: expend by stature
W = 12, p-value = 0.002122
alternative hypothesis: true location shift is not equal to 0