Computes OLS coefficients, residuals, and predicted values using the QR decomposition.


{ b, r, p } = olsqr2(depvar, indepvars)
  • depvar (Nx1 vector) – dependent variable

  • indepvars (NxP matrix) – independent variables

  • b (Px1 vector) – least squares estimates of regression of y on x. If x does not have full rank, then the coefficients that cannot be estimated will be zero.

  • r (Px1 vector) – OLS residuals. (\(r = y - x*b\))

  • p (Px1 vector) – predicted values. (\(p = x*b\))


rndseed 129727134;

// Assign random matrices
x = rndn(150, 4);
y = rndn(150, 1);

// Solve OLS coefficient using QR decomposition
{ b, r, p } = olsqr2(y, x);


This provides an alternative to \(y/x\) for computing least squares coefficients.

This procedure is slower than the / operator. However, for near singular matrices, it may produce better results.

The olsqr2() procedure handles matrices that do not have full rank by returning zeros for the coefficients that cannot be estimated.

See also

Functions olsqr(), orth(), qqr()