计算变量组合

问题描述 投票:0回答:1

我正在 Stata 中计算变量组合,我有以下四个贫困变量:

improved_toilet 0 "No" 1"Yes" // 1 means household lacks toilet,0 means no lack      
improved_water 0 "No" 1"Yes" // 1 means household lacks water,0 means no lack  
durable_house 0 "No" 1"Yes" // 1 means household lacks durable house, 0 means no lack  
living_spce 0 "No" 1"Yes" // 1 means household lacks living space, 0 means no lack

问题是,对于下面的代码,如果我有 15 个变量,每个变量有 10 个类别,那么几乎不可能手动计算组合。

我想知道是否有一种方法可以使这项工作变得更容易、更少的手动操作,并具有更高效的编码。

    ** Hence
    gen poverty= (1-improved_toilet) + (1-improved_water) + (1-durable_house) + (1-living_spce)
    
    
    ****************
    * SIngle Deprivations. => poverty == 1
    ***************
    **  
     
    * Single  combinations: there are 4C1 = 4!/1!3! = 4
                                       
    *lack improved_toilet improved_water - A
    gen lack_toilet_only = 0
    replace lack_toilet_only = 1 if improved_toilet == 1 & poverty == 1
    
    *lack improved_water only  - B
    gen lack_water_only = 0
    replace lack_water_only = 1 if improved_water == 1 & poverty == 1
    
    *lack durable_house  only  - C
    gen lack_house_only = 0
    replace lack_house_only = 1 if durable_house == 1 & poverty == 1
    
    *lack Living space  only  - D
    gen lack_living_only = 0
    replace lack_living_only = 1 if living_spce == 1 & poverty == 1
    
    *******************************************************************************
    ** Pairwise deprivations: => poverty == 2
    *******************************************************************************
    
     /* Pairwise combinations: there are 4C2 = 4!/2!2! = 6 pairs that can be generated
    *** our list improved_toilet improved_water durable_house living_spce if pair_poor==1
    -                  A            B                  C         D
    -The combos are: AB, AC, AD, BC , BD, CD.                                     */
    
                                        
    *lack improved_toilet improved_water - AB
    gen lack_toilet_water = 0
    replace lack_toilet_water  = 1 if improved_toilet==1 & improved_water ==1 &  poverty == 2 
    
    *lack improved_toilet & durable_house - AC
    gen lack_toilet_house =0
    replace lack_toilet_house = 1 if improved_toilet==1 & durable_house ==1 &  poverty == 2 
    
    *lack improved_toilet and living_spce - AD
    gen lack_toilet_living_spce =0
    replace lack_toilet_living_spce = 1 if improved_toilet==1 & living_spce ==1 &  poverty == 2 
    
    *lack improved water and durable house - BC
    gen lack_water_house =0
    replace lack_water_house = 1 if improved_water ==1 & durable_house==1 &  poverty == 2 
    
    *lack improved water and and living_spce - BD
    gen lack_water_living_spce =0
    replace lack_water_living_spce = 1 if improved_water ==1 & living_spce==1 &  poverty == 2 
    
    *lack durable house and living_spce - CD
    gen lack_house_living_spce =0
    replace lack_house_living_spce = 1 if durable_house==1 & living_spce==1 &  poverty == 2 
    
    ************************************************************************************
     ** Triple deprivations poverty: Having 3 deprivations  =>poverty == 3
     **********************************************************************************
     
     /* Poverty by 3 deprivations: since the order doesnt matter, This is combination,
    n is the total number of items and r is the number of items to be chosen, hence 4C3
    => there are = n!/(r!(n-r)!) = 4!/(3!(4-3)!) = = 24/(6*1) = 4 combinations. 
    *** our list improved_toilet improved_water durable_house living_spce if tri_poor==1
    -                  A            B                  C         D
    -The combos are: ABC,ABD,BCD,CDA.                                             */ 
                                         
    
    *lack improved_toilet improved_water and durable_house - ABC
    gen lack_toilet_water_house = 0
    replace lack_toilet_water_house  = 1 if improved_toilet==1 & improved_water ==1& durable_house ==1 & poverty == 3
    
    **lack improved_toilet improved_water and living_spce - ABD
    gen lack_toilet_water_living_spce = 0
    replace lack_toilet_water_living_spce  = 1 if improved_toilet==1 & improved_water ==1 & living_spce ==1 & poverty == 3
    
    **lack improved_water,durable_house and  living_spce - BCD
    gen lack_water_house_living_spce = 0
    replace lack_water_house_living_spce = 1 if improved_water ==1 & durable_house ==1 & living_spce ==1 & poverty == 3
    
    **lack durable_house,  living_spce and improved_toilet   - CDA
    gen lack_house_living_spce_toilet = 0
    replace lack_house_living_spce_toilet = 1 if durable_house ==1 & living_spce ==1 & improved_toilet==1  & poverty == 3
    
    
    ***********************************************************************************
    ** All 4 deprivatisons poverty:   =>poverty == 4
    ********************************************************************************
    ** Combinations Here are 4C4 = 4!/(4!1!) =1 
    
     gen lacks_toilet_water_house_living_spce = 0
     replace lacks_toilet_water_house_living_spce   = 1 if poverty == 4 
     lab var lacks_toilet_water_house_living_spce  "Household lacks all four deprivations"
combinations stata combinatorics
1个回答
0
投票

在最小的层面上,您可以通过注意(例如)来节省代码

gen lack_toilet_water_house = 0
replace lack_toilet_water_house  = 1 if improved_toilet==1 & improved_water ==1& durable_house ==1 & poverty == 3

可以简化为

gen lack_toilet_water_house = improved_toilet==1 & improved_water==1 & durable_house==1 & poverty==3

否则请参阅 SSC 的

tuples

ssc describe tuples 
ssc install tuples
© www.soinside.com 2019 - 2024. All rights reserved.