Announcement

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

  • Running regressions in loop and storing results

    Hello!

    The example of my data is provided below. My goal is to run a series of simple regressions for each of the individual asin codes (see examples below) and for each of the runs store the beta coefficient, standard error, and p-value in separate columns. What would be the appropriate way to do so?

    Thanks!

    Code:
    reg overall month if asin == "373250460"
    reg overall month if asin == "446697192"
    ///etc.
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str10 asin int month double overall
    "373250460"  528         4
    "373250460"  530         5
    "373250460"  531         5
    "373250460"  534         3
    "373250460"  535         1
    "373250460"  536         2
    "373250460"  540         3
    "373250460"  541         4
    "373250460"  543         4
    "373250460"  544         1
    "373250460"  545         1
    "373250460"  547         1
    "373250460"  548         1
    "373250460"  551         4
    "373250460"  553       1.5
    "373250460"  555         1
    "373250460"  564         3
    "373250460"  567         4
    "373250460"  568         4
    "373250460"  569         2
    "373250460"  579         2
    "373250460"  587         3
    "373250460"  591         4
    "373250460"  629         3
    "373250460"  634       4.5
    "373250460"  677         4
    "446697192"  594 3.9000001
    "446697192"  595 4.3333335
    "446697192"  596         3
    "446697192"  598         5
    "446697192"  601         4
    "446697192"  605         3
    "446697192"  611         3
    "446697192"  614         4
    "446697192"  624         2
    "446697192"  626         4
    "446697192"  628         4
    "446697192"  672         4
    "575074205"  567 3.6666667
    "575074205"  574         4
    "575074205"  583         4
    "575074205"  656         4
    "575074205"  670         2
    "575074205"  687         3
    "812566564"  478         5
    "812566564"  479         4
    "812566564"  483         4
    "812566564"  494         1
    "812566564"  504         4
    "812566564"  537         2
    "812566564"  630         2
    "812566564"  631         4
    "812566564"  650         5
    "812566564"  653         5
    "812566564"  666         5
    "812566564"  681         5
    "979526191"  610         4
    "979526191"  611         5
    "979526191"  612      3.25
    "979526191"  615         5
    "979526191"  616         5
    "979526191"  619         4
    "979526191"  633         1
    "979526191"  642         1
    "979526191"  643         1
    "979526191"  645         3
    "1039869017" 638         5
    "1039869017" 644         5
    "1059983354" 654         2
    "1059983354" 657         1
    "1059983354" 667         4
    "1059983354" 669         5
    "1060078031" 643 4.8000002
    "1060078031" 644         4
    "1060078031" 645 4.0833335
    "1060078031" 646 2.8666666
    "1060078031" 647         4
    "1060078031" 648 4.1333332
    "1060078031" 649 4.5384617
    "1060078031" 650 4.2142859
    "1060078031" 651 3.3636363
    "1060078031" 652 4.2222223
    "1060078031" 653 3.3333333
    "1060078031" 654 3.8888888
    "1060078031" 655       2.5
    "1060078031" 656 3.4000001
    "1060078031" 657         3
    "1060078031" 658         5
    "1060078031" 661         3
    "1060078031" 677         5
    "1060240718" 659         1
    "1060240718" 678         1
    "1061790827" 659         5
    "1061790827" 660         5
    "1061790827" 662         3
    "1061790827" 663         1
    "1061790827" 665         5
    "1400532620" 606         5
    "1400532620" 607         3
    "1400532620" 608 4.8333335
    end
    Last edited by Anton Ivanov; 15 Dec 2021, 16:07.

  • #2
    The simplest way to do this is:
    Code:
    rangestat (reg) overall month, by(asin) interval(overall . .)
    -rangestat- is by Robert Picard, Nick Cox, and Roberto Ferrer. It is available from SSC.

    Comment


    • #3
      Thank you Clyde Schechter

      Comment


      • #4
        Code:
        ssc install asreg
        
        . bys asin: asreg overall month
        
        . list in 1/10
        
             +----------------------------------------------------------------------------------------+
             |       asin   month     overall   _Nobs         _R2      _adjR2    _b_month     _b_cons |
             |----------------------------------------------------------------------------------------|
          1. | 1039869017     644           5     100   .02311415   .01314592   .00381027   1.1089697 |
          2. | 1039869017     638           5     100   .02311415   .01314592   .00381027   1.1089697 |
          3. | 1059983354     667           4     100   .02311415   .01314592   .00381027   1.1089697 |
          4. | 1059983354     654           2     100   .02311415   .01314592   .00381027   1.1089697 |
          5. | 1059983354     657           1     100   .02311415   .01314592   .00381027   1.1089697 |
             |----------------------------------------------------------------------------------------|
          6. | 1059983354     669           5     100   .02311415   .01314592   .00381027   1.1089697 |
          7. | 1060078031     643   4.8000002     100   .02311415   .01314592   .00381027   1.1089697 |
          8. | 1060078031     652   4.2222223     100   .02311415   .01314592   .00381027   1.1089697 |
          9. | 1060078031     646   2.8666666     100   .02311415   .01314592   .00381027   1.1089697 |
         10. | 1060078031     657           3     100   .02311415   .01314592   .00381027   1.1089697 |
             +----------------------------------------------------------------------------------------+
        More on asreg can be found here https://fintechprofessor.com/2017/12...ions-in-stata/
        Regards
        --------------------------------------------------
        Attaullah Shah, PhD.
        Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
        FinTechProfessor.com
        https://asdocx.com
        Check out my asdoc program, which sends outputs to MS Word.
        For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

        Comment


        • #5
          Thank you Attaullah Shah This is a great suggestion.

          Comment

          Working...
          X