Announcement

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

  • residual from multiple regresion

    I'm working with a base that it´s compouse from 500 samples, but I have to create a compillation of coeficients and also i need predict the residuals from each regression, but idk how can i make the ´predictions with the command i used, some idea?

    This is what I did to ger the 500 regressions, but when I try to get the residuals Stata only takes the last regression

    by sample_id : eststo : reg lwage leduc
    sttab using tabla1.rtf, nostar

  • #2
    Clarissa:
    the first issue here is to get 500 residual values, as in the following toy-example:

    Code:
    use "https://www.stata-press.com/data/r17/nlswork.dta"
    . forvalues i = 1/3 {
      2. gen counter`i'=_n if idcode==`i'
      3. quietly regress ln_wage c.age##c.age if idcode==`i'
      4. predict residual`i', res
      5. replace residual`i'=. if counter`i'==.
      6. drop counter`i'
      7.  }
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Clarissa:
      as an ETA to my previous reply, you may want to copy and paste in another .dta -residual1-...-residualn-, and use the following code to make them all start from the first cell in _n==1. Actually, my previous code allows you to obtain a number of residuals consistent with the number of observations each panel is composed of (as per its default behavior, Stata calcuilates residuals for all the panels, regardless of their inclusion in the -e(sample)-):
      Code:
      g counter=1 if residual1!=.
      replace counter=2 if residual2!=.
      replace counter=3 if residual3!=.
      g res= residual1 if residual1 !=.
      replace res= residual2 if residual2 !=.
      replace res= residual3 if residual3 !=.
      drop residual*
      bysort counter: gen res_number=_n
      drop if res==.
      reshape wide res , i( res_number ) j( counter )
      list
      
           +----------------------------------------------+
           | res_nu~r        res1        res2        res3 |
           |----------------------------------------------|
        1. |        1    .2515478    .0178176    -.053482 |
        2. |        2   -.3362141   -.2088357    .1495234 |
        3. |        3    .0707243    .0678506   -.1074609 |
        4. |        4    .1173489    .2330611   -.0166333 |
        5. |        5   -.1410086    .0317273    .0381549 |
           |----------------------------------------------|
        6. |        6   -.3514424   -.0445653     .023333 |
        7. |        7    .2739243    -.069964   -.0467983 |
        8. |        8    .1840517   -.0290784    .0326458 |
        9. |        9   -.0882073    .0140859   -.0268959 |
       10. |       10     .065576   -.0673824    -.039604 |
           |----------------------------------------------|
       11. |       11    -.009357    .0043775   -.0005375 |
       12. |       12   -.0369437    .0509058    .0083955 |
       13. |       13           .           .    .1220856 |
       14. |       14           .           .   -.0846597 |
       15. |       15           .           .    .0019334 |
           +----------------------------------------------+
      
      .
      After that you can export the table according to your research needs.
      Kind regards,
      Carlo
      (Stata 19.0)

      Comment

      Working...
      X