Announcement

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

  • Storing marginal effects in a loop of regressions

    I would like to run several regression via a foreach-loop, calculate marginal effects, store and compare them. So far I managed to calculate the loop. I am able to store normal estimates in a table. I can access one marginal effect at a time, since its in a matrix object. That's where I got stuck. Related posts are: Here is a MWE:

    HTML Code:
    *************************************************
    * Load Panel Data
    *************************************************
    webuse nlswork, clear
    xtset idcode
    
    *************************************************
    * Loop for different outcomes
    local outcomes "ln_wage hours wks_work"
    foreach o of local outcomes {
    quietly xtreg `o' age c.age#c.age ttl_exp c.ttl_exp#c.ttl_exp tenure c.tenure#c.tenure, fe
    estimates store reg_`o'
    }
    estimates table reg_ln_wage reg_hours reg_wks_work
    *************************************************
    
    *************************************************
    * Loop regressions and save marginal effects (difficult due to matrix object)
    local outcomes "ln_wage hours wks_work"
    foreach o of local outcomes {
    quietly xtreg `o' age c.age#c.age ttl_exp c.ttl_exp#c.ttl_exp tenure c.tenure#c.tenure, fe
    margins, dydx(age)
    matrix B`o' = r(b)
    }
    * to be continued
    *************************************************
    Thank you

  • #2
    If you look at the documentation on matrices, it will show you how to read one element out of a matrix.

    If you want to manipulate the output afterwards, sometimes it helps to create a counter in a local macro and host variables that are all missing, and then write each parameter into a single observation in the host variable using the counter.

    There may also be a way to do this by directly converting the matrix to variables. This is documented in the PDF documentation.

    Comment

    Working...
    X