To visualise a model, it is very useful to be able to generate an
evenly spaced grid of points from the data. data_grid
helps you
do this by wrapping around tidyr::expand()
.
Arguments
- data
A data frame
- ...
Variables passed on to
tidyr::expand()
- .model
A model. If supplied, any predictors needed for the model not present in
...
will be filled in with "typical" values.
See also
seq_range()
for generating ranges from continuous
variables.
Examples
data_grid(mtcars, vs, am)
#> # A tibble: 4 × 2
#> vs am
#> <dbl> <dbl>
#> 1 0 0
#> 2 0 1
#> 3 1 0
#> 4 1 1
# For continuous variables, seq_range is useful
data_grid(mtcars, mpg = mpg)
#> # A tibble: 25 × 1
#> mpg
#> <dbl>
#> 1 10.4
#> 2 13.3
#> 3 14.3
#> 4 14.7
#> 5 15
#> 6 15.2
#> 7 15.5
#> 8 15.8
#> 9 16.4
#> 10 17.3
#> # ℹ 15 more rows
data_grid(mtcars, mpg = seq_range(mpg, 10))
#> # A tibble: 10 × 1
#> mpg
#> <dbl>
#> 1 10.4
#> 2 13.0
#> 3 15.6
#> 4 18.2
#> 5 20.8
#> 6 23.5
#> 7 26.1
#> 8 28.7
#> 9 31.3
#> 10 33.9
# If you supply a model, missing predictors will be filled in with
# typical values
mod <- lm(mpg ~ wt + cyl + vs, data = mtcars)
data_grid(mtcars, .model = mod)
#> # A tibble: 1 × 3
#> wt cyl vs
#> <dbl> <dbl> <dbl>
#> 1 3.32 6 0
data_grid(mtcars, cyl = seq_range(cyl, 9), .model = mod)
#> # A tibble: 9 × 3
#> cyl wt vs
#> <dbl> <dbl> <dbl>
#> 1 4 3.32 0
#> 2 4.5 3.32 0
#> 3 5 3.32 0
#> 4 5.5 3.32 0
#> 5 6 3.32 0
#> 6 6.5 3.32 0
#> 7 7 3.32 0
#> 8 7.5 3.32 0
#> 9 8 3.32 0