统计案例:如何分析同一组样本多次测量的数据

对于同一组样本,进行多次测量(不同部位的测量,或多个时间点的测量),需要进行ANOVA分析,和事后多重的两两比较。
对于两两比较分析,在R语言中可用pairwise t检验,两两比较(多重事后比较),配对参数设置为TRUE,使用BH法或FDR法进行校正。
以下是实现该统计分析的具体代码:
library(tidyverse)
library(rstatix)
library(ggpubr)
data("selfesteem", package = "datarium")
head(selfesteem, 3)
#0.数据准备 宽变长----
selfesteem <- selfesteem %>%
gather(key = "time", value = "score", t1, t2, t3) %>%
convert_as_factor(id, time)
head(selfesteem, 3)
#1.ANOVA分析 若显著则进行事后多重比较----
selfesteem %>%
anova_test(score ~ time)
# ANOVA Table (type II tests)
#
# Effect DFn DFd F p p<.05 ges
# 1 time 2 27 65.261 4.56e-11 * 0.829
#2.pairwise t检验----
stat.test <- selfesteem %>%
pairwise_t_test(
score ~ time, paired = TRUE,
#"holm", "hochberg", "hommel", "bonferroni", "BH", "BY", "fdr", "none"
p.adjust.method = "BH"
)
stat.test
# # A tibble: 3 × 10
# .y. group1 group2 n1 n2 statistic df p p.adj p.adj.signif
# * <chr> <chr> <chr> <int> <int> <dbl> <dbl> <dbl> <dbl> <chr>
# 1 score t1 t2 10 10 -4.97 9 0.000772 0.000886 ***
# 2 score t1 t3 10 10 -13.2 9 0.000000334 0.000001 ****
# 3 score t2 t3 10 10 -4.87 9 0.000886 0.000886 ***
#Create the plot
myplot <- ggboxplot(selfesteem, x = "time", y = "score", add = "point", color="time")
# Add statistical test p-values
stat.test <- stat.test %>% add_xy_position(x = "time")
myplot + stat_pvalue_manual(stat.test, label = "p.adj.signif")
最后可视化效果如下

最后编辑于 2024-02-14 · 浏览 3260