Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using desmat to run saturated models to compare BIC to find best model

    I am trying to use the desmat program with macro loops to run all possible model specifications. However, I am running into problems with running the interaction terms. Has someone used desmat for this purpose using macro loops? Or do you have suggestions for any other way to run saturated models? I just need to compare the BIC of all the models and find the best fit model.

    For e.g. small part of my dataset looks like this: with Worse_hrate being my DV and female (2 categories), educ (4 categories), and race(3 categories)
    worse_~e female race educ |
    |-------------------------------------------------------------------------|
    1. | 1 0 Non-Hispanic White BA degree or more |
    2. | 0 1 Non-Hispanic Black BA degree or more |
    3. | 0 0 Non-Hispanic White High School Only |
    4. | 0 0 Non-Hispanic Mixed High School Only |
    5. | 0 0 Non-Hispanic White BA degree or more

    I want to run models with one way, all possible two ways, and three way interactions and compare BIC. Traditionally I would run it like this:
    /**Main effects only *****/
    qui desmat: logit worse_hrate female race educ, desrep(exp all);
    est sto n1;
    /**two way interaction*****/
    qui desmat: logit worse_hrate female*race educ, desrep(exp all);
    est sto n2;
    /**two way interaction*****/
    qui desmat: logit worse_hrate female race*educ, desrep(exp all);
    est sto n3;
    /**three way interaction*****/
    qui desmat: logit worse_hrate female*race*educ, desrep(exp all);
    est sto n4;
    *compare the BIC
    esttab m1 m2 m3 m4, aic bic obslast

    But I have more than 3 variables so want to create a macro loop program. I tried this but did not succeed.
    local best_BIC=.
    local best_AIC=.

    forval female=1/2{
    forval race=1/2{
    forval educ=1/2{

    **main effects
    local oneway ="female race educ"

    **two-way interactions
    local twoway=""
    foreach v1 in "female race educ"{
    foreach v2 in "female race educ"{
    if "`v'"<"`v2'" & ``v1'_level'>=2 & ``v2'_level'>=2 local twoway="`twoway' `v1'*`v2'"
    }
    }


    **three-way interactions
    local twoway=""
    foreach v1 in "female race educ"{
    foreach v2 in "female race educ"{
    foreach v3 in "female race educ"{
    if "`v'"<"`v2'" & "`v2'"<"`v3'" ``v1'_level'>=3 & ``v2'_level'>=3 & ``v3'_level'>=3 local threeway="`threewaway' `v1'*`v2'*`v3'"
    }
    }
    }

    desmat:logit worse_hrate `oneway' `twoway' `threeway'
    if -2*e(ll)+e(df_m)*ln(e(N))<`best_BIC' local best_BIC_spec="`oneway' `twoway' `threeway'"
    if -2*e(ll)+e(df_m)*ln(e(N))<`best_BIC' local best_BIC=-2*e(ll)+e(df_m)*ln(e(N))
    if -2*e(ll)+e(df_m)*2 <`best_AIC' local best_AIC_spec="`oneway' `twoway' `threeway'"
    if -2*e(ll)+e(df_m)*2 <`best_AIC' local best_AIC=-2*e(ll)+e(df_m)*2

    local current_BIC=-2*e(ll)+e(df_m)*ln(e(N))
    local current_AIC=-2*e(ll)+e(df_m)*2


    di ""
    di ""
    di "Current spec: `oneway' `twoway' `threeway'"
    di "Best BIC spec so far:`best_BIC_spec'"
    di "Best AIC spec so far:`best_AIC_spec'"
    di ""
    di "Current BIC is `current_BIC'"
    di "Best BIC so far is `best_BIC'"
    di ""
    di "Current AIC is `current_AIC'"
    di "Best AIC so far is `best_AIC'"
    }
    }
    }
Working...
X