【问题标题】:Stata: saving regressions coefficients and standard errors in .dta file when there are factor variablesStata:当存在因子变量时,将回归系数和标准误差保存在 .dta 文件中
【发布时间】:2015-10-04 17:21:12
【问题描述】:

我想运行几个回归并将它们的结果存储在一个 DTA 文件中,以便以后用于分析。我的限制是:

  1. 我无法安装模块(我正在为其他人编写代码,而不是 确定他们安装了哪些模块)
  2. 一些回归量是因子变量。
  3. 每个回归仅因因变量而异,因此我想将其存储在最终数据集中,以跟踪系数/方差对应的回归。

我在这里严重失去理智。鉴于Stata是统计软件,我觉得这可能很简单,但svmat真的不合作。目前我正在做的是:

sysuse census, clear
generate constant = 1
capture matrix drop regsresults // erase previously existing matrix
foreach depvar in marriage divorce {

    reg `depvar' popurban i.region constant, robust noconstant  // regressions
    matrix result_matrix = e(b)\vecdiag(e(V))                   // grab coeffs and their variances in a 2xK matrix
    matrix rownames result_matrix = `depvar'_b `depvar'_v       // add rownames to the two extra rows
    matrix regsresults = nullmat(regsresults)\result_matrix     // add those results matrix to the existing ones

}
matrix list regsresults
clear 
svmat regsresults, names(col)

这会为每个回归创建:一行存储系数,一行使用vecdiag(e(V)) 存储它们的方差。这两行的行名称是因变量名称,后跟 _b 表示系数,_v 表示方差。

我使用手动常量,因为在使用 svmat 时 _cons 不是变量的有效名称。

当然,我的“解决方案”不起作用,因为因子级别会生成奇怪的矩阵列名称,然后在调用 svmat 时这些名称是无效的变量名称。 (错误是一个简洁的invalid syntax。)考虑到我的限制,我很乐意用任何解决方案来克服这个问题。它不必使用svmat,系数和方差可以在同一行,如果它更容易等等。

【问题讨论】:

  • 你试过help postfile吗?值得一读(包括手动输入——非常重要)。
  • 您还可以更改矩阵列名。 help svmat有信息(命令为matname)。

标签: regression stata categorical-data


【解决方案1】:

为了完整起见,使用Roberto优秀的解决方案,这是最终代码:

sysuse census, clear
generate constant = 1
capture matrix drop regsresults // erase previously existing matrix
replace region = region +15
foreach depvar in marriage divorce {

    reg `depvar' popurban i.region constant, robust noconstant  // regressions
    matrix result_matrix = e(b)\vecdiag(e(V))                   // grab coeffs and their variances in a 2xK matrix
    matrix rownames result_matrix = `depvar'_b `depvar'_v       // add rownames to the two extra rows
    matrix regsresults = nullmat(regsresults)\result_matrix     // add those results matrix to the existing ones

}

matrix list regsresults
local rownames : rownames regsresults           // collects row names
local colnames : colfullnames regsresults       // collects column names
local newnames                                  // clean column names
foreach name of local colnames {
    local newnames `newnames' `=strtoname("`name'")'
}
matrix colnames regsresults = `newnames'        // attribute the cleaned column names

clear 
svmat regsresults, names(col)

// add the row names as its own variable rown
gen str rown = ""
order rown, 
local i = 1
foreach rowname in `rownames' {
    replace rown = "`rowname'" if _n == `i'
    local i = `i' + 1
}
br

【讨论】:

    【解决方案2】:

    重命名矩阵列是一种选择:

    sysuse census, clear
    generate constant = 1
    capture matrix drop regsresults // erase previously existing matrix
    foreach depvar in marriage divorce {
    
        reg `depvar' popurban i.region constant, robust noconstant  // regressions
        matrix result_matrix = e(b)\vecdiag(e(V))                   // grab coeffs and their variances in a 2xK matrix
        matrix rownames result_matrix = `depvar'_b `depvar'_v       // add rownames to the two extra rows
        matrix regsresults = nullmat(regsresults)\result_matrix     // add those results matrix to the existing ones
    
    }
    matrix list regsresults
    matname regsresults reg1 reg2 reg3 reg4, columns(2..5) explicit
    
    clear 
    svmat regsresults, names(col)
    

    对于更复杂的 namelists (reg1 - reg4),您可以预先构建语法,存储在 local 中,然后与 matname 一起使用。

    编辑

    相同的策略,但有一些自动化。它对矩阵使用宏扩展函数。见help extended_fcn

    sysuse census, clear
    generate constant = 1
    capture matrix drop regsresults // erase previously existing matrix
    foreach depvar in marriage divorce {
    
        reg `depvar' popurban i.region constant, robust noconstant  // regressions
        matrix result_matrix = e(b)\vecdiag(e(V))                   // grab coeffs and their variances in a 2xK matrix
        matrix rownames result_matrix = `depvar'_b `depvar'_v       // add rownames to the two extra rows
        matrix regsresults = nullmat(regsresults)\result_matrix     // add those results matrix to the existing ones
    
    }
    
    // list the matrix
    matrix list regsresults
    
    // get original column names of matrix
    local names : colfullnames regsresults
    
    // get original row names of matrix (and row count)
    local rownames : rowfullnames regsresults
    local c : word count `rownames'
    
    // make original names legal variable names
    local newnames
    foreach name of local names {
        local newnames `newnames' `=strtoname("`name'")'
    }
    
    // rename columns of matrix
    matrix colnames regsresults = `newnames'
    
    // convert matrix to dataset
    clear 
    svmat regsresults, names(col)
    
    // add matrix row names to dataset
    gen rownames = ""
    forvalues i = 1/`c' {
        replace rownames = "`:word `i' of `rownames''" in `i'
    }
    
    // list
    order rownames
    list, noobs
    

    另见ssc describe matnames

    【讨论】:

    • 谢谢,我会试试的。当然,在我的实际问题中它有点麻烦,因为我事先不知道为因子变量创建了多少虚拟变量,但我可能会使用levelsof 来知道这一点。
    • 我尝试了您提到的解决方案,但问题是我需要检索因子变量的唯一值。我需要在我的数据集中知道虚拟因子对哪个系数的取值。
    • [编辑后] 太棒了,非常感谢!它现在保留因子变量的值!也许还有一个问题,尽管您已经不顾一切了,是否可以将矩阵行名存储为变量?
    • 没关系,我找到了一种方法来传输 row_names 并在添加时读取它们。它很 hacky,但它有效,将发布我的最终代码。
    • 一种方法是使用循环(添加到答案中)。但应该还有更多。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-09
    • 2015-11-20
    相关资源
    最近更新 更多