SCM

[#6673] When contrasts.arg in sparse.model.matrix has class ddiMatrix, then results are wrong

Date:
2020-05-21 19:46
Priority:
3
State:
Open
Submitted by:
Davor Josipovic (davor)
Assigned to:
Nobody (None)
Hardware:
All
Product:
None
Operating System:
None
Component:
None
Version:
None
Severity:
major
Resolution:
None
URL:
Summary:
When contrasts.arg in sparse.model.matrix has class ddiMatrix, then results are wrong

Detailed description
Here is a test script:

--- START TEST SCRIPT ---

loadNamespace('Matrix')
sessionInfo()
options("contrasts")


df <- data.frame('a' = factor(1:3), 'b' = factor(4:6))
df

cont <- lapply(df, contrasts, contrasts = FALSE)
cont

cont.sparse <- lapply(df, contrasts, contrasts = FALSE, sparse = TRUE)
cont.sparse

Matrix::sparse.model.matrix(data = df, object = ~ . - 1, contrasts.arg = cont)

Matrix::sparse.model.matrix(data = df, object = ~ . - 1, contrasts.arg = cont.sparse)

--- END TEST SCRIPT ---

In my case, the output is the following:

--- START OUTPUT TEST SCRIPT ---

> loadNamespace('Matrix')
<environment: namespace:Matrix>
> sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

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

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

loaded via a namespace (and not attached):
[1] compiler_3.6.1 Matrix_1.2-17 tools_3.6.1 yaml_2.2.0 grid_3.6.1 lattice_0.20-38
> options("contrasts")
$contrasts
unordered ordered
"contr.treatment" "contr.poly"

>
>
> df <- data.frame('a' = factor(1:3), 'b' = factor(4:6))
> df
a b
1 1 4
2 2 5
3 3 6
>
> cont <- lapply(df, contrasts, contrasts = FALSE)
> cont
$a
1 2 3
1 1 0 0
2 0 1 0
3 0 0 1

$b
4 5 6
4 1 0 0
5 0 1 0
6 0 0 1

>
> cont.sparse <- lapply(df, contrasts, contrasts = FALSE, sparse = TRUE)
> cont.sparse
$a
3 x 3 diagonal matrix of class "ddiMatrix"
1 2 3
1 1 . .
2 . 1 .
3 . . 1

$b
3 x 3 diagonal matrix of class "ddiMatrix"
4 5 6
4 1 . .
5 . 1 .
6 . . 1

>
> Matrix::sparse.model.matrix(data = df, object = ~ . - 1, contrasts.arg = cont)
3 x 6 sparse Matrix of class "dgCMatrix"
a1 a2 a3 b4 b5 b6
1 1 . . 1 . .
2 . 1 . . 1 .
3 . . 1 . . 1
>
> Matrix::sparse.model.matrix(data = df, object = ~ . - 1, contrasts.arg = cont.sparse)
3 x 5 sparse Matrix of class "dgCMatrix"
a1 a2 a3 b4 b5
1 1 . . 1 .
2 . 1 . . 1
3 . . 1 . .

--- END OUTPUT TEST SCRIPT ---

Notice the missing `b6` column in the last output when using `contrasts.arg = cont.sparse`. I expect the resulting model matrix to be the same as in the previous command. Unless I am seeing ghosts, this is a major bug?

Comments:

No Comments Have Been Posted

Attached Files:

Changes

No Changes Have Been Made to This Item

Thanks to:
Vienna University of Economics and Business Powered By FusionForge