Announcement

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

  • not sorted error when combining bysort with xtreg

    I am getting a "not sorted error" when attempting to run a time series regression for different "styles" (ie different categories) of funds. Any suggestions on applying the bysort would be appreciated - or suggestions on alternative approaches to conducting the exercise would also be appreciated. Thank you, Dan
    The data looks like
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long id float month str1 retail_fund str4 style float(age ff size)
    90727 696 "Y" "EDYG" 10      .47116  -.5108256
    90727 697 "Y" "EDYG" 11   -.1180397  -.6931472
    90727 698 "Y" "EDYG" 12      .41804   -.356675
    90727 699 "Y" "EDYG" 13   .13305917 -.22314353
    90727 700 "Y" "EDYG" 14    -.023651 -.22314353
    90727 701 "Y" "EDYG" 15    -.001185 -.22314353
    90913 696 "Y" "EDYG" 10   .12215516  2.6602595
    90913 697 "Y" "EDYG" 11 .0046509327   2.631889
    90913 698 "Y" "EDYG" 12     .103537   2.714695
    90913 699 "Y" "EDYG" 13   .07039671   2.785011
    90913 700 "Y" "EDYG" 14  .015616804   2.827314
    90913 701 "Y" "EDYG" 15   .03278398   2.862201
    end
    format %tmYYMon month
    and the code for running the regressions across styles is
    Code:
    bysort style: xtreg ff L.ff size age if retail_fund=="Y", re
    (there are other funds in the full data set - ie some which are not retail and other types of style - but I hope this sample of the data helps set out the idea) thanks again, Dan

  • #2
    You don't show your -xtset- command. I assume it was -xtset id month-.

    Your problem is that in order to use the lag operator L., the data must be sorted by the same variables that defined the -xtset-. Because you sorted by style for this command, you scrambled the data and cannot use the lag operator.

    There are a few ways to work around this. The most direct is to simply create a lagged ff variable and then abandon the use of the lag operator in the -xtreg- command:
    Code:
    gen lff = L.ff
    bysort style: xtreg lff size age if retail_fund == "Y", re
    Another approach is to do the regressions inside a loop over style
    Code:
    levelsof style, local(styles)
    xtset
    foreach s of local styles {
        xtreg L.ff size age if retail_fund == "Y" & style == `"`s'"'
    }
    Note that these two approaches will produce the same, correct, results if each style-based group of observations contains the full range of observations for each id that appears in the style based group. (That is, style must always be constant within each group defined by a value of id.) If that assumption is not true, then the two approaches will give different results because their estimation samples will differ. And you would have to figure out which approach is what you need.

    Comment


    • #3
      Thank you very much Clyde, yes, sorry, I forgot to include the -xtset- command and, yes, you are correct it was -xtset id month-
      Thank you for your solutions - very effective, thank you also for the insight into what was going wrong.
      One tiny dilemma I've hit in wrapping the exercise up,
      I can save the results to a new file, eg
      Code:
      save demo_data1.dta
      but when I update the data, eg
      Code:
      replace demo_data1.dta
      I get a "factor-variable and time-series operators not allowed" error?
      Sorry if it's something obvious, thank you, Dan
      Last edited by Dan Daugaard; 06 Oct 2018, 22:26.

      Comment


      • #4
        replace is for replacing variable values. The correct command is:
        Code:
        save demo_data1, replace
        Steve Samuels
        Statistical Consulting
        [email protected]

        Stata 14.2

        Comment


        • #5
          yes, of course it is, thank you very much Steve, I just had a minor brain freeze, sorry, Dan

          Comment

          Working...
          X