Hi I'm trying to replicate a variation of Midrigan (2011)' s price filtering algorithm due to Minghao (2023), which needs to calculate modes in a time window like this:

My data looks like this:
id t price wanted
1001001 1 99.35517131 99.355171
1001001 2 99.35517131 95.252008
1001001 3 95.2520079 95.252008
1001001 4 91.14884448 95.252008
1001001 5 93.05388464 95.252008
1001001 6 92.32117688 95.252008
1001001 7 95.2520079 95.252008
1001001 8 96.35106952 95.252008
1001001 9 93.56678006 108.14766
1001001 10 96.20452797 108.14766
1001001 11 109.7596214 108.14766
1001001 12 108.7338306 108.14766
1001001 13 108.1476643 108.14766
1001001 14 108.1476643 108.14766
1001001 15 108.1476643 108.14766
1001001 16 108.1476643 108.14766
1001001 17 108.1476643 108.14766
1001001 18 108.1476643 108.14766
1001001 19 110.6388707 108.14766
1001001 20 109.906163 108.14766
1001001 21 109.906163 108.14766
1001001 22 106.8287904 108.14766
1001001 23 110.1992461 109.90616
1001001 24 109.906163 109.90616
and the wanted column is generated like this:
cap program drop my_mode
program my_mode
egen wanted = mode(price), min
end
rangerun my_mode, interval(t -5 5)
but this is not quite what I need, it is correct for the first and last observations but i need a longer window for the obsservations in the middle
My data looks like this:
id t price wanted
1001001 1 99.35517131 99.355171
1001001 2 99.35517131 95.252008
1001001 3 95.2520079 95.252008
1001001 4 91.14884448 95.252008
1001001 5 93.05388464 95.252008
1001001 6 92.32117688 95.252008
1001001 7 95.2520079 95.252008
1001001 8 96.35106952 95.252008
1001001 9 93.56678006 108.14766
1001001 10 96.20452797 108.14766
1001001 11 109.7596214 108.14766
1001001 12 108.7338306 108.14766
1001001 13 108.1476643 108.14766
1001001 14 108.1476643 108.14766
1001001 15 108.1476643 108.14766
1001001 16 108.1476643 108.14766
1001001 17 108.1476643 108.14766
1001001 18 108.1476643 108.14766
1001001 19 110.6388707 108.14766
1001001 20 109.906163 108.14766
1001001 21 109.906163 108.14766
1001001 22 106.8287904 108.14766
1001001 23 110.1992461 109.90616
1001001 24 109.906163 109.90616
and the wanted column is generated like this:
cap program drop my_mode
program my_mode
egen wanted = mode(price), min
end
rangerun my_mode, interval(t -5 5)
but this is not quite what I need, it is correct for the first and last observations but i need a longer window for the obsservations in the middle