1.3.3 Lists and vectors🔗ℹ

Racket’s own data needs no conversion: every fit and prediction procedure takes rows as a list or a vector of lists or vectors, and a response as a list, vector, flvector or f64vector (see Data layout). glmnet/data/nested converts the nestings to a design-matrix? value, by rows or by columns, to check the data once and fit it many times or to name its columns, and converts a design matrix back to any of them:

See Nested lists and vectors in the Reference for the two procedures.

> (require glmnet/data/nested)
> (define rows (vector #(1.0 2.0) #(2.0 1.0) #(3.0 4.0) #(4.0 3.0) #(5.0 6.0)))
> (define D (nested->design-matrix rows #:column-names '(x1 x2)))
> (design-matrix-column-names D)

'("x1" "x2")

> (define by-column (vector #(1.0 2.0 3.0 4.0 5.0) #(2.0 1.0 4.0 3.0 6.0)))
> (equal? (nested->design-matrix by-column #:by 'columns #:column-names '("x1" "x2")) D)

#t

> (elnet-result-coefficients (lasso D (vector 1.0 4.0 3.0 6.0 5.0) #:lambda 0.1))

'#(1.602368245039978 -0.6732060759253727)

> (design-matrix->nested D #:outer 'vector #:inner 'vector)

'#(#(1.0 2.0) #(2.0 1.0) #(3.0 4.0) #(4.0 3.0) #(5.0 6.0))

> (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))

The 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. A design matrix with column names is a table, from which a formula fits by name.