Stata:变量间连续出现相同值的最大次数 [英] Stata: Maximum number of consecutive occurrences of the same value across variables

查看:62
本文介绍了Stata:变量间连续出现相同值的最大次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据集中的观察值是玩家,如果玩家移动,则二进制变量temp1up等于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,否则将重置为0(下面的代码保持辅助变量不变):

    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
} 

推荐答案

战术答案

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

egen history = concat(move*) 

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

如果数字远远大于6,请使用类似

的内容
 local lookfor : di _dup(42) "1" 
 quietly forval j = 1/42 { 
     replace max = `j' if strpos(history, substr("`lookfor'", 1, `j')) 
 } 

同时比较http://www.stata-journal.com/article.html?article=dm0056

战略答案

就Stata而言,按行存储序列是不利于颗粒的。如果您reshape longtsset您的数据作为拼板数据,则可以获得更大的灵活性。注意,这里的代码使用tsspell,它必须使用ssc inst tsspell从SSC安装。

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 |
      +--------------------------------------------------------------------+

这篇关于Stata:变量间连续出现相同值的最大次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