RainbowStats · Regression and model selection
LASSO_REGRESSION
Shrink coefficients. Select predictors. Understand the trade-off.
What does lasso do?
Ordinary least squares chooses coefficients to minimize squared residuals. Lasso adds a penalty for the absolute sizes of the slopes. It can set coefficients exactly to zero, leaving a smaller model. Shrinkage accepts some training-fit bias in pursuit of more stable predictions; improvement on new data is not guaranteed.
Syntax
LASSO_REGRESSION(LIST(Y,X1,...)[,lambda[,maxIterations[,tolerance]]])
| Argument | Meaning |
|---|---|
| LIST(Y,X1,...) | Response first; at least one predictor and three common observations. |
| lambda | Optional absolute penalty, finite and nonnegative. Omit for automatic grid search. Not a percentage or requested predictor count. |
| maxIterations | Maximum coordinate sweeps; default 10000, integer range 1–1000000. |
| tolerance | KKT tolerance multiplier; default 1e-8, positive and at most 0.01. |
The equation and the units
Each predictor becomes Zij = (Xij − mean(Xj)) / sj, where sj = √[Σ(Xij − mean(Xj))²/n]. Y is centered, but is not divided by its standard deviation. The intercept is unpenalized. The displayed slopes are βj = θj/sj, and the intercept is mean(Y) − Σ βjmean(Xj).
Standardization gives predictors in different units comparable penalties. Lambda is in response units under this convention. Multiplying Y by 100 requires multiplying lambda by 100 for equivalent shrinkage. Published packages may use different objective scaling.
How the iteration works
- Center Y, standardize X and start with zero slopes.
- For one predictor, calculate its association with the residual excluding its own contribution.
- Soft-threshold that association: S(c,λ) = sign(c) max(|c| − λ,0), divided by the standardized column's mean square.
- Update the residuals and cycle through all predictors.
- Stop when the maximum violation of the optimality (KKT) conditions is at most tolerance × max(1,lambdaMax).
This is coordinate descent, not repeated unpenalized OLS. Nonconvergence produces an explicit error instead of a misleading completed fit.
Choosing lambda
Lambda zero removes the penalty. Lambda at or above lambdaMax = max |Z′(Y−mean(Y))| / n yields zero slopes. LambdaMax is reported in the summary. Constants have zero slopes; if every predictor is constant, lambdaMax is zero.
Omit lambda to search automatically using five expanding training windows and disjoint future validation blocks. At least 30 aligned observations are required. The first window uses half the observations. Each window fits its own scaling and lambdaMax using training rows only. Search 30 log-spaced fractions from 1 to 0.001, plus zero; select the smallest pooled validation MSE, with exact ties favoring the larger fraction. Refit all data using the selected fraction times full-sample lambdaMax. A failed candidate stops the search with an explicit error; it is not silently ignored. For time series, use chronological or rolling validation, fitting all scaling on each training window. The example's 0.01 is illustrative, not an optimized value. Do not choose lambda by maximizing training R².
The validation score used for selection is not an independent final test. Reserve an untouched later period for final assessment. Validation uses observed predictors, so it measures conditional prediction rather than forecasting unavailable future inputs. Irregular gaps and any frequency conversion by alignment still require care.
Automatic example
fit=LASSO_REGRESSION(LIST(LOGDIFF(HOUSTNSA),LOGDIFF(PERMITNSA))) fit
Run with explicit lambda
housing=LOGDIFF(HOUSTNSA) permits=LOGDIFF(PERMITNSA) fit=LASSO_REGRESSION(LIST(housing,permits),0.01) fit
One predictor makes shrinkage easy to see. Add more candidate series to explore variable selection.
EXTRACT(fit,"summary") EXTRACT(fit,"model") EXTRACT(fit,"slideshow")
Extractable results
| Key | Result |
|---|---|
| validation / validationtable | Validation error curve and selected penalty / complete grid table. Explicit mode explains that no search ran. |
| model | Original-unit coefficients, standardized-X coefficients and selection status. |
| summary | Sample size, retained count, lambda/lambdaMax, training R² and RMSE, DW, objective and convergence. |
| beta | Numeric array: intercept, then all slopes in input order, including zeros. |
| standardizedbeta | Numeric θ array in predictor order; no intercept. |
| selected | HTML table containing the intercept and nonzero predictors. |
| data / selecteddata | Aligned original list / list of Y and retained predictors. The selected list can contain Y alone. |
| fitted / residuals | Dated series from the penalized fit. |
| actual_vs_fitted / autocorrelation | Native D3 fit / residual diagnostic charts. |
| regression | Optional baseline RegressionOperator fitted to all original predictors on the common sample. |
| refit | Optional post-selection RegressionOperator fitted without penalty to retained predictors. |
| slideshow | Educational results presentation; also the default JSON output. |
Lasso and an OLS refit answer different questions
The lasso fitted values use the shrunk coefficients. An OLS refit estimates the selected slopes again without a penalty. It is a different model. Its standard errors and p-values do not account for selection; extracting HAC does not resolve that problem.
baseline=EXTRACT(fit,"regression") refitted=EXTRACT(fit,"refit") EXTRACT(refitted,"model")
OLS comparisons are evaluated only when requested and can fail for singular designs or insufficient residual degrees of freedom. When all slopes are zero, refit returns an explanation; the lasso model and fitted series already provide the intercept-only result.
Data preparation and interpretation
The existing getSameDateRange alignment runs once and may convert frequency. Inspect the data extract. Remaining nonfinite values, mismatched dates or dates not in strictly increasing order are rejected. Inputs are not deliberately modified. Constant predictors are excluded. The solver permits more predictors than observations and collinear designs, but highly correlated predictors may exchange roles and coefficient solutions need not be unique.
A zero is not a significance test. Retention does not establish causality. Lasso does not remove serial correlation; DW and ACF use adjacent retained observations, so irregular gaps matter. No ordinary OLS p-values, adjusted R², AIC or BIC are attached to the lasso fit.
Ordinary lasso penalizes sine and cosine separately. It is not group lasso and does not preserve harmonic pairs automatically.
Further reading
Friedman, Hastie and Tibshirani (2010), Regularization Paths for Generalized Linear Models via Coordinate Descent. See also Lasso overview.
Available extracts
Save the command result in a variable, then use EXTRACT(result, "key"). Availability can depend on the input form and fitted model.
modelsummaryvalidationvalidationtablebetastandardizedbetaselecteddataselecteddatafittedresidualsactual_vs_fittedautocorrelationregressionrefitslideshow
In indexed patterns such as beta:n, replace n with the required index.