gpt4 book ai didi

r - 使用 tidyeval 进行编程回归建模

转载 作者:行者123 更新时间:2023-12-02 13:16:56 27 4
gpt4 key购买 nike

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

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

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)

但是得到:

Error in !enquo_outcome : invalid argument type

(注意实际场景涉及更复杂的功能)。

这可能吗?

最佳答案

我们需要为 glm 创建一个公式来选取它。一种选择是粘贴

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函数

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
<小时/>

或者正如@lionel在评论中提到的get_expr可以使用

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

关于r - 使用 tidyeval 进行编程回归建模,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46439477/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com