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

📄 macro.txt

📁 宏
💻 TXT
📖 第 1 页 / 共 3 页
字号:
%macro res_plot(ds);

 /************************************************************
 /  Plots Y-residuals vs. predicted values for each PLS      /
 /      component.                                           /
 /  Variable:                                                /
 /      DS -        The input data set:  Must at least       /
 /                  contain variables N for observation      /
 /                  numbers, as well as predicted values and /
 /                  residuals and should not contain missing /
 /                  values.                                  /
 /  Required Global Variables:                               /
 /      %RESNAME -  Prefix given to Y-residuals when OUT=    /
 /                  data set was defined in PROC PLS.        /
 /      %PREDNAME - Prefix given to predictions when OUT=    /
 /                  data set was defined in PROC PLS.        /
 /      %NUM_Y    - Number of response variables Y.          /
 ************************************************************/

data _NULL_; set &ds;
  call symput('max_n',n);
run;

%do i=1 %to &num_y;

  axis1 label=(angle=270 rotate=90 "Residual")
        major=(number=5) minor=none;

  axis2 label=("Prediction for Response &i") minor=none;

  data res_anno;     *** Annotation Data Set for Plot ***;
    length text $ %length(&max_n);
    retain function 'label' position '5' hsys '3' xsys '2' ysys '2' ;
    set &ds;
    text=%str(n);  x=&predname&i;  y=y&resname&i;
  run;

  proc gplot data=&ds;
    plot y&resname&i*&predname&i/anno=res_anno vaxis=axis1
                                 haxis=axis2 vref=0 lvref=2 frame;
    symbol1 v=none i=none;
  run;

%end;

%mend;

%macro nor_plot(ds);

 /************************************************************
 /  Plots Y-residuals vs. Normal quantiles for each PLS      /
 /  component.                                               /
 /  Variable:                                                /
 /      DS -       The input data set:  Must at least        /
 /                 contain variables for observation         /
 /                 numbers, predicted values and residuals   /
 /                 and should not contain missing values.    /
 /  Required Global Variables:                               /
 /      %RESNAME - Prefix given to Y-residuals when OUT=     /
 /                 data set was defined in PROC PLS.         / 
 /      %NUM_Y   - Number of response variables Y.           /
 ************************************************************/

data ds; set &ds;
run;

data _NULL_; set &ds;
  call symput('max_n',n);
run;


%do i=1 %to &num_y;
  data ds; set ds;
    if y&resname&i=. then delete;
  run;
%end;

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

%do i=1 %to &num_y;

  proc sort data=ds; by y&resname&i;

  /***********************************************************
  /  Calculate the expected values under normality for each  /
  /  residual.                                               /
  ***********************************************************/

  data resid&i; set ds(keep=n y&resname&i);
    v=(_n_ - 0.375)/(&numobs+0.25);
    z=probit(v);
  run;

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

  axis2 label=('Normal Quantile') minor=none;

  data nor_anno;             *** Annotation Data Set for Plot ***;
    length text $ %length(&max_n);
    retain function 'label' position '5' hsys '3' xsys '2' ysys '2' ;
    set resid&i;
    text=%str(n); x=z; y=y&resname&i;
  run;

  proc gplot data=resid&i;
    plot y&resname&i*z/anno=nor_anno vaxis=axis1 haxis=axis2
                      frame;
    symbol1 v=none i=none;
  run;

%end;

%mend;

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

 /************************************************************
 /  Plots the Y-scores vs. the corresponding X-scores for    /
 /  each PLS component.                                      /
 /  Variables:                                               /
 /      DS -        The data set containing the scores and a /
 /                  variable N containing the observation    /
 /                  numbers.                                 /
 /      MAX_LV -    Number of the last PLS component to have /
 /                  its scores plotted.                      / 
 /  Required Global Variables:                               /
 /      %XSCRNAME - Prefix given to X-scores when OUT= data  /
 /                  set was defined.                         /
 /      %YSCRNAME - Prefix given to Y-scores when OUT= data  /
 /                  set was defined in PROC PLS.             /
 ************************************************************/

data dsout; set &ds;      *** Uses nonmissing observations ***;
  if n ^= .;
run;

data _NULL_; set &ds;
  call symput('max_n',n);
run;


