[R] matrix computation???

Greg Snow Greg.Snow at imail.org
Tue Feb 24 17:07:10 CET 2009


The same as what?  It is not clear what you are trying to compare.

Is this the comparison that you are looking for?:

> A <- scale(stackloss)
> 
> fit1 <- lm( stack.loss ~ . - 1, data=as.data.frame(A))
> tmp1 <- fitted(fit1)
> 
> fit2 <- svd(A[,-4])
> tmp2 <- fit2$u %*% t(fit2$u) %*% A[,4]
> 
> all.equal( as.vector(tmp1), as.vector(tmp2))
[1] TRUE
>





-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Kutlwano Ramaboa
> Sent: Tuesday, February 24, 2009 1:02 AM
> To: Greg Snow; r-help at r-project.org
> Subject: Re: [R] matrix computation???
> 
> Hi Greg
> 
> Thanks for responding.
> 
> When I scale stackloss and compute the fitted values of the scaled data
> frame, the values are not the same:
> 
> A<-data.frame(scale(stackloss))
> 
> lm<- lm(stack.loss ~ . -1, qr=T, data=A)
> summary(lm)
> fitted.values(lm)
> 
> Or should I not be comparing the two?
> 
> >>> Greg Snow <Greg.Snow at imail.org> 2009/02/19 07:31 PM >>>
> You are computing the hat matrix to predict stack.loss, but stack.loss
> is a column in the A matrix, so you predictions are all perfect (given
> stack.loss, what is stack.loss, fairly simple answer, all errors are
> 0).  I think you want to redo this using only the 3 columns other than
> stack.loss in the call to svd, then you should get the results that you
> are expecting.
> 
> Hope this helps,
> 
> --
> Gregory (Greg) L. Snow Ph.D.
> Statistical Data Center
> Intermountain Healthcare
> greg.snow at imail.org
> 801.408.8111
> 
> 
> > -----Original Message-----
> > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> > project.org] On Behalf Of Kutlwano Ramaboa
> > Sent: Wednesday, February 18, 2009 10:43 PM
> > To: r-help at r-project.org
> > Subject: [R] matrix computation???
> >
> > Hello
> >
> > Can anyone tell me what I am doing wrong below? My Y and y_hat are
> the
> > same.
> >
> >
> >
> > A<-scale(stackloss)
> > n1<- dim(A)[1];n2<-dim(A)[2]
> > X<-svd(A)
> > Y<- matrix(A[,"stack.loss"],nrow=n1)
> > Y
> > y_hat <-matrix((X$u%*% t(X$u))%*%Y,nrow=n1,byrow=T)
> > y_hat
> >
> > [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help at r-project.org 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.
> 
> ______________________________________________
> R-help at r-project.org 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.
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org 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.




More information about the R-help mailing list