Skip to contents

This function takes a formula and a data frame, and returns a design matrix based on the right-hand side of the formula. If the formula is `~.`, it treats all columns in the data as the design matrix. Otherwise, it constructs a design matrix without an intercept based on the specified formula.

Usage

get_data_from_formula(form, data)

Arguments

form

A formula object, e.g., `~ x1 + x2` or `~.`.

data

A data frame or matrix from which to extract the design matrix.

Value

A numeric matrix representing the design matrix.

Examples

data(mtcars)
# Extract a design matrix for 'mpg' and 'cyl'
X1 <- get_data_from_formula(~ mpg + cyl, mtcars)
#> Error in get_data_from_formula(~mpg + cyl, mtcars): could not find function "get_data_from_formula"
# Extract a design matrix for all columns
X2 <- get_data_from_formula(~., mtcars)
#> Error in get_data_from_formula(~., mtcars): could not find function "get_data_from_formula"
# Extract a design matrix for 'hp'
X3 <- get_data_from_formula(~hp, mtcars)
#> Error in get_data_from_formula(~hp, mtcars): could not find function "get_data_from_formula"