⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 macro.txt

📁 宏
💻 TXT
📖 第 1 页 / 共 3 页
字号:
 /    3.  Renames the columns to p1 - pA, where A is the    /
 /        number of PLS components in the final model.      /
 /  Variables:                                              /
 /      DSOUTMOD - Name of the OUTMODEL data set produced   /
 /                 by proc PLS.                             /
 /      DSXLOAD -  Name of the data set to contain the      /
 /                 X-loadings as variables.                 /
 /  Required Global Variable:                                /
 /      %XVARS -   Macro variable containing the names of    /
 /                 the X-variables in a string with space    /
 /                 delimiters.                               / 
 ***********************************************************/

data &dsxload; set &dsoutmod(keep=_TYPE_ &xvars);
  if _TYPE_='PQ' then output;

proc transpose data=&dsxload out=&dsxload; run;

data &dsxload; set &dsxload;
  n=_N_;
run;

%do i=1 %to &lv;

  data &dsxload; set &dsxload;
    rename col&i=p&i;
  run;

%end;

%mend;

%macro pltxload(ds,
                max_lv=&lv);

 /************************************************************
 /  Plots X-loadings for a given number of PLS components    /
 /  vs. those of the preceding PLS component.                /
 /  Variables:                                               /
 /      DS -       Name of the data set containing the       /
 /                 loadings as variables p1-pA, where A=LV,  /
 /                 the number of PLS components, and a       /
 /                 character variable _NAME_ containing the  /
 /                 X-variable names.                         /
 /      MAX_LV -   Number of the last PLS component to have  /
 /                 its loadings plotted.                     /
 /  Required Global Variable:                                /
 /      %XVARS -   Macro variable containing the names of    /
 /                 the X-variables in a string with space    /
 /                 delimiters.                               /
 ************************************************************/

 /***********************************************************
 /  Determine the largest label to be put on plot           /
 ***********************************************************/

%let name_len=1;

data _NULL_; set &ds;
  call symput('num_x',_N_);
run;

%do i=1 %to &num_x;
  %let temp=%scan(&xvars,&i,%str( ));
  %if %length(&temp)>&name_len %then %do;
    %let name_len=%length(&temp);
  %end;
%end;

 /***********************************************************
 /  Plot X-loadings for each PLS component                  /
 ***********************************************************/

%do i=1 %to %eval(&max_lv - 1);

  %let j=%eval(&i+1);

  data pltanno;             *** Annotation Data Set for Plot ***;
    length text $ &name_len;
    retain function 'label' position '5' hsys '3' xsys '2' ysys '2' ;
    set &ds;
    text=%str(_name_); x=p&i; y=p&j;
  run;

  axis1 label=(angle=270 rotate=90 "X loading &j")
        major=(number=5) minor=none;

  axis2 label=("X-loading &i") minor=none;

  proc gplot data=&ds;
    plot p&j*p&i/anno=pltanno vaxis=axis1 haxis=axis2 frame;
    symbol1 v=none i=none;
  run;

%end;

%mend;

%macro getyload(dsoutmod,
                dsyload=yloads);

 /***********************************************************
 / Gets Y-loadings q from OUTMODEL data set:                /
 /    1.  Gets appropriate section of OUTMODEL data set.    /
 /    2.  Transposes it so the q's are column vectors.      /
 /    3.  Renames the columns to q1 - qA, where A is the    /
 /        number of latent variables in the final model.    /
 /  Variables:                                              /
 /      DSOUTMOD - Name of the OUTMODEL data set produced   /
 /                 by proc PLS.                             /
 /      DSYLOAD -  Name of the data set to contain the      /
 /                 Y-loadings as variables.                 /
 /  Required Global Variable:                               /
 /      %YVARS -   Macro variable containing the names of   /
 /                 the Y-variables in a string with space   /
 /                 delimiters.                              /
 ***********************************************************/

data &dsyload; set &dsoutmod(keep=_TYPE_ _LV_ &yvars);
  if _TYPE_='PQ' then output;

