boxCox
expects as its argument either a formula or a fitted model. If you look at what boxCox
gets
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) %>%
> str(proj_lm)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 1 obs. of 2 variables:
$ formulas :List of 1
..$ :Class 'formula' language mpg ~ wt
.. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
$ lm_models:List of 1
..$ :List of 12
.. ..$ coefficients : Named num 37.29 -5.34
.. .. ..- attr(*, "names")= chr "(Intercept)" "wt"
.. ..$ residuals : Named num -2.28 -0.92 -2.09 1.3 -0.2 ...
.. .. ..- attr(*, "names")= chr "Mazda RX4" "Mazda RX4 Wag" "Datsun 710" "Hornet 4 Drive" ...
.. ..$ effects : Named num -113.65 -29.116 -1.661 1.631 0.111 ...
.. .. ..- attr(*, "names")= chr "(Intercept)" "wt" "" "" ...
.. ..$ rank : int 2
.. ..$ fitted.values: Named num 23.3 21.9 24.9 20.1 18.9 ...
.. .. ..- attr(*, "names")= chr "Mazda RX4" "Mazda RX4 Wag" "Datsun 710" "Hornet 4 Drive" ...
.. ..$ assign : int 0 1
.. ..$ qr :List of 5
.. .. ..$ qr : num [1:32, 1:2] -5.657 0.177 0.177 0.177 0.177 ...
.. .. .. ..- attr(*, "dimnames")=List of 2
.. .. .. .. ..$ : chr "Mazda RX4" "Mazda RX4 Wag" "Datsun 710" "Hornet 4 Drive" ...
.. .. .. .. ..$ : chr "(Intercept)" "wt"
.. .. .. ..- attr(*, "assign")= int 0 1
.. .. ..$ qraux: num 1.18 1.05
.. .. ..$ pivot: int 1 2
.. .. ..$ tol : num 1e-07
.. .. ..$ rank : int 2
.. .. ..- attr(*, "class")= chr "qr"
.. ..$ df.residual : int 30
.. ..$ xlevels : Named list()
.. ..$ call : language lm(formula = .x, data = mtcars)
.. ..$ terms :Classes 'terms', 'formula' language mpg ~ wt
.. .. .. ..- attr(*, "variables")= language list(mpg, wt)
.. .. .. ..- attr(*, "factors")= int [1:2, 1] 0 1
.. .. .. .. ..- attr(*, "dimnames")=List of 2
.. .. .. .. .. ..$ : chr [1:2] "mpg" "wt"
.. .. .. .. .. ..$ : chr "wt"
.. .. .. ..- attr(*, "term.labels")= chr "wt"
.. .. .. ..- attr(*, "order")= int 1
.. .. .. ..- attr(*, "intercept")= int 1
.. .. .. ..- attr(*, "response")= int 1
.. .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
.. .. .. ..- attr(*, "predvars")= language list(mpg, wt)
.. .. .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
.. .. .. .. ..- attr(*, "names")= chr [1:2] "mpg" "wt"
.. ..$ model :'data.frame': 32 obs. of 2 variables:
.. .. ..$ mpg: num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
.. .. ..$ wt : num 2.62 2.88 2.32 3.21 3.44 ...
.. .. ..- attr(*, "terms")=Classes 'terms', 'formula' language mpg ~ wt
.. .. .. .. ..- attr(*, "variables")= language list(mpg, wt)
.. .. .. .. ..- attr(*, "factors")= int [1:2, 1] 0 1
.. .. .. .. .. ..- attr(*, "dimnames")=List of 2
.. .. .. .. .. .. ..$ : chr [1:2] "mpg" "wt"
.. .. .. .. .. .. ..$ : chr "wt"
.. .. .. .. ..- attr(*, "term.labels")= chr "wt"
.. .. .. .. ..- attr(*, "order")= int 1
.. .. .. .. ..- attr(*, "intercept")= int 1
.. .. .. .. ..- attr(*, "response")= int 1
.. .. .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
.. .. .. .. ..- attr(*, "predvars")= language list(mpg, wt)
.. .. .. .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
.. .. .. .. .. ..- attr(*, "names")= chr [1:2] "mpg" "wt"
.. ..- attr(*, "class")= chr "lm"
>
and
str(proj_lm$lm_models[[1]])
is just the first of the three. proj_lm$lm_models[[1]] is a fitted lm
model, it has silently substituted .x
for mpg ~ wt
in the first formula, and there's no way to retrieve that without digging in parallel with proj_lm$formulas[[1]], where mpg ~ wt
is stashed. This suggests a tedious process of extracting proj_lm$lm_models[[1]]$call
where we find lm(formula = .x, data = mtcars)
and applying stringr
to swap out .x
for proj_lm$formulas[[1]]
to insert mpg ~ wt
in its place.
tibbles
may contain vectors but they are not themselves vectors, which makes isolating the vectors you need to pass to another function seriatim a huge pain.
I'm too tired to attempt it, but I'd go for nested maps, one over fomulas to produce a vector of models and the inner one to apply boxCox.