使用Tidyeval进行程序回归建模 [英] Programmatic regression modelling with tidyeval

查看:40
本文介绍了使用Tidyeval进行程序回归建模的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用tidyeval进行编程.

I am trying to get my head around programming using tidyeval.

我想编写一个函数来为选定的结果变量运行逻辑回归模型:

I want to write a function to run logistic regression models for selected outcome variables:

library(tidyverse)
set.seed(1234)

df <- tibble(id = 1:1000,
             group = sample(c("Group 1", "Group 2", "Group 3"), 1000, replace = TRUE),
             died = sample(c(0,1), 1000, replace = TRUE))

myfunc <- function(data, outcome){

enquo_var <- enquo(outcome)

fit <- tidy(glm(!!enquo_var ~ group, data=data, 
                family = binomial(link = "logit")), 
                exponentiate = TRUE, conf.int=TRUE)

fit
}


myfunc(df, died)

但是得到:

!enquo_outcome错误:参数类型无效

Error in !enquo_outcome : invalid argument type

(请注意实际情况涉及更复杂的功能).

(note real scenario involves more complex function).

这可能吗?

推荐答案

我们需要为 glm 创建一个公式以将其提取.一种选择是 paste

We need to create a formula for glm to pick it up. One option is paste

myfunc <- function(data, outcome){
  enquo_var <- enquo(outcome)
   fit <- tidy(glm(paste(quo_name(enquo_var), "group", sep="~"), data=data, 
                family = binomial(link = "logit")), 
                exponentiate = TRUE, conf.int=TRUE)

fit
}

myfunc(df, died)
#         term  estimate std.error  statistic    p.value  conf.low conf.high
#1  (Intercept) 0.8715084 0.1095300 -1.2556359 0.20924801 0.7026185  1.079852
#2 groupGroup 2 0.9253515 0.1550473 -0.5003736 0.61681204 0.6826512  1.253959
#3 groupGroup 3 1.3692735 0.1557241  2.0181864 0.04357185 1.0095739  1.859403


如果我们还需要使用tidyverse函数


If we also need to use the tidyverse functions

myfunc <- function(data, outcome){

  quo_var <- quo_name(enquo(outcome))

   fit <- tidy(glm(rlang::expr(!! rlang::sym(quo_var) ~ group), data=data, 
            family = binomial(link = "logit")), 
            exponentiate = TRUE, conf.int=TRUE)

 fit
}

myfunc(df, died)
#           term  estimate std.error  statistic    p.value  conf.low conf.high
#1  (Intercept) 0.8715084 0.1095300 -1.2556359 0.20924801 0.7026185  1.079852
#2 groupGroup 2 0.9253515 0.1550473 -0.5003736 0.61681204 0.6826512  1.253959
#3 groupGroup 3 1.3692735 0.1557241  2.0181864 0.04357185 1.0095739  1.859403


或者可以使用注释 get_expr 中的@lionel

myfunc <- function(data, outcome){

  quo_var <- enquo(outcome)

   fit <- tidy(glm(rlang::expr(!! rlang::get_expr(quo_var) ~ group), data=data, 
            family = binomial(link = "logit")), 
            exponentiate = TRUE, conf.int=TRUE)

 fit
}

myfunc(df, died)
#         term  estimate std.error  statistic    p.value  conf.low conf.high
#1  (Intercept) 0.8715084 0.1095300 -1.2556359 0.20924801 0.7026185  1.079852
#2 groupGroup 2 0.9253515 0.1550473 -0.5003736 0.61681204 0.6826512  1.253959
#3 groupGroup 3 1.3692735 0.1557241  2.0181864 0.04357185 1.0095739  1.859403


或者@lionel建议的一种更紧凑的方法,它避免了 enquo/quo_name/sym 转换,而是直接在 enexpr

 myfunc <- function(data, outcome){



   fit <- tidy(glm(rlang::expr(!! rlang::enexpr(outcome) ~ group), data=data, 
            family = binomial(link = "logit")), 
            exponentiate = TRUE, conf.int=TRUE)

 fit
}

myfunc(df, died)
#         term  estimate std.error  statistic    p.value  conf.low conf.high
#1  (Intercept) 0.8715084 0.1095300 -1.2556359 0.20924801 0.7026185  1.079852
#2 groupGroup 2 0.9253515 0.1550473 -0.5003736 0.61681204 0.6826512  1.253959
#3 groupGroup 3 1.3692735 0.1557241  2.0181864 0.04357185 1.0095739  1.859403

这篇关于使用Tidyeval进行程序回归建模的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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