clear
input j i xij x_j yij y_j
// Data from Snijders and Bosker (1999, Table 3.2):
1 1 1 2 5 6
1 2 3 2 7 6
2 1 2 3 4 5
2 2 4 3 6 5
3 1 3 4 3 4
3 2 5 4 5 4
4 1 4 5 2 3
4 2 6 5 4 3
5 1 5 6 1 2
5 2 7 6 3 2
end
// Plot as Figure 3.4 of Snijders ans Bosker (1999):
twoway (scatter yij xij) ///
(lfit yij xij) ///
(lfit y_j x_j) ///
(lfit yij xij if j == 1, lpattern(dash_dot)) ///
(lfit yij xij if j == 2, lpattern(dash_dot)) ///
(lfit yij xij if j == 3, lpattern(dash_dot)) ///
(lfit yij xij if j == 4, lpattern(dash_dot)) ///
(lfit yij xij if j == 5, lpattern(dash_dot)) ///
, legend(label(2 "Total regression") ///
label(3 "Between regression") ///
label(4 "Within regression") ///
order(2 3 4) ///
pos(1) ring(0)) ///
xtitle(X) ytitle(Y) ///
title("Within, between, and total relations") ///
xlabel(none) ylabel(none) ///
name(wbt_legend, replace)
// Lines directly labeled
twoway (scatter yij xij) ///
(lfit yij xij) ///
(lfit y_j x_j) ///
(lfit yij xij if j == 1, lpattern(dash_dot)) ///
(lfit yij xij if j == 2, lpattern(dash_dot)) ///
(lfit yij xij if j == 3, lpattern(dash_dot)) ///
(lfit yij xij if j == 4, lpattern(dash_dot)) ///
(lfit yij xij if j == 5, lpattern(dash_dot)) ///
, legend(off) ///
text(1.8 6.5 "Between") ///
text(3.0 7.5 "Total") ///
text(7.0 3.5 "Within") ///
text(5.0 5.5 "Within") ///
xtitle(X) ytitle(Y) ///
title("Within, between, and total relations") ///
xlabel(none) ylabel(none) ///
name(wbt_nolegend, replace)
graph combine wbt_legend wbt_nolegend, col(1) ysize(8) xcommon ycommon
// ysize(4) and xsize(5.5) are defaults
Reference
Snijders, Tom, and Roel Boskers. 1999.
Multilevel Analysis. An Introduction to Basic and Advanced Multilevel Modeling. Sage.