Announcement

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

  • Regression on observations prior to current id

    Hi,

    I have been struggling with coding this. I try to run to 2 loops but it failed. So, I hope someone can help me out.

    My data has, say, 2 groups (1&2). I'd like to regress coef_std on j_ln in a subsample of all observations which has id< current id, within each group. Then, I take coefficients on j_ln from that regression, assign into all observations in this subsample, which is called beta_pre

    For example:
    1) at id=3, group 1: I want to regress coef_std on j_ln of all observations with id=1 and id=2, then I take the coefficient on j_ln of that regression and assign into beta_pre of all observations with id=1 and id=2
    2) at id=22, group 2: I want to regress coef_std on j_ln of all observations with id=20, and id =21 (which is only in group 2, not from id=1 to id=19), then I take the coefficient on j_ln of that regression and assign into beta_pre of all observations with id=20 and id=21

    I hope I am clear about that.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    
    input byte Groups float(coef_std j_ln id beta_pre)
    1   -.1092731    .998055 1 .
    1  -.10569589    .998055 1 .
    1  -.05203765  -.8347107 2 .
    1 -.005533839  -.8347107 2 .
    1  -.05203765  -.8347107 2 .
    1  -.05203765  -.8347107 2 .
    1  -.10211867  -.8347107 2 .
    1  -.02341992  -.8347107 2 .
    1  -.05561486  -.8347107 2 .
    1  -.05919208  -.8347107 2 .
    1  -.11642753  -.8347107 2 .
    1  -.10569589  -.8347107 2 .
    1  -.12000475  -.8347107 3 .
    1   -.1987035  -.8347107 3 .
    1   .16974975  1.7107302 4 .
    1  -.06459367  1.6678954 4 .
    1   -.2022807  1.7107302 4 .
    1  -.09496424  -2.748872 4 .
    1   .08389656  -2.748872 4 .
    1   .04096997  1.7107302 4 .
    1  -.06051565  1.6678954 4 .
    1  -.20442705  -2.465104 5 .
    1   -.1142812  -2.465104 5 .
    1   -.0463141  -2.465104 5 .
    1   -.4634175  -2.465104 5 .
    1    -.154346  1.1565667 6 .
    1   -.0878098  -4.828314 6 .
    1  -.13431361  -4.828314 6 .
    1   -.1457607  1.1565667 6 .
    1  -.12000475  -4.828314 6 .
    1  -.12715918  -4.828314 6 .
    1  -.21480097  1.1565667 6 .
    1   -.3167516 -1.6144505 6 .
    1  -.03057435  -4.828314 6 .
    1  -.17008577  -4.828314 6 .
    1 -.034151565  1.1565667 6 .
    1  -.16185817  1.1565667 6 .
    1   -.1536306  1.1565667 6 .
    1  -.14146805 -1.6144505 6 .
    1  -.07707816  -4.828314 6 .
    1  -.13431361  -4.828314 6 .
    2 -.27092713   2.625393 20 .
    2  -.2986687   2.625393 20 .
    2  -.3726464   2.625393 20 .
    2  .08046687   2.625393 20 .
    2 -.20619665   2.625393 20 .
    2 -.13221897   2.625393 20 .
    2  -.3217868  1.1565667 21 .
    2  -.9237801  1.1565667 21 .
    2 -.12389648  1.1565667 21 .
    2   .3412382  1.1565667 21 .
    2 -.51227933  1.1565667 21 .
    2   .9450811  1.1565667 21 .
    2   .0157364  1.7107302 22 .
    2   .8017493  1.7107302 22 .
    2 -.01200523  1.7107302 22 .
    2   .5058386  1.7107302 22 .
    2  .11745571  1.7107302 22 .
    2 -.04899407  1.7107302 22 .
    2  .11745571  1.7107302 22 .
    2  .05272524  1.7107302 22 .
    2  .22842224  1.7107302 22 .
    2 -.28942153  1.7107302 23 .
    2 -.02125244 -.58519006 23 .
    2 -.12297176  1.7107302 23 .
    2  -.4373769  1.7107302 23 .
    2 -.13221897  1.7107302 23 .
    2   -.178455  1.7107302 23 .
    2 -.13221897  1.7107302 23 .
    2 -.12297176  1.7107302 23 .
    2 -.08598291  1.7107302 23 .
    2   .5613218  1.7107302 23 .
    2  -.1692078  1.7107302 23 .
    2 -.27092713 -.58519006 23 .
    2 -.03974686   1.791593 24 .
    2  .05272524   1.791593 24 .
    2  -.0767357  1.1565667 24 .
    2  -.3726464   1.791593 24 .
    2  -.2431855  1.1565667 24 .
    2 -.27092713   1.791593 24 .
    
    end

    Thank you!

  • #2
    I don't understand what you want to do here. On the one hand you say you want to use all id's less than current id. But then you say with id 2 you want to use both id = 1 and id = 2, but 2 is not less than itself. Did you mean <= current id?

    Then there is the question of where you want the results placed. When you get to, say, id 3 in group 1, you want to regress using all observations with id's 1, 2 (and 3?). Where do those results go. From your description you will put them in all the observations with ids 1 and 2 (and 3?) But that will overwrite what you did with id 2 where you used id 1 (and id 2?) and wrote results there. If you're going to overwrite like that, you may as well just do this process only for the last id in each group since everything before that will just get discarded.

    Please provide a clear description of what you want to do here.

    Comment


    • #3
      Hi,

      So, what I want is:
      At id 2, group 1: I use all observations with id=1, regress coef_std on j_ln, then assign beta_pre= the coefficient on j_ln from the regression for all observations with id=2.

      Likewise, at id=3, group 1: Use all observations with id=1 and id=2, regress coef_std on j_ln, then assign beta_pre= coefficient on j_ln from the regression for any observations with id=3.

      At id=22, group 2, use all observation with id=20 and id=21( which means any observations within group 2, and has id<22), regress coef_std on j_ln, then assign beta_pre= coefficient on j_ln from the regression, for any observations with id=22.

      And, yes, we should put the result on id=2, id=3, id=22, not what I explained in my first post. It was my mistake.

      Let me know if it is still unclear. Thank you!

      Comment


      • #4
        Thank you for the clarifications. Now I'm pretty sure I understand what you want:
        Code:
        rangestat (reg) coef_std j_ln, by(Groups) interval(id . -1)
        -rangestat- is written by Robert Picard, Nick Cox, and Roberto Ferrer. It is available from SSC.

        The output from -rangestat- will have the coefficients you are looking for in the variable b_j_ln. There are other regression results that will be found in the resulting data set as well. I have ignored your variable beta_pre. You can just use b_j_ln, but, obviously, you are free to copy b_j_ln to beta_pre if you wish, and drop the other -rangestat- outputs if they are of no use to you.

        Comment


        • #5
          The command did the trick. Thanks for your help, I appreciate it

          Comment

          Working...
          X