proc transpose data=&dsyload out=&dsyload; run;

data &dsyload; set &dsyload;
  if _NAME_='_LV_' then delete;
run;

%do i = 1 %to &lv;

  data &dsyload; set &dsyload;
    rename col&i=q&i;
  run;

%end;

%mend;

%macro plt_y_lv(dsoutmod);

 /***********************************************************
 /  Plots Y-loadings for each Y-variable versus the PLS     /
 /  component.                                              /
 /  Variable:                                               /
 /      DSOUTMOD - The OUTMODEL data set from proc PLS.     /
 /  Required Global Variable:                               /
 /      %YVARS -   Macro variable containing the names of   /
 /                 the Y-variables in a string with space   /
 /                 delimiters.                              /
 ***********************************************************/

data dsyload; set &dsoutmod(keep=_TYPE_ _LV_ &yvars);
  if _TYPE_='PQ' then output;

goptions reset=symbol;

axis1 label=(angle=270 rotate=90 'Y loading')
      major=(number=5) minor=none;
axis2 label=('PLS Component') order=(1 to &lv by 1) minor=none;

proc gplot data=dsyload;
  plot (&yvars)*_LV_/overlay legend vaxis=axis1 haxis=axis2
                     vref=0 lvref=2 frame;
run;

%mend;

%macro pltyload(ds,
                max_lv=&lv);

 /************************************************************
 /  Plots Y-loadings for a given number of PLS components    /
 /  vs. those of the preceding PLS component.                /
 /  Variables:                                               /
 /      DS -       Name of the data set containing the       /
 /                 loadings as variables q1-qA, where A=LV,  /
 /                 the number of PLS components, and a       /
 /                 character variable _NAME_ containing the  /
 /                 Y-variable names.                         /
 /      MAX_LV -   Number of the last PLS component to have  /
 /                 its loadings plotted.                     /
 /  Required Global Variable:                                /
 /      %YVARS -   Macro variable containing the names of    /
 /                 the Y-variables in a string with space    /
 /                 delimiters.                               /
 ************************************************************/

 /***********************************************************
 /  Determine the largest label to be put on plot           /
 ***********************************************************/

data _NULL_; set &ds;
  call symput('num_y',_N_);
run;

%let name_len=1;

%do i=1 %to &num_y;
  %let temp=%scan(&yvars,&i,%str( ));
  %if %length(&temp)>&name_len %then %do;
    %let name_len=%length(&temp);
  %end;
%end;

 /***********************************************************
 /  Plot Y-loadings for each PLS component                  /
 ***********************************************************/

%do i=1 %to %eval(&max_lv-1);

  %let j=%eval(&i+1);

  data pltanno;                *** Annotation Data Set for Plot ***;
    length text $ &name_len;
    retain function 'label' position '5' hsys '3' xsys '2' ysys '2' ;
    set &ds;
    text=%str(_NAME_); x=q&i; y=q&j;
  run;

  axis1 label=(angle=270 rotate=90 "Y loading &j")
        major=(number=5) minor=none;

  axis2 label=("Y-loading &i") minor=none;

  proc gplot data=&ds;
    plot q&j*q&i/anno=pltanno vaxis=axis1 haxis=axis2;
    symbol1 v=none i=none;
  run;

%end;

%mend;

