
Fast Ordinary Least Squares (OLS) Regression, Estimate-Only (C++ Backend)
Source:R/helper_glm_fit.R
fast_ols_cpp.RdSolves 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\).
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
Xif desired.- y
A numeric vector of the (continuous) response variable.
- fixed_idx
Optional integer vector of 1-indexed columns of
Xwhose coefficients should be held fixed atfixed_valuesrather 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_idxentries set tofixed_values); if the solve produces non-finite values, this is instead a vector ofNaN.
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.