rstatix::tukey_hsd 循环错误:没有适用于“tbl_vars”的方法应用于类“NULL”的对象

问题描述 投票:0回答:1

给定数据集

 df

df<-structure(list(stream = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L), levels = c("blendziava", "smeltaite", "sventoji"), class = "factor"), 
    season = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 4L, 4L, 4L, 4L), levels = c("fall", "winter", "spring", 
    "summer"), class = c("ordered", "factor")), event = structure(c(1L, 
    1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), levels = c("drought", 
    "flood"), class = "factor"), c = c(533, 596, 622, 590, 282, 
    313, 310, 293, 285, 292, 280, 262, 357, 360, 348, 337, 424, 
    464, 441, 427, 506, 703, 606, 579, 506, 605, 587, 526, 307, 
    366, 301, 330, 385, 346, 396, 210, 225, 218, 217, 228, 226, 
    270, 130, 136, 135, 143, 150, 153, 151, 222, 232, 234, 235, 
    238, 238, 240, 299, 296, 296, 297, 308, 305, 293, 360, 384, 
    382, 368, 367, 360, 362, 387, 417, 404, 389, 386, 382, 378, 
    386, 390, 397, 360, 190, 195, 191, 203, 175, 176, 173, 177, 
    235, 226, 220, 217, 316, 303, 291, 292, 366, 355, 343, 350, 
    378, 368, 341, 384), ph = c(8.31, 8.04, 8.28, 8.2, 8.35, 
    8.06, 8.02, 8.19, 8.14, 7.98, 7.91, 8.04, 7.91, 7.71, 7.82, 
    7.98, 8.3, 7.97, 8.17, 8.21, 8.05, 7.5, 7.86, 8.06, 7.86, 
    7.68, 7.88, 7.92, 9.34, 9.18, 9.34, 9.27, 9.16, 9.18, 8.97, 
    8.08, 8.05, 8, 8.03, 7, 8.07, 8.38, 7.97, 7.95, 7.97, 8.03, 
    8.05, 8.23, 8.65, 8.07, 8.17, 8.22, 8.21, 8.23, 8.27, 8.34, 
    8.3, 8.54, 8.53, 8.46, 8.49, 8.51, 8.59, 8.2, 8.18, 8.27, 
    8.3, 8.44, 8.46, 8.47, 8.04, 7.94, 8.15, 8.17, 8.29, 8.38, 
    8.39, 8.34, 8.33, 8.38, 8.26, 8.12, 8.09, 8.07, 8.05, 8.1, 
    8.1, 6.03, 8.01, 7.94, 7.94, 8, 8.16, 8.23, 8.08, 8.17, 8.16, 
    7.93, 7.87, 7.87, 7.7, 7.65, 7.71, 7.72, 7.81), t = c(12.9, 
    12.2, 12.1, 12.1, 8.2, 8.3, 8.2, 7.9, 2.5, 2.3, 2.3, 1.9, 
    4.7, 3.6, 3.3, 4, 11.5, 11.9, 10.5, 10.5, 18, 19, 17.6, 16, 
    17.3, 19.1, 17.9, 17, 10.7, 11.3, 11.3, 10.9, 11.3, 11.3, 
    11.2, 4.3, 5, 5.1, 5.2, 5.4, 5.4, 5.4, 1.6, 1.8, 1.9, 2.1, 
    2.1, 2.2, 2.4, 4.9, 4.7, 4.9, 4.2, 4.2, 4.3, 4.6, 10.5, 11.8, 
    11.5, 10.7, 12, 12.2, 11.3, 15.2, 17.5, 17.3, 16.1, 17.2, 
    16.8, 17.6, 17, 19.5, 19, 17.9, 18.6, 18.4, 19.4, 11.3, 11.1, 
    11.5, 11.5, 5.3, 5.3, 5.4, 5.6, 2.5, 2.3, 2.4, 2.4, 4.2, 
    3.6, 3.3, 3.5, 11, 10.6, 9.7, 9.8, 18, 18, 17, 16.6, 19.9, 
    20, 19, 19.6), do = c(9.2, 5.8, 7.8, 9.2, 7.9, 7.5, 8, 9.1, 
    11.7, 11.3, 11.7, 13.5, 13.3, 11.7, 13.2, 13.7, 14.9, 11.4, 
    13.8, 11.6, 7.9, 5, 5, 7.9, 8.2, 4.3, 7, 8.3, 9.9, 9.8, 10.5, 
    10.1, 10.8, 11, 10.6, 11.2, 11.5, 11.6, 11.4, 11.3, 11.9, 
    12.6, 12.4, 12.9, 12.5, 12.2, 12.3, 12.6, 14.7, 13.7, 14.9, 
    14.3, 14.3, 14.5, 14.5, 14.2, 14.2, 15.7, 15, 13.5, 13.4, 
    13.2, 13.9, 10.5, 8.6, 9.9, 10.6, 11.6, 10.6, 9.7, 10, 7.3, 
    9.7, 9.9, 10.6, 10.9, 10.9, 8.7, 9.8, 10.1, 9.8, 9.4, 9, 
    9.5, 9.8, 13, 12.5, 12.1, 12.7, 13.3, 14.4, 14.8, 15.5, 11.8, 
    12.1, 13, 14.4, 8, 8.8, 8.9, 7.3, 5.4, 8.4, 7.8, 6.1)), row.names = c(NA, 
-105L), class = c("tbl_df", "tbl", "data.frame"))

有 4 个数字环境变量,

c,ph,t,do
和三个因素
stream, season, event
,我使用以下公式运行不同的循环:

env_vars <- c("c","ph","t","do")
proc_model<-function(sub_df) {
   sapply(env_vars, function(env) {
      model <-tukey_hsd(reformulate("stream",env), data = sub_df)%>% 
         add_significance()
      list(
         mytukey_result = model
      )
   }, simplify=FALSE)
}

模型对每个数字列迭代特定测试,由

env_vars
向量表示。 然后,我在我根据其他两个因素
df 
season 
分组的
event 
上运行该函数:

 stat.test<-df%>%
   group_by(season,event) %>% 
    proc_model()
stat.test

将所有内容分组到

stat_test
中。

我对不同的管道友好测试进行了此过程,例如

rstatix::package 
anova_test()
以及它们的事后比较
kruskal_test()
tukey_hsd()
,分别。
当我特别为

dunn_test()

这样做时,问题就出现了,给了我错误:

tukey_hsd 

我怀疑是因为其他测试只返回 
Error in UseMethod("tbl_vars") : no applicable method for 'tbl_vars' applied to an object of class "NULL"

,而

data.frame 
返回
tukey_hsd 
。 有没有一种方便的方法来导出此输出,绕过上述错误? 预先感谢!
    

r pipe tibble sapply rstatix
1个回答
1
投票
tibble data.frame

rstatix::tukey_hsd
的包装函数,根据描述需要“一个拟合的模型对象,通常是aov拟合”。因此,
stats::TukeyHSD
rstatix::tukey_hsd
lm()
一起使用。
如果将 lm() 包含到代码中,错误就会消失,并且您会得到输出:

aov()

但是

,在我看来,这种方法需要更复杂的分析,包括适当的 GLM 或 GLMM 拟合,然后进行有意义的比较。

© www.soinside.com 2019 - 2024. All rights reserved.