如何在 Stata 中使用字节格式列创建虚拟变量?

huangapple go评论66阅读模式
英文:

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行数据和变量yx1x2gender的数据集,最后一个变量应该与您的类似(即,包含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()&lt;0.3
recode gender 0=1 1=2
label define gender 1 &quot;Male&quot; 2 &quot;Female&quot;
label values gender gender
gen x1 = runiform()&lt;0.7
gen x2 = runiform()&lt;0.2
gen y = runiform()&lt;0.5

I can include a interaction term using the # operator

regress y x1 x2 x1#gender

huangapple
  • 本文由 发表于 2023年3月4日 03:37:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75631233.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定