Alistair Campbell
2007-Feb-06 11:07 UTC
[R] How-To construct a cov list to use a covariance matrix in factanal?
Hi, I have a set of covariance matrices but not the original data. I want to carry out some exploratory factor analysis. So, I am trying to construct a covariance matrix list as the input for factanal. I can construct a list which includes the cov, the centers, and the n.obs. But it doesn't work. I get an error that says "Error in sqrt(diag(cv)) : Non-numeric argument to mathematical function". So, obviously I am doing something wrong. Two questions occur. Can someone either tell me how to construct a proper covmat list object or point me to a description of how to do it? The other question is whether it is possible to simply use the covariance matrix as the argument for covmat in factanal? The description implies that it is but I really have no idea of how to do this. I have tried simply making covmat the covariance matrix but it doesn't wotk. I just get the message "'covmat' is not a valid covariance list" Anyway, thanks for any thoughts you might have on this. Alistair Campbell -- Dr Alistair Campbell, PhD Senior Lecturer in Clinical Psychology School of Psychology James Cook University Townsville Queensland Australia Ph: +61 7 47816879
Prof Brian Ripley
2007-Feb-06 15:04 UTC
[R] How-To construct a cov list to use a covariance matrix in factanal?
The help page says covmat: A covariance matrix, or a covariance list as returned by 'cov.wt'. Of course, correlation matrices are covariance matrices. and there is an example of a covariance list (ability.cov).> factanal(factors = 2, covmat = ability.cov) > factanal(factors = 2, covmat = ability.cov$cov)both work. See> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.as we have no idea what you tried. On Tue, 6 Feb 2007, Alistair Campbell wrote:> Hi, > > I have a set of covariance matrices but not the original data. I want to > carry out some exploratory factor analysis. So, I am trying to construct > a covariance matrix list as the input for factanal. I can construct a > list which includes the cov, the centers, and the n.obs. But it doesn't > work. I get an error that says "Error in sqrt(diag(cv)) : Non-numeric > argument to mathematical function". So, obviously I am doing something > wrong. > > Two questions occur. Can someone either tell me how to construct a > proper covmat list object or point me to a description of how to do it? > The other question is whether it is possible to simply use the > covariance matrix as the argument for covmat in factanal? The > description implies that it is but I really have no idea of how to do > this. I have tried simply making covmat the covariance matrix but it > doesn't wotk. I just get the message "'covmat' is not a valid covariance > list" > > Anyway, thanks for any thoughts you might have on this. > > Alistair Campbell >-- 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
Alistair Campbell
2007-Feb-07 01:00 UTC
[R] How-To construct a cov list to use a covariance matrix in factanal?
Thanks for that Brian, I have worked through the examples. They work because the covmat were produced by the cov.wt which provides output as a list object. I am trying to construct my own list object to use as the covmat. There are no obvious instructions on how to do this. So, here is what I have done so far. I reconstructed the covariance matrix in the example and created a dataframe: > testmatrix general picture blocks maze reading vocab 1 24.641 5.991 33.520 6.023 20.755 29.701 2 5.991 6.700 18.137 1.782 4.936 7.204 3 33.520 18.137 149.831 19.424 31.430 50.753 4 6.023 1.782 19.424 12.711 4.757 9.075 5 20.755 4.936 31.430 4.757 52.604 66.762 6 29.701 7.204 50.753 9.075 66.762 135.292 and then used this to construct a list object like the output from the example;> tstcov<- list(cov=testmatrix, center=c(0,0,0,0,0), n.obs=112)I tested to see whether my list object looked like the examples> tstcov$cov general picture blocks maze reading vocab 1 24.641 5.991 33.520 6.023 20.755 29.701 2 5.991 6.700 18.137 1.782 4.936 7.204 3 33.520 18.137 149.831 19.424 31.430 50.753 4 6.023 1.782 19.424 12.711 4.757 9.075 5 20.755 4.936 31.430 4.757 52.604 66.762 6 29.701 7.204 50.753 9.075 66.762 135.292 $centers [1] 0 0 0 0 0 $n.obs [1] 112 It looks the same. So I then used this list as the argument in factanal and get the error message.> factanal(factors=2, covmat=tstcov, rotation="varimax")Error in sqrt(diag(cv)) : Non-numeric argument to mathematical function I know that what you see of a list is not necessarily all that is there. So, I figure I am missing some part of the object that makes this list suitable for use by factanal. So, I hope this is enough detail. Any thoughts would be appreciated. -- Dr Alistair Campbell, PhD Senior Lecturer in Clinical Psychology School of Psychology James Cook University Townsville Queensland Australia Ph: +61 7 47816879
William Revelle
2007-Feb-07 01:59 UTC
[R] How-To construct a cov list to use a covariance matrix in factanal?
Alistair,> >I have worked through the examples. They work because the covmat >were produced by the cov.wt which provides output as a list object. >I am trying to construct my own list object to use as the covmat. >There are no obvious instructions on how to do this. > >So, here is what I have done so far. > >I reconstructed the covariance matrix in the example and created a dataframe: > > > testmatrix > general picture blocks maze reading vocab >1 24.641 5.991 33.520 6.023 20.755 29.701 >2 5.991 6.700 18.137 1.782 4.936 7.204 >3 33.520 18.137 149.831 19.424 31.430 50.753 >4 6.023 1.782 19.424 12.711 4.757 9.075 >5 20.755 4.936 31.430 4.757 52.604 66.762 >6 29.701 7.204 50.753 9.075 66.762 135.292 > >and then used this to construct a list object like the output from >the example; > >> tstcov<- list(cov=testmatrix, center=c(0,0,0,0,0), n.obs=112) > >I tested to see whether my list object looked like the examples > >> tstcov >$cov > general picture blocks maze reading vocab >1 24.641 5.991 33.520 6.023 20.755 29.701 >2 5.991 6.700 18.137 1.782 4.936 7.204 >3 33.520 18.137 149.831 19.424 31.430 50.753 >4 6.023 1.782 19.424 12.711 4.757 9.075 >5 20.755 4.936 31.430 4.757 52.604 66.762 >6 29.701 7.204 50.753 9.075 66.762 135.292 > >$centers >[1] 0 0 0 0 0 > >$n.obs >[1] 112 > >It looks the same. So I then used this list as the argument in >factanal and get the error message. > >> factanal(factors=2, covmat=tstcov, rotation="varimax") >Error in sqrt(diag(cv)) : Non-numeric argument to mathematical function > >I know that what you see of a list is not necessarily all that is >there. So, I figure I am missing some part of the object that makes >this list suitable for use by factanal. >So, I hope this is enough detail. Any thoughts would be appreciated.>-- >Dr Alistair Campbell, PhD >Senior Lecturer in Clinical Psychology >School of Psychology >James Cook UniversityTownsville Queensland Australia The call to factanal requires you to specify that you have a covariance matrix (covmat). Using your matrix and specifying two factors: test.df general picture blocks maze reading vocab 1 24.641 5.991 33.520 6.023 20.755 29.701 2 5.991 6.700 18.137 1.782 4.936 7.204 3 33.520 18.137 149.831 19.424 31.430 50.753 4 6.023 1.782 19.424 12.711 4.757 9.075 5 20.755 4.936 31.430 4.757 52.604 66.762 6 29.701 7.204 50.753 9.075 66.762 135.292> test.mat <- as.matrix(test.df) > tf <- factanal(factors=2,covmat=test.mat) > tfCall: factanal(factors = 2, covmat = test.mat) Uniquenesses: general picture blocks maze reading vocab 0.455 0.589 0.218 0.769 0.052 0.334 Loadings: Factor1 Factor2 1 0.499 0.543 2 0.156 0.622 3 0.206 0.860 4 0.109 0.468 5 0.956 0.182 6 0.785 0.225 Factor1 Factor2 SS loadings 1.858 1.724 Proportion Var 0.310 0.287 Cumulative Var 0.310 0.597 The degrees of freedom for the model is 4 and the fit was 0.0572 Bill>-- William Revelle http://pmc.psych.northwestern.edu/revelle.html Professor http://personality-project.org/personality.html Department of Psychology http://www.wcas.northwestern.edu/psych/ Northwestern University http://www.northwestern.edu/