df2matrix: Construct a model matrix from data as if by hand

View source: R/df2matrix.R

df2matrixR Documentation

Construct a model matrix from data as if by hand

Description

The MOSAIC Calculus course includes a block on linear algebra. As part of this block, we want to cover creating model matrices and evaluating the model constructed from them. One way to do this is to introduce lm() and the domain specific language for specifying model terms. However, that introduces oddities. For instance, lm(mpg ~ hp + hp^2, data = mtcars) does NOT add the quadratic term to the model matrix. Also, lm() doesn't produce a residual or the model vector (ahem ... the fitted values) in the form of vectors. That's fine if you're teaching statistical modeling, but in the MOSAIC Calculus linear algebra block we are not teaching statistics, but the mathematical pre-requisites to understanding statistics.

Usage

df2matrix(..., data = NULL)

Arguments

...

Expressions, written in terms of the variable names in the data frame, that are to be collected into the model matrix.

data

The data frame from which to which the variable names will be bound.

Details

Specifically for MOSAIC Calculus, we have added this df2matrix() function. It serves much the same purpose as cbind(), that is, collecting vectors into a matrix. But is has two additional features:

  1. It has a ⁠data=⁠ argument so that it can refer to a data frame.

  2. It names the columns of the matrix with the code that was used to create each column.

The consequence of (2) is that the x vector produced by qr.solve() will have the same names as the matrix. That helps in interpreting x. But those names can also be used by makeFun() to generate a function from x.

1 is a good form in which to write the intercept term.

Value

a matrix

Examples

A <- df2matrix(1, disp, log(hp), sin(cyl)*sqrt(hp), data = mtcars)
b <- df2matrix(mpg, data = mtcars)
x <- qr.solve(A, b)
f <- makeFun(x)
f(hp=3, disp=2, cyl=4)


mosaicCalc documentation built on Sept. 11, 2024, 9:10 p.m.