data_grid: Generate a data grid.

Description Usage Arguments See Also Examples

View source: R/data-grid.R

Description

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 expand().

Usage

1
data_grid(data, ..., .model = NULL)

Arguments

data

A data frame

...

Variables passed on to 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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
data_grid(mtcars, vs, am)

# For continuous variables, seq_range is useful
data_grid(mtcars, mpg = seq_range(mpg, 10))

# If you optionally 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)
data_grid(mtcars, cyl = seq_range(cyl, 9), .model = mod)

Example output

# A tibble: 4 x 2
     vs    am
  <dbl> <dbl>
1     0     0
2     0     1
3     1     0
4     1     1
# A tibble: 10 x 1
        mpg
      <dbl>
 1 10.40000
 2 13.01111
 3 15.62222
 4 18.23333
 5 20.84444
 6 23.45556
 7 26.06667
 8 28.67778
 9 31.28889
10 33.90000
# A tibble: 1 x 3
     wt   cyl    vs
  <dbl> <dbl> <dbl>
1 3.325     6     0
# A tibble: 9 x 3
    cyl    wt    vs
  <dbl> <dbl> <dbl>
1   4.0 3.325     0
2   4.5 3.325     0
3   5.0 3.325     0
4   5.5 3.325     0
5   6.0 3.325     0
6   6.5 3.325     0
7   7.0 3.325     0
8   7.5 3.325     0
9   8.0 3.325     0

modelr documentation built on July 25, 2017, 1:01 a.m.