1.3.5 Matrices from math/matrix
A math/matrix matrix is unnamed data: its rows are the observations. A response can be a column matrix, a row matrix or a one-dimensional math/array array. (require glmnet) does not load math/matrix, so a program that does not use it does not wait for it to load.
The Quick Start of R’s glmnet fits glmnet(x, y) to the 100 × 20 matrix x and the response vector y of QuickStartExample, then predicts at new rows nx drawn at random. Here is the same fit, with x a math/matrix matrix and y a column matrix:
> (require math/matrix math/distributions glmnet/datasets) > (define-values (qs-x qs-y) (quick-start-example)) > (define x (list*->matrix (design-matrix->rows qs-x))) > (define y (->col-matrix qs-y)) > (matrix-shape x)
100
20
> (define fit (elnet-path x y)) > (coef fit #:lambda 0.1)
'#(0.15092807247412926
1.3205971945479142
0.0
0.6751102344847403
0.0
-0.8174115175489668
0.5214366714587341
0.004829335148616431
0.3194159167155697
0.0
0.0
0.14249851856673404
0.0
0.0
-1.0599787019192302
0.0
0.0
0.0
0.0
0.0
-1.0218737036830785)
> (random-seed 29) > (define nx (build-matrix 5 20 (lambda (i j) (sample (normal-dist))))) > (predict fit nx #:lambda '(0.1 0.05))
'((1.3062999979031626
2.0460482043995163
-0.3251741946880151
-1.9745626856083445
1.6552393883406682)
(1.387664042746005
2.126839326881185
-0.26250673336767805
-2.1506980670436624
1.8246177456688972))
> (equal? fit (elnet-path qs-x qs-y)) #t
Converting a large matrix takes time on every fit; convert it once with matrix->design-matrix and pass the design matrix to each fit (see The explicit route).