%do i=1 %to &max_lv;

  data pltanno;           *** Annotation Data Set for Plot ***;
    length text $ %length(&max_n);
    retain function 'label' position '5' hsys '3' xsys '2' ysys '2' ;
    set dsout;
    text=%str(n); x=&xscrname&i; y=&yscrname&i;
  run;

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

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

  proc gplot data=dsout;
    plot &yscrname&i*&xscrname&i/anno=pltanno vaxis=axis1
                                 haxis=axis2 frame;
    symbol1 v=none i=none;
  run;

%end;
%mend plot_scr;

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

 /************************************************************
 /  Plots X-scores for a given number of PLS components      /
 /  vs. those of the preceding PLS component.                /
 /  Variables:                                               /
 /      DS -        The data set containing the scores and a /
 /                  variable N containing the observation    /
 /                  numbers.                                 /
 /      MAX_LV -    Number of the last PLS component to have /
 /                  its scores plotted.                      /
 /  Required Global Variables                                /
 /      %XSCRNAME - Prefix given to X-scores when OUT= data  /
 /                  set was defined in PROC PLS.             /
 ************************************************************/

data dsout; set &ds;
  if n ^= .;               *** Uses nonmissing observations ***;
run;

data _NULL_; set &ds;
  call symput('max_n',n);
run;

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

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

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

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

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

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

%end;

%mend plotxscr;

%macro get_wts(dsoutmod,
               dsxwts=xwts);

 /***********************************************************
 /  Gets X-weights w from OUTMODEL data set:                /
 /    1.  Gets appropriate section of OUTMODEL data set.    /
 /    2.  Outputs this data set as DSXWTS1 (will be used    /
 /        in VIP calculation.)                              /
 /    3.  Transposes the data set so the w's are the        /
 /        variables.                                        /
 /    4.  Renames the columns to w1 - wA, where A is the    /
 /        number of PLS components LV in the final model.   /
 /  Variables:                                              /
 /      DSOUTMOD - Name of the OUTMODEL data set generated  /
 /                 by proc PLS.                             /
 /      DSXWTS -   Name of the data set containing the      /
 /                 X-weights as variables that is output    /
 /                 by this macro.                           /
 /  Required Global Variable:                                /
 /      %XVARS -   Macro variable containing the names of    /
 /                 the X-variables in a string with space    /
 /                 delimiters.                               / 
 ************************************************************/

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

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

data &dsxwts; set &dsxwts;
  if _NAME_='_LV_' then delete;
  n=_n_-1;
run;

%do i=1 %to &lv;

  data &dsxwts; set &dsxwts;
    rename col&i=w&i;
  run;

%end;

%mend;

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

 /************************************************************
 /  Plots X-weights for a given number of PLS components     /
 /  vs. those of the preceding PLS component.                /
 /  Variables:                                               /
 /      DS -       Name of the data set containing the       /
 /                 weights as variables w1-wA, 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 weights 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-weights for each PLS component                   /
 ***********************************************************/

%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=w&i; y=w&j;
  run;

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

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

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

%end;

%mend;

%macro pltwtfrq(ds,
                plotyvar=W,
                plotxvar=f,
                max_lv=&lv,
                label=Weight);

 /************************************************************
 /  Plots X-Weights or X-Loadings versus the frequency for   /
 /  spectrometric calibration data sets.                     /
 /  Variables:                                               /
 /      DS -       Data set containing the weights/loadings  /
 /                 as variables with each observation        /
 /                 representing the weights for a particular /
 /                 X-variable, which in this case is a       /
 /                 frequency.                                /
 /      PLOTYVAR - The name (excluding the component number) /
 /                 of the weight/loading variables.  For     /
 /                 example, PLOTYVAR=w if the variables to   /
 /                 be plotted are w1, w2, w3,...             /
 /      PLOTXVAR - The variable name of the frequency        /
 /                 variable.                                 /
 /      MAX_LV -   Number of PLS components to be plotted    /
 /      LABEL -    The label for the vertical axis in the    /
 /                 plot.                                     /
 ************************************************************/

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

%let plotvars=%str( );

%do i=1 %to &max_lv;
  %let plotvars=%str(&plotvars &plotyvar&i);
%end;

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

%mend;

%macro getxload(dsoutmod,
                dsxload=xloads);

 /***********************************************************
 /  Gets X-loadings p from OUTMODEL data set:               /
 /    1.  Gets appropriate section of OUTMODEL data set.    /
 /    2.  Transposes it so the p's are column vectors.      /

⌨️ 快捷键说明

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