1.3.1 Example datasets🔗ℹ

glmnet/datasets provides the example datasets of R’s glmnet package, which its vignettes use, and R’s mtcars and iris, which the formula examples use. (require glmnet) does not load it:

> (require glmnet/datasets)

See Example datasets in the Reference for each loader.

R loads a dataset with data, which makes a list of x and y: data(QuickStartExample); x <- QuickStartExample$x. Each dataset here has a procedure instead, which returns the arguments of its family’s fitter, in order: a design matrix of the predictors, then the response.

> (define-values (x y) (quick-start-example))
> x

#<design-matrix 100x20>

> (lasso x y #:lambda 0.1)

#<glmnet:gaussian λ=0.1 dev=0.8994 nz=9/20>

The design matrix names its columns "V1", "V2", and so on, as R’s coef names the columns of these unnamed matrices. The response has the shape its family takes (see Data layout):

R

  

Loader

  

Values

QuickStartExample

  

quick-start-example

  

100 × 20, a list of reals

BinomialExample

  

binomial-example

  

100 × 30, a list of 0/1 labels

MultinomialExample

  

multinomial-example

  

500 × 30, a list of class labels 0, 1, 2

PoissonExample

  

poisson-example

  

500 × 20, a list of counts

CoxExample

  

cox-example

  

1000 × 30, a list of times, a list of 0/1 statuses

MultiGaussianExample

  

multi-gaussian-example

  

100 × 20, a 100 × 4 design matrix

SparseExample

  

sparse-example

  

100 × 20, dense, a list of reals

So a loader’s values go straight to its fitter, and call-with-values passes them all:

> (call-with-values binomial-example
                    (lambda (x y) (logistic-fit x y #:lambda 0.05)))

#<glmnet:binomial λ=0.05 dev=0.5034 nz=13/30>

> (define-values (cx time status) (cox-example))
> (take time 3)

'(1.768777565884203 0.5452840449249775 0.04485917798982136)

> (take status 3)

'(1 1 0)

Two datasets differ from R’s. R’s MultinomialExample labels its classes 1, 2 and 3, and here they are 0, 1 and 2, the labels multinomial-fit takes. R’s SparseExample holds its predictors in a sparse matrix, and here they are dense, until sparse input is supported (#11).

mtcars and iris are tables, as R’s are data frames, whose columns are vectors. Every module that uses them shares them, so the vectors are immutable:

> (table-column-names mtcars)

'("mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear" "carb")

> (vector-take (cdr (assoc "Species" iris)) 3)

'#("setosa" "setosa" "setosa")

The datasets are CSV files in the package, under "glmnet/datasets/", which "scripts/export-datasets.R" writes from R. Every number is written with 17 significant digits, so each reads back as the same double as R’s, and the parity tests check every one of them against R’s own. They can be read like any other CSV file:

> (require glmnet/data/csv)
> (define iris-file (collection-file-path "iris.csv" "glmnet" "datasets"))
> (equal? (csv-file->table iris-file) iris)

#t