Stata 斯塔塔:找出哪种动物越过边境来到德国

Stata 斯塔塔:找出哪种动物越过边境来到德国,stata,Stata,我想知道在过去的十年里,哪种动物最常越境到德国。 我的表格结构如下: +------+----------+-------------+ | Year | Animal | Coming from | +------+----------+-------------+ | 2000 | bear | netherlands | | 2001 | ant | netherlands | | 2001 | spider | netherlands | | 2002 | do

我想知道在过去的十年里,哪种动物最常越境到德国。 我的表格结构如下:

+------+----------+-------------+
| Year |  Animal  | Coming from |
+------+----------+-------------+
| 2000 | bear     | netherlands |
| 2001 | ant      | netherlands |
| 2001 | spider   | netherlands |
| 2002 | dog      | poland      |
| 2003 | dinosaur | swiss       |
| 2004 | ant      | austria     |
| 2005 | bear     | poland      |
| 2006 | ant      | austria     |
| 2007 | spider   | belgium     |
| 2007 | cat      | luxembourg  |
| 2008 | fish     | belgium     |
| 2008 | ant      | poland      |
| 2009 | dog      | poland      |
| 2010 | dog      | netherlands |
+------+----------+-------------+
这只是示例数据:我的实际表更大,有超过7k个条目

我想知道的是:

哪种动物每年至少来德国一次。在这里,我很难解释它。我的意思是,如果一只熊每年都来德国,不管它来自哪个国家,至少11年来德国一次。所以熊得到了11分。现在我想对不同的动物进行分组,所以我得到了一个由两列组成的表格,它告诉我哪种动物每年来自德国,这可能是11年中的5年。等等

如何在Stata中实现这一点?

请安装dataex,以便更好地为Stata问题提供数据示例:

ssc inst dataex 
中讨论了计算不同值的原则

这可能有助于:

clear 
input Year   str8 Animal   str11 Coming_from 
2000  bear      netherlands 
2001  ant       netherlands 
2001  spider    netherlands 
2002  dog       poland      
2003  dinosaur  swiss       
2004  ant       austria     
2005  bear      poland      
2006  ant       austria     
2007  spider    belgium     
2007  cat       luxembourg  
2008  fish      belgium     
2008  ant       poland      
2009  dog       poland      
2010  dog       netherlands 
end 

egen tag = tag(Coming_from Animal Year)
egen distinct = total(tag), by(Coming_from Year)
tabdisp Coming_from Year, c(distinct) 

------------------------------------------------------------------------------
            |                               Year                              
Coming_from | 2000  2001  2002  2003  2004  2005  2006  2007  2008  2009  2010
------------+-----------------------------------------------------------------
    austria |                            1           1                        
    belgium |                                              1     1            
 luxembourg |                                              1                  
netherlands |    1     2                                                     1
     poland |                1                 1                 1     1      
      swiss |                      1                                          
------------------------------------------------------------------------------

egen ndistinct = total(tag), by(Coming_from Animal) 

tabdisp Coming_from Animal, c(ndistinct) 

----------------------------------------------------------------------------
            |                             Animal                            
Coming_from |      ant     bear      cat dinosaur      dog     fish   spider
------------+---------------------------------------------------------------
    austria |        2                                                      
    belgium |                                                     1        1
 luxembourg |                          1                                    
netherlands |        1        1                          1                 1
     poland |        1        1                          2                  
      swiss |                                   1                           
----------------------------------------------------------------------------