I would like to fit an lm
to a couple variables in my data set and then use the lm
to make predictions. I nest the variables, and then using purrr
map each variable to lm
. Now, I would like to create some new data that I can then pass into the predict
method, but I keep running into an error. See my reprex below:
library(tidyverse)
#> ── Attaching packages ───────────────────────────── tidyverse 1.2.1 ──
#> ✔ ggplot2 2.2.1.9000 ✔ purrr 0.2.4
#> ✔ tibble 1.4.2 ✔ dplyr 0.7.4
#> ✔ tidyr 0.8.0 ✔ stringr 1.3.0
#> ✔ readr 1.1.1 ✔ forcats 0.2.0
#> ── Conflicts ──────────────────────────────── tidyverse_conflicts() ──
#> ✖ dplyr::filter() masks stats::filter()
#> ✖ dplyr::lag() masks stats::lag()
library(modelr)
c.data =tibble::tribble(
~Age, ~Weight, ~Concentration, ~Creatinine, ~CockcroftGault,
85, 85, 47.3371693155954, 84, 68.3719433719434,
73, 82, 49.6784937598018, 64, 105.459152334152,
62, 149.2, 45.8995448818044, 137, 104.356247421941,
64, 120, 35.622, 99, 113.171022261931,
58, 89.7, 88.6297240847784, 160, 56.4757371007371,
80, 134.4, 49.308741975603, 108, 91.7280917280917
)
c.data %>%
gather(variable, value, -Concentration) %>%
group_by(variable) %>%
nest() %>%
mutate(
model = map(data, ~lm(log(Concentration) ~ ., data = .x)),
support = map2(data, variable, ~ data_grid(.x, .y = seq_range(.y,20) ))
)
#> Warning in seq.default(rng[1], rng[2], length.out = n): NAs introduced by
#> coercion
#> Error in mutate_impl(.data, dots): Evaluation error: 'from' must be a finite number.
Mapping to lm
seems fine, I just run into a problem with the data_grid
bit. variable
is a character
column, so I think there is a problem in seq_range
but I am not sure.
How can I create a nested column in my dataframe which contains the output of seq_range
for each of my nested variables?