Re: [S] recursive least squares?

John Maindonald (
Thu, 3 Dec 1998 09:33:16 +1100 (EST)

Roger Koenker wrote
> Can someone suggest an efficient S-way to compute recursive least
> squares estimates, i.e.,
> p <- ncol(X)+1
> T <- nrow(X)
> bhat <- matrix(0,p,T)
> for(t in (p+1):T){
> bhat[,t] <- lm(y[1:t]~X[1:t,])$coef
> }
> I thought that there was a clever QR-decomposition trick...but I can't
> seem to find it.
> url: Roger Koenker
> email Department of Economics

(1) Run e.g.
R <- qr.R(cbind(1,x1,x2,,...,y))

(2) Let Rsubm be a submatrix of R consisting of the first m rows
and columns of R, and let zsubm be the vector of the first m
elements of the final column of R. Then solve Rsubm b = y
to get the vector b of coefficients.

Watch out for the effects of pivoting, though. It appears from
the documentation that qr.R() undoes any pivoting (see qr), but
I've not checked this.

There are in addition ways to use lm(y~x1+x2+.., qr=T)$qr,
and then qr.coef
Basically you chop off as many trailing columns of the qr
representation as you do not want, before you call

With this approach, it is not obvious how you handle pivoting,
whereby the decomposition may be given for a version of X
in which columns are permuted.

John Maindonald email :
Statistical Consulting Unit, phone : (6249)3998
c/o CMA, SMS, fax : (6249)5549
John Dedman Mathematical Sciences Building
Australian National University
Canberra ACT 0200
This message was distributed by To unsubscribe
send e-mail to with the BODY of the
message: unsubscribe s-news