Extracting Factor Pattern Matrix Similar to Proc Factor

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Extracting Factor Pattern Matrix Similar to Proc Factor

Scott Colwell
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
Reply | Threaded
Open this post in threaded view
|

Re: Extracting Factor Pattern Matrix Similar to Proc Factor

David Carlson
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^2
    Comp.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:[hidden email]] On Behalf Of Scott Colwell
Sent: Monday, February 23, 2015 12:15 PM
To: [hidden email]
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.

______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Extracting Factor Pattern Matrix Similar to Proc Factor

Scott Colwell
Thanks David. What do you do when the input is a covariance matrix rather than a dataset?
Reply | Threaded
Open this post in threaded view
|

Re: Extracting Factor Pattern Matrix Similar to Proc Factor

David Carlson
Function principal() in psych takes a correlation matrix so use cov2cor() to convert:

library(psych)
iris.pca <- principal(cov2cor(cov(iris[,-5])), nfactors=4, rotate="none")
print(iris.pca$Structure, cutoff=0)

David
-----Original Message-----
From: R-help [mailto:[hidden email]] On Behalf Of Scott Colwell
Sent: Monday, February 23, 2015 3:34 PM
To: [hidden email]
Subject: Re: [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.

______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Extracting Factor Pattern Matrix Similar to Proc Factor

William Revelle
David and Scott,
 principal will also take a covariance matrix (set the cover option to TRUE)


library(psych)
C <- cov(iris[-5])
pc4 <- principal(C,4,covar=TRUE,rotate="none”)

However, in the case of no rotation or orthogonal rotations, the structure matrix and the pattern matrix are identical.

They differ only if you take an oblique solution.
So,

pc4 #will give you the results
print(pc4$loadings,cutoff=0) #will give the loadings (pattern)
print(pc4$Structure,cutoff=0)   #will give the structure matrix

If the input is a covariance matrix, and you want to do the analysis on the correlation matrix, principal does that automatically.

C <- cov(iris[-5])
pc4 <- principal(C,4,rotate="none”)
pc4

Bill



> On Feb 23, 2015, at 3:58 PM, David L Carlson <[hidden email]> wrote:
>
> Function principal() in psych takes a correlation matrix so use cov2cor() to convert:
>
> library(psych)
> iris.pca <- principal(cov2cor(cov(iris[,-5])), nfactors=4, rotate="none")
> print(iris.pca$Structure, cutoff=0)
>
> David
> -----Original Message-----
> From: R-help [mailto:[hidden email]] On Behalf Of Scott Colwell
> Sent: Monday, February 23, 2015 3:34 PM
> To: [hidden email]
> Subject: Re: [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.
>
> ______________________________________________
> [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.
>

William Revelle           http://personality-project.org/revelle.html
Professor           http://personality-project.org
Department of Psychology   http://www.wcas.northwestern.edu/psych/
Northwestern University   http://www.northwestern.edu/
Use R for psychology             http://personality-project.org/r
It is 3 minutes to midnight   http://www.thebulletin.org

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Extracting Factor Pattern Matrix Similar to Proc Factor

Scott Colwell
Thanks everyone