Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: including all interactions among a set of variables

From   "Nick Cox" <>
To   <>
Subject   st: RE: including all interactions among a set of variables
Date   Fri, 14 Feb 2003 11:40:45 -0000

david huffer
> Is there a command used to include every 2-way interaction
> among a set
> of variables in a regression-type model?† In SAS it is accomplished
> quite easily but so far I havenít run across a means other
> than typing
> all the possible interactions.

A way to think about this is not in terms of
a command to do it, but rather in terms of getting
Stata to help in cutting down on the typing, especially
as an explicit list of terms might _later_ need
to be edited down as you simplify from that initial
"shotgun" model.

Some help might lie in looking at the -selectvars-
program recently posted on SSC.

Suppose you had categorical variables a b c d and
wanted terms of the form i.a*i.b. -selectvars-
emits distinct tuples and you can specify limits,
so all distinct pairs are gained by

. selectvars a b c d, min(2) max(2)

There's no output. It is the saved result which
its only saving grace:

. ret li

           r(varlist) : ""a b " "a c " "a d " "b c " "b d " "c d " "

Then you can take each pair and emit an interaction term

. foreach pair in `r(varlist)' {
  2. tokenize `pair'
  3. local iterms "`iterms' i.`1'*i.`2'"
  4. }

. di "`iterms'"
i.a*i.b i.a*i.c i.a*i.d i.b*i.c i.b*i.d i.c*i.d

Of course you could type that just as easily yourself: the
example is just a toy example.

An alternative which could be as or more practical --
especially if you have interactions of different flavours,
given a mix of continuous and categorical variables --
is just to display the r(varlist) in the results
window and then copy and paste into -doedit- or
any favourite editor. Any editor worthy of the name
will remove all the " in one command, for example.


*   For searches and help try:

© Copyright 1996–2019 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index