Hi All,
I am trying to combine flexible modelling functions (using tidyeval) and then mapping over data in a nested dataframe (and attempting to learn tidy evaluation along the way). I am running into the problems of inlining expressions with the captured call (I think).
Any suggestion, exmaples, tips, or best practices for writing wrappers to simplify repetitive modelling tasks and then using them with purrr::map etc?
The example below is based on the section wrapping modelling functions from 20 Evaluation | Advanced R using the mtcars data.
library(rlang)
library(tidyverse)
lm_wrap <- function(data, traits, resp, env = caller_env(), ...) {
traits <- enexpr(traits)
resp <- enexpr(resp)
data <- enexpr(data)
dots <- enexprs(...)
lm_call <- inject(lm(!!resp ~ !!traits, data = !!data, !!!dots), env)
return(lm_call)
}
The wrapper function works for single cases
lm_wrap(traits = hp, data = mtcars, resp = mpg)
#Call:
#lm(formula = mpg ~ hp, data = mtcars)
#Coefficients:
#(Intercept) hp
# 30.09886 -0.06823
But looks like it runs into the problems of inlining expressions, at least as per this somewhat related example 20 Evaluation | Advanced R
mt_nested <- mtcars %>% group_by(cyl) %>% nest() %>%
mutate(model = map(data, lm_wrap, resp = mpg, traits = hp))
mt_nested$model[[1]]$call
#lm(formula = mpg ~ hp, data = list(mpg = c(21, 21, 21.4, 18.1,
#19.2, 17.8, 19.7), disp = c(160, 160, 258, 225, 167.6, 167.6,
#145), hp = c(110, 110, 110, 105, 123, 123, 175), drat = c(3.9,
#3.9, 3.08, 2.76, 3.92, 3.92, 3.62), wt = c(2.62, 2.875, 3.215,
#3.46, 3.44, 3.44, 2.77), qsec = c(16.46, 17.02, 19.44, 20.22,
#18.3, 18.9, 15.5), vs = c(0, 0, 1, 1, 1, 1, 0), am = c(1, 1,
#0, 0, 0, 0, 1), gear = c(4, 4, 3, 3, 4, 4, 5), carb = c(4, 4,
#1, 1, 4, 4, 6)))
Thanks in advance,
M.