运行 一系列 post-hoc t-tests R 中的方差分析

Run a series of post-hoc t-tests ANOVAs in R

我有 运行 一系列方差分析,检查 link 组成员和一系列因变量之间的关系,生成的数据框包含每个方差分析模型的结果(一行 = 一行方差分析模型)。我现在想 运行 一系列 post-hoc 测试基于我生成的因变量-独立变量组合的列表,这些组合在多次比较校正后仍然存在。我试图编写一个函数,将这些 DV-IV 组合中的每一个提交到 pairwise.t.test,尽管我的代码目前没有按预期工作。请在下面查看带有模拟数据的此代码的摘录:

# simulated data
df = data.frame(ID = c(1001, 1002, 1003, 1004, 1005, 1006,1007, 1008, 1009,   1010, 1011),
                    Group = as.numeric(c('0','1','2','0','2','1','0','2','0','1','0')),
                    testscore_1 = as.numeric(c('23','28','30','15','7','18','29','27','14','22','24')),
                    testscore_2 = as.numeric(c('1','3','2','5','8','2','5','6','7','8','2')),
                    testscore_3 = as.numeric(c('18','20','19','15','20','23','19','25','10','14','12')))

# df containing combination of IV and DV that survived multiple comparison correction 
df_results = data.frame(Independent_variable = c("Group", "Group"), Dependent_variable=c("testscore_1", "testscore_2"))

dvs_ivs <- paste0("x = df$", df_results$Group, "g = df$", df_results$Dependent_variable)

posthoc_t_test_results <- lapply(dvs_ivs, function(x) {
    pairwise.t.test(print.noquote(x), p.adjust.method = "none")
})

Error message :  Error in factor(g) : argument "g" is missing, with no default "

有什么建议吗?

您可以尝试使用 Map -

Map(function(x, y) pairwise.t.test(df[[x]], df[[y]], p.adjust.method = "none"),
  df_results$Dependent_variable, df_results$Independent_variable)

#$testscore_1

#   Pairwise comparisons using t tests with pooled SD 

#data:  df[[x]] and df[[y]] 

#     0    1   
#1 0.79 -   
#2 0.96 0.85

#P value adjustment method: none 

#$testscore_2
#
#   Pairwise comparisons using t tests with pooled SD 

#data:  df[[x]] and df[[y]] 

#     0    1   
#1 0.88 -   
#2 0.53 0.67

#P value adjustment method: none