0

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.

1 Answer 1

2

You need to pass a function to map(). Right now, you are calling a function in the second parameter, not passing a function. The quickest way to fix this is to use the formula syntax to create a function. Try

models <- df_by_group%>%
  mutate(mod=purrr::map(data, ~input_fun(data=.,outcome="out",predictor="pre",covariate=c("var1","var2"))))

This delays the evaluation of input_fun till the map actually happens and properly fills in the . value.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.