Announcement

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

  • How to refer back to a variable amount of prior observations

    Hi,

    I've a data set with two observations each for 12 different sport matches - one obs per team.

    var match_id
    var team
    var win: win dummy
    var prob: ex ante probability of team i winning match j.

    What I'm interested in is identifying consecutive wins of a team and the probability of that run (i.e. the product of the ex ante probabilities of the matches consecutively won prior to match j)

    I generate the var counter giving me prior consecutive wins as follows.

    sort team match_id
    gen counter=0
    by team: replace counter = counter[_n-1]+1 if win[_n-1] == 1

    Now my question: How do I calculate the product of the x = counter last values of prob?
    E.g. for the third obs below I'd like to calculate prob[_n-2] * prob[_n-1]; for the fourth obs prob[_n-3]*prob[_n-2]*prob[_n-1] and so on.

    My approach would have been to run a for loop for every observation counting from x=1 to x=counter and multiplying prob[_n-x] each time. But I'm struggling there and not really sure if that's the right idea.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte match_id str5 team byte win double prob float counter
     1 "Alpha" 1  .8 0
     3 "Alpha" 1 .65 1
     5 "Alpha" 1 .85 2
     7 "Alpha" 1  .6 3
     9 "Alpha" 1  .6 4
    11 "Alpha" 1 .75 5
     1 "Beta"  0  .2 0
     4 "Beta"  1  .1 0
     6 "Beta"  0  .3 1
     7 "Beta"  0  .4 0
    10 "Beta"  1  .8 0
    12 "Beta"  0  .3 1
     2 "Delta" 0  .6 0
     4 "Delta" 0  .9 0
     5 "Delta" 0 .15 0
     8 "Delta" 0  .5 0
    10 "Delta" 0  .2 0
    11 "Delta" 0 .25 0
     2 "Gamma" 1  .4 0
     3 "Gamma" 0 .35 1
     6 "Gamma" 1  .7 0
     8 "Gamma" 1  .5 1
     9 "Gamma" 0  .4 2
    12 "Gamma" 1 .07 0
    end
    Thanks a lot for your help!



  • #2
    Welcome to Statalist. Thank you, as a new poster, for taking the time to read the FAQ and present a clear description of your problem, complete with sample data presented using CODE delimiters.

    With that said, I think you have the answer staring you in the face. I based the following code for probprod on trivial changes to your code for counter. It seems to do what I understand you to want.
    Code:
    sort team match_id
    gen probprod = 1
    by team: replace probprod = prob[_n-1]*probprod[_n-1] if win[_n-1] == 1
    list, noobs sepby(team)
    Code:
    . list, noobs sepby(team)
    
      +----------------------------------------------------+
      | match_id    team   win   prob   counter   probprod |
      |----------------------------------------------------|
      |        1   Alpha     1     .8         0          1 |
      |        3   Alpha     1    .65         1         .8 |
      |        5   Alpha     1    .85         2        .52 |
      |        7   Alpha     1     .6         3       .442 |
      |        9   Alpha     1     .6         4      .2652 |
      |       11   Alpha     1    .75         5     .15912 |
      |----------------------------------------------------|
      |        1    Beta     0     .2         0          1 |
      |        4    Beta     1     .1         0          1 |
      |        6    Beta     0     .3         1         .1 |
      |        7    Beta     0     .4         0          1 |
      |       10    Beta     1     .8         0          1 |
      |       12    Beta     0     .3         1         .8 |
      |----------------------------------------------------|
      |        2   Delta     0     .6         0          1 |
      |        4   Delta     0     .9         0          1 |
      |        5   Delta     0    .15         0          1 |
      |        8   Delta     0     .5         0          1 |
      |       10   Delta     0     .2         0          1 |
      |       11   Delta     0    .25         0          1 |
      |----------------------------------------------------|
      |        2   Gamma     1     .4         0          1 |
      |        3   Gamma     0    .35         1         .4 |
      |        6   Gamma     1     .7         0          1 |
      |        8   Gamma     1     .5         1         .7 |
      |        9   Gamma     0     .4         2        .35 |
      |       12   Gamma     1    .07         0          1 |
      +----------------------------------------------------+

    Comment


    • #3
      Well, now I feel a bit stupid haha

      That's exactly what I need.

      Thank you for the help and the warm welcome!

      Comment

      Working...
      X