Can I mix symbolic terms (eg. factor) and objects (eg. matrix) in an R formula?
I would like to specify a list of components using the R formula interface. For example:
~ component1 + component2 + component3
If all the components are (functions of) columns in a data frame, this is straightforward. However, I want to be able to specify components of different type (eg. a sparse matrix).
Here is an approach using model.frame
that works for factor
and matrix
objects:
nobs <- nrow(mtcars)
Vformula <- ~ factor(cyl) + diag(nobs)
V <- model.frame(Vformula, data = mtcars)
V <- as.list(V)
V[[1]]
#> [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4
#> Levels: 4 6 8
V[[2]][1:6, 1:6]
#> [,1] [,2] [,3] [,4] [,5] [,6]
#> [1,] 1 0 0 0 0 0
#> [2,] 0 1 0 0 0 0
#> [3,] 0 0 1 0 0 0
#> [4,] 0 0 0 1 0 0
#> [5,] 0 0 0 0 1 0
#> [6,] 0 0 0 0 0 1
However, model.frame
throws an error if one of the components is a sparse matrix (of class Matrix::ddiMatrix
).
library("Matrix")
Vformula <- ~ factor(cyl) + Diagonal(nobs)
V <- model.frame(Vformula, data = data, na.action = na.pass)
#> Error in model.frame.default(Vformula, data = data, na.action = na.pass): 'data' must be a data.frame, environment, or list
The objective is to come up with a convenient way to pass these components as input to a function that can then process them differently depending on whether V[[i]]
is a symbolic term or a sparse matrix.
Created on 2024-06-09 with reprex v2.1.0