Scott Colwell
2015-Feb-23 18:15 UTC
[R] Extracting Factor Pattern Matrix Similar to Proc Factor
Hello, I am fairly new to R and coming from SAS IML. I am rewriting one of my MC simulations in R and am stuck on extracting a factor pattern matrix as would be done in IML using Proc Factor. I have found the princomp() command and read through the manual but can't seem to figure out how to save the factor pattern matrix. I am waiting for the R for SAS Users book to arrive. What I would use in SAS IML to get at what I am looking for is: PROC FACTOR Data=MODELCOV15(TYPE=COV) NOBS=10000 N=16 CORR OUTSTAT=FAC.FACOUT15; RUN; DATA FAC.PATTERN15; SET FAC.FACOUT15; IF _TYPE_='PATTERN'; DROP _TYPE_ _NAME_; RUN; Would any SAS IML to R converts be able to help me with this? Thanks, Scott Colwell, PhD -- View this message in context: http://r.789695.n4.nabble.com/Extracting-Factor-Pattern-Matrix-Similar-to-Proc-Factor-tp4703704.html Sent from the R help mailing list archive at Nabble.com.
Scott Colwell
2015-Feb-23 21:33 UTC
[R] Extracting Factor Pattern Matrix Similar to Proc Factor
Thanks David. What do you do when the input is a covariance matrix rather than a dataset? -- View this message in context: http://r.789695.n4.nabble.com/Extracting-Factor-Pattern-Matrix-Similar-to-Proc-Factor-tp4703704p4703719.html Sent from the R help mailing list archive at Nabble.com.
David L Carlson
2015-Feb-23 21:34 UTC
[R] Extracting Factor Pattern Matrix Similar to Proc Factor
The pattern matrix is easy to compute from the results of princomp(). First we need a reproducible example so we'll use the iris data set (use ?iris for details) that comes with R.> data(iris) > iris.pc <- princomp(iris[,-5], cor=TRUE) > print(iris.pc$loadings, cutoff=0)Loadings: Comp.1 Comp.2 Comp.3 Comp.4 Sepal.Length 0.521 -0.377 0.720 0.261 Sepal.Width -0.269 -0.923 -0.244 -0.124 Petal.Length 0.580 -0.024 -0.142 -0.801 Petal.Width 0.565 -0.067 -0.634 0.524 Comp.1 Comp.2 Comp.3 Comp.4 SS loadings 1.00 1.00 1.00 1.00 Proportion Var 0.25 0.25 0.25 0.25 Cumulative Var 0.25 0.50 0.75 1.00 The object iris.pc is a list with 7 elements. One of those, iris.pc$loadings contains the standardized loadings so that the sum of the squared values in each column is 1. The default print method suppresses the printing of small loadings (< .1) so I've set cutoff=0 so we see them all. To get the pattern matrix we just need to multiple each of the columns by iris.pc$sdev (the square roots of the eigenvalues):> iris.pat <- sweep(iris.pc$loadings, 2, iris.pc$sdev, "*") > print(iris.pat, cutoff=0)Loadings: Comp.1 Comp.2 Comp.3 Comp.4 Sepal.Length 0.890 -0.361 0.276 0.038 Sepal.Width -0.460 -0.883 -0.094 -0.018 Petal.Length 0.992 -0.023 -0.054 -0.115 Petal.Width 0.965 -0.064 -0.243 0.075 Comp.1 Comp.2 Comp.3 Comp.4 SS loadings 2.918 0.914 0.147 0.021 Proportion Var 0.730 0.229 0.037 0.005 Cumulative Var 0.730 0.958 0.995 1.000> iris.pc$sdev^2Comp.1 Comp.2 Comp.3 Comp.4 2.91849782 0.91403047 0.14675688 0.02071484 The sweep() function multiplies each column by its standard deviation. Now the sums of the squared values in each column sum to the eigenvalue. Alternatively, you can install the "psych" package which computes the pattern (structure) matrix directly:> library(psych) > iris.pca <- principal(iris[,-5], nfactors=4, rotate="none") > print(iris.pca$Structure, cutoff=0)------------------------------------- David L Carlson Department of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Scott Colwell Sent: Monday, February 23, 2015 12:15 PM To: r-help at r-project.org Subject: [R] Extracting Factor Pattern Matrix Similar to Proc Factor Hello, I am fairly new to R and coming from SAS IML. I am rewriting one of my MC simulations in R and am stuck on extracting a factor pattern matrix as would be done in IML using Proc Factor. I have found the princomp() command and read through the manual but can't seem to figure out how to save the factor pattern matrix. I am waiting for the R for SAS Users book to arrive. What I would use in SAS IML to get at what I am looking for is: PROC FACTOR Data=MODELCOV15(TYPE=COV) NOBS=10000 N=16 CORR OUTSTAT=FAC.FACOUT15; RUN; DATA FAC.PATTERN15; SET FAC.FACOUT15; IF _TYPE_='PATTERN'; DROP _TYPE_ _NAME_; RUN; Would any SAS IML to R converts be able to help me with this? Thanks, Scott Colwell, PhD -- View this message in context: http://r.789695.n4.nabble.com/Extracting-Factor-Pattern-Matrix-Similar-to-Proc-Factor-tp4703704.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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.