0
votes

I am trying to export the F-statistic and Prob > F for 2 tests of coefficients (for many regressions each). I'm using xml_tab to export regression results into excel. I've been able to store the results for the F-stat and probability [ r(p) and r(F) ] as locals, but cannot figure out a way to automate the process so that these results show up in my overall regressions in xml_tab. I understand that it is a user-written command, but was wondering if anyone has figured out a hack. I don't want to "copy-paste" because with 2 tests and so many regressions (I have more), I want to minimize human error. My code structure is below.

reg y x control1 control2 control3, robust plus
estimates store model_1
test control1 = control2
local p_1=r(p)
local f_1=r(F)
test control1 = control3
local p_2=r(p)
local f_2=r(F)
local x = upper(word(c(current_date),1)+word(c(current_date),2)+word(c(current_date),3))
    xml_tab author_model_1 ///
    "C:\Users\analysis_(`x').xml", ///
    replace nolabel below tstat ///
    format((S2110) (SCCB0 NCCR3 NCCR2)) ///
    keep(y x control1 control2 control3) ///
    stats(N r2 r2_a p) ///
    sheet("Analysis") ///
    title("Analysis") /// 
    cwidth(0 100, 1 80, 2 80, 3 80) ///
    cnames("Basic Regression")
1
What do you mean by "...so that these results show up in my overall regressions in xml_tab." ? Have you gone through Creating print-ready tables in Stata, by Lokshin and Sajaia, The Stata Journal (2008) 8, Number 3 ? It is available on the web for free.Roberto Ferrer
Thank you! I have checked that out, but still don't know what I can do. I know that my tests can be stored as parameters r(p) and r(F), but don't know how I would get them exported in xml_tab, let alone differentiate between the two tests. I tried calling them as stored locals with the 'stat' part, but it didn't work. I'm referring to the 'stat' part of the function - or do you recommend something with the matrices?mkamenet3
I've updated my answer.Roberto Ferrer

1 Answers

1
votes

Several errors with your code:

1. The syntax for the xml_tabcommand is:

xml_tab [namelist] [, options]

where namelist "is a list of stored estimations or matrices". You have author_model_1 which does not comply with that requirement.

2. The .xml file to be output must be specified with the option save(["]filename["]) You do not comply with this either.

3. Your keep() option includes the dependent variable, but keep() is meant to handle coefficients. The dependent variable has no coefficient.

The following code works (just change the output directory)

clear all
set more off

sysuse auto

reg price mpg weight length, robust plus
estimates store model

local x = upper(word(c(current_date),1)+word(c(current_date),2)+word(c(current_date),3))
display "`x'"

xml_tab model, ///
    save("D:\USER\Desktop\myfile_(`x').xml") ///
    replace nolabel below tstat ///
    format((S2110) (SCCB0 NCCR3 NCCR2)) ///
    keep(mpg weight length) ///
    stats(N r2 r2_a p) ///
    sheet("Analysis") ///
    title("Analysis") /// 
    cwidth(0 100, 1 80, 2 80, 3 80) ///
    cnames("Basic Regression")

All this can be resolved simply looking at help xml_tab.

The command, according to its help file, allows only stored estimation results (using estimates store) and matrices. If you want to export, for example, the results of test (which will not be saved by estimates store), then you can save its results to matrices and feed that into xml_tab. However, some testing shows that you cannot feed both at the same time, so two calls have to be made. The first for all stored estimation results; the second for all matrices. Something like this works:

clear all
set more off

sysuse auto

reg price mpg weight length, robust plus
estimates store model

test mpg = weight
matrix p1 = r(p)
matrix f1 = r(F)

test mpg = length
matrix p2 = r(p)
matrix f2 = r(F)

local x = upper(word(c(current_date),1)+word(c(current_date),2)+word(c(current_date),3))

xml_tab model, save("D:\USER\Desktop\myfile_(`x').xml") replace
xml_tab p1 f1 p2 f2, save("D:\USER\Desktop\myfile_(`x').xml") append

The results are distributed in two sheets (when opened with MS Excel). I find the output clumsy for what you pursue, but I'm no expert using the xml_tab command. You might want to explore Stata built-in commands (xml_tab is a user-written command from SSC). To export directly to spreadsheet, try help export excel, help putexcel. For more general options try help export. For popular user-written commands that allow exporting results try ssc describe estout and ssc describe tabout.