Error creating sparse.model.matrix

I am trying to create sparse.model.matrix(from a package Matrix) with a formula where there is an interaction between two factors. This is normal if my input has multiple lines, but as soon as I have only one line, I get an error:

Error in model.spmatrix(t, data, transpose = transpose, drop.unused.levels = drop.unused.levels, : cannot get a slot ("Dim") from an object of type "double"

For example: This does not work:

f<-(mpg~as.factor(cyl)*as.factor(hp))

y<-mtcars
y$cyl<-as.factor(y$cyl)
y$hp<-as.factor(y$hp)

x<-y[1,]

myMatrix<-Matrix::sparse.model.matrix(f,x)

However, duplication of x along two lines leads to the disappearance of the error:

x<-rbind(x,x)
myMatrix<-Matrix::sparse.model.matrix(f,x)

I traced the error to

Matrix:::Csparse_vertcat/ Matrix:::Csparse_horzcatinside Matrix:::model.spmatrix, but I can’t decide what this function written in C is trying to do. Any ideas? Please note, as far as I can determine, this error only occurs when processing the creation of a matrix for the interaction between two factors.

+6
source share

All Articles