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

📄 macro.txt

📁 宏
💻 TXT
📖 第 1 页 / 共 3 页
字号:
 /      DSOUT -    Name of the data set to contain the       /
 /                 regression coefficients, with the         /
 /                 variables representing columns in         /
 /                 B(PLS), and one variable naming the       /
 /                 X-variable for each row of B(PLS).        /
 /  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.                               /
 /      %NUM_Y   - Number of response variables Y.           /
 ************************************************************/

data est_wb; set &dsoutmod;  if _TYPE_='WB' then output; run;
data est_pq; set &dsoutmod;  if _TYPE_='PQ' then output; run;

proc iml;
  use est_wb;
  read all var {&xvars} into w_prime;
  read all var {_Y_} into b;
  use est_pq;
  read all var {&xvars} into p_prime;
  read all var {&yvars} into q_prime;
  W=w_prime`;
  P=p_prime`;
  Q=q_prime`;
  B_PLS = W*inv(P`*W)*diag(b)*Q`;
  b_col=('B1':"B&num_y");
  x_var={&xvars};
  create &dsout from B_PLS[colname=b_col rowname=x_var];
  append from B_PLS[rowname=x_var];
quit;

%mend;

%macro plt_bpls(ds);

 /***********************************************************
 /  Plot the PLS predictor (regression) coefficients in     /
 /  B(PLS) vs. the frequency, for each response variable.   /
 /  Variables:                                              /
 /      DS -       Data set containing the columns of       /
 /                 B(PLS) as variables, as well as a        /
 /                 variable for the frequency.              /
 /  Required Global Variable:                               /
 /      %NUM_Y   - Number of response variables Y.          /
 ***********************************************************/

data &ds; set &ds;
  f=_n_;
run;

%let plotvars=%str( );

%do i=1 %to &num_y;
  %let plotvars=%str(&plotvars b&i);
%end;

axis1 label=(angle=270 rotate=90 'Coefficient')
      major=(number=5) minor=none;
axis2 label=('Frequency') minor=none;

proc gplot data=&ds;
  plot (&plotvars)*f / overlay legend vaxis=axis1 haxis=axis2
                       vref=0 lvref=2 frame;
  symbol1 v=none i=spline;
run;

%mend;

%macro get_vip(dsoutmod,
               dsvip=vip_data);

 /************************************************************
 /  Calculate VIP:  Variable Importance for the Projection.  /
 /  This represents the importance of each X-variable in     /
 /  the PLS modeling of both the X- and Y-variables.         /
 /  Variables:                                               /
 /      DSOUTMOD - Name of the OUTMODEL data set produced    /
 /                 by proc PLS.                              /
 /      DSVIP -    Name of the data set to contain the       /
 /                 variable named 'VIP' and the names of     /
 /                 X-variables.                              /
 /  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.                               /
 ************************************************************/

data dsxwts; set &dsoutmod(keep=_TYPE_ _LV_ &xvars);
  if _TYPE_='WB' then output;

data y_rsq; set &dsoutmod(keep=_LV_ _TYPE_ &yvars _Y_);
  if _TYPE_='V' then output;
  drop _TYPE_;
run;

data y_rsq; merge y_rsq dsxwts; by _LV_;
  if _LV_=0 then delete;
run;

proc iml;
  use y_rsq;
  read all var {_Y_} into rsq_y;
  read all var {&xvars} into w_prime;
  A=nrow(rsq_y);
  K=ncol(w_prime);
  W=w_prime`;
  Wnorm=W#(1/sqrt(W[##,]));
  if A > 1 then do;
    part_rsq=rsq_y-(0//rsq_y[1:(A-1),]);
    tot_rsq=rsq_y[A,];
    vip_sq=((Wnorm##2)*part_rsq)#(K/tot_rsq);
    VIP=sqrt(vip_sq);
  end;
  else VIP=Wnorm#sqrt(K); 
  x_var={&xvars};
  create &dsvip from VIP[colname='VIP' rowname=x_var];
  append from VIP[rowname=x_var];
quit;

%mend;

%macro plot_vip(ds);

 /************************************************************
 /  Plot the VIP:  Variable Importance for the Projection.   /
 /  Variables:                                               /
 /      DS -       Data set containing the frequencies       /
 /                 the VIP for each frequency.               /
 ************************************************************/

data &ds; set &ds;
  f=_N_;
run;

axis1 label=(angle=270 rotate=90 'VIP')
      major=(number=10) minor=none;
axis2 label=('Frequency') minor=none;

proc gplot data=&ds;
  plot vip*f / overlay vaxis=axis1 haxis=axis2 vref=0.8 lvref=2
               frame;
  symbol1 v=none i=join;
run;

%mend;


%macro get_dmod(dsoutput,
                dsdmod=dmod,
                qresname=qres,
                id=n);

 /************************************************************
 /  Calculate the distance from each data point to the model /
 /  in both the X-space (DMODX) and in the Y-space (DMODY).  /
 /  Variables:                                               /
 /      DSOUTPUT - OUTPUT data set from proc PLS.            /
 /      DSDMOD -   Data set to contain the distances to      /
 /                 the model.                                /
 /      QRESNAME - Suffix of variable names for XQRES and    /
 /                 YQRES assigned by the user in the         /
 /                 proc PLS OUTPUT statement.                /
 /      ID -       Observation identification variable       /
 /                 in input data set.                        /
 /  Required Global Variable:                                /
 /      %LV -      Number of Latent Variables (PLS factors)  /
 ************************************************************/
  
data trn_out; set &dsoutput;
  if y&qresname ^= . then output;
run;

proc means data=trn_out noprint;
  var xqres;
  output out=outmeans n=n mean=xqres_mn;
run;

data _NULL_; set outmeans;
  call symput('num_trn',n);
  call symput('xqres_mn', xqres_mn);
run;

proc iml;
  use &dsoutput;
  read all var {x&qresname} into xqres;
  read all var {y&qresname} into yqres;
  read all var{&id} into id;
  dmodx=sqrt(xqres/&xqres_mn);
  
  do i=1 to nrow(xqres);
    if yqres[i]=. then 
       dmodx[i]=dmodx[i]/sqrt(&num_trn/(&num_trn-&lv-1));
  end;
  
  dmody=sqrt(yqres*(&num_trn/(&num_trn-&lv-1)));
  dmodboth=id||dmodx||dmody;
  col={&ID DMODX DMODY};
  create &dsdmod from dmodboth[colname=col];
  append from dmodboth;
quit;
  
%mend;

%macro cont_scr(est,
                out,
                dsout,
                obsnum,
                idvar=n,
                a=1);

  /************************************************************
  /  Calculates and plots the contribution of each X-variable /
  /  to an X-score for a particular observation.  This is     /
  /  useful in diagnosing the cause of an outlying X-score.   /
  /      EST -      Name of the OUTMODEL data set from        /
  /                 PROC PLS containing the weights w1-wA     /
  /                 where A=LV, the number of PLS components. /
  /      OUT -      Name of the OUTPUT data set containing    /
  /                 the X-scores as variables.                /
  /      DSOUT -    Name of the data set to be generated by   /
  /                 the macro containing the contributions    /
  /                 for each X-variable.                      /
  /      OBSNUM -   Number of the observation in              /
  /                 question.                                 /
  /      IDVAR -    Name of variable in the OUT data set      /
  /                 numbering the data points.  The default   /
  /                 is N.                                     /
  /      A -        PLS component associated with the         /
  /                 X-score of interest. The default is 1.    /
  /  Required Global Variables:                               /
  /      %XVARS -   Macro variable containing the names of    /
  /                 the X-variables in a string with space    /
  /                 delimiters.                               /
  /      %LV -      Number of Latent Variables (PLS factors)  /
  ************************************************************/

data est; set &est;
  if (_TYPE_='WB' or _TYPE_='PQ') then output;
run;

data out; set &out;
  if &idvar=&obsnum then output;
run;

proc iml;
  use est;
  read all var {&xvars} into WP;
  W=WP[1:&lv,];
  P=WP[(&lv+1):(2*&lv),];
  Wstar=W`*inv(P*W`);
  use &out;
  read all var {&xvars} into X;
  use out;
  read all var {&xvars} into x_i;
  contrib=(Wstar[,&a])` # (x_i - X[:,]);
  quantity=('contrib');
  xvar={&xvars};
  create &dsout from contrib[rowname=quantity colname=xvar];
  append from contrib[rowname=quantity];
quit;

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

data &dsout; set &dsout;
  rename col1=contrib;
run;

axis1 label=(angle=270 rotate=90 'Contribution');

axis2 label=('X-variable');

proc gplot data=&dsout;
  plot contrib * _NAME_ / haxis=axis2 vaxis=axis1;
  symbol1 i=needles v=dot;
run;
quit;

%mend;

%macro cont2scr(est,
                out,
                dsout,
                obsnum,
                idvar=n,
                a1=1,
                a2=2);

  /************************************************************
  /  Calculates and plots the contribution of each X-variable /
  /  to a pair of X-scores for a particular observation.      /
  /  This is useful in diagnosing the cause of an outlying    /
  /  pair of X-scores.                                        /
  /      EST -      Name of the OUTMODEL data set from        /
  /                 PROC PLS containing the weights w1-wA     /
  /                 where A=LV, the number of PLS components. /
  /      OUT -      Name of the OUTPUT data set containing    /
  /                 the X-scores as variables.                /
  /      DSOUT -    Name of the data set to be generated by   /
  /                 the macro containing the contributions    /
  /                 for each X-variable.                      /
  /      OBSNUM -   Number of the observation in              /
  /                 question.                                 /
  /      IDVAR -    Name of variable in the OUT data set      /
  /                 numbering the data points.  The default   /
  /                 is N.                                     /
  /      A1 -       PLS component associated with the first   /
  /                 X-score of interest.  The default is 1.   /
  /      A2 -       PLS component associated with the second  /
  /                 X-score of interest.  The default is 2.   /
  /  Required Global Variables:                               /
  /      %XVARS -   Macro variable containing the names of    /
  /                 the X-variables in a string with space    /
  /                 delimiters.                               /
  /      %XSCRNAME - Prefix given to X-scores when OUT= data  /
  /                  set was defined.                         /
  /      %LV -      Number of Latent Variables (PLS factors)  /
  ************************************************************/

data est; set &est;
  if (_TYPE_='WB' or _TYPE_='PQ') then output;
run;
 
data out; set &out;
  if &idvar=&obsnum then output;
run;

proc iml;
  use est;
  read all var {&xvars} into WP;
  W=WP[1:&lv,];
  P=WP[(&lv+1):(2*&lv),];
  Wstar=W`*inv(P*W`);  
  use &out;
  read all var {&xscrname&a1 &xscrname&a2} into T;
  read all var {&xvars} into X;
  use out;
  read all var {&xscrname&a1 &xscrname&a2} into t_i;
  read all var {&xvars} into x_i;
  delta_t1=t_i[,1]-T[:,1];
  delta_t2=t_i[,2]-T[:,2];
  sd_t1=sqrt((T[##,1]-nrow(T)*T[:,1]**2)/(nrow(T)-1));
  sd_t2=sqrt((T[##,2]-nrow(T)*T[:,2]**2)/(nrow(T)-1)); 
  w1star=Wstar[,&a1];
  w2star=Wstar[,&a2];
  v_sq=(delta_t1/sd_t1)**2*(w1star)`##2+
       (delta_t2/sd_t2)**2*(w2star)`##2;
  v=sqrt(v_sq);
  delta_x=x_i-X[:,];
  contrib=(v#delta_x);
  quantity=('contrib');
  xvar={&xvars};
  create &dsout from contrib[rowname=quantity colname=xvar];
  append from contrib[rowname=quantity];
quit;

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

data &dsout; set &dsout;
  rename col1=contrib;
run;

axis1 label=(angle=270 rotate=90 'Contribution');

axis2 label=('X-variable');

proc gplot data=&dsout;
  plot contrib * _NAME_ / haxis=axis2 vaxis=axis1;
  symbol1 i=needles v=dot;
run;
quit;

%mend;

⌨️ 快捷键说明

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