Skip to contents

Solves the ordinary least squares normal equations \(\hat\beta = (X^\top X)^{-1} X^\top y\) via Eigen's LDLT Cholesky decomposition of \(X^\top X\); if that decomposition fails (e.g. \(X\) is rank-deficient), it falls back to a column-pivoted QR decomposition of \(X\) directly (Eigen::ColPivHouseholderQR), which returns a minimum-norm least-squares solution even when \(X\) is not full rank. Estimate-only: no standard errors or covariance matrix are computed, only the coefficient vector \(\hat\beta\).

Usage

fast_ols_cpp(X, y, fixed_idx = NULL, fixed_values = NULL)

Arguments

X

A numeric matrix of predictor variables. It is assumed that an intercept column (e.g., a column of ones) is already included in X if desired.

y

A numeric vector of the (continuous) response variable.

fixed_idx

Optional integer vector of 1-indexed columns of X whose coefficients should be held fixed at fixed_values rather than estimated.

fixed_values

Optional numeric vector, parallel to fixed_idx, of the fixed coefficient values.

Value

A list containing the following component:

b

A numeric vector of the estimated regression coefficients \(\hat\beta\) (with any fixed_idx entries set to fixed_values); if the solve produces non-finite values, this is instead a vector of NaN.

Fixed (offset) coefficients

fixed_idx (1-indexed columns of X) and fixed_values optionally hold a subset of coefficients at caller-supplied constant values rather than estimating them: those columns' contribution \(X_{\mathrm{fixed}} \beta_{\mathrm{fixed}}\) is subtracted out of y first, and only the remaining ("free") columns are fit by least squares; the fixed coefficients are then copied back into \(\hat\beta\) unchanged. This is used, e.g., to fit a model with a known/offset intercept without re-estimating it.

See also

fast_ols_with_var_cpp for the variance-computing counterpart.