Examples for 'Matrix::KNex'


Koenker-Ng Example Sparse Model Matrix and Response Vector

Aliases: KNex

Keywords: datasets

### ** Examples

data(KNex)
class(KNex$mm)
[1] "dgCMatrix"
attr(,"package")
[1] "Matrix"
  dim(KNex$mm)
[1] 1850  712
image(KNex$mm)
plot of chunk example-Matrix-KNex-1
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.005   0.000   0.015 
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.003000000 0.000000000 0.008000001 
all.equal(sparse.sol, sp.sol2, tolerance = 1e-13) # TRUE
[1] "names for target but not for current"             
[2] "Length mismatch: comparison on first 0 components"
[3] "class(target) is dgeMatrix, current is numeric"   

[Package Matrix version 1.5-3 Index]