1.3.7 The explicit route
Every procedure converts its data to a design matrix and a response on each call, and checks it each time. The conversions can also be called directly: to check the data once and fit it many times, to see the matrix a fit reads, or to convert a design matrix back to a format. Rows, columns and tables convert with glmnet itself (see Input data); each other format has its module:
Data |
| Module |
| To glmnet |
| Back |
rows, columns |
|
|
| |||
tables |
|
|
| --- | ||
nested lists, vectors |
|
|
| |||
|
|
| ||||
Polars |
|
|
|
A fit from the converted data is the fit from the data itself:
> (require glmnet/data/polars)
> (define measures (polars->design-matrix flowers '("Sepal.Width" "Petal.Length" "Petal.Width"))) > (define sepal-length (polars->response flowers "Sepal.Length"))
> (equal? (lasso measures sepal-length #:lambda 0.01) (lasso flowers "Sepal.Length" #:lambda 0.01 #:predictors '("Sepal.Width" "Petal.Length" "Petal.Width"))) #t
> (for/list ([lam '(1.0 0.1 0.01)]) (coef (lasso measures sepal-length #:lambda lam)))
'(#(5.843333333333333 0.0 0.0 0.0)
#(3.85433085816735 0.19273279296149542 0.3724731460400136 0.0)
#(2.291509952135093
0.5717696868678742
0.534050705108672
-0.16945221616247616))
Column names are kept as strings, whether they were given as strings or symbols, so the same data named either way gives equal? design matrices. The columns of a design matrix with no names are named "V1", "V2" and so on, as R names them:
> (require glmnet/data/nested) > (define D (nested->design-matrix rows #:column-names '(x1 x2))) > (design-matrix-column-names D) '("x1" "x2")
> (design-matrix->nested D #:by 'columns #:outer 'vector #:inner 'vector) '#(#(1.0 2.0 3.0 4.0 5.0) #(2.0 1.0 4.0 3.0 6.0))
> (design-matrix->rows (table->design-matrix patients '("age" "dose"))) '((34.0 2.5) (51.0 5.0) (67.0 1.0) (45.0 3.5))
> (design-matrix->polars (design-matrix-select-rows measures '(0 50 100)))
shape: (3, 3)
┌─────────────┬──────────────┬─────────────┐
│ Sepal.Width ┆ Petal.Length ┆ Petal.Width │
│ --- ┆ --- ┆ --- │
│ f64 ┆ f64 ┆ f64 │
╞═════════════╪══════════════╪═════════════╡
│ 3.5 ┆ 1.4 ┆ 0.2 │
│ 3.2 ┆ 4.7 ┆ 1.4 │
│ 3.3 ┆ 6.0 ┆ 2.5 │
└─────────────┴──────────────┴─────────────┘
> (design-matrix->polars (rows->design-matrix '((1 2) (3 4))))
shape: (2, 2)
┌─────┬─────┐
│ V1 ┆ V2 │
│ --- ┆ --- │
│ f64 ┆ f64 │
╞═════╪═════╡
│ 1.0 ┆ 2.0 │
│ 3.0 ┆ 4.0 │
└─────┴─────┘
> (table->polars (list (cons "y" '(1.5 2.5)) (cons "group" '("a" "b"))))
shape: (2, 2)
┌─────┬───────┐
│ y ┆ group │
│ --- ┆ --- │
│ f64 ┆ str │
╞═════╪═══════╡
│ 1.5 ┆ a │
│ 2.5 ┆ b │
└─────┴───────┘
design-matrix->polars, the way back, is slower than the way in.