# [R] Two ?'s:Permuting a data matrix and projection operator

Douglas Bates bates at stat.wisc.edu
Thu Sep 20 16:37:44 CEST 2001

```"J. Wan" <jyw at u.washington.edu> writes:

> 2.  Is there a function which helps find a matrix... projection operator
> from p dimensional to p-1 dimensional subspace orthogonal to x, a real
> number?

A real number or a real vector?

Least squares calculations in R are done (for the most part) with QR
decompositions.  In these decompositions the matrix Q is an orthogonal
matrix that can be partitioned into an orthogonal basis for the column
span of X and an orthogonal basis for its complement.  In most
applications Q is not formed explicitly.  Instead operations such as
multiplication by Q or t(Q), projection onto the span of X, or
projection orthogonal to X are performed by special purpose functions
qr.qy, qr.qty, qr.fitted, and qr.resid.

If you want to project a vector v onto the subspace orthogonal to the
vector x use qr.resid(qr(x), v).  For example

> vv <- rnorm(6)
> x <- rep(1, 6)
> vv
[1]  0.001020799 -0.317638744  0.426937684  0.917568111 -1.350210885
[6]  0.608166124
> vproj <- qr.resid(qr(x), vv)
> vproj
[1] -0.04661972 -0.36527926  0.37929717  0.86992760 -1.39785140  0.56052561
> vproj %*% x
[,1]
[1,] -1.804112e-16

If you want the projection matrix itself you would need to use qr.Q to
generate the matrix Q then manipulate that to form the projection
matrix.  For example

> Q1 <- qr.Q(qr(x))
> Q1
[,1]
[1,] -0.4082483
[2,] -0.4082483
[3,] -0.4082483
[4,] -0.4082483
[5,] -0.4082483
[6,] -0.4082483
> proj <- diag(6) - Q1 %*% t(Q1)
> proj
[,1]       [,2]       [,3]       [,4]       [,5]       [,6]
[1,]  0.8333333 -0.1666667 -0.1666667 -0.1666667 -0.1666667 -0.1666667
[2,] -0.1666667  0.8333333 -0.1666667 -0.1666667 -0.1666667 -0.1666667
[3,] -0.1666667 -0.1666667  0.8333333 -0.1666667 -0.1666667 -0.1666667
[4,] -0.1666667 -0.1666667 -0.1666667  0.8333333 -0.1666667 -0.1666667
[5,] -0.1666667 -0.1666667 -0.1666667 -0.1666667  0.8333333 -0.1666667
[6,] -0.1666667 -0.1666667 -0.1666667 -0.1666667 -0.1666667  0.8333333

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```

More information about the R-help mailing list