## Factor analysis using maximum likelihood estimation

Solved
Occasional Contributor
Posts: 16

# Factor analysis using maximum likelihood estimation

[ Edited ]

I want to know the program running behind factor analysis maximum likelihood extraction method. Can i get the inbuilt algorithm for that so that i can verify my result mannually?

my program is given bellow:

start mlfa;

s = r_samp;/* r_samp is my generated correlation matrix data*/

/* objective function module */

start objfun (x) global(s, k, p, xpsy, sigma, lambda, omega, f);

xpsy = diag(1/sqrt(x));

A = xpsy*s*xpsy;

Omega = (eigvec(A)) [,1:k];

theta = (eigval(A)) [1:k];

lambda = diag(sqrt(x))*omega*diag(sqrt(abs(theta-1)));

sigma = lambda*t(lambda)+diag(x);

f = trace(inv(sigma)*s)- log((inv(sigma)*s)- p;

return(f);

finish;

invpsy = inv(diag(x));

g = (vecdiag(invpsy*(sigma-s)*invpsy))`;

return(g);

finish;

/* starting values for iteration */

p = ncol(s);

x = log(((1-k/(2#p))#(1/vecdiag(inv(s))));

/* Set up Options and Constraints */

option = {0 0 . 4};

con = j(1,p, 0.00001)//j(1,p,.);

call nlpqn(rc, xr, "objfun", x, option, con) grd = "gradfun";

r_star = lambda*lambda`;

h_vec = vecdiag(r_star);

all_e_vec = eigvec(R_star);

e_vec = all_e_vec[,1:k];

finish;

start mlfa_extract;

run mlfa;

if rc < 0 then m_rc0 = 1; else m_rc0 = 0;

if rc = 3 then m_rc3 = 1; else m_rc3 = 0;

if rc = 6 then m_rc6 = 1; else m_rc6 = 0;

quit;

Is it correct or i need to do any change for mle?

Accepted Solutions
Solution
‎01-12-2017 06:07 AM
SAS Super FREQ
Posts: 4,239

## Re: Factor analysis using maximum likelihood estimation

This might be a good time to mark this problem "Solved". If you have another problem, open a new thread.

All Replies
SAS Super FREQ
Posts: 4,239

## Re: Factor analysis using maximum likelihood estimation

1. The discussion forum usually doesn't say whether a program is correct. If you are having a programming problem, we help you with that problem. Is there a particular part of the program that you want help with?

2. Do you have a reference for the computations you are attempting? It is hard to assess a program without knowing the mathematical formulas it is trying to solve.

3. Do you have sample data that we can use?

4. I would eliminate the  MLFA  module and just put the data definitions and the call to NLPQN as statements in the main program.

5. The MLFA_EXTRACT module seems to be unfinished.

Overall it looks like you are off to a  good start. If you haven't read the article "Maximum likelihood estimation in SAS/IML," that is a good article to read.

Occasional Contributor
Posts: 16

## Re: Factor analysis using maximum likelihood estimation

Sir,

I am doing ML estimation of factor analysis. I am attaching ibm spss calculation for ML in factor analysis.

I am running my program on manipulated data having 10 variables for samplesize 30 and pre assumed existance of 2 factors. and if i compare my results with sas university edition inbuilt task program of factor analysis  then i am getting different results.

so i need to correct my mle function and for that i need help.

SAS Super FREQ
Posts: 4,239

## Re: Factor analysis using maximum likelihood estimation

The tasks in the SAS University Edition generate SAS code, probably by submitting code for PROC FACTOR.

We need to see the SAS code to know what options you have selected for the algorithm because there are many options in PROC FACTOR, and eash option leads to different results.

Thus the first step should be to look at the SAS code that is generated by the task. Click on the CODE tab to see the code. Then look at the PROC FACTOR documentation to determine how the options affect the factor analysis.

It seems like this isn't really a SAS/IML question. It is really a question about understanding what PROC FACTOR code is being submitted when you use the "Factor Analysis" task in SAS UE.

Occasional Contributor
Posts: 16

## Re: Factor analysis using maximum likelihood estimation

Yes sir

I am trying to use the same algorithm which is used by sas UE in   proc factor method= ml.

but even in sas documentation, i am unable to find ml method steps.

so if anyone can give me proc factor method=ml inbuilt calculation steps (software inbuilt calculation algorithm) then it would be a great help. because I am developing my simulated data and on that I can't call sas UE proc factor option directly, so i need to go for all sas statements which produces proc factor method= ml output.

SAS Super FREQ
Posts: 4,239

## Re: Factor analysis using maximum likelihood estimation

You say

> even in sas documentation, i am unable to find ml method steps

The SAS documentation says  that method=ML "performs maximum likelihood factor analysis with an algorithm due to Fuller (1987)" and gives the reference  Fuller, W. A. (1987). Measurement Error Models. New York: John Wiley & Sons.

You say

>  I am developing my simulated data and on that I can't call sas UE proc factor option directly

Yes, you can. It is easy to simulate data and call PROC FACTOR. Look for my many blog articles on simulating data or my book Simulating Data with SAS (2013) for many examples of calling SAS procedres on simulated data. Good ones to start with are

SAS Super FREQ
Posts: 4,239

## Re: Factor analysis using maximum likelihood estimation

> if anyone can give me proc factor method=ml inbuilt calculation steps (software inbuilt calculation algorithm) then it would be a great help.

Just to be clear, there is no such thing. To run the algorithm you run PROC FACTOR.  Since you are posting this question to the SAS./IML group, I will point out that you can call SAS procedures from SAS/IML programs.

Occasional Contributor
Posts: 16

## Re: Factor analysis using maximum likelihood estimation

Thank you so much sir.

I am able to use now proc factor in my simulation program.

One more question can i give name as say matrix 'samp' to only the outcome of mle extraction - rotated factor pattern matrix?

and how can i use  and save that samp in further program?

once again thanks.

SAS Super FREQ
Posts: 4,239

## Re: Factor analysis using maximum likelihood estimation

I'm sorry, but I do not understand what you are asking. Can you provide a simple example?

In SAS/IML, you can specify the names of SAS data sets at run time. You use optional arguments in the SUBMIT statement to pass data set names to PROC FACTOR.

Occasional Contributor
Posts: 16

## Re: Factor analysis using maximum likelihood estimation

[ Edited ]

Hello sir

I am attachong one output file which contains one highlited rotated pattern matrix.

In the program after calculating till this step i nedd to calculate Bias which is A1(the first matrix in that file) minus that rotated matrix and then i have to go for further calculation in same manner on rotated pattern matrix.

So I just need rotated pattern matrix('Samp') in further calculation out of all other ml outcomes.  the ml procedure which I have used in the program is as follows.

proc factor data=WORK.data1 method=ml rotate=varimax noint nfactors=2
norm=kaiser heywood plots=(scree) outstat=WORK.Factor_ml1;
var col1 col2 col3 col4 col5 col6 col7 col8 col9 col10;
run;

How to just save the rotteted pattern like A1. so that i can find bias. Even I am attaching one excel sheet how to find mannually bias and its average.

SAS Super FREQ
Posts: 4,239

## Re: Factor analysis using maximum likelihood estimation

You can use the ODS OUTPUT statement to create a SAS data set from any output from any SAS procedure. The steps are

1) Put ODS TRACE ON; before you run  PROC FACTOR.

