residuals from lm

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

residuals from lm

chuck.01
Hi,
I was playing around with something else and I noticed this matrix code for residuals in a linear model doesn't say what lm() says.  Please tell me if I am completely misguided here.

data(mtcars)
Y <- as.matrix(mtcars[,1])
X <- as.matrix(mtcars[,c(2:11)])

# shouldnt this:
H <- X %*% solve(t(X) %*% X) %*% t(X)
(diag(dim(H)[1]) - H) %*% Y

# be equal to this:
residuals(lm(Y~X))

# ???      
# thanks
Reply | Threaded
Open this post in threaded view
|

Re: residuals from lm

chuck.01
FYI:
As you are likely thinking:  this doesn't belong here (It just occurred to me), I am questioning what I did, not the output of lm()
But if someone knows why I am wrong please let me know.


chuck.01 wrote
Hi,
I was playing around with something else and I noticed this matrix code for residuals in a linear model doesn't say what lm() says.  Please tell me if I am completely misguided here.

data(mtcars)
Y <- as.matrix(mtcars[,1])
X <- as.matrix(mtcars[,c(2:11)])

# shouldnt this:
H <- X %*% solve(t(X) %*% X) %*% t(X)
(diag(dim(H)[1]) - H) %*% Y

# be equal to this:
residuals(lm(Y~X))

# ???      
# thanks
Reply | Threaded
Open this post in threaded view
|

Re: residuals from lm

chuck.01
This post was updated on .
In reply to this post by chuck.01
nevermind...
I forgot the column of 1's for the intercept term

X <- as.matrix(data.frame(rep(1, nrow(mtcars)), mtcars[,c(2:11)]) )


chuck.01 wrote
Hi,
I was playing around with something else and I noticed this matrix code for residuals in a linear model doesn't say what lm() says.  Please tell me if I am completely misguided here.

data(mtcars)
Y <- as.matrix(mtcars[,1])
X <- as.matrix(mtcars[,c(2:11)])

# shouldnt this:
H <- X %*% solve(t(X) %*% X) %*% t(X)
(diag(dim(H)[1]) - H) %*% Y

# be equal to this:
residuals(lm(Y~X))

# ???      
# thanks
Reply | Threaded
Open this post in threaded view
|

Re: residuals from lm

Rolf Turner-3
In reply to this post by chuck.01
On 03/07/12 03:58, chuck.01 wrote:

> Hi,
> I was playing around with something else and I noticed this matrix code for
> residuals in a linear model doesn't say what lm() says.  Please tell me if I
> am completely misguided here.
>
> data(mtcars)
> Y <- as.matrix(mtcars[,1])
> X <- as.matrix(mtcars[,c(2:11)])
>
> # shouldnt this:
> H <- X %*% solve(t(X) %*% X) %*% t(X)
> (diag(dim(H)[1]) - H) %*% Y
>
> # be equal to this:
> residuals(lm(Y~X))
>
> # ???
> # thanks
You are forgetting about the constant term.

Try

     X <- cbind(1,X)
     H <- X %*% solve(t(X) %*% X) %*% t(X)
     R1 <-(diag(dim(H)[1]) - H) %*% Y
     R2 <-residuals(lm(Y~X))
     range(R1-R2)

I get:
[1] -3.886808e-12  1.262768e-11

OMMMMMMMMMMMMMMMM!!! :-)

     cheers,

         Rolf Turner

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.