寻找连续几个月的最长连胜纪录

时间:2019-02-03 16:56:37

标签: stata

我每个月都有不同产品的获利数据:

* Example generated by -dataex-. To install: ssc install dataex
clear
input float(id months)
1  6
1  7
1  8
1 10
1 11
1 13
1 14
1 15
1 16
1 17
2 10
2 11
2 12
2 16
2 17
2 18
2 19
2 21
2 22
2 25
end

社区贡献命令tsspell无法返回正确的结果:

tsspell, f(L.months == .)

如何找到每种产品连续几个月的最长连胜纪录?

2 个答案:

答案 0 :(得分:3)

首先计算该咒语,然后生成其持续时间,最后找到后者的最大值:

bysort id: generate s = sum((months - months[_n-1]) > 1)
bysort id s: generate d = _N
egen max = max(d), by(id)

list, sepby(id)

     +---------------------------+
     | id   months   s   d   max |
     |---------------------------|
  1. |  1        6   1   3     5 |
  2. |  1        7   1   3     5 |
  3. |  1        8   1   3     5 |
  4. |  1       10   2   2     5 |
  5. |  1       11   2   2     5 |
  6. |  1       13   3   5     5 |
  7. |  1       14   3   5     5 |
  8. |  1       15   3   5     5 |
  9. |  1       16   3   5     5 |
 10. |  1       17   3   5     5 |
     |---------------------------|
 11. |  2       10   1   3     4 |
 12. |  2       11   1   3     4 |
 13. |  2       12   1   3     4 |
 14. |  2       16   2   4     4 |
 15. |  2       17   2   4     4 |
 16. |  2       18   2   4     4 |
 17. |  2       19   2   4     4 |
 18. |  2       21   3   2     4 |
 19. |  2       22   3   2     4 |
 20. |  2       25   4   1     4 |
     +---------------------------+

答案 1 :(得分:2)

您的问题令人困惑,因为您无法证实tsspell不能返回正确结果的说法。但是,据我所知,tsspell确实按照您的数据示例的预期执行了-并且其结果与@Pearly Spencer的解决方案相匹配。

tsset id months
tsspell, f(L.months == .)
egen longest = max(_seq), by(id)

list, sepby(id _spell)

     +----------------------------------------------+
     | id   months   _spell   _seq   _end   longest |
     |----------------------------------------------|
  1. |  1        6        1      1      0         5 |
  2. |  1        7        1      2      0         5 |
  3. |  1        8        1      3      1         5 |
     |----------------------------------------------|
  4. |  1       10        2      1      0         5 |
  5. |  1       11        2      2      1         5 |
     |----------------------------------------------|
  6. |  1       13        3      1      0         5 |
  7. |  1       14        3      2      0         5 |
  8. |  1       15        3      3      0         5 |
  9. |  1       16        3      4      0         5 |
 10. |  1       17        3      5      1         5 |
     |----------------------------------------------|
 11. |  2       10        1      1      0         4 |
 12. |  2       11        1      2      0         4 |
 13. |  2       12        1      3      1         4 |
     |----------------------------------------------|
 14. |  2       16        2      1      0         4 |
 15. |  2       17        2      2      0         4 |
 16. |  2       18        2      3      0         4 |
 17. |  2       19        2      4      1         4 |
     |----------------------------------------------|
 18. |  2       21        3      1      0         4 |
 19. |  2       22        3      2      1         4 |
     |----------------------------------------------|
 20. |  2       25        4      1      1         4 |
     +----------------------------------------------+
相关问题