# Re: st: Plotting 3way interactions

 From Philip Ender To statalist@hsphsun2.harvard.edu Subject Re: st: Plotting 3way interactions Date Tue, 18 Aug 2009 13:13:26 -0700

```On Monday, August 17th Kris Anderson wrote that she was having trouble
plotting 3-way interactions based on code from

http://www.ats.ucla.edu/stat/stata/faq/con3wayb.htm

There were three problems with her code.
1)  There is an -if- in the -regress- command but not anywhere else.
2)  The -twoway function- command used the name of a "real" variable
instead of the generic variable x.  I have added a note to our webpage
3)  The variable in the -twoway scatter- should have been csocanx and
not cpn.  This was do to an error on our web page which has been
corrected.

Kris has emailed me to indicate that with these corrections the
program is now functioning correctly.

Here is Kris' code:

regress avgdrink age sex gradedich white hisp csocanx caffil cpn SAF
SP AP SAP if currentdrinker > 0
sum csocanx
global hi=r(max)
global lo=r(min)
sum caffil
global HAF=r(mean)+r(sd)
global LAF=r(mean)-r(sd)
sum cpn
global HPN=r(mean) +r(sd)
global LPN=r(mean) -r(sd)
sum age
global mage=r(mean)
sum white
global mwhite=r(mean)
sum hisp
global mhisp=r(mean)
global HPNHAF "csocanx + (\$HPN)*SP + (\$HAF)*SAF + (\$HPN)*(\$HAF)*SAP"
global HPNLAF "csocanx + (\$HPN)*SP + (\$LAF)*SAF + (\$HPN)*(\$LAF)*SAP"
global LPNHAF "csocanx + (\$LPN)*SP + (\$HAF)*SAF + (\$LPN)*(\$HAF)*SAP"
global LPNLAF "csocanx + (\$LPN)*SP + (\$LAF)*SAF + (\$LPN)*(\$LAF)*SAP"
lincom \$HPNHAF
global b1 = r(estimate)
lincom \$HPNLAF
global b2 = r(estimate)
lincom \$LPNHAF
global b3 = r(estimate)
lincom \$LPNLAF
global b4 = r(estimate)
lincom _cons + \$HPN*cpn + \$HAF*caffil + \$HPN*\$HAF*AP + \$mage*age +
global c1 = r(estimate)
lincom _cons + \$HPN*cpn + \$LAF*caffil + \$HPN*\$LAF*AP + \$mage*age +
global c2 = r(estimate)
lincom _cons + \$LPN*cpn + \$HAF*caffil + \$LPN*\$HAF*AP + \$mage*age +
global c3 = r(estimate)
lincom _cons + \$LPN*cpn + \$LAF*caffil + \$LPN*\$LAF*AP + \$mage*age +
global c4 = r(estimate)
***twoway(function y=\$c1+\$b1*csocanx, range(\$lo \$hi))(function
y=\$c2+\$b2*csocanx, range(\$lo \$hi))(function y=\$c3+\$b3*csocanx,
range(\$lo \$hi))(function y=\$c4+\$b4*csocanx, range(\$lo \$hi))(scatter
avgdrink cpn, msym(oh) jitter(3)), legend(order(1 "1 HPNHAF" 2 "2
HPNLAF" 3 "3 LAPHAF" 4 "4 LAPLAF")) xtitle (X) ytitle(Y)
name(con3wayb, replace)
lincom (\$HPNHAF)- (\$HPNLAF)
lincom (\$HPNHAF)- (\$LPNHAF)
lincom (\$HPNLAF)- (\$LPNLAF)
lincom (\$LPNHAF)- (\$LPNLAF)
lincom (\$HPNHAF)- (\$LPNLAF)
lincom (\$HPNLAF)- (\$LPNHAF)

--
Phil Ender
Statistical Consulting Group