I am computing combinations of variables in Stata and I have four poverty variables below:
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
The issue is, with my code below, if I had say 15 variables with say 10 categories each it would be almost impossible to manually compute the combinations.
I would like to know if there is a way to make this work easier and less manual, with more efficient coding.
** 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"
At the smallest level you can save on code by noting that (e.g.)
gen lack_toilet_water_house = 0
replace lack_toilet_water_house = 1 if improved_toilet==1 & improved_water ==1& durable_house ==1 & poverty == 3
can be reduced to
gen lack_toilet_water_house = improved_toilet==1 & improved_water==1 & durable_house==1 & poverty==3
Otherwise see tuples
from SSC.
ssc describe tuples
ssc install tuples