如何编写创建模型并具有引用同一模型的函数的 for 循环

How to write a for loop which creates a model and has a function which references that same model

我正在尝试 运行 使用 rstatix 包中的 anova_test 函数对不平衡双向方差分析进行 post 临时分析。我需要迭代地 运行 这个 post 临时测试,因为我有 ~26 个响应 (y) 变量。我的第一步是创建与 grouptreatment 相关的所有 y 变量的模型。我已经成功地做到了这一点,创建了一个包含 26 个模型的列表:

models <- map(data[,y1:y26], ~(lm(.x ~data$group*data$treatment)))

现在是我坚持的部分。迭代地参考这些模型。我想 运行 以下代码用于 每个 y 我有的变量:

group_by(group) %>%
anova_test(y ~ treatment, error = models(y), type = 3)

我的 y 每次都会更改,并且 "model"(在 error = 术语中引用)也会相应更新。我正在努力解决这个问题,因为我制作的第一组模型用于通知第二组模型。

但是,如果我 运行 一次只通过这一整段代码的一个 y 变量,我会得到适当的结果。

model <- lm(y ~ group*treatment, data = data)

data %>%
group_by(group) %>%
anova_test(y ~ treatment, error = model, type = 3)

我已经尝试创建一个 for 循环以及使用 purrr 包中的 map 函数,但我没有成功。我是 for 循环和 purrr 的新手,所以我确信这是一个简单的修复,我只是看不到它。

基本上我想要一种方法 运行

data %>%
group_by(group) %>%
anova_test(y ~ treatment, error = model, type = 3)

迭代不同的 y 变量 (y1, y2, ..., y26),同时也引用适当的 model (model$y1, model$y2, ..., model$26).

感谢您的帮助!

好吧,你没有提供任何数据,所以让我们使用牙齿生长。您似乎喜欢模型格式,所以让我们构建一个模型列表。您可以以自动方式执行此操作,但为了清楚起见,让我们手动执行此操作。使用 anova_test 函数调用 purrr::map。你会得到一个列表。由于您负责命名列表元素,因此请前往城镇。

5 月 18 日更新了答案。现在使用 map2,因为您希望传递两个不同的模型,为每个构建一个列表...

library(rstatix)
library(purrr)

ToothGrowth$len2 <- ToothGrowth$len^2 # for variety

models <- list(model1 = lm(len ~ supp*dose, ToothGrowth), 
               model2 = lm(len ~ dose*supp, ToothGrowth),
               model3 = lm(len2 ~ dose*supp, ToothGrowth),
               model4 = lm(len2 ~ supp*dose, ToothGrowth))

models2 <- list(model1 = lm(len ~ supp, ToothGrowth), 
               model2 = lm(len ~ dose, ToothGrowth),
               model3 = lm(len2 ~ dose, ToothGrowth),
               model4 = lm(len2 ~ supp, ToothGrowth))


# one model
purrr::map(models, ~ anova_test(.x, type = 3))


# now with model for error term
purrr::map2(models, models2, ~ anova_test(.x, error = .y, type = 3))
#> Coefficient covariances computed by hccm()
#> Coefficient covariances computed by hccm()
#> Coefficient covariances computed by hccm()
#> Coefficient covariances computed by hccm()
#> $model1
#> ANOVA Table (type III tests)
#> 
#>      Effect DFn DFd      F        p p<.05   ges
#> 1      supp   1  58  4.058 0.049000     * 0.065
#> 2      dose   1  58 12.717 0.000734     * 0.180
#> 3 supp:dose   1  58  1.588 0.213000       0.027
#> 
#> $model2
#> ANOVA Table (type III tests)
#> 
#>      Effect DFn DFd      F        p p<.05   ges
#> 1      dose   1  58 33.626 2.92e-07     * 0.367
#> 2      supp   1  58 10.729 2.00e-03     * 0.156
#> 3 dose:supp   1  58  4.200 4.50e-02     * 0.068
#> 
#> $model3
#> ANOVA Table (type III tests)
#> 
#>      Effect DFn DFd      F        p p<.05   ges
#> 1      dose   1  58 36.028 1.35e-07     * 0.383
#> 2      supp   1  58  7.128 1.00e-02     * 0.109
#> 3 dose:supp   1  58  2.709 1.05e-01       0.045
#> 
#> $model4
#> ANOVA Table (type III tests)
#> 
#>      Effect DFn DFd      F        p p<.05   ges
#> 1      supp   1  58  2.684 0.107000       0.044
#> 2      dose   1  58 13.566 0.000508     * 0.190
#> 3 supp:dose   1  58  1.020 0.317000       0.017

感谢来自 rstudio 社区论坛的 Nirgrahamuk 的回答:

map(names(models_1) ,
    ~ anova_test(data=group_by(df,edge),
                 formula = as.formula(paste0(.x,"~ trt")),
                 error = models_1[[.x]],
                 type = 3))

(查看他们的完整答案:https://community.rstudio.com/t/trouble-using-group-by-and-map2-together/66730/8?u=mvula

reprex package (v0.3.0)

于 2020-05-20 创建