[R] Different results for sparse and dense version of model matrix using contrasts and interactions

Jelmer Ypma jelmerypma at gmail.com
Mon Apr 23 11:41:00 CEST 2012


Dear all,

I've been getting different results from the sparse and dense version
of model.Matrix when used with sparse contrasts and interactions
between factors. The same happens when using model.matrix and
sparse.model.matrix. When calculating list.contrasts I get the same
results for sparse and dense contrasts (except the type of the matrix
is different of course). However, when I use these contrasts to
calculate a model.Matrix, the results differ. See for instance the
example below, where a matrix with interactions for two factors are
constructed. Factor 1, f1, has three levels, and factor 2, f2, has two
levels.The dense model.Matrix returns all 3 + 2 + 3*2 = 11
interactions, but the sparse version of model.Matrix with sparse
contrasts returns only 6 interactions.

I was expecting these functions to return the same objects, except
that one would be sparse and the other would be dense. Is this the
indended behaviour or am I using the function incorrectly?

Many thanks in advance,
Jelmer

> sessionInfo()
R version 2.15.0 (2012-03-30)
Platform: i386-pc-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=Dutch_Netherlands.1252  LC_CTYPE=Dutch_Netherlands.1252
   LC_MONETARY=Dutch_Netherlands.1252
[4] LC_NUMERIC=C                       LC_TIME=Dutch_Netherlands.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] MatrixModels_0.3-1 Matrix_1.0-6       lattice_0.20-6

loaded via a namespace (and not attached):
[1] grid_2.15.0  tools_2.15.0

#
# Example
#
library('MatrixModels')

# create a data.frame with two factors
dat <- data.frame( "f1" = as.factor(c(1,1,2,2,3,3)), "f2" =
as.factor(c(1,2,1,2,1,2)) )

# create contrasts, both dense and sparse
(list.contrasts        <- lapply( dat, contrasts, contrasts=FALSE,
sparse=FALSE ))
(list.contrasts.sparse <- lapply( dat, contrasts, contrasts=FALSE,
sparse=TRUE ))

# create a formula with interactions
formula <- ~ -1 + f1*f2

# create sparse and non-sparse model matrices
# m2 is not as expected, and different from m1 and m3
(m1 <- model.Matrix( formula, data=dat, sparse=TRUE,  contrasts.arg =
list.contrasts ))
(m2 <- model.Matrix( formula, data=dat, sparse=TRUE,  contrasts.arg =
list.contrasts.sparse ))
(m3 <- model.Matrix( formula, data=dat, sparse=FALSE, contrasts.arg =
list.contrasts ))

all.equal( m1, m3 )
all.equal( m1, m2 )
all.equal( m2, m3 )

# using sparse.model.matrix directly
sparse.model.matrix( formula, data=dat, contrasts.arg = list.contrasts )
sparse.model.matrix( formula, data=dat, contrasts.arg = list.contrasts.sparse )
model.matrix( formula, data=dat, contrasts.arg = list.contrasts )



More information about the R-help mailing list