Skip to content

This is a thin wrapper around stats::model.matrix() which returns a tibble. Use it to determine how your modelling formula is translated into a matrix, an thence into an equation.

Usage

model_matrix(data, formula, ...)

Arguments

data

A data frame

formula

A modelling formula

...

Other arguments passed onto stats::model.matrix()

Value

A tibble.

Examples

model_matrix(mtcars, mpg ~ cyl)
#> # A tibble: 32 × 2
#>    `(Intercept)`   cyl
#>            <dbl> <dbl>
#>  1             1     6
#>  2             1     6
#>  3             1     4
#>  4             1     6
#>  5             1     8
#>  6             1     6
#>  7             1     8
#>  8             1     4
#>  9             1     4
#> 10             1     6
#> # ℹ 22 more rows
model_matrix(iris, Sepal.Length ~ Species)
#> # A tibble: 150 × 3
#>    `(Intercept)` Speciesversicolor Speciesvirginica
#>            <dbl>             <dbl>            <dbl>
#>  1             1                 0                0
#>  2             1                 0                0
#>  3             1                 0                0
#>  4             1                 0                0
#>  5             1                 0                0
#>  6             1                 0                0
#>  7             1                 0                0
#>  8             1                 0                0
#>  9             1                 0                0
#> 10             1                 0                0
#> # ℹ 140 more rows
model_matrix(iris, Sepal.Length ~ Species - 1)
#> # A tibble: 150 × 3
#>    Speciessetosa Speciesversicolor Speciesvirginica
#>            <dbl>             <dbl>            <dbl>
#>  1             1                 0                0
#>  2             1                 0                0
#>  3             1                 0                0
#>  4             1                 0                0
#>  5             1                 0                0
#>  6             1                 0                0
#>  7             1                 0                0
#>  8             1                 0                0
#>  9             1                 0                0
#> 10             1                 0                0
#> # ℹ 140 more rows