Announcement

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

  • Looping a regression command sequentially by observation and storing each model.

    Hi all,

    Apologies if this is very simple or has been asked before. I have searched through many topics and have struggled to find an answer. Any help is greatly appreciated.

    I am trying to run a regression command for each observation in a dataset, increasing by one observation each time. It may be more useful if I post the code I have that works so far:

    Code:
    foreach x in 1/11 1/12 1/13 1/14 1/15 1/16 1/17 1/18 1/19 1/20 1/21 1/22 1/23 1/24 1/25 1/26 1/27 1/28 1/29 1/30 1/31 1/32 1/33 1/34 1/35 {
    
    reg DV IV1 IV2 IV3 in `x'
    }
    This works (although I am sure there must be a more efficient way of doing this) but I believe it is creating errors down the line when I try to add the following:

    Code:
     foreach x in 1/11 1/12 1/13 1/14 1/15 1/16 1/17 1/18 1/19 1/20 1/21 1/22 1/23 1/24 1/25 1/26 1/27 1/28 1/29 1/30 1/31 1/32 1/33 1/34 1/35 {
    
    reg DV IV1 IV2 IV3 in `x'
    
    est store model_`x'
    }
    When attempting this code, it returns the error of:
    Code:
    / invalid name
    11 invalid name
    I interpret this as: it's trying to store my estimate, but having / in the estimate name is causing the error?

    Although, I know I could be completely wrong here. I am very much a Stata beginner and I am making a lot of errors along the way!

    Any help is greatly appreciated!

    Thanks

  • #2
    Rob, an example code as follows.

    Code:
    sysuse auto, clear
    
    forvalues obs = 11/35 {
        reg price mpg if _n <= `obs'
        est store model_`obs' 
    }

    Comment


    • #3
      Rob:
      you may also want to take a look at -statsby-.
      Kind regards,
      Carlo
      (Stata 19.0)

      Comment


      • #4
        You guys are brilliant! Thank you.

        Comment


        • #5
          The error in #1 is that the slash may not be part of a name.

          Even so, this is going to be a bit (or in other circumstances a lot) faster:

          Code:
          sysuse auto, clear  
          
          forvalues obs = 11/35 {    
               reg price mpg in 1/`obs'    
               est store model_`obs'  
          }
          Last edited by Nick Cox; 07 Nov 2021, 10:04.

          Comment


          • #6
            Rob:
            just applying a very minor surgery on a very interesting Nick Cox 's post (thanks Nick!) on another forum he contributes to (https://stackoverflow.com/questions/...coefficients):
            Code:
            . sysuse auto.dta, clear
            (1978 Automobile Data)
            
            . tempname myresults
            
            . postfile `myresults' num_obs intercept mpg_coeff mpg_se using myresults.dta
            
            .
            . quietly forval x = 2(1)74 {
            .
            . regress price mpg length gear_ratio i.foreign in 1/`x'
            .
            . post `myresults' (`x') (`=_b[_cons]') (`=_b[mpg]') (`=_se[mpg]')
            .
            . }
            
            .
            . postclose `myresults'
            
            .
            . use myresults
            
            .
            . list
            
            +-------------------------------------------+
            | num_obs interc~t mpg_coeff mpg_se|
            |-------------------------------------------|
            1. | 2 6959 -130 0 |
            2. | 3 4812.333 -173.3333 0 |
            3. | 4 3370.359 -130.31 0 |
            4. | 5 1161.01 -185.7874 310.1894 |
            5. | 6 4342.634 -321.1453 306.7237 |
            |-------------------------------------------|
            6. | 7 1729.177 -.4641546 116.8822 |
            7. | 8 1979.505 -3.478176 102.0471 |
            8. | 9 3922.224 -299.1741 289.8163 |
            9. | 10 5575.879 -270.3074 290.458 |
            10. | 11 8045.566 -299.5507 311.4781 |
            |-------------------------------------------|
            11. | 12 21283.88 -462.3358 382.374 |
            12. | 13 13031.65 110.2493 376.0489 |
            13. | 14 13687.06 17.89214 303.7362 |
            14. | 15 14237.47 37.40229 309.9285 |
            15. | 16 15862.72 -20.57691 298.0098 |
            |-------------------------------------------|
            16. | 17 18497.53 -78.16478 277.9452 |
            17. | 18 17663.69 -113.0256 269.2007 |
            18. | 19 15694.79 -107.9036 276.4348 |
            19. | 20 10888.18 -15.41563 271.5362 |
            20. | 21 11114.55 -35.95477 279.8067 |
            |-------------------------------------------|
            21. | 22 9707.108 -10.38597 273.8028 |
            22. | 23 10054.53 -11.01054 266.3543 |
            23. | 24 12848.46 -22.28206 261.4966 |
            24. | 25 12749 -19.8053 249.3439 |
            25. | 26 11741.51 -40.52274 246.4133 |
            |-------------------------------------------|
            26. | 27 11277.44 -84.12788 250.2435 |
            27. | 28 18861.74 -212.315 244.9058 |
            28. | 29 16204.84 -192.1921 239.9254 |
            29. | 30 17197.74 -162.5354 238.1236 |
            30. | 31 15372.99 -159.089 240.3489 |
            |-------------------------------------------|
            31. | 32 13584.69 -160.6033 241.8861 |
            32. | 33 13656.96 -140.6935 235.7217 |
            33. | 34 14236.24 -134.7314 232.8587 |
            34. | 35 13316.22 -113.5074 203.3133 |
            35. | 36 13403.14 -111.648 200.0028 |
            |-------------------------------------------|
            36. | 37 13548.01 -108.5492 197.0977 |
            37. | 38 15606.05 -131.2431 194.191 |
            38. | 39 15901.91 -127.9984 191.3153 |
            39. | 40 15804.45 -139.3503 187.0644 |
            40. | 41 16824.46 -148.5528 185.4286 |
            |-------------------------------------------|
            41. | 42 16194.86 -124.7119 178.6325 |
            42. | 43 15147.52 -78.84817 160.0226 |
            43. | 44 15567.19 -93.93787 158.3768 |
            44. | 45 11405.17 -68.06149 160.3926 |
            45. | 46 11650.25 -61.63638 157.4141 |
            |-------------------------------------------|
            46. | 47 12379.09 -68.92999 155.3936 |
            47. | 48 12375 -66.1585 152.3788 |
            48. | 49 12499.16 -65.63114 150.7698 |
            49. | 50 12590.24 -63.47343 149.3211 |
            50. | 51 12928.23 -62.88809 147.8992 |
            |-------------------------------------------|
            51. | 52 12592.28 -69.14545 146.4715 |
            52. | 53 12592.28 -69.14545 146.4715 |
            53. | 54 12777.39 -71.34354 144.4498 |
            54. | 55 11348.56 -46.87218 142.1075 |
            55. | 56 11344.62 -45.21507 140.4135 |
            |-------------------------------------------|
            56. | 57 13295.98 -88.66376 122.9301 |
            57. | 58 12558.13 -81.63242 122.5333 |
            58. | 59 12581.88 -82.92133 121.1154 |
            59. | 60 9252.308 -45.91871 118.5079 |
            60. | 61 8607.44 -59.23783 117.826 |
            |-------------------------------------------|
            61. | 62 7754.326 -60.0155 117.1789 |
            62. | 63 7848.948 -62.71835 115.6931 |
            63. | 64 9341.412 -131.3444 113.446 |
            64. | 65 8713.549 -124.0916 110.9693 |
            65. | 66 9109.488 -130.7823 101.7588 |
            |-------------------------------------------|
            66. | 67 7188.92 -110.3453 100.3528 |
            67. | 68 7676.8 -135.2735 96.7711 |
            68. | 69 6146.195 -118.3158 95.47392 |
            69. | 70 5978.788 -121.315 94.76587 |
            70. | 71 4076.07 -83.25227 84.51991 |
            |-------------------------------------------|
            71. | 72 4041.676 -82.50186 83.24106 |
            72. | 73 4341.403 -89.66816 82.50635 |
            73. | 74 4715.698 -94.406 82.50603 |
            +-------------------------------------------+
            
            .
            Last edited by Carlo Lazzaro; 08 Nov 2021, 05:28.
            Kind regards,
            Carlo
            (Stata 19.0)

            Comment

            Working...
            X