Stata - 使用XML_Tab导出F测试结果

时间:2014-02-05 19:32:06

标签: xml export regression stata

我正在尝试导出F统计量和Prob> F用于2次系数测试(每次回归很多)。我正在使用xml_tab将回归结果导出到excel中。我已经能够将F-stat和概率[r(p)和r(F)]的结果存储为本地人,但无法找到自动化流程的方法,以便这些结果显示在我的整体回归中在xml_tab中。我知道这是一个用户编写的命令,但是想知道是否有人想出了一个黑客。我不想“复制粘贴”,因为有2个测试和很多回归(我有更多),我想尽量减少人为错误。我的代码结构如下。

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 个答案:

答案 0 :(得分:1)

您的代码有几处错误:

1。 xml_tab命令的语法为:

xml_tab [namelist] [, options]

其中名单“是存储的估算或矩阵的列表”。您的author_model_1不符合该要求。

2. 必须使用选项 save(["]filename["])指定要输出的.xml文件 你也不遵守这一点。

3。您的keep()选项包含因变量,但keep()用于处理系数。因变量没有系数。

以下代码有效(只需更改输出目录)

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")

只需查看help xml_tab即可解决所有这些问题。

该命令根据其帮助文件仅允许存储的估计结果(使用estimates store)和矩阵。 例如,如果您想要导出test的结果( 不会estimates store保存),那么您可以将其结果保存到矩阵并将其输入到xml_tab。但是,有些测试显示您无法同时提供两者,因此必须进行两次调用。所有存储的估算结果的第一个;所有矩阵的第二个。这样的事情有效:

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

结果分为两张(使用MS Excel打开时)。我发现输出的输出很笨拙,但我不是使用xml_tab命令的专家。您可能想要探索Stata内置命令(xml_tab是来自SSC的用户编写的命令)。要直接导出到电子表格,请尝试help export excelhelp putexcel。有关更多常规选项,请尝试help export。对于允许导出结果的常用用户编写命令,请尝试ssc describe estoutssc describe tabout