Interaction terms
There are several ways to specify interaction terms:
- One alternative is to first generate the interaction term as a new variable and to then include it in the regression
- A more flexible way of including interactions is to use # or ## within the regression command
* Including interaction terms
webuse nhanes2, clear
// creating interaction term manually
gen interaction = age * female
reg bmi age female interaction
// A more flexible way of including interactions is to use # or ##. This is identical to the one above:
reg bmi c.age##i.female
// The prefix c. indicates that age is to be treated as a continuous variable and i. indicates that female is a categorical (binary) variable.
// Put ## between the variables to include both main effects and interaction terms.
// Put # between the variables to include only the interaction terms(hard to interpret):
reg bmi c.age#i.female
// If you want to interact each level of age with female replace c. by i.
reg bmi i.age#i.female