BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
lakshmishree
Fluorite | Level 6

Hey all,

I need to find 95% confidence intervals of correlation matrix for the first to fifth eigenvalues of a dataset (n=178). Where, I tried giving it using proc iml as below:

 

proc iml;
reset print;
eig=eigval(THC);
E=eigvec(THC);
alpha=0.05;
zright=quantile("Normal", 1-alpha/2);
CI_1_lower=eig[1]/(1+zright*sqrt(2/178));
CI_1_upper=eig[1]/(1-zright*sqrt(2/178));
CI_2_lower=eig[2]/(1+zright*sqrt(2/178));
CI_2_upper=eig[2]/(1-zright*sqrt(2/178));
CI_3_lower=eig[3]/(1+zright*sqrt(2/178));
CI_3_upper=eig[3]/(1-zright*sqrt(2/178));
CI_4_lower=eig[4]/(1+zright*sqrt(2/178));
CI_4_upper=eig[4]/(1-zright*sqrt(2/178));
CI_5_lower=eig[5]/(1+zright*sqrt(2/178));
CI_5_upper=eig[5]/(1-zright*sqrt(2/178));

 

I get only the following output but not intervals.

alpha 1 row 1 col (numeric)
0.05
zright 1 row 1 col (numeric)
1.959964

 

Can anyone tell me where am i going wrong?

 

Thanks,

lakshmi

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

Same problem as last time: You aren't reading the data. You have to read the data into IML before you can use it.

 

Try this:

proc iml;
use THC;
read all var _all_ into X;
close;

R = corr(X);
eig=eigval(R);
do i = 1 to 5;
  CI_lower=eig[i]/(1+1.645*sqrt(2/178));
  CI_upper=eig[i]/(1-1.645*sqrt(2/178));
  print i (eig[i])[L="eig"] CI_Lower CI_Upper;
end;

View solution in original post

11 REPLIES 11
SAS_Rob
SAS Employee

Do you have a SAS LOG you could post to see the code in context?

lakshmishree
Fluorite | Level 6
ERROR: (execution) Matrix has not been set to a value.

operation : CORR at line 75 column 7
operands : THC

THC 0 row 0 col (type ?, size 0)


statement : ASSIGN at line 75 column 1
76
77 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
89
Rick_SAS
SAS Super FREQ

Whenever you submit a SAS program, you should look at the log to make sure there are no errors. In this case, the log displays

 

3 eig=eigval(THC);
ERROR: (execution) Matrix has not been set to a value.

operation : EIGVAL at line 3 column 11
operands : THC

THC 0 row 0 col (type ?, size 0)

 

The error message tells you that the THC matrix (which is the argument to the EIGVAL function) is not defined. Before you try to use it, you need to read it from a data set or define it manually:

THC = {5 4 3 2 1 ,
       4 5 4 3 2 ,
       3 4 5 4 3 ,
       2 3 4 5 4 ,
       1 2 3 4 5 };


      

 

 

Jenkos
Calcite | Level 5

I'm also trying to do this. I've added read all var _all_ into x; to read the THC dataset into a matrix as below, how I'm getting the following errors: 

 

1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
72
73 proc iml;
NOTE: IML Ready
74 use THC;
75 reset print;
76 read all var _all_ into x;
77 eig=eigval(x);
ERROR: (execution) Matrix should be square.
 
operation : EIGVAL at line 77 column 11
operands : x
x 178 rows 14 cols (numeric)
 
statement : ASSIGN at line 77 column 1
78 e_vec=eigvec(x);
ERROR: (execution) Matrix should be square.
 
operation : EIGVEC at line 78 column 13
operands : x
x 178 rows 14 cols (numeric)
 
statement : ASSIGN at line 78 column 1
79 CI_1_lower=eig[1]/(1+1.645*sqrt(2/178));
ERROR: (execution) Matrix has not been set to a value.

 

proc iml;
use THC;
reset print;
read all var _all_ into x;
eig=eigval(x);
e_vec=eigvec(x);
CI_1_lower=eig[1]/(1+1.645*sqrt(2/178));
CI_1_upper=eig[1]/(1-1.645*sqrt(2/178));
CI_2_lower=eig[2]/(1+1.645*sqrt(2/178));
CI_2_upper=eig[2]/(1-1.645*sqrt(2/178));
CI_3_lower=eig[3]/(1+1.645*sqrt(2/178));
CI_3_upper=eig[3]/(1-1.645*sqrt(2/178));
CI_4_lower=eig[4]/(1+1.645*sqrt(2/178));
CI_4_upper=eig[4]/(1-1.645*sqrt(2/178));
CI_5_lower=eig[5]/(1+1.645*sqrt(2/178));
CI_5_upper=eig[5]/(1-1.645*sqrt(2/178));

