Stata:跨变量的相同值的最大连续出现次数

use*_*264 1 stata

我的数据集中的观察是玩家,temp1如果玩家进行了移动,则二进制变量up等于1,否则等于零.我想计算每位玩家的最大连续移动次数.

+------------+------------+-------+-------+-------+-------+-------+-------+
| simulation | playerlist | temp1 | temp2 | temp3 | temp4 | temp5 | temp6 |
+------------+------------+-------+-------+-------+-------+-------+-------+
|          1 |          1 |     0 |     1 |     1 |     1 |     0 |     0 |
|          1 |          2 |     1 |     0 |     0 |     0 |     1 |     1 |
+------------+------------+-------+-------+-------+-------+-------+-------+

我的想法是在循环中生成辅助变量,它会计算连续的重复项,然后应用egen,rowmax():

+------------+------------+------+------+------+------+------+------+------+
| simulation | playerlist | aux1 | aux2 | aux3 | aux4 | aux5 | aux6 | _max |
+------------+------------+------+------+------+------+------+------+------+
|          1 |          1 |    0 |    1 |    2 |    3 |    0 |    0 |    3 |
|          1 |          2 |    1 |    0 |    0 |    0 |    1 |    2 |    2 |
+------------+------------+------+------+------+------+------+------+------+

我正在努力引入一个本地计数器变量,如果连续移动将逐渐增加1,否则将重置为零(下面的代码保持辅助变量固定..):

    quietly forval i = 1/42 { /*42 is max number of variables temp*/
    local j = 1 
    gen aux`i'=.    
    local j = `j'+1
    replace aux`i'= `j' if temp`i'!=0
} 
Run Code Online (Sandbox Code Playgroud)

Nic*_*Cox 5

战术答案

您可以将move*变量连接成一个字符串,并查找1s的最长子字符串.

egen history = concat(move*) 

gen max = 0 
quietly forval j = 1/6 { 
    replace max = `j' if strpos(history, substr("111111", 1, `j')) 
} 
Run Code Online (Sandbox Code Playgroud)

如果数字远远超过6,请使用类似的东西

 local lookfor : di _dup(42) "1" 
 quietly forval j = 1/42 { 
     replace max = `j' if strpos(history, substr("`lookfor'", 1, `j')) 
 } 
Run Code Online (Sandbox Code Playgroud)

参阅http://www.stata-journal.com/article.html?article=dm0056

战略答案

就Stata而言,以行为顺序存储序列对谷物起作用.如果您reshape longtsset您的数据作为面板数据,则可以获得更大的灵活性.请注意,此处使用的代码tsspell必须使用SSC安装ssc inst tsspell.

tsspell致力于识别某些条件仍然存在的法术或运行.这里的条件是变量是1,因为唯一的其他允许值是0,等于变量为正.tsspell创建三个变量,给出法术标识符,法术中的序列以及该法术是否结束.这里法术的最大长度只是每个游戏的最大序列号.

. input simulation playerlist temp1 temp2 temp3 temp4 temp5 temp6 

 simulat~n  playerl~t      temp1      temp2      temp3      temp4      temp5      temp6
 1.   1   1  0  1  1  1  0  0 
 2.   1   2  1  0  0  0  1  1 
 3. end 

. reshape long temp , i(sim playerlist) j(seq) 
(note: j = 1 2 3 4 5 6)

 Data                               wide   ->   long
 -----------------------------------------------------------------------------
 Number of obs.                        2   ->      12
 Number of variables                   8   ->       4
 j variable (6 values)                     ->   seq
 xij variables:
                   temp1 temp2 ... temp6   ->   temp
 -----------------------------------------------------------------------------

. egen id = group(sim playerlist) 

. tsset id seq 
   panel variable:  id (strongly balanced)
    time variable:  seq, 1 to 6
            delta:  1 unit

. tsspell, p(temp) 

. egen max = max(_seq), by(id) 

. l

      +--------------------------------------------------------------------+
      | simula~n   player~t   seq   temp   id   _seq   _spell   _end   max |
      |--------------------------------------------------------------------|
   1. |        1          1     1      0    1      0        0      0     3 |
   2. |        1          1     2      1    1      1        1      0     3 |
   3. |        1          1     3      1    1      2        1      0     3 |
   4. |        1          1     4      1    1      3        1      1     3 |
   5. |        1          1     5      0    1      0        0      0     3 |
      |--------------------------------------------------------------------|
   6. |        1          1     6      0    1      0        0      0     3 |
   7. |        1          2     1      1    2      1        1      1     2 |
   8. |        1          2     2      0    2      0        0      0     2 |
   9. |        1          2     3      0    2      0        0      0     2 |
  10. |        1          2     4      0    2      0        0      0     2 |
      |--------------------------------------------------------------------|
  11. |        1          2     5      1    2      1        2      0     2 |
  12. |        1          2     6      1    2      2        2      1     2 |
      +--------------------------------------------------------------------+
Run Code Online (Sandbox Code Playgroud)