STOP ******************************************************************************** *** NCRM ONLINE RESOURCE *** PRODUCING AUTOMATED PUBLICATION OUTPUTS **# PART 3: OUTPUTTING SAVED RESULTS /* There are times when it is useful to output individual results to be used in the text of a report of statistical results (e.g. in a Word file). This file will demonstrate how to output saved results to Word files using the putdocx commands. */ ******************************************************************************** **# 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 * Examine some variables tab1 sex rural agegrp diabetes summ height weight * Keep complete cases keep if !missing(sex, rural, agegrp, height, weight, heartatk) count ******************************************************************************** **# Saved results /* The results of Stata commands are stored in Stata's memory so that they can be accessed later. These stored commands can be used in other commands and in calculations. Using stored results prevents you from cutting and pasting results which may lead to errors. Using stored results also promotes efficiency in your code. Stata commands are categorised into different 'classes' which store results slightly differently. You can read about all the different types of commands in the Stata manual: https://www.stata.com/manuals/rstoredresults.pdf Commands for producing statistical results are usually either r-class or e-class. e-class are commands for estimation (e.g. regression models) r-class are commsnds for other statistical results (e.g. descriptives) e-class commands store results in e() and you can view the results obtained from an e-class command by typing 'ereturn list' r-class commands store results in r() and you can view the results obtained from an r-class command by typing 'return list' Stata help files usually contain a section on 'stored results' which tells you where the results are stored and what their names are. */ summarize weight help return list return list regress weight ib1.sex ib0.rural ib3.agegrp height, allbaselevels help ereturn list ereturn list /* Results are replaced the next time you execute another command of the same class. Below we show some examples of the use of stored results. Here we centre a variable on the mean using stored results from summarize (an r-class command). */ summarize age return list * The mean weight is stored under r(mean) gen age_centre = age - r(mean) summarize age_centre scatter age age_centre * Here we undertake a logistic regression (an e-class command). logit heartatk ib1.sex ib0.rural ib3.agegrp, allbaselevels /* We can see what results are stored from this command using ereturn list */ ereturn list /* We can also get Stata to show the coefficient labels for the stored results using the 'coeflegend' option. */ logit heartatk ib1.sex ib0.rural ib3.agegrp, allbaselevels coeflegend * We can look at particular stored results using 'display' display _b[2.sex] display _se[4.agegrp] /* Here we calculate the confidence interval for the female coefficient stored results for the standard error and coefficient. */ display _b[2.sex] - (1.96*_se[2.sex]) display _b[2.sex] + (1.96*_se[2.sex]) /* You could include a stored result in a graph. For example, using the logistic regression model above we could produce a graph of log odds and standard errors and then add a note of the Pseudo R-Squared value and sample size. You will need to install the coefplot command to draw this coefplot if you don't already have it. */ ssc install coefplot logit heartatk ib1.sex ib0.rural ib3.agegrp, allbaselevels /* In the code below we save the stored values for Pseudo R-Squared (r2_p) and sample size (N) as local macros and we then include these stored results in the note of the graph. Local macros are shortcuts. We ascribe a piece of information (e.g. a saved result) to a macro. More info on macros is available here: https://www.stata.com/manuals/pmacro.pdf Note: This block of code includes local macros which Stata only remembers within a block of code so you will need to run the code below all at once, as one block. */ local r2 = e(r2_p) local n = e(N) coefplot, omitted baselevels drop(_cons) xline(0) /// title("Logistic Regression: Prior Heart Attack", size(med)) /// subtitle("Log Odds and 95% Standard Errors") /// note("Data: UKHLS, n = `n', Pseudo R2 = `r2'.") /// scheme(s1color) /* You will see that Pseudo R-Squared is reported to a large number of decimal places. You can change this using Stata's format options in the local macro. */ local r2 : display %6.3f e(r2_p) local n = e(N) coefplot, omitted baselevels drop(_cons) xline(0) /// coeflabels(age_dv = "Age") /// title("Logistic Regression: Voted in Last Election", size(med)) /// subtitle("Log Odds and 95% Standard Errors") /// note("Data: NHANES, n = `n', Pseudo R2 = `r2'.") /// scheme(s1color) /* Let's save a copy of this graph. Note: you can only export a graph while the graph window is still open. */ graph export "coefplot.png", replace ******************************************************************************** **# The putdocx commands /* StataCorp notes that Stata was designed with reproducibility in mind from the outset. It's strength in backwards compatibility supports this. In the last two versions of the Stata (versions 16 and 17) there have been many more advances in functionality for reproducible reporting. The Stata Reporting Reference Manual is an excellent resource and explains the use of a number of commands designed to enable the production of Word, Excel, PDF of HTML files which include formatted text, Stata results and graphs. Stata Reporting Reference Manual: https://www.stata.com/manuals/rpt.pdf Here we provide an example of the putdocx commands which allow you to output stored results and graphs directly into a Word document. */ /* Here we begin a Word document. Note: there is not much to see until you open the Word file at the end of this section. */ putdocx begin * We add a paragraph to the document using the 'heading' format. putdocx paragraph, style(Heading1) * We specify the content of the heading. putdocx text ("Results") /* Here we run the logistic regression model again and prepare some saved results in local macros which we will report in the Word file we are compiling. We write a block of text between the 'begin' and 'end' commands. We include references to the local macros using <> tags. Note: You should highlight this block of code and run it as one as it uses local macros. */ logit heartatk ib1.sex ib0.rural ib3.agegrp, allbaselevels local r2 : display %6.3f e(r2_p) local n = e(N) local sex : display %6.3f _b[2.sex] putdocx textblock begin There was a significant association between sex and suffering a heart attack. The log odds for women is <>. There are <> sample members. The Pseudo R2 of the logistic regression model is <>. putdocx textblock end * We start a new paragraph and we insert the coefplot we saved earlier. putdocx paragraph putdocx image "coefplot.png" * We specify where we want to save the Word file. putdocx save "mypaper.docx", replace /* If you open this Word file you will see the content you compiled. It would probably be a little cumbersome to write a whole paper via putdocx but exporting numbers and graphs to Word will reduce the opportunity for errors and promote efficiency. If you were regularly producing the same statistical output (e.g. a weekly report) it would be time well spend to set up a template via putdocx. This would allow you to rerun the analysis to update the numbers in the report very efficiently! We have used the putdocx commands here which produce a Word document. You can use putpdf and putexcel to produce pdf and excel documents in a similar manner. Stata's dyndoc command can be used to convert a Markdown document (a document containing both formatted text and Stata commands) into an HTML or Word document. Further details of these commands are available in the Stata help files and in the Stata Reporting Reference Manual: https://www.stata.com/manuals/rpt.pdf */ ******************************************************************************** **# END OF FILE *** Roxanne Connelly, University of Edinburgh