**[R] contrast** -- Contrasts and linear hypothesis tests after estimation

__Syntax__

**contrast** *termlist* [**,** *options*]

where *termlist* is a list of factor variables or interactions that appear
in the current estimation results. The variables may be typed with or
without contrast operators, and you may use any factor-variable syntax.

See the *operators (op.)* table below for the list of contrast operators.

*options* Description
-------------------------------------------------------------------------
Main
__over__**all** add a joint hypothesis test for all specified
contrasts
__asobs__**erved** treat all factor variables as observed
**lincom** treat user-defined contrasts as linear
combinations

Equations
__eq__**uation(***eqspec***)** perform contrasts in *termlist* for equation
*eqspec*
__ateq__**uations** perform contrasts in *termlist* within each
equation

Advanced
**emptycells(***empspec**)* treatment of empty cells for balanced factors
**noestimcheck** suppress estimability checks

Reporting
__l__**evel(***#***)** confidence level; default is **level(95)**
__mcomp__**are(***method***)** adjust for multiple comparisons; default is
**mcompare(noadjust)**
__noeff__**ects** suppress table of individual contrasts
__ci__**effects** show effects table with confidence intervals
__pv__**effects** show effects table with p-values
__eff__**ects** show effects table with confidence intervals and
p-values
**nowald** suppress table of Wald tests
__noatlev__**els** report only the overall Wald test for terms that
use the within **@** or nested **|** operator
__nosvy__**adjust** compute unadjusted Wald tests for survey results
**sort** sort the individual contrast values in each term
**post** post contrasts and their VCEs as estimation
results
*display_options* control column formats, row spacing, line width,
and factor-variable labeling
*eform_option* report exponentiated contrasts

**df(***#***)** use t distribution with *#* degrees of freedom for
computing p-values and confidence intervals
-------------------------------------------------------------------------
**df(***#***)** does not appear in the dialog box.

Term Description
-------------------------------------------------------------------------
Main effects
**A** joint test of the main effects of **A**
*r.***A** individual contrasts that decompose **A** using *r.*
Interaction effects
**A#B** joint test of the two-way interaction effects of
**A** and **B**
**A#B#C** joint test of the three-way interaction effects
of **A**, **B**, and **C**
*r.***A#***g.***B** individual contrasts for each interaction of **A**
and **B** defined by *r.* and *g.*
Partial interaction effects
*r.***A#B** joint tests of interactions of **A** and **B** within
each contrast defined by *r.***A**
**A#***r.***B** joint tests of interactions of **A** and **B** within
each contrast defined by *r.***B**
Simple effects
**A@B** joint tests of the effects of **A** within each
level of **B**
**A@B#C** joint tests of the effects of **A** within each
combination of the levels of **B** and **C**
*r.***A@B** individual contrasts of **A** that decompose **A@B**
using *r.*
*r.***A@B#C** individual contrasts of **A** that decompose **A@B#C**
using *r.*
Other conditional effects
**A#B@C** joint tests of the interaction effects of **A** and
**B** within each level of **C**
**A#B@C#D** joint tests of the interaction effects of **A** and
**B** within each combination of the levels of **C**
and **D**
*r.***A#***g.***B@C** individual contrasts for each interaction of **A**
and **B** that decompose **A#B@C** using *r.* and *g.*

Nested effects
**A|B** joint tests of the effects of **A** nested in each
level of **B**
**A|B#C** joint tests of the effects of **A** nested in each
combination of the levels of **B** and **C**
**A#B|C** joint tests of the interaction effects of **A** and
**B** nested in each level of **C**
**A#B|C#D** joint tests of the interaction effects of **A** and
**B** nested in each combination of the levels of
**C** and **D**
*r.***A|B** individual contrasts of **A** that decompose **A|B**
using *r.*
*r.***A|B#C** individual contrasts of **A** that decompose **A|B#C**
using *r.*
*r.***A#***g.***B|C** individual contrasts for each interaction of **A**
and **B** defined by *r.* and *g.* nested in each
level of **C**
Slope effects
**A#c.x** joint test of the effects of **A** on the slopes of
**x**
**A#c.x#c.y** joint test of the effects of **A** on the slopes of
the product (interaction) of **x** and **y**
**A#B#c.x** joint test of the interaction effects of **A** and **B**
on the slopes of **x**
**A#B#c.x#c.y** joint test of the interaction effects of **A** and **B**
on the slopes of the product (interaction) of
**x** and **y**
*r.***A#c.x** individual contrasts of **A**'s effects on the
slopes of **x** using *r.*
Denominators
**.../***term2* use *term2* as the denominator in the F tests of
the preceding terms
**.../** use the residual as the denominator in the F
tests of the preceding terms (the default if
no other **/**s are specified
-------------------------------------------------------------------------
**A**, **B**, **C**, and **D** represent any factor variable in the current estimation
results.
**x** and **y** represent any continuous variable in the current estimation
results.
*r***.** and *g***.** represent any contrast operator. See the table below.
**c.** specifies that a variable be treated as continuous; see fvvarlist.
Operators are allowed on any factor variable that does not appear to the
right of **@** or **|**. Operators decompose the effects of the associated
factor variable into one-degree-of-freedom effects (contrasts).
Higher-level interactions are allowed anywhere an interaction operator
(**#**) appears in the table.
Time-series operators are allowed if they were used in the estimation.
**_eqns** designates the equations in **manova**, **mlogit**, **mprobit**, and **mvreg** and
can be specified anywhere a factor variable appears.
**/** is allowed only after **anova**, **cnsreg**, **manova**, **mvreg**, or **regress**.

*operators (op.)* Description
-------------------------------------------------------------------------
**r**. differences from the reference (base) level; the
default
**a**. differences from the next level (adjacent
contrasts)
**ar**. differences from the previous level (reverse
adjacent contrasts)

As-balanced operators
**g**. differences from the balanced grand mean
**h**. differences from the balanced mean of subsequent
levels (Helmert contrasts)
**j**. differences from the balanced mean of previous
levels (reverse Helmert contrasts)
**p**. orthogonal polynomial in the level values
**q**. orthogonal polynomial in the level sequence

As-observed operators
**gw**. differences from the observation-weighted grand
mean
**hw**. differences from the observation-weighted mean
of subsequent levels
**jw**. differences from the observation-weighted mean
of previous levels
**pw**. observation-weighted orthogonal polynomial in
the level values
**qw**. observation-weighted orthogonal polynomial in
the level sequence
-------------------------------------------------------------------------
One or more individual contrasts may be selected by using the *op#.* or *op***(**
*numlist***).** syntax. For example, **a3.A** selects the adjacent contrast for
level 3 of **A**, and **p(1/2).B** selects the linear and quadratic effects of
**B**. Also see *Orthogonal polynomial contrasts* and *Beyond linear models*
in **[R] contrast**.

Custom contrasts Description
-------------------------------------------------------------------------
**{A** *numlist***}** user-defined contrast on the levels of factor **A**
**{A#B** *numlist***}** user-defined contrast on the levels of
interaction between **A** and **B**
-------------------------------------------------------------------------
Custom contrasts may be part of a term, such as **{A** *numlist***}#B**, **{A**
*numlist***}@B**, **{A** *numlist***}|B**, **{A#B** *numlist***}**, and **{A** *numlist***}#{B** *numlist***}**.
The same is true of higher-order custom contrasts, such as **{A#B**
*numlist***}@C**, **{A#B** *numlist***}#***r***.C**, and **{A#B** *numlist***}#c.x**.
Higher-order interactions with at most eight factor variables are allowed
with custom contrasts.

*method* Description
-------------------------------------------------------------------------
__noadj__**ust** do not adjust for multiple comparisons; the
default
__bon__**ferroni** [**adjustall**] Bonferroni's method; adjust across all terms
__sid__**ak** [**adjustall**] Sidak's method; adjust across all terms
__sch__**effe** Scheffe's method
-------------------------------------------------------------------------

__Menu__

**Statistics > Postestimation**

__Description__

**contrast** tests linear hypotheses and forms contrasts involving factor
variables and their interactions from the most recently fit model. The
tests include ANOVA-style tests of main effects, simple effects,
interactions, and nested effects. **contrast** can use named contrasts to
decompose these effects into comparisons against reference categories,
comparisons of adjacent levels, comparisons against the grand mean,
orthogonal polynomials, and such. Custom contrasts may also be
specified.

**contrast** can be used with **svy** estimation results; see **[SVY] svy**
**postestimation**.

Contrasts can also be computed for margins of linear and nonlinear
responses; see **[R] margins, contrast**.

__Options__

+------+
----+ Main +-------------------------------------------------------------

**overall** specifies that a joint hypothesis test over all terms be
performed.

**asobserved** specifies that factor covariates be evaluated using the cell
frequencies observed in the estimation sample. The default is to
treat all factor covariates as though there were an equal number of
observations in each level.

**lincom** specifies that user-defined contrasts be treated as linear
combinations. The default is to require that all user-defined
contrasts sum to zero. (Summing to zero is part of the definition of
a contrast.)

+-----------+
----+ Equations +--------------------------------------------------------

**equation(***eqspec***)** specifies the equation from which contrasts are to be
computed. The default is to compute contrasts from the first
equation.

**atequations** specifies that the contrasts be computed within each
equation.

+----------+
----+ Advanced +---------------------------------------------------------

**emptycells(***empspec***)** specifies how empty cells are handled in interactions
involving factor variables that are being treated as balanced.

**emptycells(strict)** is the default; it specifies that contrasts
involving empty cells be treated as not estimable.

**emptycells(reweight)** specifies that the effects of the observed cells
be increased to accommodate any missing cells. This makes the
contrast estimable but changes its interpretation.

**noestimcheck** specifies that **contrast** not check for estimability. By
default, the requested contrasts are checked and those found not
estimable are reported as such. Nonestimability is usually caused by
empty cells. If **noestimcheck** is specified, estimates are computed in
the usual way and reported even though the resulting estimates are
manipulable, which is to say they can differ across equivalent models
having different parameterizations.

+-----------+
----+ Reporting +--------------------------------------------------------

**level(***#***)** specifies the confidence level, as a percentage, for confidence
intervals. The default is **level(95)** or as set by **set level**.

**mcompare(***method***)** specifies the method for computing p-values and
confidence intervals that account for multiple comparisons within a
factor-variable term.

Most methods adjust the comparisonwise error rate, alpha_c, to
achieve a prespecified experimentwise error rate, alpha_e.

**mcompare(noadjust)** is the default; it specifies no adjustment.

alpha_c = alpha_e

**mcompare(bonferroni)** adjusts the comparisonwise error rate based on
the upper limit of the Bonferroni inequality

alpha_e <= m * alpha_c

where *m* is the number of comparisons within the term.

The adjusted comparisonwise error rate is

alpha_c = alpha_e/m

**mcompare(sidak)** adjusts the comparisonwise error rate based on the
upper limit of the probability inequality

alpha_e <= 1 - (1 - alpha_c)^m

where *m* is the number of comparisons within the term.

The adjusted comparisonwise error rate is

alpha_c = 1 - (1 - alpha_e)^(1/m)

This adjustment is exact when the *m* comparisons are independent.

**mcompare(scheffe)** controls the experimentwise error rate using the F
or chi-squared distribution with degrees of freedom equal to the
rank of the term.

**mcompare(***method* **adjustall)** specifies that the multiple-comparison
adjustments count all comparisons across all terms rather than
performing multiple comparisons term by term. This leads to more
conservative adjustments when multiple variables or terms are
specified in *marginslist*. This option is compatible only with
the **bonferroni** and **sidak** methods.

**noeffects** suppresses the table of individual contrasts with confidence
intervals. This table is produced by default when the **mcompare()**
option is specified or when a term in *termlist* implies all individual
contrasts.

**cieffects** specifies that a table containing a confidence interval for
each individual contrast be reported.

**pveffects** specifies that a table containing a p-value for each individual
contrast be reported.

**effects** specifies that a single table containing a confidence interval
and p-value for each individual contrast be reported.

**nowald** suppresses the table of Wald tests.

**noatlevels** indicates that only the overall Wald test be reported for each
term containing within or nested (**@** or **|**) operators.

**nosvyadjust** is for use with **svy** estimation commands. It specifies that
the Wald test be carried out without the default adjustment for the
design degrees of freedom. That is to say the test is carried out as
W/k ~ F(k,d) rather than as (d-k+1)W/(kd) ~ F(k,d-k+1), where k is
the dimension of the test and d is the total number of sampled PSUs
minus the total number of strata.

**sort** specifies that the table of individual contrasts be sorted by the
contrast values within each term.

**post** causes **contrast** to behave like a Stata estimation (e-class) command.
**contrast** posts the vector of estimated contrasts along with the
estimated variance-covariance matrix to **e()**, so you can treat the
estimated contrasts just as you would results from any other
estimation command. For example, you could use **test** to perform
simultaneous tests of hypotheses on the contrasts, or you could use
**lincom** to create linear combinations.

*display_options*: **vsquish**, __nofvlab__**el**, **fvwrap(***#***)**, **fvwrapon(***style***)**,
**cformat(***%fmt***)**, **pformat(%***fmt***)**, **sformat(%***fmt***)**, and **nolstretch**.

**vsquish** specifies that the blank space separating factor-variable
terms or time-series-operated variables from other variables in
the model be suppressed.

**nofvlabel** displays factor-variable level values rather than attached
value labels. This option overrides the **fvlabel** setting; see **[R]**
**set showbaselevels**.

**fvwrap(***#***)** specifies how many lines to allow when long value labels
must be wrapped. Labels requiring more than *#* lines are
truncated. This option overrides the **fvwrap** setting; see **[R] set**
**showbaselevels**.

**fvwrapon(***style***)** specifies whether value labels that wrap will break
at word boundaries or break based on available space.

**fvwrapon(word)**, the default, specifies that value labels break at
word boundaries.

**fvwrapon(width)** specifies that value labels break based on
available space.

This option overrides the **fvwrapon** setting; see **[R] set**
**showbaselevels**.

**cformat(%***fmt***)** specifies how to format contrasts, standard errors, and
confidence limits in the table of estimated contrasts.

**pformat(%***fmt***)** specifies how to format p-values in the table of
estimated contrasts.

**sformat(%***fmt***)** specifies how to format test statistics in the table of
estimated contrasts.

**nolstretch** specifies that the width of the table of estimated
contrasts not be automatically widened to accommodate longer
variable names. The default, **lstretch**, is to automatically widen
the table of estimated contrasts up to the width of the Results
window. To change the default, use **set lstretch off**. **nolstretch**
is not shown in the dialog box.

*eform_option* specifies that the contrasts table be displayed in
exponentiated form. exp(contrast) is displayed rather than contrast.
Standard errors and confidence intervals are also transformed. See
**[R]** *eform_option* for the list of available options.

The following option is available with **contrast** but is not shown in the
dialog box:

**df(***#***)** specifies that the t distribution with *#* degrees of freedom be used
for computing p-values and confidence intervals. The default is to
use **e(df_r)** degrees of freedom or the standard normal distribution if
**e(df_r)** is missing.

__Examples__

---------------------------------------------------------------------------
Setup for one-way model
**. webuse cholesterol**
**. regress chol i.agegrp**

Test that the cell means are the same for all age groups, that is, test
the main effects of age group
**. contrast agegrp**

Reference category contrasts
**. contrast r.agegrp**

Reverse adjacent contrasts
**. contrast ar.agegrp**

Orthogonal polynomial contrasts
**. contrast p.agegrp**

Setup for one-way model
**. anova chol i.race**

Grand mean contrasts, adjusting p-values for multiple comparisons using
Bonferroni's adjustment
**. contrast g.race, mcompare(bonferroni)**

User-defined contrasts for reference category effects, testing that the
cell mean of category 1 is equal to the cell mean of category 2 and that
the cell mean of category 1 is equal to the cell mean of category 3
**. contrast {race -1 1 0} {race -1 0 1}**

---------------------------------------------------------------------------
Setup for two-way model
**. webuse bpchange**
**. anova bpchange dose##gender**

Simple effects of gender
**. contrast r.gender@dose**

Reverse adjacent simple effects of dose
**. contrast ar.dose@gender**

Interaction effects decomposed into individual contrasts
**. contrast ar.dose#r.gender**

Main effects decomposed into individual contrasts
**. contrast ar.dose r.gender**

Partial interaction effects
**. contrast ar.dose#gender**
**. contrast dose#r.gender**

---------------------------------------------------------------------------
Setup for nested model
**. webuse sat**
**. anova score method / class|method /**

Simple effects of class nested within method
**. contrast class|method**

Main effects with nested error term and reweighting of empty cells
**. contrast method / class|method, emptycells(reweight)**

---------------------------------------------------------------------------
Setup for unbalanced data
**. webuse cholesterol**

ANOVA model with unbalanced data
**. anova chol race##agegrp**

Reference category effects treating all factors as balanced
**. contrast r.race**

Reference category effects, using observed marginal frequencies
**. contrast r.race, asobserved**

Grand mean contrasts using observed cell frequencies
**. contrast gw.race**

Weighted grand mean contrasts, using observed marginal frequencies
**. contrast gw.race, asobserved wald cieffects**

---------------------------------------------------------------------------
Setup for continuous covariate
**. webuse census3**
**. anova brate region##c.medage**

Reference category effects of region on the intercept
**. contrast r.region**

Reference category effects of region on the slope of **medage**
**. contrast r.region#c.medage**

---------------------------------------------------------------------------
Setup for nonlinear model
**. webuse hospital**
**. logistic satisfied hospital##illness**

ANOVA-style table of tests for main effects and interaction effects
**. contrast hospital##illness**

---------------------------------------------------------------------------
Setup for multivariate response
**. webuse jaw**
**. mvreg y1 y2 y3 = gender##fracture**

Test the effects of **gender**, **fracture**, and their interaction in the first
equation
**. contrast gender##fracture**

Test these effects in equation **y2**
**. contrast gender##fracture, equation(y2)**

Test the same effects for each equation, suppressing blank space between
terms
**. contrast gender##fracture, atequations vsquish**

Test for a marginal effect on the means between dependent variables
**. contrast _eqns**

Test whether the main effects of gender differ between the dependent
variables
**. contrast gender#_eqns**

---------------------------------------------------------------------------

__Video example__

Introduction to contrasts in Stata: One-way ANOVA

__Stored results__

**contrast** stores the following in **r()**:

Scalars
**r(df_r)** variance degrees of freedom
**r(k_terms)** number of terms in *termlist*
**r(level)** confidence level of confidence intervals

Macros
**r(cmd)** **contrast**
**r(cmdline)** command as typed
**r(est_cmd)** **e(cmd)** from original estimation results
**r(est_cmdline)** **e(cmdline)** from original estimation results
**r(title)** title in output
**r(overall)** **overall** or empty
**r(emptycells)** *empspec* from **emptycells()**
**r(mcmethod)** *method* from **mcompare()**
**r(mctitle)** title for *method* from **mcompare()**
**r(mcadjustall)** **adjustall** or empty
**r(margin_method)** **asbalanced** or **asobserved**

Matrices
**r(b)** contrast estimates
**r(V)** variance-covariance matrix of the contrast
estimates
**r(error)** contrast estimability codes;
**0** means estimable,
**8** means not estimable
**r(L)** matrix of contrasts applied to the model
coefficients
**r(table)** matrix containing the contrasts with their standard
errors, test statistics, p-values, and confidence
intervals
**r(F)** vector of F statistics; **r(df_r)** present
**r(chi2)** vector of chi-squared statistics; **r(df_r)** not
present
**r(p)** vector of p-values corresponding to **r(F)** or **r(chi2)**
**r(df)** vector of degrees of freedom corresponding to **r(p)**
**r(df2)** vector of denominator degrees of freedom
corresponding to **r(F)**

**contrast** with the **post** option stores the following in **e()**:

Scalars
**e(df_r)** variance degrees of freedom
**e(k_terms)** number of terms in *termlist*

Macros
**e(cmd)** **contrast**
**e(cmdline)** command as typed
**e(est_cmd)** **e(cmd)** from original estimation results
**e(est_cmdline)** **e(cmdline)** from original estimation results
**e(title)** title in output
**e(overall)** **overall** or empty
**e(emptycells)** *empspec* from **emptycells()**
**e(margin_method)** **asbalanced** or **asobserved**
**e(properties)** **b V**

Matrices
**e(b)** contrast estimates
**e(V)** variance-covariance matrix of the contrast
estimates
**e(error)** contrast estimability codes;
**0** means estimable,
**8** means not estimable
**e(L)** matrix of contrasts applied to the model
coefficients
**e(F)** vector of unadjusted F statistics; **e(df_r)** present
**e(chi2)** vector of chi-squared statistics; **e(df_r)** not
present
**e(p)** vector of unadjusted p-values corresponding to **e(F)**
or **e(chi2)**
**e(df)** vector of degrees of freedom corresponding to **e(p)**
**e(df2)** vector of denominator degrees of freedom
corresponding to **e(F)**