Stata can perform contrasts involving categorical variables and their interactions after almost any estimation command. Stata’s contrast provides a set of contrast operators that make it easy to specify named contrasts such as reference-level contrasts, adjacent contrasts, Helmert contrasts, and orthogonal polynomial contrasts. You can also specify your own custom contrasts. contrast can perform joint tests of these contrasts and can produce ANOVA-style tests of main effects, interaction effects, simple effects, and nested effects.
We can use contrasts to answer questions about the way a categorical variable relates to the response. If we fit the model
regress y i.agegroup we could use reverse adjacent contrasts, which are specified with the ar. operator, to test whether any age group could be combined with the previous age group.
. contrast ar.agegroup, nowald effects Contrasts of marginal linear predictions Margins : asbalanced
|Contrast Std. Err. t P>|t| [95% Conf. Interval]|
|(20-29 vs 10-19)||8.203575 3.771628 2.18 0.033 .6812991 15.72585|
|(30-39 vs 20-29)||13.33748 3.771628 3.54 0.001 5.815204 20.85976|
|(40-59 vs 30-39)||8.60962 3.771628 2.28 0.025 1.087345 16.1319|
|(60-79 vs 40-59)||8.611533 3.771628 2.28 0.025 1.089257 16.13381|
We could test whether there is a linear, quadratic, cubic, or even quartic trend using orthogonal polynomial contrasts, which are specified with the p. operator.
. contrast p.agegroup, noeffects Contrasts of marginal linear predictions Margins : asbalanced
|df F P>F|
|(linear)||1 139.11 0.0000|
|(quadratic)||1 0.15 0.6962|
|(cubic)||1 0.37 0.5448|
|(quartic)||1 0.43 0.5153|
|Joint||4 35.02 0.0000|
If we fit a two-way model
regress y agegroup##sex
we can test for main effects and interaction effects.
. contrast agegroup##sex, noeffects Contrasts of marginal linear predictions Margins : asbalanced
|df F P>F|
|agegroup||4 19.51 0.0000|
|sex||1 5.60 0.0229|
|agegroup#sex||4 1.75 0.1577|
In this case, we could have obtained these tests from anova. However, contrast can perform tests of main and interaction effects after other types of models.
We can test for a difference in the estimated cell means for men and women within each age group.
. contrast r.sex@agegroup Contrasts of marginal linear predictions Margins : asbalanced
|df F P>F|
|(female vs male) 10-19||1 1.13 0.2945|
|(female vs male) 20-29||1 3.36 0.0743|
|(female vs male) 30-39||1 5.00 0.0310|
|(female vs male) 40-59||1 0.41 0.5279|
|(female vs male) 60-79||1 2.71 0.1076|
|Joint||5 2.52 0.0448|
|Contrast Std. Err. [95% Conf. Interval]|
|(female vs male) 10-19||6.841855 6.441542 -6.176987 19.8607|
|(female vs male) 20-29||-11.80631 6.441542 -24.82515 1.212534|
|(female vs male) 30-39||-14.40607 6.441542 -27.42491 -1.387228|
|(female vs male) 40-59||-4.101691 6.441542 -17.12053 8.917151|
|(female vs male) 60-79||-10.60137 6.441542 -23.62022 2.417469|
margins works with contrast operators as well so that we can obtain contrasts of any margins that can be specified with this command, such as contrasts of the marginal predicted probabilities after logistic regression.