组合多项以创建一个虚拟变量

combining multiple items to create one dummy variable

我在 Stata 中有 7 个 items/variables 解决了相同的调查问题。这7项分别是不同的体重控制行为(节食、运动、吃药等)。我试图组合这些变量来创建一个单一的体重控制行为虚拟变量,它被编码为是(参与体重控制)和否(没有参与体重控制)。

对于给定的体重控制行为,每个变量的响应选项看起来像这样

dieted
11438 0 not marked
2771 1 marked
16 6 refused
6508 7 legitimate skip
13 8 don’t know

这是我的代码。我将所有 7 个变量的 6、7、8 重新编码为缺失:

tab1 h1gh30a-h1gh30g,m`
foreach X of varlist h1gh30a-h1gh30g {
    replace `X'=. if `X' > 1
}
egen wgt_control= rowmax(h1gh30a-h1gh30g)
ta wgt_control
gen wgt_control_new=wgt_control
replace wgt_control_new = 1 if wgt_control>0 & wgt_control!=. 
replace wgt_control_new= 0 if wgt_control <1   
ta wgt_control_new

我使用 rowmax() 组合了所有 7 个项目,但我的问题是当我将它制成表格时,响应选项 0 或 No 没有出现。我只得到那些回答 yes=1.

这里没有最小可重现的例子,所以我们无法独立重现问题。

从您的代码来看,h1gh30a-h1gh30g 似乎被重新编码,因此所有值均为 0、1 或缺失,因此它们的最大值采用相同的值之一。

gen wgt_control_new = wgt_control
replace wgt_control_new = 1 if wgt_control>0 & wgt_control!=. 
replace wgt_control_new= 0 if wgt_control <1   

似乎归结为克隆变量:

gen wgt_control_new = wgt_control 

简而言之,我在你的代码中看不出为什么你永远不会看到 0 作为可能的结果。

编辑

对是否存在未按应有显示的零进行最低限度检查

egen max = rowmax(h1gh30a-h1gh30g)  

list high30a-high30g if max == 0 
```

这里有一个建议,其中包含一个我认为最干净的方法的可重现示例。我还提供了一些关于调查数据最佳实践的主动建议

* Example generated by -dataex-. For more info, type help dataex
clear
input double(h1gh30a h1gh30b h1gh30c)
1 1 1
1 0 1
6 1 8
0 0 0
7 6 8
end

* Explicit coding is better, so if possible, which it is with 7 vars,
* create a local with the vars are explicitly listed
local wgt_controls h1gh30a h1gh30b h1gh30c

* Recode is a better command to use here. And do not destroy information,
* there is a survey data quality assurance difference between respondent 
* refusing to answer, not knowing or question skipped. You can replace this
* survey codes with these extended missing values that behaves like missing values
* but retain the differences in the survey codes
recode `wgt_controls' (6=.a) (7=.b) (8=.c)

* While rowmax() could be used, I think it seems like anymatch() fits
* what you are trying to do better
egen wgt_control = anymatch(`wgt_controls'), values(1)