1.1.3 Fitting from named columns
A table associates column names with their values. Convert the Polars dataframe with polars->table, then express the same model as a formula. Name the response and the two predictors directly:
> (define data (polars->table cars))
> (define named-fit (formula-fit (~ mpg (+ wt hp)) data #:lambda 0 #:thresh 1e-10)) > named-fit #<glmnet:gaussian (~ mpg (+ wt hp)) λ=0 dev=0.8268 nz=2/2>
> (coef named-fit)
'(("(Intercept)" . 37.22729275591213)
("wt" . -3.8778430581971657)
("hp" . -0.03177283120189043))
> (equal? (predict named-fit data) (predict fit X)) #t
The formula predicts mpg from wt and hp, leaving the other columns out. With #:lambda 0, this fits the same OLS model as fit. #:thresh 1e-10 matches the tighter convergence threshold used by ols. The coefficients are keyed by name, with the intercept under "(Intercept)" instead of occupying the first position in a list. #:family chooses among the six families; formula-path and formula-cv fit a path and cross-validate one. See Formulas and named data for the formula interface.