Announcement

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

  • Saving residuals in panel regression

    Hi everyone

    I have a question about saving the residuals in panel data.

    Features of my dataset are as follows:
    1) # of panel: 300
    2) time id: 1 to 400

    example of the dataset can be shown as
    company_id time_id weekly_sales
    1 1 100
    1 2 150
    1 3 170
    1 4 120
    1 5 125
    Let's say there is an equation:

    weekly_sales (i,t) = a + b*weekly_sales(i,t-1) + resid

    What I want to acquire is the residuals of the equation above.

    I tried the code below, but I think the result is not what I was looking for.

    ------
    gen resid_weekly_sales=.
    levelsof(company_id), local(local_company_id)

    foreach id of local local_company_id {

    regress weekly_sales L.weekly_sales if company_id==`id'
    predict r if company_id==`id', resid
    replace resid_weekly_sales = r if company_id==`id'

    drop r
    }

    ------
    I think the reason why the results are wrong is that I did not account for time_id in the coding.

    I would appreciate it if someone can help me figure out the solution.

    Thank you.


  • #2
    Jay:
    welcome to this forum.
    Can't you simply use -predict-?:
    Code:
    . use "https://www.stata-press.com/data/r17/nlswork.dta"
    (National Longitudinal Survey of Young Women, 14-24 years old in 1968)
    
    . xtreg ln_wage c.age##c.age, fe vce(cluster idcode)
    
    Fixed-effects (within) regression               Number of obs     =     28,510
    Group variable: idcode                          Number of groups  =      4,710
    
    R-squared:                                      Obs per group:
         Within  = 0.1087                                         min =          1
         Between = 0.1006                                         avg =        6.1
         Overall = 0.0865                                         max =         15
    
                                                    F(2,4709)         =     507.42
    corr(u_i, Xb) = 0.0440                          Prob > F          =     0.0000
    
                                 (Std. err. adjusted for 4,710 clusters in idcode)
    ------------------------------------------------------------------------------
                 |               Robust
         ln_wage | Coefficient  std. err.      t    P>|t|     [95% conf. interval]
    -------------+----------------------------------------------------------------
             age |   .0539076    .004307    12.52   0.000     .0454638    .0623515
                 |
     c.age#c.age |  -.0005973    .000072    -8.30   0.000    -.0007384   -.0004562
                 |
           _cons |    .639913   .0624195    10.25   0.000     .5175415    .7622845
    -------------+----------------------------------------------------------------
         sigma_u |   .4039153
         sigma_e |  .30245467
             rho |  .64073314   (fraction of variance due to u_i)
    ------------------------------------------------------------------------------
    
    . predict Residual_epsilon, e
    (24 missing values generated)
    
    . list idcode Residual_epsilon in 1/10
    
         +--------------------+
         | idcode   Residua~n |
         |--------------------|
      1. |      1   -.3836936 |
      2. |      1   -.8380948 |
      3. |      1   -.3073495 |
      4. |      1   -.1464718 |
      5. |      1   -.2049846 |
         |--------------------|
      6. |      1   -.2537888 |
      7. |      1     .438062 |
      8. |      1    .4524961 |
      9. |      1    .2650439 |
     10. |      1     .427596 |
         +--------------------+
    
    .
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment

    Working...
    X