Stata: combining coefficients/standard errors from several regressions in a single dataset (number of variables may differ)

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-02 05:33:17

You can follow the strategy of appending datasets, making small changes to the code in the question you reference:

clear
set more off

save test.dta, emptyok replace

foreach depvar in marriage divorce {

    // test data
    sysuse census, clear 
    generate constant = 1
    replace marriage = . if region == 4 

    // regression
    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

    // get original column names of matrix
    local names : colfullnames result_matrix

    // get original row names of matrix (and row count)
    local rownames : rowfullnames result_matrix
    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 result_matrix = `newnames'

    // from matrix to dataset
    clear
    svmat result_matrix, names(col)

    // add matrix row names to dataset
    gen rownames = ""
    forvalues i = 1/`c' {
        replace rownames = "`:word `i' of `rownames''" in `i'
    }

    // append
    append using "test.dta"
    save "test.dta", replace

}

// list
order rownames
list, noobs

The result is what you want. However, the problem is that the dataset is re-loaded every time around the loop; it loads data as many times as regressions you estimate.

You may want to take a look at post and check if you can manage a more efficient solution. statsby could also work, but you need to find a smart way of renaming the stored variables.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!