Announcement

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

  • Piecewise Regression with a median as a knot

    Dear Stata Users

    My data is in panle format (firm, time). I need to estimate piesewise linear regression which unables to estimate two diffrent regression coefficients for a past return variable (ret, t-1).One estimated slope should refer to the segment above median past return (Winner), whereas the other slope referes to the segment below the median (Loser). Also I need to compute t-stat with two way clustered standard errors (firm and time).

    Regression: Flow j, t = Alpha+ Beta1 Ret j, t-1(Winner, above past cross-sectional return) + Beta2 Ret j, t-1 (Loser, below past cross-sectional median)+Error j,t

    Is there any specific Stata regression command which can execute the above equation?

    P.S. I have tried to create two sepate variables for Winner and Loser, but when i ran regress :
    Code:
    regress Flow Winner Loser, vce(cluster double_cluster)
    I get the following error: no observations r(2000);

    Thank you.

  • #2
    P.S. I have tried to create two separate variables for Winner and Loser, but when i ran regress ... I get the following error: no observations r(2000);
    Perhaps you should be addressing the question "why did this not work?" instead of "how can I figure out something new to try because I haven't figured out why this did not work?"

    I'd suggest you start with a review the Statalist FAQ linked to from the top of the page. Note especially sections 9-12 on how to best pose your question.

    Section 12.1 is particularly pertinent

    12.1 What to say about your commands and your problem

    Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!
    To do so, you should copy your commands that created the two separate variables and the subsequent regression command and what Stata did in respone from your Stata Results window and paste this into your Statalist post using code delimiters [CODE] and [/CODE] as described in section 12. My offhand guess is that you have

    The more you help others understand your problem, the more likely others are to be able to help you solve your problem.

    Comment


    • #3
      William

      Thanks for your reply.

      Below is a code along with error:

      Code:
      egen double_cluster=group(id date)
      gen winner= rtr if rtr>0
      gen loser=rtr if rtr<0
      regress Flow winner loser , vce(cluster double_cluster)
      no observations
      r(2000);
      summarize winner
      
          Variable |        Obs        Mean    Std. Dev.       Min        Max
      -------------+---------------------------------------------------------
            winner |  1,064,414     .023822     .027488   7.74e-06    .326012
      
      summarize loser
      
          Variable |        Obs        Mean    Std. Dev.       Min        Max
      -------------+---------------------------------------------------------
             loser |  1,111,980   -.0221023    .0246494  -.3730419  -4.92e-07
      The reason I have asked
      Is there any specific Stata regression command which can execute the above equation?
      is because I wanted to know if there is a specifically designed Stata command for piecewise regression with a median as a knot rather than going in a roundabout way.
      Last edited by Olena Onishchenko; 18 Dec 2018, 20:40.

      Comment


      • #4
        The problem lies with:
        Code:
        gen winner= rtr if rtr>0
        gen loser=rtr if rtr<0
        Each of these variables takes the value of rtr when rtr > 0 (winner) or when rtr < 0 (lower) and has missing values in all other observations. Consequently, either winner or loser is always going to have a missing value. Since an observation is omitted from a regression whenever any of the regression variables has a missing value, this means that every observation is omitted, so there are no observations left for the regression. You can verify this by running
        Code:
        count if !missing(winner, loser)
        and you will see the result is 0.

        You need to instead define winner and loser to be 0, not missing value, in the complementary regions.

        Code:
        gen winner = (cond(rtr > 0, rtr, 0))
        gen loser = cond(rtr < 0, 0, rtr)
        Or, you can accomplish the same thing by using Stata's -mkspline- command:
        Code:
        mkspline loser 0 winner = rtr
        See -help mkspline-

        Comment


        • #5
          Clyde

          Thank you. Your code suggestion worked.

          Comment

          Working...
          X