如何在SAS中使用不同参数拟合线性回归模型

时间:2014-10-09 03:59:51

标签: loops macros sas

我有一个包含三个独立变量的线性模型。我有下面的最终模型。我想使用increase_po和decrease_po变量来重新估计回归模型中的y值。

Dependent  Variable    Estimate    increase_po   decrease_po
Rate       Rate_lag1   0.54        0.60          0.49
Rate       UN          0.07        0.08          0.06
Rate       SQ          0.03        0.03          0.02

我想要做的是写一个do循环来产生六种可能性组合:

comb1  comb2   comb3  comb4   comb5  comb6
0.60   0.49    0.08   0.06    0.03   0.02
0.07   0.07    0.54   0.54    0.54   0.54
0.03   0.03    0.03   0.03    0.07   0.07

我想使用这些参数中的每一个来重新拟合模型并获得估计的y值。

fitted y1= b0 + 0.60*Rate_lag1 + 0.07*UN + 0.03*SQ  (only Rate_lag1 change parameter)

fitted y2= b0 + 0.49*Rate_lag1 + 0.07*UN + 0.03*SQ  (only Rate_lag1 change parameter)

fitted y3= b0 + 0.54*Rate_lag1 + 0.08*UN + 0.03*SQ  (only UN change parameter)

....................

因此,如果不使用宏和循环,甚至是不可能的。

1 个答案:

答案 0 :(得分:1)

我看到了你的另一个帖子,但想在这里发帖。

如果我理解正确: 您已经为数据拟合了3个模型,每个模型使用相同的三个独立变量/预测变量。 您已显示与3个模型的每个预测变量对应的beta参数。 您希望从3个原始模型开始创建6个新模型,并且一次只更改一个beta参数。

下面是一些我认为可以做你想做的SAS代码。

然而,您的3个原始模型中的beta估计值非常相似!所以我不知道这个练习可以用最好的"来表达。模型。

祝你好运!

data have;
    infile cards;
    input Dependent $  Variable $   Estimate    increase_po   decrease_po;
    cards;
Rate       Rate_lag1   0.54        0.60          0.49
Rate       UN          0.07        0.08          0.06
Rate       SQ          0.03        0.03          0.02
;
run;

*** TRANSPOSE SO EACH PREDICTOR VARIABLE IS A COLUMN AND EACH MODEL IS A ROW ***;
*** NOTE: RATE_LAG1 CHANGES TO RATE_LAG IN THE TRANSPOSE ***;
proc transpose data=have out=have_transpose;
    id variable;
    var Estimate    increase_po   decrease_po;
run;

*** CREATE VARIABLE FOR MODEL NUMBERS 1-3 ***;
data have_transpose;
    set have_transpose;
    modelnum=_N_;
run;    

proc print data=have_transpose;
run;

*** PUT EACH COLUMN INTO A SEPARATE DATASET AND KEEP ORIGINAL MODEL NUMBER IN EACH DATASET ***;
data col1(keep=rate_lag modelnum rename=(modelnum=rate_lag_num)) 
        col2(keep=un modelnum rename=(modelnum=un_num)) 
        col3(keep=sq modelnum rename=(modelnum=sq_num)) 
    ;
    set have_transpose;
run;

*** USE SQL TO DO A MANY-TO-MANY MERGE FOR ALL THREE DATASETS ***;
*** THE CREATED DATASET WILL CONTAIN ALL POSSIBLE COMBINATIONS OF PARAMETER ESTIMATES FROM ALL MODELS ***;
*** IN THIS CASE THERE WILL BE 3x3x3 = 27 RECORDS ***;
proc sql;
    create table col123 as
    select *
    from col1, col2, col3
;
quit;

data almost;
    set col123;
    *** FOR EACH POSSIBLE COMBINATION, COUNT HOW MANY PARAMETERS ARE UNCHANGED FROM MODEL 1 ***;
    flag = (rate_lag_num=1) + (un_num=1) + (sq_num=1);
run;

proc print data=almost;
run;

*** WANT TO ONLY KEEP MODELS WHERE TWO PARAMETERS ARE UNCHANGED ESTIMATES (WHERE FLAG=2) ***;
data want;
    set almost;
    if flag=2;
    keep rate_lag un sq ;
run;

*** THIS DATASET CONTAINS 6 RECORDS ***;
proc print data=want;
run;


*** FROM HERE YOU CAN USE SQL TO DO A MANY-TO-MANY MERGE WITH YOUR 6 NEW MODELS AND DATASET WITH THE PREDICTOR VARIABLES ***;
*** AND THEN CREATE YOUR NEW ESTIMATES FOR EACH MODEL ***;
*** SOMETHING LIKE THIS BELOW ***;
/*

*** RENAME VARIABLES AND CREATE NEW MODEL NUMBER ***;
data betas;
    set want;
    new_model_num = _N_;
    rename  rate_lag=rate_lag_beta  un=un_beta  sq=sq_beta ;
run;

proc sql;
    create table all as
    select * 
    from betas, ORIGINAL_DATA;  *** CHANGE "ORIGINAL_DATA" TO YOUR DATA SET NAME ***;
run;

data new_model;
    set all;
    fitted = b0 + (Rate_lag_beta * Rate_lag1) + (un_beta * un) + (sq_beta * sq);
run;
*/

*** NOTE: IN YOUR EXAMPLE, YOU ASSUME THE SAME B0 FOR ALL MODELS
*** HOWEVER, YOUR THREE STARTER MODELS MAY HAVE DIFFERENT B0 ESTIMATES ***;
*** SO YOU WILL HAVE TO THINK ABOUT THE BEST WAY TO HANDLE THAT ***;