[R] Matrix::sparse.model.matrix produces inconsistent result

Wush Wu wush978 at gmail.com
Sun Oct 26 10:51:39 CET 2014


Hi,

I notice that the Matrix::sparse.model.matrix produces inconsistent matrix
when the contrasts is a sparse matrix.

Here is a minimal example:

```r
library(Matrix)
class(CO2[[1]]) <- class(CO2[[2]]) <- "factor"
get_contr <- function(is_sparse) {
  contr <- list()
  contr[["Plant"]] <- contrasts(CO2[[1]], FALSE, sparse = is_sparse)
  contr[["Type"]] <- contrasts(CO2[[2]], FALSE, sparse = is_sparse)
  contr[["Treatment"]] <- contrasts(CO2[[3]], FALSE, sparse = is_sparse)
  contr
}
m1 <- sparse.model.matrix(~.^2, CO2, get_contr(TRUE))
m2 <- sparse.model.matrix(~.^2, CO2, get_contr(FALSE))
dim(m1)
dim(m2)
```

The dimension is different:

> dim(m1)

[1] 84 66

> dim(m2)

[1]  84 104

>

The example should be reproduced with docker image rocker/r-base

Wush
PhD Student of Graduate Institute of Electrical Engineering, National
Taiwan University

	[[alternative HTML version deleted]]



More information about the R-help mailing list