[R] qr() and Gram-Schmidt

cruz cruadam at gmail.com
Mon Nov 3 10:26:10 CET 2008


Hi,

Why the qr() produces a negative Q compared with Gram-Schmidt? (note
example below, except Q[2,3])

Here is an example, I calculate the Q by Gram-Schmidt process and
compare the output with qr.Q()


a <- c(1,0,1)
b <- c(1,0,0)
c <- c(2,1,0)
x <- matrix(c(a,b,c),3,3)

##########################
# Gram-Schmidt
##########################

A <- matrix(a,3,1)
q1 <- (1/sqrt(sum(A^2)))*A
B <- b - (q1%*%b)%*%q1
q2 <- (1/sqrt(sum(B^2)))*B
C <- c - (q1%*%c)%*%q1 - (q2%*%c)%*%q2
q3 <- (1/sqrt(sum(C^2)))*C
Orthonormal.basis <- matrix(c(q1,q2,q3),3,3)
> Orthonormal.basis
                [,1]            [,2] [,3]
[1,] 0.7071068  0.7071068    0
[2,] 0.0000000  0.0000000    1
[3,] 0.7071068 -0.7071068    0


##########################
# QR Factorisation  X = QR
##########################

x.qr <- qr(x)
Q <- qr.Q(x.qr)
R <- qr.R(x.qr)
X <- qr.X(x.qr)
> Q
                 [,1]            [,2] [,3]
[1,] -0.7071068 -0.7071068    0
[2,]  0.0000000  0.0000000    1
[3,] -0.7071068  0.7071068    0


Thanks,
cruz



More information about the R-help mailing list