从矩阵SAS中选择​​相关性最高的对

时间:2018-11-13 11:22:24

标签: arrays select macros sas select-into

我有一个像这样的数据集

data have;
    do i = 1 to 1000;
    y = ranuni(0);
    x1 = y ** 2;
    x2 = x1 ** 3;
    x3 = x2 - x1/2;
    output;
    end;
run;

我建立了这样一个相关矩阵:

proc corr
    data = have
    out = correlation_matrix
        (where = (_TYPE_ = "CORR"))
    noprint;
run;

我试图大声想出一些代码,这些代码可以实现与我想要的东西相似的语法,逻辑或正确的语法,但是我要去描述我要寻找的东西

proc sort
    data = correlation_matrix
    by _NAME_;
run;

data _temp;
    set correlation_matrix;
    array col[*] _numeric_;

    by _NAME_;

    do i = 1 to dim(col);
        if col(i) > 0.6 then do;
            %let list = append(vname(col));
    end;
run;

从相关矩阵中,我正在寻找一种方法来返回相关性为60%或高于某个阈值的对,然后将这些对用于构建像这样的散点图/直方图矩阵

proc contents;
    data = high_correlation_pairs
    out  = contents
    noprint;
run;

proc sort
    data = contents
    nodupkey;
    by name;
run;

proc sql noprint;
    select name INTO: highly_correlated_pairs
        separated by " "
            from contents
;
quit;

ODS GRAPHICS /
        IMAGEMAP=OFF;
OPTIONS VALIDVARNAME=ANY;
    PROC SGSCATTER 
        DATA=have;
        TITLE "Scatter Plot Matrix";
        FOOTNOTE;
        MATRIX &highly_correlated_pairs
                / 
                DIAGONAL=(HISTOGRAM  )
                START=TOPLEFT
                NOLEGEND
    ;
    RUN;
TITLE; FOOTNOTE;

我只是不确定如何从矩阵中选择一对相关度超过60%的变量,甚至可以通过 NAME 来返回corr超过60%的列

2 个答案:

答案 0 :(得分:0)

您可以获得这样的对-关键是vname函数,该函数返回数组元素的名称:

data high_corrs;
  set correlation_matrix;
  array coefs i--x3;
  length var1 var2 $32.;
  do j = 1 to dim(coefs);
    corr = coefs(j);
    if _n_ < j and corr > 0.6 then do;
      var1 = vname(coefs(_n_));
      var2 = vname(coefs(j));
      output;
    end;
  end;
  keep var1 var2 corr;
run;

也许您可以在那里解决其余的问题?

答案 1 :(得分:0)

编辑:包括完整答案:

PROC TRANSPOSE用于将相关矩阵转置为x,y对,并将子集转换为感兴趣的相关。创建一个宏变量以在PROC SGSCATTER中使用。

注意:PLOTREQUESTS = x1 * x2 x1 * y x2 * x3 x2 * y

data have;
   do i = 1 to 1000;
      y = ranuni(0);
      x1 = y ** 2;
      x2 = x1 ** 3;
      x3 = x2 - x1/2;
      output;
      end;
   run;
proc corr data=have out=corr noprint;
   run;
proc transpose name=with data=corr out=pair(where=(.6 le abs(col1) lt 1));
   where _type_ eq 'CORR';
   by _name_ notsorted;
   run;
data pairV / view=pairv;
   set pair;
   call sortc(_name_,with);
   run;
proc sort data=pairv out=pair2 nodupkey;
   by _name_ with;
   run;
proc sql noprint;
   select catx('*',_name_,with) into :plotrequests separated by ' ' from pair2;
   quit;
%put NOTE: &=plotrequests;
proc sgscatter data=have;
   plot &plotrequests;
   run;
   quit;

enter image description here