Previous: Writing New Models Up: Writing New Models Next: Describe the Statistical Model


Managing Statistical Model Inputs

Most statistical models require a matrix of explanatory variables and a matrix of dependent variables. Rather than have users create matrices themselves, R has a convenient user interface to create matrices of response and explanatory variables on the fly. Users simply specify a formula in the form of dependent ~ explanatory variables, and developers use the following functions to transform the formula into the appropriate matrices. Let mydata be a data frame.

> formula <- y ~ x1 + x2                   # User input

# Given the formula above, programmers can use the following standard commands
> D <- model.frame(formula, data = mydata) # Subset & listwise deletion
> X <- model.matrix(formula, data = D)     # Creates X matrix
> Y <- model.response(D)                   # Creates Y matrix
where Depending on the model, Y may be a column vector, matrix, or other data structure.



Subsections

Gary King 2011-11-29