8 Stata Output as Tables
To include tables of Stata output in a document’s text (as opposed to including Stata output in a specially formatted output block), requires three steps
- save the Stata output in a form that can be used for a table
- read the output into R as a matrix or data frame
- display the output with
8.2 Saving Stata output
Example data set:
(1978 automobile data)
putexcel command to save a table of results.
quietly regress mpg weight displacement quietly putexcel set regress.xlsx, replace matrix coef = r(table)' matrix(coef), names putexcel A1 = quietly putexcel save
file regress.xlsx saved
8.3 Read into R
readxl::readxlsx to import the saved table.
<- readxl::read_xlsx("regress.xlsx") x names(x) <- "variable" # the first column needs a name unlink("regress.xlsx") # cleanup the output file