Announcement

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

  • Generating a local of locals for use in a foreach loop of regression specifications

    Dear all,

    I'm hoping to get a little help on an issue I'm having creating a so-called 'local of locals' in Stata for use in a foreach loop of regression specifications. I currently have a list of 6 global macros that each define a Base_ or Alt_ regression specification, with each of these locals comprised of 2-6 variables. I'm then looking to store each of these specification macros in a single global aka. Spec global that can then be used in a foreach loop that will allow me to run sequential regressions of my dependent variable on each of the Base_ or Alt_ locals stored within the Spec local. I would prefer to do all this with locals, but have been running in to trouble storing the individual locals within a single local.

    My current code appears to be such that when I run the foreach loop intended to regress the dependent variable on each of the specifications stored within the Spec global, Stata runs a series of regressions of the dependent variable on each independent variable within each local, rather than on the set of independent variables stored within each global. For reference, I've attached the code below.

    Code:
    ** 4.1 Load Panel Dataset for Analysis 
    use "`pathdata'\2. DTA Files\Panel_GTI_foranalysis.dta", clear
    
    ** 4.2 Specify Panel Data
    xtset Year Country_id
    
    ** 4.3 Define Baseline and Alternative Specifications 
    global Base ln_GDPpc 
    global Base_2 ln_GDPpc ln_Population Unemployment_R
    global Alt_1_CivLib ln_GDPpc ln_Population Unemployment_R Low_CivLib Med_CivLib 
    global Alt_1_PolFree ln_GDPpc ln_Population Unemployment_R Low_PolFree Med_PolFree 
    global Alt_2_CivLib ln_GDPpc ln_Population Unemployment_R Low_CivLib Med_CivLib Dailycaloricsupplykcalperso
    global Alt_2_PolFree ln_GDPpc ln_Population Unemployment_R Low_CivLib Med_CivLib Dailycaloricsupplykcalperso
    global Alt_3_CivLib ln_GDPpc ln_Population Unemployment_R Low_CivLib Med_CivLib Dailycaloricsupplykcalperso
    global Alt_3_PolFree ln_GDPpc ln_Population Unemployment_R Low_CivLib Med_CivLib Dailycaloricsupplykcalperso Refugees_asylum
    global Specs $Base_2 $Alt_1_CivLib $Alt_1_PolFree $Alt_2_CivLib $Alt_2_PolFree $Alt_3_CivLib $Alt_3_PolFree
    
    ** 4.3 Regressions 
    
    //** 4.3.1 Baseline Spec
    //xtreg ln_GTI $Base, vce(cluster Year)
    
    ** 4.3.2 Loop Through Local Specifications 
    
    foreach Spec of global Specs { 
        xtreg ln_GTI `Spec' 
    }
    I've been a user of Stata for a good period of time, however have been unable to find a solution to this. Any advice would therefore be greatly appreciated!

    All the best,

    Scott

  • #2
    Hi Scott, perhaps this would help:
    Code:
    global Base ln_GDPpc 
    global Base_2 ln_GDPpc ln_Population Unemployment_R
    global Alt_1_CivLib ln_GDPpc ln_Population Unemployment_R Low_CivLib Med_CivLib 
    global Alt_1_PolFree ln_GDPpc ln_Population Unemployment_R Low_PolFree Med_PolFree 
    global Alt_2_CivLib ln_GDPpc ln_Population Unemployment_R Low_CivLib Med_CivLib Dailycaloricsupplykcalperso
    global Alt_2_PolFree ln_GDPpc ln_Population Unemployment_R Low_CivLib Med_CivLib Dailycaloricsupplykcalperso
    global Alt_3_CivLib ln_GDPpc ln_Population Unemployment_R Low_CivLib Med_CivLib Dailycaloricsupplykcalperso
    global Alt_3_PolFree ln_GDPpc ln_Population Unemployment_R Low_CivLib Med_CivLib Dailycaloricsupplykcalperso Refugees_asylum
    global Specs "\$Base_2" "\$Alt_1_CivLib" "\$Alt_1_PolFree" "\$Alt_2_CivLib" "\$Alt_2_PolFree" "\$Alt_3_CivLib" "\$Alt_3_PolFree" 
    
    foreach s of global Specs {
    display "`s'"
    }
    Results in:
    ln_GDPpc ln_Population Unemployment_R
    ln_GDPpc ln_Population Unemployment_R Low_CivLib Med_CivLib
    ln_GDPpc ln_Population Unemployment_R Low_PolFree Med_PolFree
    ln_GDPpc ln_Population Unemployment_R Low_CivLib Med_CivLib Dailycaloricsupplykcalperso
    ln_GDPpc ln_Population Unemployment_R Low_CivLib Med_CivLib Dailycaloricsupplykcalperso
    ln_GDPpc ln_Population Unemployment_R Low_CivLib Med_CivLib Dailycaloricsupplykcalperso
    ln_GDPpc ln_Population Unemployment_R Low_CivLib Med_CivLib Dailycaloricsupplykcalperso Refugees_asylum
    HTH
    Fernando

    Comment


    • #3
      This can be made much simpler.


      Code:
      global Base ln_GDPpc 
      global Base_2 ln_GDPpc ln_Population Unemployment_R
      global Alt_1_CivLib ln_GDPpc ln_Population Unemployment_R Low_CivLib Med_CivLib 
      global Alt_1_PolFree ln_GDPpc ln_Population Unemployment_R Low_PolFree Med_PolFree 
      global Alt_2_CivLib ln_GDPpc ln_Population Unemployment_R Low_CivLib Med_CivLib Dailycaloricsupplykcalperso
      global Alt_2_PolFree ln_GDPpc ln_Population Unemployment_R Low_CivLib Med_CivLib Dailycaloricsupplykcalperso
      global Alt_3_CivLib ln_GDPpc ln_Population Unemployment_R Low_CivLib Med_CivLib Dailycaloricsupplykcalperso
      global Alt_3_PolFree ln_GDPpc ln_Population Unemployment_R Low_CivLib Med_CivLib Dailycaloricsupplykcalperso Refugees_asylum
      
      foreach Spec in "$Base_2" "$Alt_1_CivLib" "$Alt_1_PolFree" "$Alt_2_CivLib" "$Alt_2_PolFree" "$Alt_3_CivLib" "$Alt_3_PolFree" { 
          display "`Spec'"
      }
      So no need to set up yet another macro or play games with delayed evaluation. The main trick to lean on is just that double quotes bind.

      You could (and arguably should) do the same kind of thing with locals.

      Comment


      • #4
        Hi guys,

        Thanks for the response. Of course, the double quotes would allow me to refer to the specific macros. Thank you Fernando and Nick for your help!

        All the best,

        Scott

        Comment

        Working...
        X