A helper function to construct a design matrix from a formula
Source:R/model-design-tools.R
enw_design.Rd
This function is a wrapper around stats::model.matrix()
that
can optionally return a sparse design matrix defined as the unique
number of rows in the design matrix and an index vector that
allows the full design matrix to be reconstructed. This is useful
for models that have many repeated rows in the design matrix and that
are computationally expensive to fit. This function also allows
for the specification of contrasts for categorical variables.
Arguments
- formula
An R formula.
- data
A
data.frame
containing the variables in the formula.- no_contrasts
A vector of variable names that should not be converted to contrasts. If
no_contrasts = FALSE
then all categorical variables will use contrasts. Ifno_contrasts = TRUE
then no categorical variables will use contrasts.- sparse
Logical, if TRUE return a sparse design matrix. Defaults to TRUE.
- ...
Arguments passed on to
stats::model.matrix
See also
Functions used to formulate models
enw_add_cumulative_membership()
,
enw_add_pooling_effect()
,
enw_effects_metadata()
,
enw_one_hot_encode_feature()
Examples
data <- data.frame(a = 1:3, b = as.character(1:3), c = c(1,1,2))
enw_design(a ~ b + c, data)
#> $formula
#> [1] "a ~ b + c"
#>
#> $design
#> (Intercept) b2 b3 c
#> 1 1 0 0 1
#> 2 1 1 0 1
#> 3 1 0 1 2
#>
#> $index
#> [1] 1 2 3
#>
enw_design(a ~ b + c, data, no_contrasts = TRUE)
#> $formula
#> [1] "a ~ b + c"
#>
#> $design
#> (Intercept) b1 b2 b3 c
#> 1 1 1 0 0 1
#> 2 1 0 1 0 1
#> 3 1 0 0 1 2
#>
#> $index
#> [1] 1 2 3
#>
enw_design(a ~ b + c, data, no_contrasts = c("b"))
#> $formula
#> [1] "a ~ b + c"
#>
#> $design
#> (Intercept) b1 b2 b3 c
#> 1 1 1 0 0 1
#> 2 1 0 1 0 1
#> 3 1 0 0 1 2
#>
#> $index
#> [1] 1 2 3
#>
enw_design(a ~ c, data, sparse = TRUE)
#> $formula
#> [1] "a ~ c"
#>
#> $design
#> (Intercept) c
#> 1 1 1
#> 3 1 2
#>
#> $index
#> [1] 1 1 2
#>
enw_design(a ~ c, data, sparse = FALSE)
#> $formula
#> [1] "a ~ c"
#>
#> $design
#> (Intercept) c
#> 1 1 1
#> 2 1 1
#> 3 1 2
#> attr(,"assign")
#> [1] 0 1
#>
#> $index
#> [1] 1 2 3
#>