Last data update: 2014.03.03

R: Koenker-Ng Example Sparse Model Matrix and Response Vector
KNexR Documentation

Koenker-Ng Example Sparse Model Matrix and Response Vector

Description

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.

Usage

data(KNex)

References

Roger Koenker and Pin Ng (2003). SparseM: A sparse matrix package for R; J. of Statistical Software, 8 (6), http://www.jstatsoft.org/

Examples

data(KNex)
class(KNex$mm)
  dim(KNex$mm)
image(KNex$mm)
str(KNex)

system.time( # a fraction of a second
  sparse.sol <- with(KNex, solve(crossprod(mm), crossprod(mm, y))))

head(round(sparse.sol,3))

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

all.equal(sparse.sol, sp.sol2, tolerance = 1e-13) # TRUE

Results


R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(Matrix)
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/Matrix/KNex.Rd_%03d_medium.png", width=480, height=480)
> ### Name: KNex
> ### Title: 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)
> 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.000   0.004   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] "names for target but not for current"             
[2] "Length mismatch: comparison on first 0 components"
[3] "class(target) is dgeMatrix, current is numeric"   
> 
> 
> 
> 
> 
> dev.off()
null device 
          1 
>