简体   繁体   中英

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

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM