# [R] qr and Moore-Penrose

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Wed Jun 30 13:35:13 CEST 1999

```Torsten Hothorn <hothorn at amadeus.statistik.uni-dortmund.de> writes:

> yesterday I had a little shock using qr (or lm). having a matrix
>
> X <- cbind(1,diag(3))
> y <- 1:3
>
> the qr.coef returns one NA (because X is singular). So I computed the
> Moore-Penrose inverse of X (just from the definition) and I get a correct
> result. Whats wrong about qr in this situation?

Ummm, what exactly do you perceive as a problem here?

> X <- cbind(1, diag(3));	# singular matrix
> y <- 1:3
> Xp <- qr(X);
> b1 <- qr.coef(Xp, y);	# contains NA
> b2 <- mpinv(X)%*%y	# least square fit using Moore-Penrose
> X%*%b2			# == y

...and so is X[,-4] %*% b1[-4] (or, equivalently, X%*%b1 if you
replace the NA with a 0).

--
O__  ---- Peter Dalgaard             Blegdamsvej 3
c/ /'_ --- Dept. of Biostatistics     2200 Cph. N
(*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```