如若不满足前提条件 - 合并某些类别(注意自由度的改变) - Fisher’s exact test (Fisher精确检验) - Permutation test
分类变量间的相关性: 2 × 2 表的Fisher 精确检验
示例:2 × 2 表中数据不满足列联表的卡方检验的前提
观察频数
结果
A
B
发生X事件
15
6
未发生X事件
7
322
期望频数
结果
A
B
发生 X 事件
1.3
19.7
未发生 X 事件
20.7
308.3
分类变量间的相关性: 2 × 2 表的Fisher 精确检验
Fisher 检验怎样计算 P 值?
列出 成立下与观察结果一样或更极端的2×2 列联表
行和列的总计都不变( 假设的独立性)
改变其中一个格子的数值(更极端), 并调整其它个字的值以匹配总计
分类变量间的相关性: 2 × 2 表的Fisher 精确检验
用 R 做 Fisher Exact Test
sparse <-rbind(Event =c(A =15, B =6),No_event =c(A =7, B =322))fisher.test(sparse)
Fisher's Exact Test for Count Data
data: sparse
p-value <2e-16
alternative hypothesis: true odds ratio is not equal to 1
95 percent confidence interval:
29.95 457.27
sample estimates:
odds ratio
108.4
n <-length(height)se <-sd(height) /sqrt(n)t_obs <- (mean(height) -10) / sep_value <-2*pt(-abs(t_obs), df = n -1) # 从左尾计算P值data.frame(SE = se, t = t_obs, df = n -1, p = p_value)
SE t df p
1 0.2519 2.316 11 0.04089
# p = 2 * pt(abs(t_obs), df = n - 1, lower.tail = FALSE) # 从右尾计算P值
双侧检验把 分布的两端计入 值
abs(t_obs)为观测 值距离 0 有多远
单样本 -test
R 操作:t.test() 检验
result <-t.test(height, mu =10)result
One Sample t-test
data: height
t = 2.3, df = 11, p-value = 0.04
alternative hypothesis: true mean is not equal to 10
95 percent confidence interval:
10.03 11.14
sample estimates:
mean of x
10.58
单样本 -test
总览
检验统计量
结论:拒绝零假设
那么,样本均值相对参考值 的偏离方向是怎样的?
幼苗平均高度比参考值高还是低?相差多少?
单样本 -test
有哪些前提条件?
数据是从总体中随机抽取的样本
变量在总体中呈正态分布(小样本尤其应检查重偏态与极端值)
用原始点图和 Q–Q 图检查数据
样本较大时,均值推断对一般的非正态较稳健
配对 检查差值,独立双样本 检查各组数值
单样本 -test
R 诊断:通过图形检查数据是否存在明显偏态或极端值
点图:通过展示每个数据点来观察其分布范围、聚集情况
Q–Q 图:把排序后的样本值与正态分布的理论分位数对应起来
par(mfrow =c(1, 2), mar =c(4, 3.5, 1, 1))stripchart(height, method ="stack", pch =19,xlab ="Height (cm)")qqnorm(height, main ="", pch =19)qqline(height, col ="#B9472B", lwd =2)
Paired t-test
data: after and before
t = 3.5, df = 7, p-value = 0.01
alternative hypothesis: true mean difference is not equal to 0
95 percent confidence interval:
1.319 6.681
sample estimates:
mean difference
4
t.test(change, mu =0)
One Sample t-test
data: change
t = 3.5, df = 7, p-value = 0.01
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
1.319 6.681
sample estimates:
mean of x
4
独立双样本 检验
示例:比较两种光照下的叶面积 - 不同植株分别接受两种光照:shade vs sun - 每株记录一个叶面积(cm²),各植株独立接受处理
shade <-c(14, 12, 17, 10, 13, 15, 20, 11)sun <-c(10, 8, 11, 9, 10, 12, 9, 13, 10, 11)rbind(shade =c(n =length(shade), mean =mean(shade), var =var(shade)),sun =c(n =length(sun), mean =mean(sun), var =var(sun)))
Welch Two Sample t-test
data: shade and sun
t = 2.9, df = 9.3, p-value = 0.02
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
0.8699 6.5301
sample estimates:
mean of x mean of y
14.0 10.3
那么,t.test() 中的变量顺序如何影响结果的解读?
t.test(sun, shade)
独立双样本 检验
Welch -test:如何报告结果?
原始数据可以算得:
遮阴组平均叶面积为 ,日照组为 ;
统计检验结果:
Welch ,。
结论?
在 水平下,拒绝两组总体均值相等的零假设;
遮阴组比日照组的叶面积平均大 ;
95% CI 为 ;
独立双样本 检验
等方差双样本 检验
假设数值变量的标准差和方差在两个总体中相同:
稳健性(robust)
即使 和 差三倍以上,适度样本量时(如 )依然适用;
稍微偏离正态分布依然适用;
合并方差:用两组样本方差的自由度加权平均
其中 ;新的自由度为 ;
独立双样本 检验
等方差 检验中的标准误与统计量
R 操作 var.equal = TRUE
t.test(shade, sun, var.equal =TRUE)
Two Sample t-test
data: shade and sun
t = 3.2, df = 16, p-value = 0.006
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
1.236 6.164
sample estimates:
mean of x mean of y
14.0 10.3