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
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;
Do you have a SAS LOG you could post to see the code in context?
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 };
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:
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));
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.
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.
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));
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;
You can modify the READ statement like this:
use THC;
read all var ('chem1':'chem13') into X;
close THC;
Thank you, it worked
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.