Rick_SAS
SAS Super FREQ

Mathematically, you can only find eigenvalues/eigenvectors for a square matrix. This is what the error message is telling you. Apparently, you are passing in a 178 x 14 matrix.

 

ERROR: (execution) Matrix should be square.
 
operation : EIGVAL at line 77 column 11
operands : x
x 178 rows 14 cols (numeric)
Rick_SAS
SAS Super FREQ

In multivariate statistics (such as principal component analysis), it is useful to find the eigenvalues of a correlation or a covariance matrix. Is that what you are trying to do? If so, use

R = corr(X);

or

S = cov(X);

to get the correlation or covariance matrix and then analyze the eigenvalues of R or S.

lakshmishree
Fluorite | Level 6

you are right, I am here trying to find the eigenvalues of a correlation matrix which is 179*14 matrix. 

Tried running following command as you said, but got same errors.

 

proc princomp data=THC;
run;

/* 95% confidence intervals for the first to fifth eigenvalues */
proc iml;
reset print;
R=corr(THC);
eig=eigval(R);
E=eigvec(R);
alpha=0.05;
zright=quantile("Normal", 1-alpha/2);
CI_1_lower=eig[1]/(1+zright*sqrt(2/179));
CI_1_upper=eig[1]/(1-zright*sqrt(2/179));
CI_2_lower=eig[2]/(1+zright*sqrt(2/179));
CI_2_upper=eig[2]/(1-zright*sqrt(2/179));
CI_3_lower=eig[3]/(1+zright*sqrt(2/179));
CI_3_upper=eig[3]/(1-zright*sqrt(2/179));
CI_4_lower=eig[4]/(1+zright*sqrt(2/179));
CI_4_upper=eig[4]/(1-zright*sqrt(2/179));
CI_5_lower=eig[5]/(1+zright*sqrt(2/179));
CI_5_upper=eig[5]/(1-zright*sqrt(2/179));

 

 

 

 

ERROR: (execution) Matrix has not been set to a value.
 
operation : CORR at line 75 column 7
operands : THC
 
THC 0 row 0 col (type ?, size 0)
 
 
statement : ASSIGN at line 75 column 1
76
77 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
89
Rick_SAS
SAS Super FREQ

Same problem as last time: You aren't reading the data. You have to read the data into IML before you can use it.

 

Try this:

proc iml;
use THC;
read all var _all_ into X;
close;

R = corr(X);
eig=eigval(R);
do i = 1 to 5;
  CI_lower=eig[i]/(1+1.645*sqrt(2/178));
  CI_upper=eig[i]/(1-1.645*sqrt(2/178));
  print i (eig[i])[L="eig"] CI_Lower CI_Upper;
end;
lakshmishree
Fluorite | Level 6
Now i am able to print the Confidence interval values. Whereas, eigenvalues seems to be different from what i obtained. i Shall copy that code and the output i obtained from both:

SAS code:
proc princomp data=THC;
var chem1 chem2 chem3 chem4 chem5 chem6 chem7 chem8 chem9 chem10 chem11 chem12 chem13;
run;

SAS output:
Eigenvalue
1 4.70585025
2 2.49697373
3 1.44607197
4 0.91897392
5 0.85322818

Whereas when i am trying to run your code i obtain different eigenvalues which is actually of correlation matrix again, since i have "varieties" as another variable in the data and by reading all here, i assume it's including even that variable in it. Could you help me with what changes i would read only selected variables? that is if i want to read only variables : chem1 chem2 chem3 chem4 chem5 chem6 chem7 chem8 chem9 chem10 chem11 chem12 chem13
IanWakeling
Barite | Level 11

You can modify the READ statement like this:

  use THC;
  read all var ('chem1':'chem13') into X;
  close THC;
lakshmishree
Fluorite | Level 6

Thank you, it worked

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 11 replies
  • 1995 views
  • 5 likes
  • 5 in conversation