我现在有30个虚拟变量,我想定义另一个解释变量与这30个虚拟变量的交互项,也就是30个交互项,不知怎么一次生成,请教高手。
你可以试一下使用stata自带的自动生成交叉变量的命令,Interaction expansion,或者是使用 Data > Create or change data > Other variable-creation commands > Interaction expansion 来生成交叉变量
如果是时间虚拟变量的话,回归命令:reg y x1 c.x1#i.year
这样做肯定不行,交互项不是简单的相乘,比如0,1和另一组也是0,1,按照交互原则,应该是有四个组合的,但这里相乘的话只有0,1两种结果;又比如1,2和1,2,3,这里应该是有6种情况,但如果相乘的话,就只有1,2,3,4,6五种情况了,肯定是不行的
Stata12做回归如何建立交互项
比如说 X、Y两个自变量,若要在stata12中进行回归时建立交互项,那么交互项该如何表示
建立新变量
gen z=x*y |
Title
[U] 11.4.3 Factor variables Description Factor variables are extensions of varlists of existing variables. When a command allows factor variables, in i.varname i.varname#i.varname i.varname#i.varname#i.varname i.varname##i.varname i.varname##i.varname##i.varname Factor variables create indicator variables from categorical variables, interactions of indicators of categorical There are four factor-variable operators: Operator Description The indicators and interactions created by factor-variable operators are referred to as virtual variables. They Categorical variables to which factor-variable operators are applied must contain nonnegative integers with values Factor variables may be combined with the L. and F. time-series operators. Remarks Remarks are presented under the following headings: Basic examples Basic examples Here are some examples of use of the operators: Factor i.group#i.sex indicators for each combination of levels of group and sex, a two-way interaction group#sex same as i.group#i.sex group#sex#arm indicators for each combination of levels of group, sex, and arm, a three-way interaction group##sex same as i.group i.sex group#sex group##sex##arm same as i.group i.sex i.arm group#sex group#arm sex#arm group#sex#arm sex#c.age two variables — age for males and 0 elsewhere, and age for females and 0 elsewhere; if age sex##c.age same as i.sex age sex#c.age c.age same as age c.age#c.age age squared c.age#c.age#c.age age cubed Base levels You can specify the base level of a factor variable by using the ib. operator. The syntax is Base If you want to use group==3 as the base in a regression, you can type, . regress y i.sex ib3.group You can also permanently set the base levels of categorical variables by using the fvset command. |
我觉得做交互项最好的方式还是先乘起来,再进入模型
anova wage children married children#married c.education
你用的周广肃的教材吧?他用stata10.0做的,你当然运行不了。没看提示嘛,那个命令只适合于11.0以下版本