// Simulate data
clear
set seed 1
set obs 100
generate e = 0 + (100000 - 0) * runiform() // To generate random variates over the
generate x1 = 0 + (600 - 0) * runiform() // interval [a,b), a+(b-a)*runiform()
generate x2 = 0 + (600 - 0) * runiform()
generate y = (x1 + x2 + (x1*x2) + e) / 1000
// Calculations for Aiken & West (1991) style plot
qui regress y c.x1##c.x2
qui sum x1
local x1_minsd = r(mean) - r(sd)
local x1_mean = r(mean)
local x1_plusd = r(mean) + r(sd)
qui sum x2
local x2_minsd = r(mean) - r(sd)
local x2_mean = r(mean)
local x2_plusd = r(mean) + r(sd)
// Calculate predicted values for plot
margins, at(c.x2 = (`x2_minsd' `x2_mean' `x2_plusd') ///
c.x1 = (`x1_minsd' `x1_mean' `x1_plusd')) vsquish
// Plot
qui marginsplot, recastci(rarea) ciopts(color(gs10)) ///
xlabel(`x2_minsd' "-1 SD" `x2_mean' "Average x2" `x2_plusd' "+1 SD") ///
title("Aiken & West (1991)-style plot for interactions") ///
ytitle("Predicted y") ///
xtitle("") ///
plotopts(msymbol(none)) /// // Turn off markers
plot1opts(lpattern(longdash)) /// // Define line types here
plot2opts(lpattern(solid)) ///
plot3opts(lpattern(shortdash)) ///
legend(subtitle(x1) ///
order(4 "- 1 SD" 5 "Average x1" 6 "+ 1 SD" 2 "95% CI")) ///
name(lines, replace)
// For some reason, marginsplot ignores the -label option-, therefore
// -order- is used here
// Calculations for contour plot
qui regress y c.x1##c.x2
predict y_hat
// Plot
qui twoway contour y_hat x1 x2, ///
title("Contour plot for interactions") ///
ztitle("Predicted y") ///
name(contours, replace)
// Combine figures
graph combine lines contours, col(1) ysize(8)
Showing posts with label plot1opts. Show all posts
Showing posts with label plot1opts. Show all posts
May 6, 2015
Random graphs (46): Plotting the contours of interactions
Next to the classical layout for interaction plots, it is also possible to make use of contour plots for visualizing interactions.
Labels:
Interaction plot,
local,
margins,
marginsplot,
plot1opts,
Random graphs,
rnormal(),
Simulation,
twoway contour
Mar 17, 2014
Random graphs (19): Simple interaction plots with -marginsplot- for continuous variables
sysuse auto, clear
regress mpg c.weight##c.gear_ratio
// Obtain means and standard deviation for plots
qui sum weight
local w_minsd = r(mean) - r(sd)
local w_mean = r(mean)
local w_plusd = r(mean) + r(sd)
qui sum gear_ratio
local g_minsd = r(mean) - r(sd)
local g_mean = r(mean)
local g_plusd = r(mean) + r(sd)
// Calculate predicted values for plot
margins, at(c.gear_ratio = (`g_minsd' `g_mean' `g_plusd') ///
c.weight = (`w_minsd' `w_mean' `w_plusd')) vsquish
// Plot
marginsplot, recastci(rarea) ciopts(color(gs10)) ///
title("Weight moderates the gear ratio{char 0150}mileage association") ///
xlabel(`g_minsd' "-1 SD" `g_mean' "Average gear ratio" `g_plusd' "+1 SD") ///
ytitle("Predicted mileage") ///
xtitle("Gear ratio") ///
plotopts(msymbol(none)) /// // Turn off markers
plot1opts(lpattern(longdash)) /// // Define line types here
plot2opts(lpattern(solid)) ///
plot3opts(lpattern(shortdash)) ///
legend(subtitle(Weight) ///
order(4 "- 1 SD" 5 "Mean" 6 "+ 1 SD" 2 "95% CI"))
// For some reason, marginsplot ignores the -label option-, therefore
// -order- is used here
gr export plot.png, as(png) replace
Subscribe to:
Posts (Atom)

