Stata Tidbits

These tidbits contain bits and pieces of information I hope you find helpful to use Stata more effectively. You can receive notifications of new tidbits as they are added (via email) by clicking on the subscribe box at the left. (Every email has an unsubscribe link, making it a snap to unsubscribe.)
« Graph schemes | Changing directories (folders) »
Tuesday
Sep212010

What the ##?

Stata 11 introduced a new way to form interactions using the # and ## interaction operators. These contrast with the old fashioned method of using the xi prefix and then using the * operator to form interactions among variables that included the i. prefix. There has been some confusion about # and ## operators, so let's have a look at them using the nlsw dataset.

. use nlsw (NLSW, 1988 extract)

Let's use the old fashioned xi command to run a regression predicting the hourly wage from whether someone belongs to a union and whether they are a college graduate, as well as the interaction of these two variables.

. xi: regress wage i.union*i.collgrad i.union _Iunion_0-1 (naturally coded; _Iunion_0 omitted) i.collgrad _Icollgrad_0-1 (naturally coded; _Icollgrad_0 omitted) i.union*i.col~d _IuniXcol_#_# (coded as above) Source | SS df MS Number of obs = 1878 -------------+------------------------------ F( 3, 1874) = 110.64 Model | 4907.16304 3 1635.72101 Prob > F = 0.0000 Residual | 27706.2793 1874 14.7845674 R-squared = 0.1505 -------------+------------------------------ Adj R-squared = 0.1491 Total | 32613.4424 1877 17.3753023 Root MSE = 3.8451 ------------------------------------------------------------------------------ wage | Coef. Std. Err. t P>|t| [95% Conf. Interval] -------------+---------------------------------------------------------------- _Iunion_1 | 1.454899 .2462993 5.91 0.000 .9718493 1.937949 _Icollgrad_1 | 3.748679 .2453873 15.28 0.000 3.267418 4.22994 _IuniXcol_~1 | -1.098846 .4553523 -2.41 0.016 -1.991897 -.2057956 _cons | 6.36869 .1158806 54.96 0.000 6.141421 6.595958 ------------------------------------------------------------------------------

Now, let's do this the Stata 11 way, as shown below.

. regress wage i.union i.collgrad i.union#i.collgrad Source | SS df MS Number of obs = 1878 -------------+------------------------------ F( 3, 1874) = 110.64 Model | 4907.16304 3 1635.72101 Prob > F = 0.0000 Residual | 27706.2793 1874 14.7845674 R-squared = 0.1505 -------------+------------------------------ Adj R-squared = 0.1491 Total | 32613.4424 1877 17.3753023 Root MSE = 3.8451 ------------------------------------------------------------------------------ wage | Coef. Std. Err. t P>|t| [95% Conf. Interval] -------------+---------------------------------------------------------------- 1.union | 1.454899 .2462993 5.91 0.000 .9718493 1.937949 1.collgrad | 3.748679 .2453873 15.28 0.000 3.267418 4.22994 | union#| collgrad | 1 1 | -1.098846 .4553523 -2.41 0.016 -1.991897 -.2057956 | _cons | 6.36869 .1158806 54.96 0.000 6.141421 6.595958 ------------------------------------------------------------------------------

Note that the results are exactly the same, but it seems like we had to do more typing this new way, entering both the main effects and interaction terms separately. Well, we can save ourselves this effort by instead specifying the model as shown below.

. regress wage i.union##i.collgrad Source | SS df MS Number of obs = 1878 -------------+------------------------------ F( 3, 1874) = 110.64 Model | 4907.16304 3 1635.72101 Prob > F = 0.0000 Residual | 27706.2793 1874 14.7845674 R-squared = 0.1505 -------------+------------------------------ Adj R-squared = 0.1491 Total | 32613.4424 1877 17.3753023 Root MSE = 3.8451 ------------------------------------------------------------------------------ wage | Coef. Std. Err. t P>|t| [95% Conf. Interval] -------------+---------------------------------------------------------------- 1.union | 1.454899 .2462993 5.91 0.000 .9718493 1.937949 1.collgrad | 3.748679 .2453873 15.28 0.000 3.267418 4.22994 | union#| collgrad | 1 1 | -1.098846 .4553523 -2.41 0.016 -1.991897 -.2057956 | _cons | 6.36869 .1158806 54.96 0.000 6.141421 6.595958 ------------------------------------------------------------------------------

By specifying i.union##i.collgrad, this was a shorthand for asking for the main effects and interaction all in one fell swoop, much like the * operator worked when using the xi command. Unless you are completely sure of what you are doing, you probably want to avoid specifying this kind of model.

. * AVOID THIS! . regress wage i.union#i.collgrad Source | SS df MS Number of obs = 1878 -------------+------------------------------ F( 3, 1874) = 110.64 Model | 4907.16304 3 1635.72101 Prob > F = 0.0000 Residual | 27706.2793 1874 14.7845674 R-squared = 0.1505 -------------+------------------------------ Adj R-squared = 0.1491 Total | 32613.4424 1877 17.3753023 Root MSE = 3.8451 ------------------------------------------------------------------------------ wage | Coef. Std. Err. t P>|t| [95% Conf. Interval] -------------+---------------------------------------------------------------- union#| collgrad | 0 1 | 3.748679 .2453873 15.28 0.000 3.267418 4.22994 1 0 | 1.454899 .2462993 5.91 0.000 .9718493 1.937949 1 1 | 4.104732 .3366364 12.19 0.000 3.44451 4.764953 | _cons | 6.36869 .1158806 54.96 0.000 6.141421 6.595958 ------------------------------------------------------------------------------

Now, each cell is compared to the omitted cell (the cell in which someone is neither part of a union nor a college graduate). This can be useful, but it is rarely what one intends when specifying such a model.

That concludes this tidbit about # and ##.

You can download the example data files from this tidbit (as well as all of the other tidbits) as shown below. These will download all of the example data files into the current folder on your computer. (If you have done this before, then you may need to specify net get stowdata, replace to overwrite the existing files.

net from http://www.MichaelNormanMitchell.com/storage/stowdata net get stowdata
If you have thoughts on this Stata Tidbit of the Week, you can post a comment. You can also send me an email at MichaelNormanMitchell and then the at sign and gmail dot com. If you are receiving this tidbit via email, you can find the web version at http://www.michaelnormanmitchell.com/ .

PrintView Printer Friendly Version

EmailEmail Article to Friend

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.
Editor Permission Required
You must have editing permission for this entry in order to post comments.