Announcement

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

  • Looping using list of var with similar prefix

    Hi everyone!

    I'm getting stuck with something probably silly.

    I have a number of variables that share a common prefix which I would like to regress individually using reghdfe.
    What I have come up with is not working. I'm getting an 'unexpected end of file'.

    Code:
    foreach var of spei_* {
        quietly reghdfe ln_agprod var, absorb(country_id continent_id#year) vce(cluster country_id)
        estimates store est_`var'
    }
    My variables are of the form spei_n20 to spei_n0 and spei_1 to spei_20.

    Many thanks for your help!

  • #2
    Alexandre:
    provided that it's not a trivial typo, the first thing that I would amend in the second line of your code is:
    Code:
    `var'
    instead of:
    Code:
    var
    The following toy-example may help:
    Code:
    set obs 100
    number of observations (_N) was 0, now 100
    
    . g y=runiform()*1000
    
    . g Carlo_A=runiform()*100
    
    . g Carlo_B=runiform()*10
    
    . g Carlo_C=runiform()*20
    
    . foreach var of varlist Carlo_* {
      2. regress y `var'
      3. estimates store `var'
      4.  }
    
          Source |       SS           df       MS      Number of obs   =       100
    -------------+----------------------------------   F(1, 98)        =      0.11
           Model |  10846.5994         1  10846.5994   Prob > F        =    0.7377
        Residual |  9422817.35        98  96151.1974   R-squared       =    0.0011
    -------------+----------------------------------   Adj R-squared   =   -0.0090
           Total |  9433663.95        99  95289.5348   Root MSE        =    310.08
    
    ------------------------------------------------------------------------------
               y |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
         Carlo_A |   .3687117   1.097785     0.34   0.738    -1.809808    2.547231
           _cons |   478.5269   64.02046     7.47   0.000     351.4804    605.5735
    ------------------------------------------------------------------------------
    
          Source |       SS           df       MS      Number of obs   =       100
    -------------+----------------------------------   F(1, 98)        =      0.01
           Model |  664.331479         1  664.331479   Prob > F        =    0.9340
        Residual |  9432999.62        98  96255.0981   R-squared       =    0.0001
    -------------+----------------------------------   Adj R-squared   =   -0.0101
           Total |  9433663.95        99  95289.5348   Root MSE        =    310.25
    
    ------------------------------------------------------------------------------
               y |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
         Carlo_B |   .8930472   10.74964     0.08   0.934    -20.43926    22.22536
           _cons |   492.4435   66.59521     7.39   0.000     360.2874    624.5995
    ------------------------------------------------------------------------------
    
          Source |       SS           df       MS      Number of obs   =       100
    -------------+----------------------------------   F(1, 98)        =      2.26
           Model |  212690.526         1  212690.526   Prob > F        =    0.1359
        Residual |  9220973.42        98  94091.5655   R-squared       =    0.0225
    -------------+----------------------------------   Adj R-squared   =    0.0126
           Total |  9433663.95        99  95289.5348   Root MSE        =    306.74
    
    ------------------------------------------------------------------------------
               y |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
         Carlo_C |  -8.099759   5.387328    -1.50   0.136    -18.79074    2.591217
           _cons |   577.8113    61.6906     9.37   0.000     455.3883    700.2343
    ------------------------------------------------------------------------------
    Last edited by Carlo Lazzaro; 17 Oct 2021, 04:04.
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      thanks you so much.
      I was too quick with posting a question. I figured it out.
      Is there a way to delete the post?

      Comment


      • #4
        Alexandre:
        you can edit your post within 1 hour from posting it.
        But there's nothing sinister in leaving in a post about which you have a second thought, as your original query may be useful to other listers.
        That said, if you have other questions, feel free to post them under a meaningful title. Thanks.
        Kind regards,
        Carlo
        (Stata 19.0)

        Comment


        • #5
          All this is explained in the FAQ all posters are asked to read before posting. See https://www.statalist.org/forums/help#closure

          You cannot delete a post.
          Naturally, there are circumstances in which the OP regrets a post. And usually that arises from modesty and/or embarrassment. We all ask silly questions sometimes.

          But the bigger deal is that on Statalist an OP has no rights here over postings, which belong to the forum -- certainly no rights over the posts of anyone who replies. More positively, the ideal is that other people may find your post interesting or helpful. If one person can ask a question, another person may have the same question.

          In one place I contribute to, the OP can delete -- well, hide -- an entire thread, including lengthy replies intended to help. In practice that means, most often, students with assignments covering their tracks -- sometimes even to stop help being visible to their classmates. Long discussion possible, but that's some way from the ethos we want here.

          Comment

          Working...
          X