Closed Tutuchan closed 6 years ago
You need to use !!! here:
library(modelr)
mtcars_formulas <- formulas(
~disp,
additive = ~drat + cyl,
interaction = ~drat * cyl,
full = add_predictors(interaction, ~am, ~vs)
)
models <- mtcars %>%
fit_with(lm, mtcars_formulas)
mtcars %>%
gather_predictions(!!!models) %>%
tibble::as_tibble()
#> # A tibble: 96 x 13
#> model mpg cyl disp hp drat wt qsec vs am gear carb
#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 addi… 21 6 160 110 3.9 2.62 16.5 0 1 4 4
#> 2 addi… 21 6 160 110 3.9 2.88 17.0 0 1 4 4
#> 3 addi… 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1
#> 4 addi… 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1
#> 5 addi… 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2
#> 6 addi… 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1
#> 7 addi… 14.3 8 360 245 3.21 3.57 15.8 0 0 3 4
#> 8 addi… 24.4 4 147. 62 3.69 3.19 20 1 0 4 2
#> 9 addi… 22.8 4 141. 95 3.92 3.15 22.9 1 0 4 2
#> 10 addi… 19.2 6 168. 123 3.92 3.44 18.3 1 0 4 4
#> # ... with 86 more rows, and 1 more variable: pred <dbl>
Created on 2018-05-10 by the reprex package (v0.2.0).
This is a general pattern that we haven't yet figured out how to document, but basically anywhere in the tidyverse that normally takes individual arguments, you can always supply !!!list(x, y, z)
Unless I'm mistaken,
gather_predictions
does not interact well with the output fromfit_with
:I believe this is due to the
models <- tibble::lst(...)
ingather_predictions
. I tried usingdo.call
but I don't think there is an easy way to make this work with the current code.One possible solution would be to add a class to the list output from
fit_with
and check ingather_predictions
if the input inherits from this class and proceed accordingly.Here is a tentative code but it's not very elegant and I'm pretty sure there is a better way:
Once again, there may be something very obvious that I didn't see. Any thoughts ?