%macro pltxywts(dsxwts,
               dsyloads,
               norm=1,
               max_lv=&lv);

 /************************************************************
 /  Plots X-weights and Y-loadings on the same axes for a    /
 /  given number of PLS components vs. those of the          /
 /  preceding PLS component.                                 /
 /  Variables:                                               /
 /      DSXWTS -   Name of the data set containing the X-    /
 /                 weights as variables w1-wA, where A=LV,   /
 /                 the number of PLS components, and a       /
 /                 character variable _NAME_ containing the  /
 /                 X-variable names.                         /
 /      DSYLOADS - Name of the data set containing the Y-    /
 /                 loadings as variables q1-qA, where A=LV,  /
 /                 the number of PLS components, and a       /
 /                 character variable _NAME_ containing the  /
 /                 Y-variable names.                         /
 /      NORM -     Indicates whether to normalize the        /
 /                 X-weights and Y-loadings or not.          /
 /                 (1=Yes, 0=No)                             /
 /      MAX_LV -   Number of the last PLS component to have  /
 /                 its weights plotted.                      /
 /  Required Global Variables:                               /
 /      %XVARS -   Macro variable containing the names of    /
 /                 the X-variables in a string with space    /
 /                 delimiters.                               /
 /      %YVARS -   Macro variable containing the names of    /
 /                 the Y-variables in a string with space    /
 /                 delimiters.                               /
 ************************************************************/

 /***********************************************************
 /  Determine the largest label to be put on plot           /
 ***********************************************************/

data _NULL_; set &dsxwts;
  call symput('num_x',_N_);
run;

data _NULL_; set &dsyloads;
  call symput('num_y',_N_);
run;

%let name_len=1;

%do i=1 %to &num_x;
  %let temp=%scan(&xvars,&i,%str( ));
  %if %length(&temp)>&name_len %then %do;
    %let name_len=%length(&temp);
  %end;
%end;

%let nameleny=1;

%do i=1 %to &num_y;
  %let temp=%scan(&yvars,&i,%str( ));
  %if %length(&temp)>&nameleny %then %do;
    %let nameleny=%length(&temp);
  %end;
%end;

%if &name_len < &nameleny %then %let name_len = &nameleny;

 /***********************************************************
 /  Normalize weights if desired                            /
 ***********************************************************/

%if %eval(&norm) %then %do;

proc iml;
  use &dsxwts;
  read all var ("w1":"w&max_lv") into W;
  use &dsyloads;
  read all var ("q1":"q&max_lv") into Q;
  W=W#sqrt(1/W[##,]);  *** Normalize X-weights ***;
  Q=Q#sqrt(1/Q[##,]);  *** Normalize Y-loadings ***;
  w_col=("WQ1":"WQ&max_lv");
  _NAME_={&xvars};
  create dsxwts from W[colname=w_col rowname=_NAME_];
  append from W[rowname=_NAME_];
  q_col=("WQ1":"WQ&max_lv");
  _NAME_={&yvars};  
  create dsyloads from Q[colname=q_col rowname=_NAME_];
  append from Q[rowname=_NAME_];
quit;  
  
%end;

%else %do;

data dsxwts; set &dsxwts;

data dsyloads; set &dsyloads;

%end;


 /***********************************************************
 /  Plot X-weights and Y-loadings for each PLS component    /
 ***********************************************************/

%if &name_len>&nameleny %then %do;
  data ds; set dsxwts dsyloads;
%end;
%else %do;
  data ds; set dsyloads dsxwts;
%end;

%do i=1 %to %eval(&max_lv-1);

  %let j=%eval(&i+1);

  data wt_anno;              *** Annotation Data Set for Plot ***;
    length text $ &name_len;
    retain function 'label' position '5' hsys '3' xsys '2' ysys '2' ;
    set ds;
    text=%str(_name_); x=wq&i; y=wq&j;
  run;

  axis1 label=(angle=270 rotate=90 "Component &j Weight")
        major=(number=5) minor=none;

  axis2 label=("Component &i Weight") minor=none;

  proc gplot data=ds;
    plot wq&j*wq&i/anno=wt_anno vaxis=axis1 haxis=axis2 frame
                              vref=0 href=0;
    symbol1 v=none i=none;
  run;

%end;

%mend;

%macro get_bpls(dsoutmod,
                dsout=bpls);

 /************************************************************
 /  Gets B(PLS), the matrix of PLS regression coefficients   /
 /  of Y on X.  For each Y, the values represent the         /
 /  importance of each X-variable in the modeling of the     /
 /  corresponding Y-variable.                                /
 /  Variables:                                               /
 /      DSOUTMOD - Name of the OUTMODEL data set produced    /
 /                 by proc PLS.                              /

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -