Announcement

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

  • Plotting the same regression coefficient over time with monthly data

    Hi everyone,


    I am trying to plot a regression coefficient over time with monthly data.
    I the the following commands so far:

    foreach i of varlist Date {
    reg ret1 CSV1 if Date == `i'
    estimates store Date`i'
    local allyears `allyears' Date`i' ||
    local labels `labels' `i'
    }

    I do not get the "allyears" variable.

    Does anyone know what the problem is?

    Thank you in advance.


    Attached Files

  • #2
    Your loop is not doing anything close to what you need.

    Look at what you wrote:
    Code:
    foreach i of varlist Date {
        reg ret1 CSV1 if Date == `i'
       // etc.
    }
    Now, when Stata sees that -foreach- command it says, everything after the word varlist is a list of variable names, and it is desired to use each of these variable names in turn going through the loop. The first, and only, such variable name turns out to be Date. So on the first time through the loop, Stata will execute
    Code:
    reg ret1 CSV1 if Date == Date
    because it substitutes Date for `i'. Now, of course Date == Date is always true, so this is just

    Code:
    reg ret1 CSV1
    In other words, the loop just does the regression on the entire data set. There are no second or later iterations in the loop because there are no other variable names listed after Date.

    What I think you want to do is to loop over the values of the variable Date. That requires a different approach.

    Code:
    levelsof Date, local(dates)
    foreach d of local dates {
        reg ret1 CSV1 if Date == `d'
        estimates store Date`d'
        local allyears `allyears' Date`d'
        local labels `labels' `d'
    }
    I do not get the "allyears" variable.
    There is no allyears variable to get. You have created allyears as a local macro, not a variable. And given what you have put in it, it wouldn't make any sense as a variable in any case. If you want to "see" it, run
    Code:
    display `"`allyears'"'
    after the loop.

    Finally, please learn to use the -dataex- command correctly. Posting a screenshot of the -dataex- output completely defeats the purpose of using -dataex-. The whole point of -dataex- is that somebody can copy and paste it from this Forum into the Stata do-editor and run it to create a replica of your example data. There is no way to import data from a screenshot!!!! So when you run -dataex- to create your example, copy the output to the clipboard and then paste that directly into the Forum editor. Please, never again post screenshots of anything to show data. I know you are trying to be helpful, but screenshots are not helpful at all.

    Comment


    • #3
      Thank you Clyde for your Feedback.
      I will copy the dataex data the next time.
      I tried to run your code, but I got the following error message:
      insufficient observations
      Do you know where the error is?
      input float(ret1 CSV1) long Date
      -.0033942265 .11431616 195212
      -.0026963584 .1160078 195301
      -.01419824 .10556794 195302
      -.027903305 .10272208 195303
      .005213543 .11152158 195304
      -.018722516 .11169485 195305
      .02429032 .10729206 195306
      -.0441937 .094188 195307
      .0020020013 .08268259 195308
      .04707441 .08208 195309
      .02870425 .09014045 195310
      .000300045 .09276966 195311
      .05263864 .1054452 195312
      .016840225 .09274592 195401
      .037174303 .09118357 195402
      .04362476 .11326773 195403
      .03138236 .11295878 195404
      .01075745 .1146617 195405
      .05116598 .13451011 195406
      -.023128344 .14632386 195407
      .06598579 .15163665 195408
      -.016561328 .14701386 195409
      .09834006 .14033738 195410
      .05632933 .1258591 195411
      .006018036 .14047754 195412
      .030660646 .1515092 195501
      -.0015987207 .14270325 195502
      .03158866 .14474168 195503
      .00934338 .12223857 195504
      .067692734 .11514062 195505
      .01918165 .10023466 195506
      .0021022065 .10095424 195507
      -.003593528 .08941474 195508
      -.026444066 .10444304 195509
      .072829984 .09997597 195510
      .015011558 .09315722 195511
      -.029845556 .09048647 195512
      .03841966 .09222206 195601
      .06865409 .09598375 195602
      .0028039236 .10618235 195603
      -.05067113 .10467258 195604
      .035412606 .09724966 195605
      .04959041 .11837994 195606
      -.031299695 .11820288 195607
      -.05010137 .12448286 195608
      .005213543 .11321782 195609
      .003606488 .12336638 195610
      .03210458 .12589112 195611
      -.03516676 .11634982 195612
      -.02038927 .11031085 195701
      .021528464 .108548 195702
      .0435204 .10250375 195703
      .03510203 .10545922 195704
      -.007372688 .1059801 195705
      .006621828 .12032104 195706
      -.04981635 .12308076 195707
      -.05804709 .11352706 195708
      -.04228017 .1189667 195709
      .02326654 .12696272 195710
      -.03834546 .14193478 195711
      .04770285 .16070096 195712
      -.015085063 .13627808 195801
      .03324052 .13581073 195802
      .03138236 .13421364 195803
      .02336887 .13257481 195804
      .02973347 .12150589 195805
      .04487786 .12550123 195806
      .01928357 .10454264 195807
      .04770285 .099738 195808
      .02562276 .1159516 195809
      .030557584 .13760905 195810
      .05284918 .15221724 195811
      .007125265 .11890766 195812
      .009545268 .12738687 195901
      .0028039236 .12621173 195902
      .03727803 .1618194 195903
      .017450511 .2108589 195904
      -.002496878 .18487665 195905
      .0322078 .211912 195906
      -.01380384 .1866824 195907
      -.04686621 .1552222 195908
      .01288227 .13904184 195909
      .016128685 .1554542 195910
      .02480259 .1745053 195911
      -.067419685 .1540375 195912
      .011768713 .1541551 196001
      -.016167874 .1549197 196002
      -.016954625 .1219312 196003
      .031691823 .17893103 196004
      .021017827 .19442762 196005
      -.02342136 .18610296 196006
      .030557584 .18163817 196007
      -.05814128 .1837728 196008
      -.007074854 .1636616 196009
      .0480172 .17600906 196010
      .04822683 .2092799 196011
      .06396235 .18753386 196012
      .036344897 .154421 196101
      .02932166 .17344926 196102
      .002904209 .1623458 196103
      end
      [/CODE]



      Comment


      • #4
        According to your data example you have just one observation for each distinct data. That's insufficient for regression. Perhaps you want instead regression for disjoint or moving windows, but you need to say more.

        Comment


        • #5
          Should be .... each distinct date.

          Comment


          • #6
            Thank for your reply.
            I gave the wrong code.
            the loop should include a rolling window regression of a year, thus 12 months
            I thought about something like this:

            foreach q of varlist dm {
            rangestat (reg) ret1 CSV1, int(dm 0 12) if dm == `q'
            estimates store dm`q'
            local allyears `allyears' dm`q' ||
            local labels `labels' `q'
            }

            but the "if" option does not work.

            Comment


            • #7
              you have told -foreach- that you are using a variable list but your "if" is checking values against one variable; I am not entirely sure what you are trying to do here but my guess is that you want to start with -levelsof-, saving the result in a local, and then use -foreach- on the local; see the last example in
              Code:
              help levelsof

              Comment


              • #8
                Several problems here.

                0.
                rangestat is from SSC. You're asked to explain that for the benefit of all readers who may not know.

                1. Your loop is a loop over one variable and so no loop at all. See Clyde in #2 commenting on your #1.

                2. if is not an option! The bug is that your if qualifier appears after the comma. You don't need it, any way.

                3. estimates store won't work as you wish after rangestat. At best it would remember only the last set of estimates but in fact rangestat doesn't call up regress.

                4. I don't understand what you think the local statements will do. See also point 1 above.

                On the face of it

                Code:
                rangestat (reg) ret1 CSV1, int(dm 0 12)
                may be all you need, followed by a suitable graph command.


                Comment


                • #9
                  Thank you Nick.
                  Indeed the rangestat command is all I need

                  Comment

                  Working...
                  X