如何在SAS中使用PROC IML构建协方差矩阵

时间:2018-02-16 15:10:01

标签: sas

我在SAS写作,我正在尝试从我的数据中创建一个协方差矩阵:

PROC IML;
     USE nydata;
     varNames = {"aa10i" "ac10a" "ba10" "bc20" "ca10" "sex" "cityrur" "edu3" "hinc3rel" "age" "ga10j" "ca10bin"};
     READ ALL INTO X;       *read X1 & X2 to X matrix;
     N = NROW(X);           *N = number of observation: NROW is the number of row;
     ONE = J(N,1,1);        *J(nrow,ncol, value) i.e. 12x1 vector containing ones: J rprecents a function to creat matrix of a given dimention;
     A=ONE`;                *vector transpose, it can be also writen as T(ONE) ;
     C= A*X;                *will give 1x2 matrix, wtih a summed value of each columens;
     MEAN = C/N;            *matric containg means;
     MEANS = ONE*MEAN;
     XM= X-MEANS;           *Corrected mean matrix;
     SSCPM=XM`*XM;          *sume of squares and cross products matrix;
     DF=N-1;                *degree of freedom;
     S=SSCPM/DF;            *Covariance matrix;
     D=DIAG(S);             *taking only the individual variances;
     XS=XM*SQRT(INV(D));    *XS,standardized data,SQRT stands of squear root and INV, standes to inverted matrix;
     R=t(XS)*XS/(N-1);      *computing the coorelation matrix;
     GV=DET(S);              * computing from the determinant of the covariance matrix;


     create kovarians from S[colname={"aa10i" "ac10a" "ba10" "bc20" "ca10" "sex" "cityrur" "edu3" "hinc3rel" "age" "ga10j" "ca10bin"}
rowname={"aa10i" "ac10a" "ba10" "bc20" "ca10" "sex" "cityrur" "edu3" "hinc3rel" "age" "ga10j" "ca10bin"}]; /** create data set **/
     append from S;
     close S;
 QUIT;

我的数据看起来像这样。没有列,而不是你看到的列。

enter image description here

我收到以下错误消息:

590       append from S;
ERROR: Number of columns in S does not match with the number of variables in the data set.

 statement : APPEND at line 590 column 6
591       close S;
NOTE: Cannot close WORK.S; it is not open.
592   QUIT;
NOTE: Exiting IML.

有人请你告诉我我做错了什么?

0 个答案:

没有答案