I want to loop glm/lm over multiple outcomes and predictors while stratified by groups. nest() and map() functions from purrr package seems to provide an elegant solution to stratification analysis. However, when I use a customized function which takes multiple input, map() doesn't seem to work.
In almost all the tutorials on map() from purrr I have seen,regression model examples are static -- the dependent and independent variables are explicitly defined in the function. Because I want to loop over dozens of outcomes and predictors, I am trying to write a lm() function that can iterate over different combinations.
library(dplyr)
library(broom)
library(tidyr)
library(purrr)
# example data set
set.seed(20)
df <- data.frame(
out = rep(c(0,1),5,replace=TRUE),
pre = sample(c(1:4),10,replace = TRUE),
var1 = sample(c(1:2),10,replace = TRUE),
var2 = sample(c(1:50),10,replace = TRUE),
group = sample(c(1:2),10,replace = TRUE)
)
explicit_fun<-function(data){
glm(out ~ pre + var1 + var2, data=data, family = binomial())
}
input_fun<-function(data, outcome, predictor, covariate){
glm(as.formula(paste(outcome,"~",predictor,"+",paste(covariate,collapse = "+"))),data=data,family = binomial())
}
# nesting the data set
df_by_group<-df%>%
group_by(group)%>%
nest()
it works fine with the explicit function
models <- df_by_group%>%
mutate(mod=purrr::map(data,explicit_fun))
models <- models%>%
mutate(
glance_glm=purrr::map(mod,broom::glance),
tidy_glm=purrr::map(mod,broom::tidy),
augment_glm=purrr::map(mod,broom::augment)
)
unnest(models,data)
unnest(models,glance_glm,.drop = TRUE)%>% View()
unnest(models,tidy_glm) %>% View()
it stops working when using the function has multiple inputs
models<-df_by_group%>%
mutate(mod=purrr::map(data,input_fun(data=.,outcome="out",predictor="pre",covariate=c("var1","var2"))))
I expect the input_fun would work the same as the explicit_fun, but I received the following error message:
Error in mutate_impl(.data, dots) :
Evaluation error: Can't convert a `glm/lm` object to function
Call `rlang::last_error()` to see a backtrace.