Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re - st: Battling Mata docx commands - automation?


From   [email protected] (Hua Peng, StataCorp)
To   [email protected]
Subject   Re - st: Battling Mata docx commands - automation?
Date   Thu, 03 Oct 2013 13:00:28 -0500

Steve Kay <[email protected]> would like to automate the process
of outputting regression tables to a docx file:

>I'm running numerous regressions all with the same predictors but varying via
>the dependent variable. A simple example of what I'm managed to achieve is
>below:
>
>sysuse auto,clear
>label variable mpg "Miles Per Gallon"
>label variable price "Price (£'s)"
>
>foreach var of varlist mpg price {
>	regress `var' foreign headroom
>	mat define r_table = r(table)'
>	matselrc r_table `var'Betas,  c(1/6) // might need to download associated ado 
>}
>
>mata
>dh = _docx_new() 
>_docx_paragraph_new_styledtext(dh, "Miles Per Gallon", "Heading1")
>tid = _docx_add_matrix(dh, "mpgBetas", "%8.4f", 1, 1)
>_docx_table_mod_cell(dh, tid, 2, 1, "Foreign")
>_docx_table_mod_cell(dh, tid, 3, 1, "Headroom")
>_docx_table_mod_cell(dh, tid, 4, 1, "Constant")
>
>_docx_paragraph_new_styledtext(dh, "Price (£'s)", "Heading1")
>tid = _docx_add_matrix(dh, "priceBetas", "%8.4f", 1, 1)
>_docx_table_mod_cell(dh, tid, 2, 1, "Foreign")
>_docx_table_mod_cell(dh, tid, 3, 1, "Headroom")
>_docx_table_mod_cell(dh, tid, 4, 1, "Constant")
>res = _docx_save(dh, "Laborious Method.docx")
>end
>
>I'm trying to write a Mata function which I can call from my foreach loop so
>I can do away with having to write individual mata code for each dependent
>variable (as in the example above). The function would take the dependent
>variable's label to use as a heading and Beta matrix. It's defeated me.  Can
>anyone provide code to do such? Any help much appreciated - even knowing it
>can't be done.


The following example would produce the same docx file as Steve's code.  The
trick is to modify the matrix rownames before calling _docx_add_matrix().

Also note the call to _docx_close(dh) at the end of my code after
the file has been saved.  This is important to make sure the
handle to the .docx file is closed.

//--------------------------------------------------------------------------

cscript
clear mata

cap erase test.docx

sysuse auto,clear
label variable mpg "Miles Per Gallon"
label variable price "Price (£'s)"

mata:
dh = _docx_new() 
end

foreach var of varlist mpg price {
	regress `var' foreign headroom
	mat define r_table = r(table)'
	matselrc r_table `var'Betas,  c(1/6) 

	/* -------------------------------------------------------------- */
	/*      Replace _cons with Constant then make the rownames proper */
	local rowNames : rownames `var'Betas
	local rowNames = subinstr(`"`rowNames'"', "_cons", "Constant", 1)
	local rowNames = proper(`"`rowNames'"')
	
	/* -------------------------------------------------------------- */
	/*      Change the rownames of the matrix to be proper names      */	
	matrix rownames `var'Betas = `rowNames'
	
	/* -------------------------------------------------------------- */
	/*      Construct the Heading1 based on the variable label        */		
	local varLabel : variable label `var'
	local varBetas = "`var'Betas"

	/* -------------------------------------------------------------- */
	/*      Call the mata function to output variable label           */
	/*      and the matrix to the docx                                */		
	mata: _docx_paragraph_new_styledtext(dh, st_local("varLabel"), "Heading1")
	mata:  tid = _docx_add_matrix(dh, st_local("varBetas"), "%8.4f", 1, 1)
}

mata:
res = _docx_save(dh, "example.docx")
_docx_close(dh)
end

exit
//--------------------------------------------------------------------------

Hua 
[email protected]

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index