0

I have several models that I am trying to generate at once and then run separate boxCox() transformations. But every time I try to run boxCox(), I am getting a strange error.

I think it has to do with how the formula changes to .x when using map(). Here is a small reprex to demonstrate my error:

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(purrr)
library(broom)
library(car)
#> Loading required package: carData
#> 
#> Attaching package: 'car'
#> The following object is masked from 'package:purrr':
#> 
#>     some
#> The following object is masked from 'package:dplyr':
#> 
#>     recode

formulas <- c(mpg ~ wt,
              sqrt(mpg) ~ wt, 
              sqrt(mpg) ~ sqrt(wt))

proj_lm <- tibble(formulas) %>% 
  mutate(lm_models = map(formulas, ~lm(formula = .x, data = mtcars)))

boxCox(proj_lm$lm_models[[1]])
#> Error in stats::model.frame(formula = .x, data = mtcars, drop.unused.levels = TRUE): object '.x' not found

Created on 2019-01-11 by the reprex package (v0.2.1)

dylanjm
  • 2,011
  • 9
  • 21
  • Try `mutate(lm_models = map(formulas, ~lm(mtcars, data = .x)))` – Chabo Jan 11 '19 at 18:54
  • @dylanjm It looks to be an issue with the way boxCox uses the formula. It's looking to the global environment for `.x`. If you run the code `.x=formulas[[1]]` prior to the boxCox call, you'll see that the `boxCox` will now work. Unfortunately I'm not skilled enough to understand potential pitfalls of assigning something to `.x` in the global environment or how to overcome this issue. – c.custer Jan 11 '19 at 19:25

0 Answers0