Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why using with() as function in a map() call does not work in this example?

Tags:

r

purrr

library(tidyverse)

formulas <- list(
  mpg ~ disp,
  mpg ~ I(1 / disp),
  mpg ~ disp + wt,
  mpg ~ I(1 / disp) + wt
)

# this works
map(formulas, ~ {lm(.x, mtcars)}) 

# this doesn't
map(formulas, ~ {with(mtcars, lm(.x))}) 

 Error in eval(predvars, data, env) : object 'disp' not found 

Working through the exercises in https://adv-r.hadley.nz/functionals.html#exercises-28, I tried to solve exercise number 6, by trying to evaluate lm() inside mtcars environment with with(), but it throws an error.

Why the last call doesn't work?

like image 653
Pablo Rod Avatar asked Apr 25 '26 00:04

Pablo Rod


1 Answers

It is the environment issue. One option would be quote the components so that it would not be executed

formulas <- list(
  quote(mpg ~ disp),
  quote(mpg ~ I(1 / disp)),
   quote(mpg ~ disp + wt),
  quote(mpg ~ I(1 / disp) + wt)
  )

out1 <- map(formulas, ~ with(mtcars, lm(eval(.x))))
out1
#[[1]]

#Call:
#lm(formula = eval(.x))

#Coefficients:
#(Intercept)         disp  
#   29.59985     -0.04122  


#[[2]]

#Call:
#lm(formula = eval(.x))

#Coefficients:
#(Intercept)    I(1/disp)  
#      10.75      1557.67  


#[[3]]

#Call:
#lm(formula = eval(.x))

#Coefficients:
#(Intercept)         disp           wt  
#   34.96055     -0.01772     -3.35083  


#[[4]]

#Call:
#lm(formula = eval(.x))

#Coefficients:
#(Intercept)    I(1/disp)           wt  
#     19.024     1142.560       -1.798  

It should also work with the first method

out2 <- map(formulas, ~ lm(.x, mtcars))

There would be slight changes in the attributes and in the call, but if that is ignored,

out1[[1]]$call <- out2[[1]]$call
all.equal(out1[[1]], out2[[1]], check.attributes = FALSE)
#[1] TRUE
like image 71
akrun Avatar answered Apr 27 '26 13:04

akrun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!