stats.lm

Linear regression by least squares, with R's lm(). Name the model with a formula, or with a target and a column selector for the features. Writes a fitted SpadeModel that models.predict scores without R, the coefficient table, and the fit statistics, with optional cross-validation.

The model records the design columns R produced -- treatment coding, the full coding R uses without an intercept, interactions -- so prediction reproduces them exactly. Formula variables are limited to columns, log(), log1p(), sqrt(), exp() and I() arithmetic; how they combine (:, *, ^, + 0) is not restricted. For tables too large for memory, models.linear fits the same model by streaming.

  • Kind: standard
  • Network: no

Inputs🔗

NameTypeDescription
datafile (Parquet or CSV)Training table: one row per observation, with the target, the predictor columns, and any weights or id columns.
formulastringModel formula, e.g. ESTIMATE ~ B5 + log(B25) + FORTYPE + 0. Use instead of target and features. Default empty.
targetstringThe response column (when no formula is given). Default empty.
featuresstringColumn selector for the predictors (when no formula is given): names, globs and exclusions, e.g. B*,MB*,-B0. Default empty.
interceptbooleanInclude an intercept when building the model from target and features. With a formula, write + 0 in the formula instead. Default true.
id_columnsstringColumn selector for identifier columns, never used as predictors; kept as text when reading CSV. Default empty.
weightsstringColumn of non-negative observation weights. Empty fits unweighted.
categoricalstringColumn selector for predictors to treat as categorical even though they are stored as numbers. Default empty.
na_actionstringfail (default, naming the columns) or drop: what to do with a row that has a missing value in a model column.
seednumberSeed for cross-validation fold assignment. Default 0.
cv_foldsnumberK-fold cross-validation (0 for none). The model is always fitted on all rows. Default 0.
cv_groupstringColumn whose values are kept together in one fold (e.g. a county id), so spatially related rows do not leak between folds. Default empty.

Outputs🔗

NameTypeDescription
modelfile (SpadeModel)The fitted model (family linear), for models.predict.
coefficientsfile (CSV)One row per design column: term, estimate, std_error, statistic, p_value.
fitjsonRows used and dropped, the arguments that shaped the fit, and metrics: rmse, mae, r_squared, adj_r_squared, aic, bic, sigma, df_residual, and cv_rmse, cv_mae, cv_r_squared when cross-validated.

Example🔗

- id: "@fit"
  name: stats.lm
  inputs:
    - "@plots"
  args:
    formula: "biomass ~ canopy_cover + log(stand_age) + forest_type"
    cv_folds: 5
    cv_group: "county"