Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.

# Re: st: interaction notation for lincom (and test)

 From jpitblado@stata.com (Jeff Pitblado, StataCorp LP) To statalist@hsphsun2.harvard.edu Subject Re: st: interaction notation for lincom (and test) Date Thu, 29 Nov 2012 10:13:37 -0600

```Ben Hoen <bhoen@lbl.gov> is using interaction terms that contain variables
with long names, and would like to compute tests and linear combinations of
their coefficients:

> This is a bit of a shot in the dark...
>
> I have interacted variables in a regression that I want to compare using
> lincom (or test) and wanted to know if there was a shorthand way to recall
> the variables.  The difficulty is that a number of the names of the
> variables are quite long (and not something I can change), so having a
> shorthand way to get at their coefficients and their se would be preferred.
>
>
> For example, say I had this:
> *==================================================
> sysuse auto, clear
> g long_name_a=round(runiform())+1
> g long_name_b=round(2*runiform())+1
>
> areg price mpg i.long_name_a#ib3.long_name_b, abs(foreign)
>
> matrix list e(b)
>
> * I can access the stored results of the variables like this
> lincom 1b.long_name_a#1.long_name_b-2.long_name_a#1.long_name_b
>
> *but I hoped that I could get at the same via some matrix notation from e(b)
>
> *====================================================
>
>
> Admittedly I am a novice in how to work with matrices but I thought I would
> try the list.

I built a working example based on Ben's example:

. set seed 1234
. sysuse auto
. gen long_name_a = ceil(2*runiform())
. gen long_name_b = ceil(3*runiform())

. areg price mpg i.l~e_a#ib3.l~e_b, abs(foreign)

Side note -- notation shortcut:

Notice that I used the tilde character '~' as a way to cut down on the
number of characters I have to type while referencing variables with
long names.  Here '~' is the abbreviation character that tells Stata
to look for the unique variable that fits the specified pattern.  In
Ben's example, the long variable names all have prefix 'long_name_',
and since there are no other variables that also use a prefix like
this, we can use 'l~e_' as our prefix.  This is merely a shortcut in
specifying the variables and does not answer Ben's original question.

On to Ben's question.

Ben would like to compute tests and linear combinations of the coefficients on
the elements of interaction terms.  The -lincom- example provided was:

. lincom 1b.long_name_a#1.long_name_b-2.long_name_a#1.long_name_b

Neither -test- nor -lincom- allows the '~' shortcut, so that is no help.

Stata 12 has two new postestimation commands that were specifically developed
to take advantage of factor variables notation to perform the kinds of

-pwcompare- performs pairwise comparisons across levels of factor
variables in the current model.  It will also report the marginal
linear predictions for each cell of a specified factor variables term.

-contrast- computes contrasts (and tests) across levels of factor
variables in the current model, and has a more direct bearing on Ben's
question.

So using the above -lincom- as an example, the equivalent -constrast- command
is:

. contrast a.l~e_a@l~e_b

The revelant output from our example follows after my signature.

The -contrast- command extends the factor variables notation to include
operators for several kinds of contrasts.  The ones used here are 'a.',
the adjacent contrast operator, which compares the current level with the
next; and '@' which tells -contrast- to compute contrasts of the preceeding
variable within levels of the following variable.  Thus breaking the above
syntax into its parts we have

a.l~e_a	-- compute adjacent contrasts of l~e_a
@l~e_b	-- compute the above contrasts within each level of l~e_b

Since l~e_a has 2 levels and l~e_b has 3 levels, this will yield a contrast of
1.l~e_a versus 2.l~e_a for each of the 3 levels of l~e_b.  These comparisons
are between the marginal linear predictions within each cell defined by
the interaction term l~e_a#l~e_b originally specified in the model.  Ben can
use the -cimeans- option of -pwcompare- with the interaction term of interest
to see what the marginal linear predictions are.  In our example, that would
be

. pwcompare l~e_b#l~e_a, cimargins

--Jeff

***** BEGIN:
. areg price mpg i.l~e_a#ib3.l~e_b, abs(for)

Linear regression, absorbing indicators           Number of obs   =         74
F(   6,     66) =       4.84
Prob > F        =     0.0004
R-squared       =     0.3072
Root MSE        =  2581.8232

------------------------------------------------------------------------------
price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
mpg |  -280.9069   59.85255    -4.69   0.000    -400.4063   -161.4074
|
long_name_a#|
long_name_b |
1 1  |   -540.983   1057.622    -0.51   0.611    -2652.594    1570.628
1 2  |  -405.8745   1010.844    -0.40   0.689     -2424.09    1612.341
2 1  |  -286.6583   1199.695    -0.24   0.812    -2681.926     2108.61
2 2  |   658.2365    1111.33     0.59   0.556    -1560.606    2877.079
2 3  |   500.0031   1071.185     0.47   0.642    -1638.687    2638.693
|
_cons |   12188.71    1436.19     8.49   0.000     9321.264    15056.15
-------------+----------------------------------------------------------------
foreign |          F(1, 66) =      6.310   0.014           (2 categories)

. lincom 1b.long_name_a#1.long_name_b-2.long_name_a#1.long_name_b

( 1)  1b.long_name_a#1.long_name_b - 2.long_name_a#1.long_name_b = 0

------------------------------------------------------------------------------
price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
(1) |  -254.3247   1159.891    -0.22   0.827    -2570.121    2061.472
------------------------------------------------------------------------------

. contrast a.l~e_a@l~e_b

Contrasts of marginal linear predictions

Margins      : asbalanced

-----------------------------------------------------------
|         df           F        P>F
------------------------+----------------------------------
long_name_a@long_name_b |
(1 vs 2) 1  |          1        0.05     0.8271
(1 vs 2) 2  |          1        1.13     0.2909
(1 vs 2) 3  |          1        0.22     0.6422
Joint  |          3        0.47     0.7062
|
Residual |         66
-----------------------------------------------------------

-------------------------------------------------------------------------
|   Contrast   Std. Err.     [95% Conf. Interval]
------------------------+------------------------------------------------
long_name_a@long_name_b |
(1 vs 2) 1  |  -254.3247   1159.891     -2570.121    2061.472
(1 vs 2) 2  |  -1064.111   999.4268     -3059.531    931.3089
(1 vs 2) 3  |  -500.0031   1071.185     -2638.693    1638.687
-------------------------------------------------------------------------

. pwcompare l~e_b#l~e_a, cimargins

Pairwise comparisons of marginal linear predictions

Margins      : asbalanced

-------------------------------------------------------------------------
|     Margin   Std. Err.     [95% Conf. Interval]
------------------------+------------------------------------------------
long_name_b#long_name_a |
1 1  |   11647.73   1556.118      8540.836    14754.62
1 2  |   11902.05   1513.169      8880.911    14923.19
2 1  |   11782.83   1473.998      8839.903    14725.77
2 2  |   12846.95    1554.33      9743.626    15950.27
3 1  |   12188.71    1436.19      9321.264    15056.15
3 2  |   12688.71    1326.12      10041.03     15336.4
-------------------------------------------------------------------------
***** END:
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/
```