5.1单样本t检验
daily.intake<-c(5260,5470,5640,6180,6390,6515,6805,7515,8230,8770)
mean(daily.intake)#求均值
[1] 6677.5
sd(daily.intake)#计算标准差
[1] 1174.11
quantile(daily.intake)#四分位数点
0% 25% 50% 75% 100%
5260.0 5775.0 6452.5 7337.5 8770.0
t.test(daily.intake,mu=7725)#检验分布是否满足mu等于7725,
One Sample t-test
data: daily.intake
t = -2.8213, df = 9, p-value = 0.02001
alternative hypothesis: true mean is not equal to 7725
95 percent confidence interval:
5837.592 7517.408
sample estimates:
mean of x
6677.5
#wilcoxon符号秩检验
wilcox.test(daily.intake,mu=7725)#correct选项为是否需要进行连续性修正,使用逻辑值控制
#exact选项为是否需要进行精确的计算,使用逻辑值控制
Wilcoxon signed rank test
data: daily.intake
V = 6, p-value = 0.02734
alternative hypothesis: true location is not equal to 7725
5.3双样本t检验
library(ISwR)
attach(energy)
The following objects are masked from energy (pos = 4):
expend, stature
The following objects are masked from energy (pos = 7):
expend, stature
扫描二维码关注公众号,回复: 5139864 查看本文章
energy
expend stature
1 9.21 obese
2 7.53 lean
3 7.48 lean
4 8.08 lean
5 8.09 lean
6 10.15 lean
7 8.40 lean
8 10.88 lean
9 6.13 lean
10 7.90 lean
11 11.51 obese
12 12.79 obese
13 7.05 lean
14 11.85 obese
15 9.97 obese
16 7.48 lean
17 8.79 obese
18 9.69 obese
19 9.68 obese
20 7.58 lean
21 9.19 obese
22 8.11 lean
t.test(expend~stature)#对energy数据框下expend变量数据通过stature作为分类变量进行双样本t检验
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 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)#使用控制方差相等的参数var.equal=T
Two Sample t-test
data: expend by stature
t = -3.9456, df = 20, p-value = 0.000799
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-3.411451 -1.051796
sample estimates:
mean in group lean mean in group obese
8.066154 10.297778
5.4比较方差
var.test(expend~stature)#对双样本方差进行F检验
F test to compare two variances
data: expend by stature
F = 0.78445, num df = 12, denom df = 8, p-value = 0.6797
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
5.5双样本wilcoxon检验
wilcox.test(expend~stature,exact=F)#对energy数据框下expend变量数据通过stature作为分类变量进行双样本wilcoxon检验,并关闭精确计算
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
5.6配对t检验
attach(intake)
The following objects are masked from intake (pos = 4):
post, pre
intake
pre post
1 5260 3910
2 5470 4220
3 5640 3885
4 6180 5160
5 6390 5645
6 6515 4680
7 6805 5265
8 7515 5975
9 7515 6790
10 8230 6900
11 8770 7335
post-pre#计算妇女月经前后能量摄入之差
[1] -1350 -1250 -1755 -1020 -745 -1835 -1540 -1540 -725 -1330 -1435
t.test(pre,post,paired = T)#参数paird=T对变量两组数据进行配对t检验
Paired t-test
data: pre and post
t = 11.941, df = 10, p-value = 3.059e-07
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
1074.072 1566.838
sample estimates:
mean of the differences
1320.455
t.test(pre,post)#此时两组数据会被合并进行t检验
Welch Two Sample t-test
data: pre and post
t = 2.6242, df = 19.92, p-value = 0.01629
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
270.5633 2370.3458
sample estimates:
mean of x mean of y
6753.636 5433.182
5.7配对wilcoxon检验
wilcox.test(pre,post,paired=T)#配对wilcoxon检验
Warning in wilcox.test.default(pre, post, paired = T): cannot compute exact
p-value with ties
`
Wilcoxon signed rank test with continuity correction
data: pre and post
V = 66, p-value = 0.00384
alternative hypothesis: true location shift is not equal to 0