version 8 capture log close set matsize 500 set more off set scheme s2mono log using st8ch6, text replace // * // * RM4STATA Ch 6: Models for Nominal Outcomes - 5/26/2003 // * // * Section 6.2: the multinomial logit model * construct new outcome variables with 2 or 3 categories use nomocc2, clear tab occ gen occ3 = occ recode occ3 1/3=1 4=2 5=3 label def locc3 1 Manual 2 WhiteCol 3 Prof label val occ3 locc3 tab occ occ3,miss gen wc_man= occ3==2 if occ3~=3 gen prof_man = occ3==3 if occ3~=2 gen prof_wc = occ3==3 if occ3~=1 label def lwm 1 WhiteCol 0 Manual label def lpm 1 Prof 0 Manual label def lpw 1 Prof 0 WhiteCol label val wc_man lwm label val prof_man lpm label val prof_wc lpw tab occ3 wc_man, miss tab occ3 prof_man, miss tab occ3 prof_wc, miss tab prof_man, miss * binary logits tab prof_man, miss logit prof_man ed, nolog * estimate the corresponding multinomial logit tab occ3, miss mlogit occ3 ed, nolog * effect of different category values for outcome gen occ999 = occ3 recode occ999 1=-3 2=0 3=999 mlogit occ3 ed, nolog b(3) mlogit occ999 ed, nolog b(999) // * Section 6.2.1: example of occupational attainment desc white ed exper sum white ed exper tab occ mlogit occ white ed exper, basecategory(5) nolog // * Section 6.2.2: using different base categories listcoef white, help listcoef, pvalue(.05) // * Section 6.2.3: predicting perfectly (example not in book) use ordwarm2, clear gen byte dprst = (prst<20 & warm==1) tab dprst warm, miss mlogit warm yr89 male white age ed dprst, nolog drop if dprst==1 mlogit warm yr89 male white age ed, nolog // * Section 6.3.2: testing the effects of the independent variables * LR tests of single coefficient using -lrtest- use nomocc2, clear mlogit occ white ed exper, basecategory(5) nolog estimates store fmodel * test of white quietly mlogit occ ed exper, basecategory(5) nolog estimates store nmodel_white lrtest fmodel nmodel_white * test of ed quietly mlogit occ white exper, basecategory(5) nolog estimates store nmodel_ed lrtest fmodel nmodel_ed * test of exper quietly mlogit occ white ed, basecategory(5) nolog estimates store nmodel_exper lrtest fmodel nmodel_exper * tests using -mlogtest- quietly mlogit occ white ed exper, basecategory(5) nolog mlogtest, lr * Wald tests of single variable using -test- quietly mlogit occ white ed exper, basecategory(5) nolog test white test ed test exper * tests using -mlogtest- mlogtest, wald * LR test of multiple variables using -lrtest- mlogit occ white ed exper, basecategory(5) nolog estimates store fmodel mlogit occ white, basecategory(5) nolog estimates store nmodel lrtest fmodel nmodel * test using -mlogtest- mlogit occ white ed exper, basecategory(5) nolog mlogtest, lr set(ed exper) * Wald test of multiple variables with -test- mlogit occ white ed exper, basecategory(5) nolog test ed exper * test using -mlogtest- mlogit occ white ed exper, basecategory(5) nolog mlogtest, wald set(ed exper) // * Section 6.3.3: tests for combining dependent categories * Wald test of combining categories using -test- mlogit occ white ed exper, basecategory(5) nolog mlogtest, combine test [Menial] test [Menial=Craft] * LR test of combining categories using -mlogtest- mlogit occ white ed exper, basecategory(5) nolog mlogtest, lrcomb * example of the constraint imposed by -mlogtest- mlogit occ white ed exper, nolog estimates store fmodel constraint define 999 [Menial] mlogit occ exper ed white, base(2) constraint(999) nolog estimates store nmodel lrtest fmodel nmodel // * Section 6.4: independence of irrelevant alternatives * Hausman test of iia mlogit occ white ed exper, basecategory(5) nolog mlogtest, hausman base * example using alternative outcomes (not in book) quietly mlogit occ white ed exper, basecategory(1) nolog mlogtest, hausman base quietly mlogit occ white ed exper, basecategory(2) nolog mlogtest, hausman base quietly mlogit occ white ed exper, basecategory(3) nolog mlogtest, hausman base quietly mlogit occ white ed exper, basecategory(4) nolog mlogtest, hausman base * Small-Hsiao test of iia quietly mlogit occ white ed exper, basecategory(5) nolog set seed 339487731 mlogtest, smhsiao set seed 8675309 mlogtest, smhsiao // * Section 6.6.2: in sample predicted probabilities with -predict- * estimate model and compute predictions mlogit occ white ed exper, basecategory(5) nolog predict ProbM ProbB ProbC ProbW ProbP desc Prob* sum Prob* * using -predict- to compare -mlogit- and -ologit- use ordwarm2,clear ologit warm yr89 male white age ed prst, nolog predict SDologit Dologit Aologit SAologit label var Dologit "ologit-D" mlogit warm yr89 male white age ed prst, nolog predict SDmlogit Dmlogit Amlogit SAmlogit label var Dmlogit "mlogit-D" dotplot Dologit Dmlogit, ylabel(0(.25).75) /// ysize(2.0254) xsize(3.0381) graph export 06omlogdot.eps, replace * another interesting way to compare the predictions (not in book) corr SDologit SDmlogit corr Dologit Dmlogit corr Aologit Amlogit corr SAologit SAmlogit graph twoway scatter Dologit Dmlogit, /// xtitle("mlogit: Pr(y=Disagree)") /// ytitle("ologit: Pr(y=Disagree)") /// ylabel(0(.25).75) xlabel(0(.25).75) sort graph export omloggraph.eps, replace // * Section 6.6.3: individual predicted probabilities with -prvalue- use nomocc2, clear mlogit occ white ed exper, basecategory(5) nolog quietly prvalue, x(white=0) rest(mean) save prvalue, x(white=1) rest(mean) dif // * Section 6.6.4: tables of predicted probabilities with -prtab- label def lwhite 0 NonWhite 1 White label val white lwhite prtab ed white, novarlbl outcome(1) // * Section 6.6.5: graphing predicted probabilities with -prgen- * plotting probabilities to compare two groups quietly mlogit occ white ed exper, basecategory(5) prgen ed, x(white=1) from(6) to(20) generate(wht) ncases(15) desc wht* prgen ed, x(white=0) from(6) to(20) generate(nwht) ncases(15) desc nwht* label var whtp1 "Whites" label var nwhtp1 "Nonwhites" graph twoway connected whtp1 nwhtp1 nwhtx, /// xtitle("Years of Education") /// ytitle("Pr(Menial Job)") /// ylabel(0(.25).50) xlabel(6 8 12 16 20) /// ysize(2.7051) xsize(4.0421) graph export 06prmenial.eps, replace * plotting probabilities for all outcomes for one group label var whts1 "Menial" label var whts2 "Blue Collar" label var whts3 "Craft" label var whts4 "White Collar" graph twoway connected whts1 whts2 whts3 whts4 whtx, /// xtitle("Whites: Years of Education") /// ytitle("Summed Probability") /// xlabel(6(2)20) /// ylabel(0(.25)1) /// ysize(2.6195) xsize(4.0421) graph export 06prsum.eps, replace gen zero = 0 gen one = 1 graph twoway (rarea zero whts1 whtx, bc(gs1)) /// (rarea whts1 whts2 whtx, bc(gs4)) /// (rarea whts2 whts3 whtx, bc(gs8)) /// (rarea whts3 whts4 whtx, bc(gs11)) /// (rarea whts4 one whtx, bc(gs14)), /// ytitle("Summed Probability") /// legend( order( 1 2 3 4 5) /// label( 1 "Menial") /// label( 2 "Blue Collar") label( 3 "Craft") /// label(4 "White Collar") label(5 "Professional")) /// xtitle("Whites: Years of Education") /// xlabel(6 8 12 16 20) ylabel(0(.25)1) /// ysize(2.6195) xsize(4.0421) /// plotregion(margin(zero)) graph export 06prsumrevised.eps, replace // * Section 6.6.6: changes in predicted probabilities mlogit occ white ed exper, basecategory(5) nolog * prchange prchange * mfx compute: patience, this can take a long time! mfx compute, predict(outcome(1)) // * Section 6.6.7: plotting discrete changes with mlogview * Examples do not use -mlogview- since -mlogview- cannot automatically * reproduce graphs in do files; instead we use -mlogplot- mlogplot, std(0uu) p(.1) dc ntics(9) mlogplot, std(0ss) p(.1) min(-.2) max(.4) dc ntics(7) // * Section 6.6.8: odds ratios using -listcoef- and -mlogview- * plot hypothetical values: relative to A matrix mnlbeta = (-.693, .693, .347 \ .347, -.347, .693 ) matrix mnlsd = (1, 2, 4) global mnlname = "x1 x2 x3" global mnlcatnm = "B C A" global mnldepnm "depvar" mlogplot, matrix std(uuu) vars(x1 x2 x3) packed graph export 06orploteg.eps, replace * plot hypothetical values: relative to B matrix mnlbeta = (-.693, .693, .347 \ .347, -.347, .693 ) matrix mnlsd = (1, 2, 4) global mnlname = "x1 x2 x3" global mnlcatnm = "B C A" global mnldepnm "depvar" * note: use base(1) since B is the first letter in -mnlcatnm- mlogplot, matrix std(uuu) vars(x1 x2 x3) packed base(1) graph export 06orplotegb.eps, replace * plot odds ratios from occupational outcomes example mlogit occ white ed exper,basecategory(5) nolog listcoef white, help * or plot without significance levels mlogplot white ed exper, std(0ss) b(5) p(1) min(-2.75) max(.55) or /// packed ntics(7) graph export 06orplotocc1.eps,replace * or plot with significance levels mlogplot white ed exper, prob(.1) std(0ss) b(5) min(-2.75) max(.55) /// or ntics(7) graph export 06orplotocc2.eps, replace * or plot with significance levels and discrete change prchange mlogplot white ed exper, prob(.1) std(0ss) b(5) min(-2.75) max(.55) /// dc or ntics(7) graph export 06orplotocc3.eps, replace // Section 6.6.9: using -mlogplot- * or plot using -mlogplot- mlogit occ white ed exper, basecategory(5) nolog prchange mlogplot white ed exper, std(0ss) p(.1) min(-2.75) max(.55) or ntics(7) * other examples using -mlogplot- (not in book) mlogplot white ed exper, std(0ss) p(.1) min(-2.75) max(.55) or dc ntics(7) mlogplot white ed exper, dc std(0ss) min(-.5) max(.5) mlogplot white ed exper, or std(0ss) min(-2.5) max(.5) mlogplot white ed exper, or dc std(0ss) min(-.5) max(.5) mlogplot white ed exper, dc or std(0ss) min(-2.5) max(.5) p(.1) // * Section 6.6.10: plotting estimates from matrices with -mlogplot- * plot example data matrix mnlbeta = (-.693, .693, .347 \ .347, -.347, .693 ) matrix mnlsd = (1, 2, 4) global mnlname = "x1 x2 x3" global mnlcatnm = "B C A" global mnldepnm "depvar" mlogplot, matrix std(uuu) vars(x1 x2 x3) packed graph export 06matrix1.eps, replace * combining coefficients from two groups * estimate two sets of coefficients use nomocc2, clear mlogit occ ed exper if white==1, base(5) nolog mlogit occ ed exper if white==0, base(5) nolog * save coefficients for ed to mnlbeta matrix mnlbeta = /// (-.8307514, -.9225522, -.6876114, -.4196403 \ /// -.7012628, -.560695 , -.882502 , -.5311514 ) * transpose to make columns correspond to variables matrix mnlbeta = mnlbeta' * compute sd of ed and add to matrix sum ed matrix mnlsd = (2.946427,2.946427) global mnlname = "White NonWhite" global mnlcatnm = "Menial BlueCol Craft WhiteCol Prof" mlogplot, vars(White NonWhite) packed /// or matrix std(ss) /// note("Racial Differences in Effects of Education") graph export 06matrix2.eps, replace // * Section 6.7.1: data arrangement for conditional logit use travel2.dta, clear tab mode list id mode train bus time invc choice in 1/6, sepby(id) list id mode train bus time invc choice in 16/18, nodisplay // * Section 6.7.2: estimating the conditional logit model clogit choice train bus time invc, group(id) nolog // * Section 6.7.3: interpreting results from -clogit- * odds ratios listcoef * predicted probabilities predict prob list train bus time invc choice prob in 1/3 list train bus time invc choice prob in 16/18 // * Section 6.7.4: estimating MNLM with clogit * load data used for -mlogit- use nomocc2, clear * id for each case gen id = _n * make one record for each outcome expand 5 sort id * define indicators for outcomes and choice gen alt = mod(_n, 5) replace alt = 5 if alt == 0 gen choice = (occ==alt) gen menial = (alt==1) gen bluecol = (alt==2) gen craft = (alt==3) gen whitecol = (alt==4) list id menial bluecol craft whitecol choice in 1/10, sepby(id) * interactions between outcomes and variables gen whiteXm = white*menial gen whiteXbc = white*bluecol gen whiteXc = white*craft gen whiteXwc = white*whitecol gen edXm = ed*menial gen edXbc = ed*bluecol gen edXc = ed*craft gen edXwc = ed*whitecol gen experXm = exper*menial gen experXbc = exper*bluecol gen experXc = exper*craft gen experXwc = exper*whitecol list id menial bluecol craft whitecol whiteXm whiteXbc whiteXc whiteXwc /// choice in 1/5, nodisplay list menial bluecol craft whitecol edXm edXbc edXc edXwc in 1/5 * to test if we did this right: mlogit occ white ed exper if alt==1, nolog * estimating the model with clogit clogit choice whiteXm edXm experXm menial whiteXbc edXbc experXbc /// bluecol whiteXc edXc experXc craft whiteXwc edXwc experXwc /// whitecol, group(id) nolog use nomocc2, clear mlogit occ white ed exper, nolog base(5) // * Section 6.7.5: using clogit to estimate a mixed models * create interactions with individual-specific variables use travel2, clear gen hincXbus = hinc*bus gen hincXtrn = hinc*train gen sizeXbus = psize*bus gen sizeXtrn = psize*train * estimate the model clogit choice train bus time invc hincXbus hincXtrn sizeXbus sizeXtrn, /// group(id) nolog listcoef log close