如何写一个for循环,创建一个模型,并有一个函数引用该模型。

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

我正试图使用该软件对一个非平衡的双向anova进行事后分析。anova_test 中的作用 rstatix 包。我需要反复运行这个事后测试,因为我有大约26个响应(y)变量。我的第一步是为我所有的 y 变量与 grouptreatment. 我已经成功地做到了这一点,创建了一个有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 每一次都会发生变化,而当它发生变化时,"模型"(指的是在 error = term)相应地更新。我对这一点很苦恼,因为我做的第一套模型是用来通知第二套模型的。

然而,如果我只运行一个 y 变量一次通过这整段代码,我得到了相应的结果。

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

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

我试过创建一个for循环,也试过使用 map 中的功能 purrr 包,但我一直没有成功。我对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 funciton对非平衡的双向anova进行事后分析。

r for-loop purrr anova
1个回答
3
投票

好吧,你没有给出任何数据,所以我们用牙种。 你似乎喜欢模型格式,所以让我们建立一个模型列表。 你可以用自动化的方式来做,但为了让它更清晰,让我们用手来做。 调用 purrr::map 随着 anova_test 功能。 你会得到一个列表回来。 既然你负责给列表元素命名就去镇上吧。

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

0
投票

感谢来自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.comttrouble-using-group-by-and-map2-together667308?u=mvula。)

创建于2020-05-20 重读包 (v0.3.0)

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