如何在函数内使用 purrr::map 和 aov 函数并将函数参数传递到 aov 中?

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

我正在尝试创建一个函数来概括此working行:

aov_res <- map(my_data[metals_list], ~tryCatch(aov(. ~ my_x * my_z, data = my_data), error = function(e) NULL))
,其中
df
是一个数据框,
metals_list
是用于方差分析测试的Y变量列表。

在更通用的函数内部。像这样的东西:

anova_fun <- function(df,x.var,z.var){
  aov_res <- map(df[metals_list], ~tryCatch(aov(. ~ x.var * z.var, data = df), error = function(e) NULL))
}
anova_fun(my_data,my_x,my_z)

由于某种原因,函数版本创建了一个空列表列表,有 11 条警告,针对

metals_list
中的每种金属:

Warning: restarting interrupted promise evaluationWarning: restarting interrupted promise evaluationWarning: restarting interrupted promise evaluationWarning: restarting interrupted promise evaluationWarning: restarting interrupted promise evaluationWarning: restarting interrupted promise evaluationWarning: restarting interrupted promise evaluationWarning: restarting interrupted promise evaluationWarning: restarting interrupted promise evaluationWarning: restarting interrupted promise evaluationWarning: restarting interrupted promise evaluation
r parameter-passing purrr anova
1个回答
0
投票

您误用了

map
。在您的情况下,
map
的第一个参数不应该是带有 y 变量的数据框,而应该是 y 变量名称。

解决方案

这是您问题的解决方案,用

mtcars
演示,其中 y 变量是
mpg
drat
wt
(三个方差分析模型),x 和 z 变量是
disp
和分别是
vs

# Reading required libraries
library(tidyverse)
library(glue)

# prep work
df <- mtcars
y_vars <- c("mpg", "drat", "wt") # this is what you call `metals_list`
x_var <- "disp" # your x-variable
z_var <- "vs" # your z-variable

# a basic function that has variable names, as strings, for input
create_aov <- function(y_var, x_var, z_var) {
  aov(formula = as.formula(glue("{y_var} ~ {x_var}*{z_var}")),
      data = df)
}

# A safe version of the function (alternative to `tryCatch`)
safe_aov <- possibly(create_aov)

# A complete run of the function
final_res <- map(y_vars, ~safe_aov(., x_var, z_var))

# extract first result using
# final_res[[1]]$result

输出

打印时输出

final_res
:

[[1]]
Call:
   aov(formula = as.formula(glue("{y_var} ~ {x_var}*{z_var}")),
    data = df)

Terms:
                    disp       vs  disp:vs Residuals
Sum of Squares  808.8885   8.7179  61.5273  246.9135
Deg. of Freedom        1        1        1        28

Residual standard error: 2.969569
Estimated effects may be unbalanced

[[2]]
Call:
   aov(formula = as.formula(glue("{y_var} ~ {x_var}*{z_var}")),
    data = df)

Terms:
                    disp       vs  disp:vs Residuals
Sum of Squares  4.470189 0.073905 0.698982  3.619246
Deg. of Freedom        1        1        1        28

Residual standard error: 0.3595258
Estimated effects may be unbalanced

[[3]]
Call:
   aov(formula = as.formula(glue("{y_var} ~ {x_var}*{z_var}")),
    data = df)

Terms:
                     disp        vs   disp:vs Residuals
Sum of Squares  23.401940  0.345362  0.316624  5.614822
Deg. of Freedom         1         1         1        28

Residual standard error: 0.4478051
Estimated effects may be unbalanced

评论

改进#1

在函数中,如果你像这样使用

broom::tidy
,你可以提取一个整洁的结果:

create_aov <- function(y_var, x_var, z_var) {
  aov(formula = as.formula(glue("{y_var} ~ {x_var}*{z_var}")),
      data = df) |>
    broom::tidy()
}

改进#2

如果您想要错误消息的不同排列(如果出现),请将

possibly
替换为
safely

重要!关于在 StackOverflow 中提问

如果您提供了一个最小的示例(例如,像我一样使用 mtcars)以及所需输出的示例,那么您的问题可能会得到改善(

并且可能会更快地回答
)。

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