Announcement

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

  • Storing More Estimates Than Allowed by System Memory

    Hi all,

    I have 32 countries in my sample, 18 of which have adopted a certain policy, and 14 of which have not. I want to pair each country that has not adopted the policy with each country that has, and then regress over outcomes a,b, ... n, with 18 total outcomes. In addition, I want to store the output from one of the coefficients in each model (it's a diff in diff so, in this case, the interaction between treatment time and the binary indicating adoption). I then want to be able to use those estimates for something else. The system memory, however, does not allow for storing the number of results that I require.

    My request may not be clear, so let me explain what I mean. Currently, I have tagged each country that has not adopted the policy a value i = 1/18 and each that has not a value j = 19/32. I then use the following code to run the regressions on each of 18 outcomes within each country pair. I then combine the output from the excel file manually:

    Code:
    local tablepath "X~X"
    local outcomes a b c etc.
    local covariates x y z etc.
    
    est drop _all
    foreach outcome of local outcomes {
    forvalues i = 1/14 {
    forvalues j = 19/32 {
    eststo: qui reg `outcome' `covariates' if (notadopt_id == `i' | adopt_id == `j') [pw = round(weight, 1)], cluster(countryname) // this regresses all potential pairs of countries
    }
    }
    esttab using "`tablepath'/title_`outcome'.csv", not nostar noobs keep(1.adopt#1.treatment) // write the output to excel
    est drop _all
    }
    Where "adopt" is a binary indicator of policy adoption and "treatment" is the indicator for the post-treatment period. This code produces 252 1x18 excel files, where each stores the results for each outcome among one individual country pair. Combining the excel outputs yields something like this:

    Click image for larger version

Name:	example.png
Views:	1
Size:	4.1 KB
ID:	1711928



    It is then easy to import into Stata the combined excel file. My method works, but combining the files is tedious and not ideal for replication purposes.

    The crux of my issue is this: how do I store hundreds of regression estimates in a similar format to the excel sheet pictured above, without ever leaving Stata?

  • #2
    Here's how I would do this:
    Code:
    local tablepath "X~X"
    local outcomes a b c etc.
    local covariates x y z etc.
    
    frame create results str30 outcome int (nontadopt_id adopt_id) float b_
    foreach outcome of local outcomes {
        forvalues i = 1/14 {
            forvalues j = 19/32 {
                reg `outcome' `covariates' if (notadopt_id == `i' | adopt_id == `j') ///
                    [pw = round(weight, 1)], cluster(countryname)
                frame post results ("`outcome'") (`i') (`j') (_b[interaction_term])
            }
        }
    }
    
    frame change results
    format b_ %05.3f
    reshape wide b_, i(notadopt_id adopt_id) j(outcome) string
    rename b_* *
    At the end of this code, the data in frame results will be precisely the table you are looking for. You can then save it as a Stata data set, or export it to a spreadsheet, or whatever.

    Note: Untested, so beware of typos or other errors, but this is the gist of the solution.

    In your regression command there is no mention of the treatment, time, or interaction variables. So you need to add those in. And in the -frame post- command replace interaction_term by the proper name of the interaction coefficient. It will probably be something like 1.adoption_group_indicator#1.post_intervention, depending on how you name your variables.

    Comment


    • #3
      Thank you so much, Clyde! This worked perfectly.

      Comment

      Working...
      X