enw_design: A helper function to construct a design matrix from a formula

View source: R/model-design-tools.R

enw_designR Documentation

A helper function to construct a design matrix from a formula

Description

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.

Usage

enw_design(formula, data, no_contrasts = FALSE, sparse = TRUE, ...)

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. If no_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

object

an object of an appropriate class. For the default method, a model formula or a terms object.

Value

A list containing the formula, the design matrix, and the index.

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)
enw_design(a ~ b + c, data, no_contrasts = TRUE)
enw_design(a ~ b + c, data, no_contrasts = c("b"))
enw_design(a ~ c, data, sparse = TRUE)
enw_design(a ~ c, data, sparse = FALSE)

seabbs/epinowcast documentation built on Oct. 8, 2024, 8:35 p.m.