2) Run PROC FACTOR. In the SAS lof you will see the names of all ODS tables that were created. The one that you want appears to be OrthRotFactPat.

3) Put ODS OUTPUT OrthRotFactPat=RotFactPattern; before the PROC FACTOR step. Rerun. The data set RotFactPattern contains the information you want. You can use PROC PRINT and PROC CONTENTS to figure out names of variables.

Altogether, it will look like this example:

``````ods trace on;
proc factor data=sashelp.cars method=ml rotate=varimax noint nfactors=2
norm=kaiser heywood plots=(scree) outstat=WORK.Factor_ml1;
/* use all numeric vars */
run;
ods trace off;

/* from the SAS log, it looks like the name of the table
we want is OrthRotFactPat */
ods output OrthRotFactPat=RotFactPattern;
proc factor data=sashelp.cars method=ml rotate=varimax noint nfactors=2
norm=kaiser heywood plots=(scree) outstat=WORK.Factor_ml1;
run;

proc print data=RotFactPattern; run;

``````

Occasional Contributor
Posts: 16

## Re: Factor analysis using maximum likelihood estimation

Thank you so much sir

I am able to use Ods trace in my program and getting that rotated factor matrix in my output.

further can you please guide me how to find Bias which is difference between A1 and rotated factor matrix.

In the last post I have attached mannual excel file for this. after calculating rotated factor pattern matix for the generated sample, I need to compare the original population factor matrix A1 and Mle method generated factor matix using comparision parameters like Bias, Mean square Error, Phi, Factor loading sensitivity and specificity. so if I am able to do calculation for bias may be I will try for other parameters in same manner.

SAS Super FREQ
Posts: 4,239

## Re: Factor analysis using maximum likelihood estimation

If the population parameters are in the matrix A1 in a SSA/IML program, then

1) Read the RotFactPattern matrix into the same SAS/IML session.

use RotFactPattern;

read all var {Factor1 Factor2} into R;   / * matrix R contains rotated factors */

close RotFactPattern;

2) Compute the statistics. For example, if bias is the  different between the parameters and the estimates, then

Bias = A1 -  R;

I feel confident that if you work at this that you will succeed. Best wishes and good luck to you.

Occasional Contributor
Posts: 16

## Re: Factor analysis using maximum likelihood estimation

[ Edited ]

Hello sir

I have first developed the population matrix. Then I derived sample correlation matrix and using CREATE converted into work.samp.data  and used proc factor process by using submit and endsubmit statements on that data.. Only  rotated factor matrix is used as work.samp_ml.data and on that I used different comparison parameters of rotated matrix and pop matrix A1.

Now I want to replicate the same process say for 1000 sample correlation matrices  and then I want to take up average of all comparision parameters.

While doing the replication in proc iml  I am getting following error:

ERROR: You cannot open WORK.SAMP_ML.DATA for output access with member-level control because WORK.SAMP_ML.DATA is in use by  you in resource environment IML (2).

statement : SUBMIT at line....column 1

Can you help me to solve this?

SAS Super FREQ
Posts: 4,239

## Re: Factor analysis using maximum likelihood estimation

That error message will appear if you open a data set, do not close it, and then try to open it again.  Use the CLOSE statement, like this:

do i = 1 to 10;

use samp_ml;      /* open the data set */

read all var _num_ into X;

CLOSE samp_ml;  /* <== this is the statement you need */

end;

☑ This topic is solved.