[R] prcomp(): How do I multiply two matrices

peter dalgaard pdalgd at gmail.com
Fri Oct 7 20:42:05 CEST 2016


You need to check your theory, and the dimensions of your data structures. Typically, data is (n x p) and your rotation matrix is (p x p) so pre-multiplying  by coef1 fits like a round peg in a square hole. Post-multiplying has a better chance, but I have long forgotten whether you need to transpose the rotation matrix first.

- Peter D.

> On 07 Oct 2016, at 20:24 , T.Riedle <tr206 at kent.ac.uk> wrote:
> 
> Dear R-users,
> 
> I am trying to do a principal components analysis using the attached data. My code looks as follows. I want to calculate the time series of the principal components (PC) . To this end, I transform the coefficients and the data into matrices and employ a matrix multiplication but it does not work.
> 
> 
> 
> data<-equityfunds[,-1]
> 
> PC<-prcomp(data)
> 
> coef1<-PC$rotation
> 
> data<-as.matrix(data)
> 
> PC1<-coef1 %*% data
> 
> 
> 
> This is the error message I get.
> 
> Error in coef1 %*% data : non-conformable arguments
> 
> 
> 
> So, what is wrong with my code? How do I multiply the coefficients with the observations?
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list