在 R 中使用 lapply 在函数调用中从向量返回实际函数参数

标签 r function lapply

请考虑以下事项。

我想使用 lapply() 随后将存储在字符向量中的几个函数参数应用到其他函数。一个最小的可重现示例可以是将两个或多个“系列”应用到 glm() 函数。请注意,该示例对于应用此类系列可能毫无意义,仅用于说明目的。

以下摘自?glm()中的例子

counts <- c(18,17,15,20,10,20,25,13,12)
outcome <- gl(3,1,9)
treatment <- gl(3,3)
data.frame(treatment, outcome, counts) # showing data

我们现在可以运行具有族“高斯”或“泊松”的 GLM

glm(counts ~ outcome + treatment, family = "gaussian")
glm(counts ~ outcome + treatment, family = "poisson")

这也可以通过创建具有这些姓氏的字符向量来“自动化”:

families <- c("poisson", "gaussian")

并在 lapply() 函数中使用它。

但是一旦运行,返回的函数调用不再返回家族名称,而是返回匿名函数参数 x

lapply(families, function(x) glm(counts ~ outcome + treatment, family = x))
#> [[1]]
#> 
#> Call:  glm(formula = counts ~ outcome + treatment, family = x)
#> 
#> Coefficients:
#> (Intercept)     outcome2     outcome3   treatment2   treatment3  
#>   3.045e+00   -4.543e-01   -2.930e-01   -3.242e-16   -2.148e-16  
#> 
#> Degrees of Freedom: 8 Total (i.e. Null);  4 Residual
#> Null Deviance:       10.58 
#> Residual Deviance: 5.129     AIC: 56.76
#> 
#> [[2]]
#> 
#> Call:  glm(formula = counts ~ outcome + treatment, family = x)
#> 
#> Coefficients:
#> (Intercept)     outcome2     outcome3   treatment2   treatment3  
#>   2.100e+01   -7.667e+00   -5.333e+00    2.221e-15    2.971e-15  
#> 
#> Degrees of Freedom: 8 Total (i.e. Null);  4 Residual
#> Null Deviance:       176 
#> Residual Deviance: 83.33     AIC: 57.57

问题: 如何在 lapply() 之后的函数调用中保留/显示向量 families 中的姓氏?


期望的结果: 结果应如下所示:

#> [[1]]
#> 
#> Call:  glm(formula = counts ~ outcome + treatment, family = "gaussian")
#> 
#> Coefficients:
#> (Intercept)     outcome2     outcome3   treatment2   treatment3  
#>   3.045e+00   -4.543e-01   -2.930e-01   -3.242e-16   -2.148e-16  
#> 
#> Degrees of Freedom: 8 Total (i.e. Null);  4 Residual
#> Null Deviance:       10.58 
#> Residual Deviance: 5.129     AIC: 56.76
#> 
#> [[2]]
#> 
#> Call:  glm(formula = counts ~ outcome + treatment, family = "poisson")
#> 
#> Coefficients:
#> (Intercept)     outcome2     outcome3   treatment2   treatment3  
#>   2.100e+01   -7.667e+00   -5.333e+00    2.221e-15    2.971e-15  
#> 
#> Degrees of Freedom: 8 Total (i.e. Null);  4 Residual
#> Null Deviance:       176 
#> Residual Deviance: 83.33     AIC: 57.57

我按照此处的建议尝试了 eval(bquote(x)):R: Passing named function arguments from vector ,但这没有用。见:

lapply(families, function(x) glm(counts ~ outcome + treatment, family = eval(bquote(x))))
#> [[1]]
#> 
#> Call:  glm(formula = counts ~ outcome + treatment, family = eval(bquote(x)))
#> 
#> Coefficients:
#> (Intercept)     outcome2     outcome3   treatment2   treatment3  
#>   3.045e+00   -4.543e-01   -2.930e-01   -3.242e-16   -2.148e-16  
#> 
#> Degrees of Freedom: 8 Total (i.e. Null);  4 Residual
#> Null Deviance:       10.58 
#> Residual Deviance: 5.129     AIC: 56.76
#> 
#> [[2]]
#> 
#> Call:  glm(formula = counts ~ outcome + treatment, family = eval(bquote(x)))
#> 
#> Coefficients:
#> (Intercept)     outcome2     outcome3   treatment2   treatment3  
#>   2.100e+01   -7.667e+00   -5.333e+00    2.221e-15    2.971e-15  
#> 
#> Degrees of Freedom: 8 Total (i.e. Null);  4 Residual
#> Null Deviance:       176 
#> Residual Deviance: 83.33     AIC: 57.57

reprex package 创建于 2022-07-22 (v2.0.1)

谢谢!

最佳答案

更直接的方法是直接在 lapply 中构建和评估调用

lapply(families, function(x) {
  eval(as.call(list(quote(glm), 
               formula = counts ~ outcome + treatment, 
               data = quote(df), 
               family = x)))
  })
#> [[1]]
#> 
#> Call:  glm(formula = counts ~ outcome + treatment, family = "poisson", 
#>     data = df)
#> 
#> Coefficients:
#> (Intercept)     outcome2     outcome3   treatment2   treatment3  
#>   3.045e+00   -4.543e-01   -2.930e-01    1.338e-15    1.421e-15  
#> 
#> Degrees of Freedom: 8 Total (i.e. Null);  4 Residual
#> Null Deviance:       10.58 
#> Residual Deviance: 5.129     AIC: 56.76
#> 
#> [[2]]
#> 
#> Call:  glm(formula = counts ~ outcome + treatment, family = "gaussian", 
#>     data = df)
#> 
#> Coefficients:
#> (Intercept)     outcome2     outcome3   treatment2   treatment3  
#>   2.100e+01   -7.667e+00   -5.333e+00    2.056e-16    7.252e-16  
#> 
#> Degrees of Freedom: 8 Total (i.e. Null);  4 Residual
#> Null Deviance:       176 
#> Residual Deviance: 83.33     AIC: 57.57

reprex package 创建于 2022-07-22 (v2.0.1)

关于在 R 中使用 lapply 在函数调用中从向量返回实际函数参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73080823/

相关文章:

r - 根据 dplyr 中的范围总结条件

javascript - 当用户在警报框上单击 "ok"时运行另一个 javascript

php - PhP 中是否有更优雅的方法将一个函数传递给另一个函数?

r - 尝试理解这段代码加法器[[1]](5)

r - 使用过滤器在不同变量的数据框中重叠

r - 使用 `lm`参数在 `lapply`中调用 `weights`时出错

Rmarkdown 到 Word 输出 - 在标题上方插入图像

R:使用相同的 ifelse 条件将多个变量转换为有序因子

r - PAM 聚类 - 使用另一个数据集中的结果

php - 在 Python 中,您可以将一个变量分配给一个函数,然后将参数传递给该变量,您如何称呼该功能?