Srinivas Iyyer
2005-Mar-26 14:34 UTC
[R] PCA - princomp can only be used with more units than variables
Hi all: I am trying to do PCA on the following matrix. N1 N2 A1 A2 B1 B2 gene_a 90 110 190 210 290 310 gene_b 190 210 390 410 590 610 gene_c 90 110 110 90 120 80 gene_d 200 100 400 90 600 200>dataf<-read.table("matrix")> pca<-princomp(dataf,cor=TRUE,scores=TRUE)Error in princomp.default(dataf, cor = TRUE, scores TRUE) : princomp can only be used with more units than variables Can any one help me, whats wrong here. thanks srini
Prof Brian Ripley
2005-Mar-26 15:00 UTC
[R] PCA - princomp can only be used with more units than variables
On Sat, 26 Mar 2005, Srinivas Iyyer wrote:> Hi all: > I am trying to do PCA on the following matrix. > > > N1 N2 A1 A2 B1 B2 > gene_a 90 110 190 210 290 310 > gene_b 190 210 390 410 590 610 > gene_c 90 110 110 90 120 80 > gene_d 200 100 400 90 600 200 > > >> dataf<-read.table("matrix") > >> pca<-princomp(dataf,cor=TRUE,scores=TRUE) > Error in princomp.default(dataf, cor = TRUE, scores > TRUE) : > princomp can only be used with more units than > variables > > > Can any one help me, whats wrong here.What is wrong is that you failed to read the help page before posting as the posting guide asks you to. The explanation *IS* on the help page. Please don't expect a help list to read the help for you. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
a.mewes at yahoo.co.uk
2011-Jul-18 15:53 UTC
[R] PCA - princomp can only be used with more units than variables
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@groundesign.net www. www.groundesign.net [[alternative HTML version deleted]]
Joshua Wiley
2011-Jul-18 17:07 UTC
[R] PCA - princomp can only be used with more units than variables
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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles https://joshuawiley.com/
Joshua Wiley
2011-Jul-20 02:56 UTC
[R] PCA - princomp can only be used with more units than variables
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-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles https://joshuawiley.com/
Joshua Wiley
2011-Jul-20 23:26 UTC
[R] PCA - princomp can only be used with more units than variables
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/