Announcement

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

  • Saving several coefficients from different regression models in the same excel sheet

    Dear Statalist,

    I have several regression models with always the same variable of interest but with different outcomes. I would like to save the coefficients from my main variable of interest in the same Excel sheet (or tabulate them in Stata if possible).

    I am using the following loop to run my regression

    poisson outcome1 i.year i.age_cat i.education i.employment i.rural group_*, irr vce(robust)
    poisson outcome2 i.year i.age_cat i.education i.employment i.rural group_*, irr vce(robust)
    poisson outcome3 i.year i.age_cat i.education i.employment i.rural group_*, irr vce(robust)

    foreach num of numlist 1/10 {
    tab group if group==`num'

    poisson outcome1 i.year i.age_cat i.education i.employment i.rural if group==`num', irr vce(robust)
    poisson outcome2 i.year i.age_cat i.education i.employment i.rural if group==`num', irr vce(robust)
    poisson outcome3 i.year i.age_cat i.education i.employment i.rural if group==`num', irr vce(robust)

    }

    The code produces 33 regression models. My main variable of interest is time (2010,2011,2012 with 2010 as ref) and I am interested in the RR+CI for the 2011 and 2012 coefficient for each regression model

    If I use the option coeflegend, Stata tells me that results I am interested in are stored as _b[2011.year] and _b[2012.year]. I should be able to store them all and either tabulate in Stata or save them in Excel but I am not sure how.


    I would need something as follow:
    Group Outcome RR LCI UCI
    1 1 .. .. ..
    1 2 .. .. ..
    1 3 .. .. ..
    2 1 .. .. ..

    I know I could produce such table manually but I am trying to polish my code to make it more efficient. Thanks in advance.


  • #2
    You'll increase your chances of a helpful answer if your follow the FAQ asking questions - provide Stata code in code delimiters, Stata output, and sample data using dataex. With sample data, we might be able to replicate your problem and be more helpful.

    If you look, you'll find this has been discussed within the last 3 weeks on this list serve (just not for poisson, but that makes no difference).

    You can save these in a matrix or as a variable. If you want a variable, you create a variable with all missing values ( g beta=.) and a counter (local counter=1). Then after each regression, you replace the value in the beta variable and increment the counter.
    replace beta=_b[x] in `counter'`counter/
    local counter=`counter' + 1

    Naturally, you'll have something different in the _b[x] instead of x.

    Comment

    Working...
    X