A model matrix mm and corresponding response vector y used in an example by Koenker and Ng. The matrix mm is a sparse matrix with 1850 rows and 712 columns but only 8758 non-zero entries. It is a "dgCMatrix" object. The vector y is just numeric of length 1850.

data(KNex)

References

Roger Koenker and Pin Ng (2003). SparseM: A sparse matrix package for R; J. of Statistical Software, 8 (6), doi:10.18637/jss.v008.i06

Examples

data(KNex, package = "Matrix")
class(KNex$mm)
#> [1] "dgCMatrix"
#> attr(,"package")
#> [1] "Matrix"
dim(KNex$mm)
#> [1] 1850  712
image(KNex$mm)

str(KNex)
#> List of 2
#>  $ mm:Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
#>   .. ..@ i       : int [1:8755] 0 2 25 27 163 165 1258 1261 1276 1278 ...
#>   .. ..@ p       : int [1:713] 0 13 17 26 38 43 52 56 61 67 ...
#>   .. ..@ Dim     : int [1:2] 1850 712
#>   .. ..@ Dimnames:List of 2
#>   .. .. ..$ : NULL
#>   .. .. ..$ : NULL
#>   .. ..@ x       : num [1:8755] 0.277 0.277 0.277 0.277 0.277 ...
#>   .. ..@ factors : list()
#>  $ y : num [1:1850] 64.07 5.88 64.03 5.96 76.41 ...

system.time( # a fraction of a second
  sparse.sol <- with(KNex, solve(crossprod(mm), crossprod(mm, y))))
#>    user  system elapsed 
#>   0.002   0.000   0.003 

head(round(sparse.sol,3))
#> 6 x 1 Matrix of class "dgeMatrix"
#>         [,1]
#> [1,] 823.361
#> [2,] 340.116
#> [3,] 472.976
#> [4,] 349.317
#> [5,] 187.560
#> [6,] 159.052

## Compare with QR-based solution ("more accurate, but slightly slower"):
system.time(
  sp.sol2 <- with(KNex, qr.coef(qr(mm), y) ))
#>    user  system elapsed 
#>   0.004   0.000   0.004 

all.equal(sparse.sol, sp.sol2, tolerance = 1e-13) # TRUE
#> [1] "typeof(target) is \"S4\", typeof(current) is \"double\""                                       
#> [2] "class(target) is structure(\"dgeMatrix\", package = \"Matrix\"), class(current) is \"numeric\""
#> [3] "dim: < Modes: numeric, NULL >"                                                                 
#> [4] "dim: < Lengths: 2, 0 >"                                                                        
#> [5] "dim: < target is numeric, current is NULL >"