我正试着用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)但是得到:
错误in !enquo_outcome :无效参数类型
(注:实际场景涉及更复杂的函数)。
这个是可能的吗?
发布于 2017-09-27 04:29:43
我们需要为glm创建一个公式来获取它。一种选择是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函数
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发布于 2017-11-02 19:46:54
基本NSE似乎也有效:
library(broom)
myfunc <- function(data, outcome){
outcome_subst <- substitute(outcome)
fit <- tidy(glm(paste(as.name(outcome_subst), "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.8238636 0.1121528 -1.727556 0.08406792 0.6606245 1.025838
2 groupGroup 2 1.2587484 0.1571734 1.464102 0.14316606 0.9253116 1.713937
3 groupGroup 3 1.2490778 0.1550546 1.434369 0.15146698 0.9220209 1.693699https://stackoverflow.com/questions/46439477
复制相似问题