****************************** *Model Visualization in Stata* *********Alex Kirss*********** ****************************** **DRAFT CODE** **I have NO idea what other packages I've downloaded to make this work** * It is slightly harder to visualize model results in Stata than in R * This is mainly because in Stata you can only have one data frame in memory at a time * In order to 'crack' and 'pack' model results, therefore, you have to first 'crack' and save model outputs out to a specified file path * You then 'pack' these saved files together using the 'append' command * The main benefits of estimating models in Stata are: * 1) It is easier to estimate non linear fixed effects models * 2) It is easier to calculate complicated marginal effects (e.g. w/ interaction terms) * 3) Standard error calculations can be easier * First, set your working directory to a "Figures" folder that you can save output to clear cd "/Users/alexanderkirss/Desktop/Ongoing Projects/StatsWorkshops/181128ModelViz/Figures" * Then load your data use "/Users/alexanderkirss/Desktop/Ongoing Projects/StatsWorkshops/181128ModelViz/togd.dta" *************************** *Logistic Model Estimation* *************************** * First, estimate your model * I generally use the 'eststo' command first in order to store the full model results * I also generally use 'qui' to model it quietly (i.e. not display output) eststo: qui logit PresRun ForeignCom Party Age AgeQuad ***************************** *Estimating Marginal Effects* ***************************** * Since this is a nonlinear model, I want to extract the marginal effect of my treatment variable, which I save out to "Figures" * Can also save out all margins, but this becomes squirrelly with a mix of continuous and binary independent variables * 'marginsplot' can be a good way to visualize all the margins from a model * First, "post" the marginal effect (no one knows what this does) margins, dydx(ForeignCom) post * Then save out the marginal effect using the 'parmest' command * If I hadn't set up a working directory then I would need to write out the full file path parmest, label list(parm estimate min* max* p) saving(marg1, replace) * Iterate this across your various models sequentially * Model 2 eststo: qui logit PresRun ForeignCom Party Age AgeQuad Chamberseni margins, dydx(ForeignCom) post parmest, label list(parm estimate min* max* p) saving(marg2, replace) * Model 3 eststo: qui logit PresRun ForeignCom Party Age AgeQuad AveragePresApproval margins, dydx(ForeignCom) post parmest, label list(parm estimate min* max* p) saving(marg3, replace) * Model 4 eststo: qui logit PresRun ForeignCom Party Age AgeQuad Chamberseni AveragePresApproval margins, dydx(ForeignCom) post parmest, label list(parm estimate min* max* p) saving(marg4, replace) * You can output the full model results in tabular form using 'esttab' esttab using logit.csv, replace eststo clear **************************** *"Packing" Marginal Effects* **************************** * In order to "pack" these marginal effects together, clear the data in the memory clear * Then load the first marginal effect you've saved use "marg1.dta" * You can merge in other marginal effects by row using the 'append' command append using marg2.dta append using marg3.dta append using marg4.dta ******************* *Graphical Wrapper* ******************* * We will be using the 'eclplot' command as a graphical wrapper * It is slightly more customizable than the 'marginsplot' command, which generally is used for only one model * Label your models with a destringed identifier replace label = "1" in 1 replace label = "2" in 2 replace label = "3" in 3 replace label = "4" in 4 destring label, generate(label2) * Then generate the eclplot, saving it out to your figures folder * What is in this code? * 1) the first line sets up the actual plot, highlighting the parameter of interest (estimate) the CI (min95 max95) and label (label2) * 2) next line determines the plot type (rspike) and fills in the dots * 3) third line puts a line at zero and flips the plot on its side * 4) Here I label my labels, placing text so I can tell each model apart (could also label different variables if those are my estimates) * 5) Finally I title it eclplot estimate min95 max95 label2, /// rplottype(rspike) estopts(msym(o)) /// hori xline(0, lpattern(shortdash)) /// ylabel(1 "Model 1 (Logit)" 2 "Model 2 (Logit)" 3 "Model 3 (Logit)" 4 "Model 4 (Logit)") /// ytitle("P(Presidential Run)") xtitle(Marginal Effect (AME)) /// title("Figure 1: Effect of SFRC Membership on Presidential Run") graph export "Figure1.png", replace ******************************** *Fixed Effects Logit Estimation* ******************************** * It is FAR easier to calculate fixed effect logit models in Stata than R * Just use the 'xtlogit' command and the code from above * Have to reload data clear use "/Users/alexanderkirss/Desktop/Ongoing Projects/StatsWorkshops/181128ModelViz/togd.dta" * First, define your indicators using 'xtset' xtset ICPSR * Then calculate your models as above, saving out the marginal effects * Model 1 eststo: qui xtlogit PresRun ForeignCom Party Age AgeQuad, fe margins, dydx(ForeignCom) post parmest, label list(parm estimate min* max* p) saving(margFE1, replace) * Model 2 eststo: qui xtlogit PresRun ForeignCom Party Age AgeQuad Chamberseni, fe margins, dydx(ForeignCom) post parmest, label list(parm estimate min* max* p) saving(margFE2, replace) * Model 3 eststo: qui xtlogit PresRun ForeignCom Party Age AgeQuad AveragePresApproval, fe margins, dydx(ForeignCom) post parmest, label list(parm estimate min* max* p) saving(margFE3, replace) * Model 4 eststo: qui xtlogit PresRun ForeignCom Party Age AgeQuad Chamberseni AveragePresApproval, fe margins, dydx(ForeignCom) post parmest, label list(parm estimate min* max* p) saving(margFE4, replace) * You can output the full model results in tabular form using 'esttab' esttab using logitfe.csv, replace eststo clear * "Pack" these into a new data frame, alongside simple logit if you like clear use "marg1.dta" append using marg2.dta append using marg3.dta append using marg4.dta append using margFE1.dta append using margFE2.dta append using margFE3.dta append using margFE4.dta * Then plot all together replace label = "1" in 1 replace label = "2" in 2 replace label = "3" in 3 replace label = "4" in 4 replace label = "5" in 5 replace label = "6" in 6 replace label = "7" in 7 replace label = "8" in 8 destring label, generate(label2) eclplot estimate min95 max95 label2, /// rplottype(rspike) estopts(msym(o)) hori xline(0, lpattern(shortdash)) /// ylabel(1 "Model 1 (Logit)" 2 "Model 2 (Logit)" 3 "Model 3 (Logit)" 4 "Model 4 (Logit)" 5 "Model 1 (FE)" 6 "Model 2 (FE)" 7 "Model 3 (FE)" 8 "Model 4 (FE)") /// ytitle("P(Presidential Run)") xtitle(Marginal Effect (AME)) title("Figure 2: Effect of SFRC Membership on Presidential Run, Logit vs. FE") graph export "Figure2.png", replace