Announcement

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

  • foreach--Hoynes code

    Can someone help me understand part of this code?
    I pasted all of it for help, but at the very bottom the loop calls foreach alt in 1, foreach r in 2, and foreach sex in 0. Can you help me understand what this means? I am not sure what exactly the loop is doing.
    Thank you!

    /* Hoynes, Schanzenbach and Almond
    TABLES 2, 3, 4
    */

    #delimit;
    clear;
    capture log close;
    set more off;
    set linesize 200;
    disp "DateTime: $S_DATE $S_TIME";

    log using "Table234.log", replace ;
    disp "DateTime: $S_DATE $S_TIME";

    use "psidAdultHealth.dta";

    ************************************************** ************************************************** ;
    * Final Sample;
    ************************************************** ************************************************** ;
    * Drop PILOT COUNTIES. ;
    drop if fs_yearYOB < 1964;

    * drop if no FSP observed for this observation;
    drop if fs_yearYOB ==.;

    * drop if moved into Core family after 1968;
    drop if weight == 0 ;

    * given that the oldest cohort is 1956 and last year is 2009, the oldest person should be 53;
    * There is one guy with bad age variable so drop him;
    list inum1968 person1968 yob Datayear age if age>52;
    drop if age>53;

    * Even though we select heads/wives only, some are still really young. Limit sample to those 18+;
    drop if age<18;

    * Set up sample selection for different outcome variables;
    * health variables: include all observations, no selection;
    * economic variables (earnings, educ, income, poverty): ages 25+;
    gen econsamp=0;
    replace econsamp=1 if age>=25;

    **********************************************;
    * This do-file makes all the variables we subsequently use ;
    **********************************************;
    do "makeregdata.do";

    **********************************************;
    * DEFININING SUBSAMPLES FOR MODELS;
    ***********************************************;

    local rest1 "dummy1 == 1";
    local rest2 " edcatParent == 1";
    local rest3 " edcatParent == 3";

    local rest1label everyone ;
    local rest2label lowinc ;
    local rest3label hied ;

    local gender0 "dummy1 == 1" ;
    local gender1 "sex == 1" ;
    local gender2 "sex == 2" ;

    local gen0 "all" ;
    local gen1 "male";
    local gen2 "female";
    ***********************************************;
    * DEFINIING OUTCOME VARIABLES;
    ***********************************************;
    local healthOutcore "std_count3 diabetes highBlood obesity heartdis heartatt goodHealth disab height_below5p height_below10p" ;
    local healthOutlong "goodHealth disab diabetes highBlood obesity healthwgt std_count1 std_count2 std_count3 std_count4 heartatt stroke heartdis arthritus asthma cancer height countbad1 countbad2 countbad3 countbad4 anybad1 anybad2 anybad3 anybad4 height_abovemed height_below5p height_below10p" ;

    local econOutcore "stdecon3 HSplus notpoor nottanf notfoodstamp employed earntrim lnFamTotInctrim totInctrim" ;
    local econOutlong "HSplus lnFamTotInctrim earntrim employed notpoor stdecon1 stdecon2 stdecon3 nottanf notfoodstamp lnFamTotInc earn totInc totInctrim countecon1 countecon2";

    local behavOutcore "eversmoked drinks3plus" ;
    local behavOutlong "smokenow eversmoked drinknow drinks3plus" ;

    ***********************************************;
    * DEFINIING FSP VARIABLES;
    ***********************************************;
    local foodstamp1 "shareFSPage0_5 ";
    local foodstamp2 "inutero1tri shareFSPage0_18 ";
    local foodstamp3 "inutero1tri";
    local foodstamp4 "inutero1tri";
    local foodstamp5 " shareFSPage5_18";
    local foodstamp6 "shareFSPage0_5 fsp_05_pg";

    * THESE ARE THE LABELS FOR THE ALTERNATIVE RUNS ;
    local foodstamp1alt "shareFSPageIU_5" ;
    local foodstamp2alt "shareFSPageIU_5 inutero1tri" ;
    local foodstamp3alt "shareFSPageIU_5 shareFSPage5_18" ;
    local foodstamp4alt "shareFSPageIU_5 inuteroAny" ;
    local label1 IU_5 ;
    local label2 IU_5_tri ;
    local label3 IU_5_18 ;
    local label4 IU_5_inuteroANY ;

    local restfs1 "dummy2 == 1";
    local restfs2 "dummy2 == 1";
    local restfs3 "model1_12_33 == 1";
    local restfs4 "dummy2 == 1";

    ***********************************************;
    * Defining local county controls;
    ***********************************************;
    local controls0 " " ;
    local controls1 "TpcRet_0_5yravg TpcMedCARE1_0_5yravg TpcIncPA1_0_5yravg";
    local controls2 "beds_pc_0_5yravg nhosp_pc_0_5yravg";
    local controls3 "shareCHCageIU_5";
    local controls4 "`controls1' `controls2' `controls3'";
    local controls5 "`controls2' `controls3'";

    local labcon0 "base" ;
    local labcon1 "reis";
    local labcon2 "aha";
    local labcon3 "chc";
    local labcon4 "all";
    local labcon5 "aha_chc";

    ************************************************** ************************************************** ;
    * more options;
    ************************************************** ************************************************** ;
    local options "noast stats(coef se pval) bdec(5) nocons noparen addstat(Y-mean,r(mean))";

    local demog "yobI_* yearI_* male nonwhite HS gt_HS age age2 married";
    local demogecn "yobI_* yearI_* male nonwhite age age2 married";
    local fambackgrnd " femheadParent avg_incneedParent edlshsParent";


    ************************************************** ***********************************;
    * Regressions start here;
    ************************************************** ***********************************;

    foreach alt in 1 { ;
    foreach r in 2 { ;
    foreach sex in 0 { ;
    local spec 1 ;
    local append "replace";
    foreach y in std_count3 diabetes highBlood obesity heartdis heartatt { ;
    di "OUTCOME `y', FSP spec `spec', TREATMENT AT COUNTY LEVEL - group `rest`r'label' ";
    areg `y' `foodstamp`alt'alt' `demog' `fambackgrnd' time_sobI_* time_*CB if `rest`r'' & `gender`sex'' [aw=weight], cluster(StCtyYOB) absorb(StCtyYOB);
    disp "health `rest`r'label' `label`alt'' - summary: `y' " ;
    summ `y' [aw=weight] if e(sample) ;
    outreg2 `foodstamp`alt'alt' using "Table2.xls", `options' ctitle( "`y' ") `append';
    local append "append";
    } ; /* end of loop over health outcomes */
    } ;
    } ;
    } ;

  • #2
    I stop at knowing that Hoynes was a VP in The West Wing, an excellent fantasy series. Otherwise I think the point was that


    Code:
    foreach alt in 1 { ;
    foreach r in 2 { ;
    foreach sex in 0 { ;
    
    } ;
    } ;
    } ;
    is just a template to be altered at whim. For example if you had sex coded 0 1 and you wanted to see results for both of those then you would extend the code.

    Code:
    foreach sex in 0  1 { ;
    The point is general and applies to whatever categories and whatever number of categories make sense to you and your dataset. I have no idea what is in psidAdultHealth.dta unless it is implied by this code.

    In general, such do-files serve as documentation for some authors as proof of repeatability, but they are utterly fragile to your data having the same details. I trust it's explained somewhere where to get outreg2; otherwise this is incomplete.

    Comment


    • #3
      Oh I see!! Thank you so so much. I thought it was specifically referring to some variables above and I could not figure out which ones as "alt" and "r" were not variables. I really appreciate this help.

      Comment


      • #4
        I would look at documentation for foreach and local macros. This code can’t be understood precisely without understanding those.

        Comment

        Working...
        X