英文:
How do I create a dummy variables using a byte format column in Stata?
问题
我有一个性别列,其中要么是"Female",要么是"Male",要么是缺失条目的"."。我想创建一个新列,如果性别是女性,则为1,如果性别是男性,则为0(对于缺失值继续为.)。我尝试执行gen Fem = gender == "Female"
,但这不起作用,我收到了r(109)错误。
我假设这是因为"gender"列是字节格式列。
另外,在Stata中,如何将虚拟变量列与回归模型中的变量交互?
英文:
I have a gender column that says either "Female", "Male", or . for missing entry. I want to create a new column that gives dummy variable 1 if gender is female and 0 is gender is male (and continuing to give . for missing value). I tried to do gen Fem = gender = "Female"
but this does not work and I get r(109) error.
I am assuming this is because the gender
column is a byte format column.
Also, how do I interact dummy variable column with variables in a regression model on Stata?
答案1
得分: 2
查看help fvvarlist
,并查看有关二进制运算符#
的信息。
对于一个具体的示例,请查看下面的代码,其中我创建了一个包含100行数据和变量y
、x1
、x2
和gender
的数据集,最后一个变量应该与您的类似(即,包含Male和Female标签的字节型变量)。
clear
set obs 100
set seed 123
gen byte gender = runiform() < 0.3
recode gender 0=1 1=2
label define gender 1 "Male" 2 "Female"
label values gender gender
gen x1 = runiform() < 0.7
gen x2 = runiform() < 0.2
gen y = runiform() < 0.5
我可以使用#
运算符来包含交互项:
regress y x1 x2 x1#gender
英文:
See help fvvarlist
, and check out the information on the binary operator #
For a specific example, see below where I create a 100 row dataset with variables y
, x1
, x2
, and gender
, the last of which should be similar to yours (i.e. byte with Male and Female labels)
clear
set obs 100
set seed 123
gen byte gender = runiform()<0.3
recode gender 0=1 1=2
label define gender 1 "Male" 2 "Female"
label values gender gender
gen x1 = runiform()<0.7
gen x2 = runiform()<0.2
gen y = runiform()<0.5
I can include a interaction term using the #
operator
regress y x1 x2 x1#gender
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论