friendly / matlib

Matrix Functions for Teaching and Learning Linear Algebra and Multivariate Statistics
http://friendly.github.io/matlib/
65 stars 16 forks source link

QR Function #23

Closed dumbdrummer182 closed 6 years ago

dumbdrummer182 commented 6 years ago

The QR function does not seem to be working correctly. It does not return the same value as the base R qr function. Also, the t(q) %% q doesn't equal I, and q r doesn't equal A.

philchalmers commented 6 years ago

Thanks. Can you provide a simple reproducible example?

dumbdrummer182 commented 6 years ago

I noticed it while working with the dummy matrix:

X <- matrix(seq(1, 9), nrow = 3, ncol = 3, byrow = TRUE)

john-d-fox commented 6 years ago

Here's what I get:

library(matlib) X <- matrix(seq(1, 9), nrow = 3, ncol = 3, byrow = TRUE) res <- QR(X) (. . . some warnings about deprecated computations) q <- res$Q zapsmall(t(q) %% q) [,1] [,2] [,3] [1,] 1 0 0 [2,] 0 1 0 [3,] 0 0 0 q %% res$R [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9

So what's the problem? Your matrix X is of rank 2 -- what did you expect to see?

Best, John


John Fox, Professor Emeritus McMaster University Hamilton, Ontario, Canada Web: socialsciences.mcmaster.ca/jfox/

-----Original Message----- From: dumbdrummer182 [mailto:notifications@github.com] Sent: Thursday, December 28, 2017 2:43 PM To: friendly/matlib matlib@noreply.github.com Cc: Subscribed subscribed@noreply.github.com Subject: Re: [friendly/matlib] QR Function (#23)

I noticed it while working with the dummy matrix:

X <- matrix(seq(1, 9), nrow = 3, ncol = 3, byrow = TRUE)

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/friendly/matlib/issues/23#issuecomment-354346424 , or mute the thread https://github.com/notifications/unsubscribe- auth/ANcgQhsjWmrBhkx5gxQp9JPVS22__wx1ks5tE-82gaJpZM4ROisT . https://github.com/notifications/beacon/ANcgQujNsqzzd63HyRcjk4buF1GgX Fhbks5tE-82gaJpZM4ROisT.gif

dumbdrummer182 commented 6 years ago

Sorry. On the res$R I am getting [,1] [,2] [,3] [1,] -8.124 -9.6011 -11.0782 [2,] 0.000 -0.9045 -1.8091 [3,] 0.000 0.0000 0.0000

versus [,1] [,2] [,3] [1,] -8.124 -9.6011 -11.0782 [2,] 0.000 0.9045 1.8091 [3,] 0.000 0.0000 0.0000 when using the base R, qr function.

john-d-fox commented 6 years ago

Yes, and there's a compensating sign reversal in the Q matrix. As I showed you in my previous response, X is properly recovered.

John

-----Original Message----- From: dumbdrummer182 [mailto:notifications@github.com] Sent: Thursday, December 28, 2017 4:16 PM To: friendly/matlib matlib@noreply.github.com Cc: Fox, John jfox@mcmaster.ca; Comment comment@noreply.github.com Subject: Re: [friendly/matlib] QR Function (#23)

Sorry. On the res$R I am getting [,1] [,2] [,3] [1,] -8.124 -9.6011 -11.0782 [2,] 0.000 -0.9045 -1.8091 [3,] 0.000 0.0000 0.0000

versus [,1] [,2] [,3] [1,] -8.124 -9.6011 -11.0782 [2,] 0.000 0.9045 1.8091 [3,] 0.000 0.0000 0.0000 when using the base R, qr function.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/friendly/matlib/issues/23#issuecomment-354359034 , or mute the thread https://github.com/notifications/unsubscribe- auth/ANcgQtXlbDkHeikldiYByuOTGOOdS6xQks5tFATzgaJpZM4ROisT . https://github.com/notifications/beacon/ANcgQuQ8HkfIZPYGY9oY1apr5YwOHk2 Gks5tFATzgaJpZM4ROisT.gif