如何通过stata中的多个变量保留崩溃中所有可能的组合?

时间:2016-10-17 18:57:51

标签: stata

当我通过多个变量collapse数据集时,默认情况下,Stata仅保留有观察值的组合,因此可以计算统计数据。我可以做些什么来保留所有组合,并且无法计算无法计算的统计数据?在一个例子中:

sysuse nlsw88, clear

collapse (mean) grade, by(race occupation)

table occupation race

--------------------------------------------
                       |        race        
            occupation | white  black  other
-----------------------+--------------------
Professional/technical |     1      1      1
        Managers/admin |     1      1      1
                 Sales |     1      1      1
    Clerical/unskilled |     1      1       
             Craftsmen |     1      1       
            Operatives |     1      1      1
             Transport |     1      1       
              Laborers |     1      1      1
               Farmers |     1              
         Farm laborers |     1      1       
               Service |     1      1       
     Household workers |     1              
                 Other |     1      1       
--------------------------------------------

. list if race == 2 & occupation == 9

你可以看到没有对黑人农民的观察。我想拥有的是:

. list if race == 2 & occupation == 9

     +--------------------------+
     |  race   occupa~n   grade |
     |--------------------------|
 32. | black    Farmers       . |
     +--------------------------+

1 个答案:

答案 0 :(得分:2)

您可以使用fillin

sysuse nlsw88, clear
collapse (mean) grade, by(race occupation)

fillin occupation race
table occupation race


                       |        race        
            occupation | white  black  other
-----------------------+--------------------
Professional/technical |     1      1      1
        Managers/admin |     1      1      1
                 Sales |     1      1      1
    Clerical/unskilled |     1      1      1
             Craftsmen |     1      1      1
            Operatives |     1      1      1
             Transport |     1      1      1
              Laborers |     1      1      1
               Farmers |     1      1      1
         Farm laborers |     1      1      1
               Service |     1      1      1
     Household workers |     1      1      1
                 Other |     1      1      1
--------------------------------------------

list if race == 2 & occupation == 9

     +------------------------------------+
     |  race   occupa~n   grade   _fillin |
     |------------------------------------|
 26. | black    Farmers       .         1 |
     +------------------------------------+

根据fillin的帮助文件:

  

fillin 添加缺少数据的观察结果,以便 varlist 的所有互动都存在