在Excel中存储Stata回归的结果?

时间:2015-10-28 14:50:39

标签: stata

我对如何在循环中存储Stata中的OLS回归结果有疑问。这是我的代码的结构:

foreach i2 of numlist 1 2 3{
        foreach i3 of numlist 1 2 3 4{
        *select dependent variable and 5 covariates
        *quiet: eststo: reg dep covariates, robust
        *QUESTION: I want to store in one row of an Excel file 
        the number of observations, estimated constant and 5 estimated
        coefficients
}
}

在循环结束时,我想在Excel中有一个由12行(每个模型一个)和7列(观察数,估计常数,5个估计系数)组成的表。在每行i中,我想找到模型i的观测数,估计常数和5个估计系数。有什么建议吗?

1 个答案:

答案 0 :(得分:2)

只需使用社区贡献的命令esttab即可创建这样的表:

sysuse auto, clear
eststo clear

eststo m1: quietly regress price weight
eststo m2: quietly regress price weight mpg

quietly esttab
matrix A = r(coefs)'
matrix C = r(stats)'

tokenize "`: rownames A'"
forvalues i = 1 / `=rowsof(A)' {
    if strmatch("``i''", "*b*") matrix B = nullmat(B) \ A[`i', 1...]
}

matrix C = B , C
matrix rownames C = "Model 1" "Model 2"

结果:

esttab matrix(C) using table.csv, eqlabels(none) mlabels(none) varlabels("Model 1" "Model 2")

----------------------------------------------------------------
                   weight          mpg        _cons            N
----------------------------------------------------------------
Model 1          2.044063                 -6.707353           74
Model 2          1.746559    -49.51222     1946.069           74
----------------------------------------------------------------