Showing posts with label xlabel(none). Show all posts
Showing posts with label xlabel(none). Show all posts

Jul 1, 2014

Random graphs (23): Between and within regression

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.

Apr 29, 2014

Random graphs (22): Graphing functions

clear

twoway (function y = 2-.01*x, range(0 100))  ///
       (function y = 1-.01*x, range(0 100)), ///
    text(2 20 "High SES") ///
    text(1 20 "Low SES") ///
    xtitle(Age) ytitle(Health) ylabel(0(1)2.5, nolabel noticks) /// 
    /* ylabel(none) doesn't allow to control axis, thus
       this is a helpful workaround */ ///
    xlabel(0 100, nolabel noticks) ///
    legend(off) ///
    /*legend(label(1 "High SES") label(2 "Low SES") pos(6) col(2))*/ ///
    title("Status maintenance", box bexpand) ///
    name(maint, replace) 
twoway (function y = 2-.004*x, range(0 100))  ///
       (function y = 1-.01*x, range(0 100)), ///
    text(2 20 "High SES") ///
    text(1 20 "Low SES") ///
    xtitle(Age) ytitle(Health) ylabel(0(1)2.5, nolabel  noticks) ///
    xlabel(0 100, nolabel noticks) ///
    legend(off) ///
    /*legend(label(1 "High SES") label(2 "Low SES")) */ ///
    title("Cumulative (dis-)advantage", box bexpand) ///
    name(cumulat, replace) 
twoway (function y = 2-.015*x, range(0 100))  ///
       (function y = 1-.01*x, range(0 100)), ///
    text(2 20 "High SES") ///
    text(1 20 "Low SES") ///
    xtitle(Age) ytitle(Health) ylabel(0(1)2.5, nolabel noticks) ///
    xlabel(0 100, nolabel noticks) ///
    legend(off) ///
    /*legend(label(1 "High SES") label(2 "Low SES")) */ ///
    title("Age as leveler", box bexpand) ///
    name(leveler, replace) 
graph combine cumulat maint leveler, xcommon col(1) xsize(3) ysize(8)
graph export Graph.png, replace

// Same graph with one legend at the bottom:
twoway (function y = 2-.01*x, range(0 100))  ///
       (function y = 1-.01*x, range(0 100)), ///
    xtitle(Age) ytitle(Health) ylabel(0(1)2.5, nolabel noticks) /// 
    /* ylabel(none) doesn't allow to control axis, thus
       this is a helpful workaround */ ///
    xlabel(0 100, nolabel noticks) ///
    legend(label(1 "High SES") label(2 "Low SES") pos(6) col(2)) ///
    title("Status maintenance", box bexpand) ///
    name(maint, replace) 
twoway (function y = 2-.004*x, range(0 100))  ///
       (function y = 1-.01*x, range(0 100)), ///
    xtitle(Age) ytitle(Health) ylabel(0(1)2.5, nolabel  noticks) ///
    xlabel(0 100, nolabel noticks) ///
    legend(off) ///
    title("Cumulative (dis-)advantage", box bexpand) ///
    name(cumulat, replace) 
twoway (function y = 2-.015*x, range(0 100))  ///
       (function y = 1-.01*x, range(0 100)), ///
    xtitle(Age) ytitle(Health) ylabel(0(1)2.5, nolabel noticks) ///
    xlabel(0 100, nolabel noticks) ///
    legend(off) ///
    title("Age as leveler", box bexpand) ///
    name(leveler, replace) 
grc1leg cumulat maint leveler, xcommon col(1) ///
xsize(3) ysize(8) name(combined, replace) legendfrom(maint)
// Problem: -grc1leg- appears to ignore -xsize()- and -ysize()-
graph display combined, xsize(3) ysize(8)
// Solution: Redraw so that size commands take effect
graph export Graphwlegend.png, replace

Jul 22, 2013

Random graphs (15): Caterpillar plots

use http://www.ats.ucla.edu/stat/examples/imm/imm23, clear
// Data from Kreft and De Leeuw (1998: 58)

xtmixed math || schid:, var

predict means1, reffects 
predict means1se, reses

egen pickone = tag(schid)

sort means1
egen means1rank = rank(means1) if pickone == 1

serrbar means1 means1se means1rank if pickone == 1, ///
        scale(1.96) yline(0) xlabel(none) ///
        xtitle("Schools") ytitle("School effects on math scores") ///
        title("Null model") ///
        name(means1, replace)

xtmixed math meanses public ratio || schid:, var

predict means2, reffects 
predict means2se, reses

serrbar means2 means2se means1rank if pickone == 1, ///
        scale(1.96) yline(0) xlabel(none) ///
        xtitle("Schools") ytitle("School effects on math scores") ///
 title("After controlling for average SES, school type, ///
        and student–teacher ratio") ///
        name(means2, replace)
  
graph combine means1 means2, ycommon xcommon col(1) 

 

Reference

Kreft, Ita, and Jan De Leeuw. 1998. Introducing Multilevel Modeling. Sage.