1.3.2 Tables🔗ℹ

A table is named columns. It can be an association list from names to columns, a hash, or a design matrix with column names, and a column can be a list, a vector, an flvector or an f64vector (see Tables). A column of numbers is a predictor or a numeric response, and a column of strings, symbols or booleans is a factor, as R’s character and logical columns are (see Categorical predictors). A fit from an association list or a hash reads the columns it names, and a formula the columns its terms use:

> (define patients
    (list (cons "age" '(34 51 67 45))
          (cons "site" '("Boston" "Portland" "Portland" "Boston"))
          (cons "dose" #(2.5 5.0 1.0 3.5))
          (cons "response" '(3.1 5.2 1.9 4.0))))
> (coef (lasso patients "response" #:predictors '("age" "dose") #:lambda 0.1))

'#(1.2675630080533637 0.0 0.7608123306488788)

> (coef (formula-fit (response . ~ . age + dose + site) patients #:lambda 0.1))

'(("(Intercept)" . 1.2675630080533637)

  ("age" . 0.0)

  ("dose" . 0.7608123306488788)

  ("sitePortland" . 0.0))

A design matrix is unnamed data to a fit: it reads every column and predicts by position:

> (define age+dose (table->design-matrix patients '("age" "dose")))
> (define by-position (lasso age+dose (dict-ref patients "response") #:lambda 0.1))
> (coef by-position)

'#(1.2675630080533637 0.0 0.7608123306488788)

> (predict by-position '((40 3.0)))

'(3.5500000000000003)