[R] PCA - princomp can only be used with more units than variables

Joshua Wiley jwiley.psych at gmail.com
Thu Jul 21 01:26:51 CEST 2011


Hi Armin,

Please copy the list on your emails.  Providing your matrix A (or some
other reproducible example) would be useful to anyone who wanted to
help you.  It is easy to do by copying the output from your console
from running:

dput(A)

This would at least let us try out your code on your data.

On Wed, Jul 20, 2011 at 3:28 AM, a.mewes at yahoo.co.uk
<a.mewes at yahoo.co.uk> wrote:
> Thanks for your continued support Joshua!
>
> I first have run prcomp() and now use principal() to reduce the principal
> components with varimax.
>
> Here is the code so far.....
>
> print("START.............................")
> print("reading in Matrix A")
> A=read.delim('C:/Work/Docs/Merlyn Stuff/Leachate/A.txt')
> print("printing the initial Matrix A")
> A
> print("scaling determinands in Matrix A to maximum")
> ncols=ncol(A)
> c=1
> while (c <= ncols)
> {
> V=A[,c]
> max=max(V)
> V=V/max
> A[,c]=V
> c=c+1
> }
>
> print("printing Matrix A with scaled determinands")
> A
> print("performing PCA of Matrix A to Matrix Apc")
> Apc = prcomp(A)
> print("calculating scores of Apc as Matrix Apcs")
>
> Apcs = as.matrix(A) %*% as.matrix(Apc$rotation)
>
> print("printing scores from the PCA (Matrix Apcs)")
> Apcs
>
> print("performing Varimax rotation of Matrix Apcs to Matrix Apcsv")
>
> library(psych)
> Apcsv<-principal(Apcs,nfactors = 3,rotate = "varimax")
>
> print("scaling Matrix Apcsv to range (0,1)")
> Apcsv=Apcsv-min(Apcsv)
> Apcsv=Apcsv*(1/max(Apcsv))
> print("printing Matrix Apcsv scaled to range (0,1)")
> Apcsv
>
> But there is an error on this part.....
>
>> Apcsv<-principal(Apcs,nfactors = 3,rotate = "varimax")
> Error in eigen(r) : infinite or missing values in 'x'
> In addition: Warning message:
> In sqrt(diag(r)) : NaNs produced
>
> Have you got any ideas?

I have already tried to tell you that what you are doing does not make
sense to me.  The only other advice I can offer is to seek the advice
or help of a local statistician.

>
> Regards,
>
> Armin
>
>
> I have got so far as to use the principal function
>
> On 20/07/2011 03:56, Joshua Wiley wrote:
>
> On Mon, Jul 18, 2011 at 10:48 AM, a.mewes at yahoo.co.uk
> <a.mewes at yahoo.co.uk> wrote:
>
> Ok thank you Josh.
>
> Basically I have a matrix A with 7 rows and 18 columns.
>
> If i < j (where i is the number of rows in your matrix and j is the
> number of columns), then the determinant of the covariance (or
> correlation) matrix |Sigma_A| will be 0 (or very near zero, you can
> easily convince yourself of this by running det(cov(matrix(rnorm(90),
> 9))) as many times as you need).  From Cramer's Rule, if the
> determinant of the matrix is 0, there is not a unique solution
> (clarifications/corrections are welcome if any of this is wrong).
>
>
> What I am told is I need the 'varimax rotated scores from the PCA analysis
> of matrix A'
>
> Who told you that?  Is this homework?  You could look at the
> ?principal function in package "psych".  That said, if this is
> homework I would talk with your instructor more, and if this is
> anything beyond an exercise (i.e., has real world implications), I
> would seek the advice/help of a local statistician.
>
> I can choose from 3 up to 7 components. My problem is how to carry out the
> above.
>
> Have you any ideas?
>
> Would appreciate your help!
>
> Armin
>
> On 18/07/2011 18:07, Joshua Wiley wrote:
>
> Hi,
>
> You need to explain what you want to do.  This is not a software
> issue, you simply cannot create more uncorrelated variables than you
> have observations.
>
> Josh
>
> On Mon, Jul 18, 2011 at 8:53 AM, a.mewes at yahoo.co.uk
> <a.mewes at yahoo.co.uk>  wrote:
>
> Hi,
>
> May I ask a question about a thread
> https://stat.ethz.ch/pipermail/r-help/2005-March/068365.html?
>
> I understand I need to use prcomp instead of princomp when i have less
> units than variables.
>
> However, when I use prcomp the scores is NULL. How can I overcome this?
>
> Regards,
>
> Armin
>
> --
> Kind Regards,
>
> Armin Mewes
> Groundesign
> 10 Jerusalem street
> Belfast
> BT7 1QN
>
> Tel.    (0044)(0)2890280887
> Email.  enquiries at groundesign.net
> www.    www.groundesign.net
>
>
>        [[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.
>
>
>
> --
> Kind Regards,
>
> Armin Mewes
> Groundesign
> 10 Jerusalem street
> Belfast
> BT7 1QN
>
> Tel.    (0044)(0)2890280887
> Email.  enquiries at groundesign.net
> www.    www.groundesign.net
>
>
> --
> Kind Regards,
>
> Armin Mewes
> Groundesign
> 10 Jerusalem street
> Belfast
> BT7 1QN
>
> Tel. 	(0044)(0)2890280887
> Email. 	enquiries at groundesign.net
> www.	www.groundesign.net

-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
https://joshuawiley.com/



More information about the R-help mailing list