Sparse matrix multiplication with NA

Performing matrix multiplication on sparse matrices in R gives a different result as the same operation performed on dense forms of the same matrices, if there is data in the data NA.

Some data to demonstrate:

library(Matrix)
set.seed(123)
m1  <- Matrix(data=sample(c(0,0,0,0,0,1,2,NA),25, T), ncol = 5, nrow = 5, sparse = F)
m2  <- Matrix(data=sample(c(0,0,0,0,0,1,2,NA),25, T), ncol = 5, nrow = 5, sparse = F)
sm1 <- Matrix(m1, sparse = T)
sm2 <- Matrix(m2, sparse = T)

Now if we do

m1 %*% m2

# 5 x 5 Matrix of class "dgeMatrix"
#      [,1] [,2] [,3] [,4] [,5]
# [1,]   NA   NA   NA   NA   NA
# [2,]    2   NA    0    0    2
# [3,]   NA   NA   NA   NA   NA
# [4,]   NA   NA   NA   NA   NA
# [5,]   NA   NA   NA   NA   NA

we get another result:

sm1 %*% sm2

# 5 x 5 sparse Matrix of class "dgCMatrix"
#                  
# [1,]  . NA  . . NA
# [2,]  2 NA  . .  2
# [3,]  . NA NA .  2
# [4,] NA NA  . . NA
# [5,] NA NA  . .  2

The reason for this is that while it 0 * NAreturns NA, a zero (or missing location) in the sparse matrix returns zero when multiplied by NA.

We can see this behavior in

0 %*% NA
     [,1]
[1,]   NA

Matrix(data=0, sparse=T) %*% NA
1 x 1 Matrix of class "dgeMatrix"
     [,1]
[1,]    0

Is there a way to get sparse matrix multiplication to always give the same result as dense matrix multiplication, when there can be NA in the data (except, of course, transform into their dense forms, which defeat the object of using sparse matrices in the first place)?

Update

Comments suggest that different people see diverse behaviors. I tested on 64-bit Linux (kubuntu 16.04), R 3.2.3, Matrix 1.2-3, and on R 3.3.1, Matrix 1.2-6.

Below are all the same results for me:

m1 %*% m2
as.matrix(m1) %*% as.matrix(m2)
as.matrix(sm1) %*% as.matrix(sm2)

While

sm1 %*% sm2

gives a different meaning as shown above. Similar discrepancies are observed when matrices contain Infor NaN.

-

However, the behavior of @ user20650 is slightly different, which says in the comments that they also see different results on Ubuntu 14.04 (x32), Matrix v1.2-6, R v3.3.1, but in different combinations for me, In their case m1 %*% m2and sm1 %*% sm2both give the same answer as for sm1 %*% sm2. But, as.matrix(m1) %*% as.matrix(m2)and as.matrix(sm1) %*% as.matrix(sm2)both give the result that I get for m1 %*% m2.
+4

All Articles