STOP ******************************************************************************** *** NCRM ONLINE RESOURCE *** PRODUCING AUTOMATED PUBLICATION OUTPUTS **# PART 1: TABLES OF REGRESSION RESULTS /* This file introduces code to produce Word tables of regression results. Examples are provided using the etable and collect commands, the estout commands, and the asdoc command. */ ******************************************************************************** **# Set-Up clear all version 17 /* Open the nhanes2b dataset These data are from a US health survey, the National Health and Nutrition Survey. */ webuse nhanes2b, clear numlabel, add * Set svy (for complex survey design) svyset psuid [pweight=finalwgt], strata(stratid) * Examine some variables tab1 sex rural agegrp diabetes summ height weight * Keep complete cases keep if !missing(sex, rural, agegrp, height, weight, heartatk) count ******************************************************************************** **# The etable command /* etable is a built in Stata command that is new to Stata 17. To use etable you estimate a model and then run the etable command to store the results in Stata as a 'collection'. You can specify various options to format the table exactly as desired. You can then export this table to Word (or another format). You can read about the etable command in the Stata Customizable Tables and Collected Results Reference Manual: https://www.stata.com/manuals/tables.pdf You can also view the etable documentation using the 'help' command. */ help etable * We start by clearing any collections of results from Stata's memory collect clear * Now we run a linear regression model regress weight ib1.sex ib0.rural ib3.agegrp height, allbaselevels * We run the etable command to store the results as a collection etable * We export the results to a Word file called 'table1.docx' collect export "table.docx", replace /* You can open the Word file by clicking on the blue text. Close the Word file after you have viewed your table, otherwise you will not be able to edit this file. Note you can specify the location where you want this file to be saved. For example: collect export "H:/tables/table.docx", replace Or you could use a global path to specify the location of this file. For example: global table "H:/tables/" collect export "$table/table.docx", replace */ * You can display an etable in Stata's memory using the replay option. etable, replay /* Now we will specify some options to format the table. All of the options you can specify are described in the etable documentation. */ help etable /* Here we edit the formatting of the coefficients and standard errors. We display both to 2 decimal places. */ etable, replay cstat(_r_b, nformat(%4.2f)) /// cstat(_r_se, nformat(%6.2f)) /* The table doesn't show the reference category. We can add this using the command below. */ help collect style showbase collect style showbase all etable, replay cstat(_r_b, nformat(%4.2f)) /// cstat(_r_se, nformat(%6.2f)) * We could also add confidence intervals etable, replay cstat(_r_b, nformat(%4.2f)) /// cstat(_r_se, nformat(%6.2f)) /// cstat(_r_ci, cidelimiter(,) nformat(%6.2f)) * We could show significant stars etable, replay cstat(_r_b, nformat(%4.2f)) /// cstat(_r_se, nformat(%6.2f)) /// showstars showstarsnote /// stars(.05 "*" .01 "**" .001 "***", attach(_r_b)) * We could add some model fit statistics etable, replay cstat(_r_b, nformat(%4.2f)) /// cstat(_r_se, nformat(%6.2f)) /// showstars showstarsnote /// stars(.05 "*" .01 "**" .001 "***", attach(_r_b)) /// mstat(N) mstat(aic) mstat(bic) mstat(r2_a) /* We might want to change some of the variable names in the table. One way to do this would be to change the variable labels in the dataset, but you might not want to alter those. Alternatively you can change the names which the variables are stored under in the 'collection' of results in Stata's memory. Here we change the names in the collection for the 'Rural' and 'agegrp' variables You will notice that the variable names in the dataset do not change. */ help collect label collect label levels colname rural "Rural Resident", modify collect label levels colname agegrp "Age Group", modify etable, replay cstat(_r_b, nformat(%4.2f)) /// cstat(_r_se, nformat(%6.2f)) /// showstars showstarsnote /// stars(.05 "*" .01 "**" .001 "***", attach(_r_b)) /// mstat(N) mstat(aic) mstat(bic) mstat(r2_a) /* We might want to edit the column title to describe the statistics shown in the table (i.e. coefficients and standard errors). The current column label is 'weight' which is the dependent variable name. This is the default etable_depvar label. We can change this label in the collection. */ collect label levels etable_depvar 1 "Coef. (SE)", modify etable, replay cstat(_r_b, nformat(%4.2f)) /// cstat(_r_se, nformat(%6.2f)) /// showstars showstarsnote /// stars(.05 "*" .01 "**" .001 "***", attach(_r_b)) /// mstat(N) mstat(aic) mstat(bic) mstat(r2_a) * We could add a title and note to the table etable, replay cstat(_r_b, nformat(%4.2f)) /// cstat(_r_se, nformat(%6.2f)) /// showstars showstarsnote /// stars(.05 "*" .01 "**" .001 "***", attach(_r_b)) /// mstat(N) mstat(aic) mstat(bic) mstat(r2_a) /// title("Table 3: Linear Regression Model Of Weight") /// titlestyles(font(Arial Narrow, size(14) bold)) /// note("Data Source: nhanes2b") /// notestyles(font(Arial Narrow, size(10) italic)) /* Once you have your table of results exactly as you would like it to look, you can export the results to Word using the export option. Here we specify the 'replace' option, this will replace any tables in this file with the new table. Alternatively we could specify 'append' which would add this table to the Word file. */ etable, replay cstat(_r_b, nformat(%4.2f)) /// cstat(_r_se, nformat(%6.2f)) /// showstars showstarsnote /// stars(.05 "*" .01 "**" .001 "***", attach(_r_b)) /// mstat(N) mstat(aic) mstat(bic) mstat(r2_a) /// title("Table 3: Linear Regression Model Of Weight") /// titlestyles(font(Arial Narrow, size(14) bold)) /// note("Data Source: nhanes2b") /// notestyles(font(Arial Narrow, size(10) italic)) /// export("table.docx", replace) /* You can also use etable with svy when analysing complex survey data. You just need to add the svy prefix to your model as usual. */ collect clear svy: regress weight ib1.sex ib0.rural ib3.agegrp height, allbaselevels etable collect style showbase all collect label levels etable_depvar 1 "Coef. (SE)", modify etable, replay cstat(_r_b, nformat(%4.2f)) /// cstat(_r_se, nformat(%6.2f)) /// showstars showstarsnote /// stars(.05 "*" .01 "**" .001 "***", attach(_r_b)) /// mstat(N) mstat(r2) /// title("Table 3: Linear Regression Model Of Weight") /// titlestyles(font(Arial Narrow, size(14) bold)) /// note("Data Source: nhanes2b, results adjusted for sample design.") /// notestyles(font(Arial Narrow, size(10) italic)) /// export("table.docx", replace) /* etable can also produce a table of a series of regression models. We add 'quietly' to our code here so that the models are run but not printed in the Stata window. Note that we first use 'etable' alone then 'etable, append' to add models to the table. */ collect clear quietly regress weight ib1.sex, allbaselevels etable quietly regress weight ib1.sex ib0.rural, allbaselevels etable, append quietly regress weight ib1.sex ib0.rural ib3.agegrp height, allbaselevels etable, append collect style showbase all etable, replay column(index) export("table.docx", replace) * Here we specify some additional options to edit the format of this table collect style showbase all collect label levels etable_depvar 1 "Model 1" /// 2 "Model 2" /// 3 "Model 3", modify collect style cell, font(Times New Roman) etable, replay column(depvar) /// cstat(_r_b, nformat(%4.2f)) /// cstat(_r_se, nformat(%6.2f)) /// showstars showstarsnote /// stars(.05 "*" .01 "**" .001 "***", attach(_r_b)) /// mstat(N) mstat(r2_a) mstat(aic) mstat(bic) /// title("Table 1: Linear Regression Models of Weight, Coef. (SE)") /// titlestyles(font(Times New Roman, size(12) bold)) /// notestyles(font(Times New Roman, size(11) italic)) /// export("table.docx", replace) /* You can edit the options for this table to format it the way you would prefer. Remember all options are described in the etable documentation. */ help etable /* You can also prepare tables which combine regression results, and the results of postestimation commands such as marginal effects. Below we run a logistic regression model of heartatk (whether someone previously had a heart attack). Then we calculate average marginal effects. We use the etable command along with collect commands to format this table. */ collect clear logit heartatk ib1.sex ib0.rural ib3.agegrp etable margins, dydx(sex rural agegrp) asobserved nose etable, append margins /// cstat(_r_b, nformat(%4.2f)) /// cstat(_r_se, nformat(%6.2f))showstars showstarsnote /// stars(.05 "*" .01 "**" .001 "***", attach(_r_b)) /// mstat(N) mstat(r2_p) collect style showbase all collect layout collect label levels etable_depvar 1 "Log Odds (SE)" /// 2 "AME", modify collect layout collect export "table.docx", replace /* etable can also be used with other types of regression model including more complex types of model such as multilevel models. For further options and functionality see the Stata Customizable Tables and Collected Results Reference Manual: https://www.stata.com/manuals/tables.pdf and the etable documentation. */ help etable ******************************************************************************** **# The estout commands /* An alternative command to produce automated tables of regression results is esttab which is part of the estout suite of commands. These commands were developed by Professor Ben Jann: Jann, Ben (2005): Making regression tables from stored estimates. The Stata Journal 5(3): 288-308. Jann, Ben (2007): Making regression tables simplified. The Stata Journal 7(2): 227-244. This website provides a range of estout examples: http://repec.org/bocode/e/estout/esttab.html To use estout you will need to first install it if you have not done so already. */ ssc install estout help estout * First we clear any stored estimates from Stata's memory estimates clear /* To use esttab, you first run your model. You then save the results of the model using 'estimates store' and specify a name for the stored model (e.g. model1). You then output your table using 'esttab'. Here we output the results of a linear regression model. */ regress weight ib1.sex ib0.rural, allbaselevels estimates store model1 esttab model1 using "table.rtf", replace /* We export the table using the rtf (rich text format). This can be used with word processors, such as Word. Remember to close the Word file after you have viewed your table, otherwise you will not be able to edit this file. */ /* We could estimate a second model and output the results of both models. */ regress weight ib1.sex ib0.rural ib3.agegrp height, allbaselevels estimates store model2 esttab model1 model2 using "table.rtf", replace /* You can specify many different options with esttab to format the output as desired. By default esttab outputs coefficients and t statistics. You can request different statistics such as standard errors. You can also request additional model summary statistics such as R-Squared or bic. */ esttab model1 model2 using "table.rtf", replace /// se scalars(r2 bic aic) /* You can ask for standard errors to be shown next to the coefficient rather than below it using the 'wide' option. */ esttab model1 model2 using "table.rtf", replace /// se scalars(r2 bic aic) wide /* You can use esttab with a range of different models and you can use esttab with svy. */ svy: logit heartatk ib1.sex ib0.rural ib3.agegrp, allbaselevels estimates store logitsvy esttab logitsvy using "table.rtf", replace /* The estout website provides lots of examples and further details of the options you can specify to edit your table format when using esttab. http://repec.sowi.unibe.ch/stata/estout/ You can also explore all of the formatting options using the detailed esttab help file. */ help esttab ******************************************************************************** **# The asdoc command /* An alternative command to produce automated tables of regression results is asdoc. These commands were developed by Professor Attaullah Shah: Shah, A. (2018). "asdoc: Create high-quality tables in MS Word from Stata output" This website provides a number of asdoc tutorials: https://fintechprofessor.com/2018/01/31/asdoc/ To use asdoc you will need to first install it if you have not done so already. */ ssc install asdoc help asdoc * First we clear any stored estimates from Stata's memory estimates clear /* Now we rerun the logistic regression model we estimated previously. */ logit heartatk ib1.sex ib0.rural ib3.agegrp, allbaselevels /* To output regression results using asdoc you simply add the asdoc prefix to the start of your code. */ asdoc logit heartatk ib1.sex ib0.rural ib3.agegrp, allbaselevels replace /* Clicking on the blue text under the table output will open the Word file containing your table. You could also specify the name and location of this Word file. */ asdoc logit heartatk ib1.sex ib0.rural ib3.agegrp, allbaselevels /// save(table.doc) replace /* We can specify various options to edit the format of this table. Here we edit the significance levels used in the table. By default asdoc displays the 10% significance level which we would not normally use in the social sciences. You can easily change this. */ asdoc logit heartatk ib1.sex ib0.rural ib3.agegrp, allbaselevels /// setstars(***@.001, **@.01, *@.05) /// save(table.doc) replace /* We can specify the number of decimal places in the output using the 'dec' option. */ asdoc logit heartatk ib1.sex ib0.rural ib3.agegrp, allbaselevels /// setstars(***@.001, **@.01, *@.05) /// dec(2) /// save(table.doc) replace /* You can present a more compact summary of the regrssion output by using the 'nest' option. You can also use this option to present a series of nested models. Note: We start with replace, and then append the next two models. */ asdoc logit heartatk ib1.sex , allbaselevels /// setstars(***@.001, **@.01, *@.05) /// dec(2) nest /// save(table.doc) replace asdoc logit heartatk ib1.sex ib0.rural, allbaselevels /// setstars(***@.001, **@.01, *@.05) /// dec(2) nest /// save(table.doc) append asdoc logit heartatk ib1.sex ib0.rural ib3.agegrp, allbaselevels /// setstars(***@.001, **@.01, *@.05) /// dec(2) nest /// save(table.doc) append /* You can specify the inclusion of additional statistics (e.g. model fit statistics) in these outputs using the 'stat' option. */ asdoc logit heartatk ib1.sex , allbaselevels /// setstars(***@.001, **@.01, *@.05) /// dec(2) nest /// stat(aic bic) /// save(table.doc) replace asdoc logit heartatk ib1.sex ib0.rural, allbaselevels /// setstars(***@.001, **@.01, *@.05) /// dec(2) nest /// stat(aic bic) /// save(table.doc) append asdoc logit heartatk ib1.sex ib0.rural ib3.agegrp, allbaselevels /// setstars(***@.001, **@.01, *@.05) /// dec(2) nest /// stat(aic bic) /// save(table.doc) append /* asdoc can be used with other types of regression models (e.g. ols, mlogit, poisson). asdoc can also be used with complex samples data by adding the svy prefix. Note that the svy prefix comes after the asdoc prefix. */ asdoc svy: logit heartatk ib1.sex ib0.rural ib3.agegrp, allbaselevels /// setstars(***@.001, **@.01, *@.05) /// dec(2) nest /// save(table.doc) replace /* The asdoc website provides lots of examples and further details of the options you can specify to edit your table format when using asdoc. https://fintechprofessor.com/2018/01/31/asdoc/ You can also explore all of the formatting options with the aid of the very detailed asdoc help file. */ help asdoc ******************************************************************************** **# END OF FILE *** Roxanne Connelly, University of Edinburgh