如何编写一个用于创建模型并具有引用同一模型的函数的 for 循环 [英] How to write a for loop which creates a model and has a function which references that same model

查看:25
本文介绍了如何编写一个用于创建模型并具有引用同一模型的函数的 for 循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to run a post hoc analysis on an unbalanced two way anova using the anova_test funciton in the rstatix package. I need to run this post hoc test iteratively, as I have ~26 response (y) variables. My first step is to create models of all my y variables with relation to group and treatment. I have successfully managed to do this, creating a single list with 26 models:

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

现在是我坚持的部分.反复引用这些模型.我想为我拥有的 每个 y 变量运行以下代码:

Now comes the part I'm stuck on. Referring to these models iteratively. I would like to run the following code for every y variable I have:

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

在我的 y 每次都发生变化的地方,模型"(在 error = 术语中引用)相应地更新.我正在努力解决这一问题,因为我制作的第一组模型用于通知第二组模型.

where my y changes every time and as it does, the "model" (referred to in the error = term) is updated accordingly. I'm struggling with this bit since first set of models I make is used to inform the second set of models.

然而,如果我一次只在整个代码中运行一个 y 变量,我会得到适当的结果.

However, if I run just one y variable through this whole bit of code at one time, I get the appropriate results.

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

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

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

I have tried creating a for loop as well as using the map function in the purrr package but I have been unsuccessful. I am new to for loops and purrr so I am sure it's a simple fix I just can't see it.

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

Basically I want a way to run

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

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

iteratively for different y variables (y1, y2, ..., y26) while also referring to the approprite model (model$y1, model$y2, ..., model$26).

感谢您的帮助!

推荐答案

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

Thanks to Nirgrahamuk from the rstudio community forum for this answer:

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)

(see their full answer at: https://community.rstudio.com/t/trouble-using-group-by-and-map2-together/66730/8?u=mvula)

reprex 包 (v0.3.0) 于 2020 年 5 月 20 日创建

Created on 2020-05-20 by the reprex package (v0.3.0)

这篇关于如何编写一个用于创建模型并具有引用同一模型的函数的 for 循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