简体   繁体   English

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

[英]How to fitted a linear regression model using different parameters in SAS

I have a linear model that containing three independent variables. 我有一个包含三个自变量的线性模型。 I have the final model below. 我在下面有最终模型。 I want to use the increase_po and decrease_po variables to re-estimate the y value in the regression model. 我想使用increas_po和reducing_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

What I want to do is to write a do loop to produce the six possibility combinations: 我想做的是编写一个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

I want to use each of these parameters to re-fit the model and get estimated y value. 我想使用这些参数中的每一个来重新拟合模型并获得估计的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)

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

Therefore, it is difficult even impossible if not using macro and loop. 因此,即使不使用宏和循环,也很难甚至不可能。

I saw your other thread but wanted to post here. 我看到了您的其他主题,但想在这里发布。

If I understand correctly: You have already fit 3 models to your data, and each model uses the same three independent variables / predictors. 如果我理解正确:您已经为数据拟合了3个模型,并且每个模型都使用相同的三个独立变量/预测变量。 You have shown the beta parameters corresponding to each predictor for the 3 models. 您已经显示了对应于3个模型的每个预测变量的beta参数。 You want to create 6 new models by starting with the 3 original models and changing only one beta parameter at a time. 您要通过从3个原始模型开始并一次仅更改一个beta参数来创建6个新模型。

Below is some SAS code that I think will do what you want. 以下是一些我想可以做您想要的SAS代码。

However, the beta estimates in your 3 original models are all very similiar! 但是,您的3个原始模型中的beta估算值都非常相似! So I don't know that this exercise reveal anything in terms of a "best" model. 因此,我不知道此练习可以揭示“最佳”模型方面的任何内容。

Good Luck! 祝好运!

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 ***;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM