Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: capturing estimated means and standard errors into a new data set


From   Kevin Geraghty <[email protected]>
To   statalist <[email protected]>
Subject   st: capturing estimated means and standard errors into a new data set
Date   Tue, 15 May 2012 15:53:09 -0700 (PDT)

I am sure many have confronted this problem:

I have a variable with some partition of subgroups defined , and want to capture the mean of that variable and accompanying standard error, for each value of the subgroup partition, into a stata data set.

I have written code to confront this problem pulling data out of the returned  matrices and dumping it into a temporary dset, but it is frankly pretty ugly and I am hoping someone can show me a more economical way to do this.

Here's what I have. note that "plat" is the name of the partition variable which defines subgroups. I am using the complex survey version of the mean command, but I believe the problem is identical with the standard version of the mean command:

    tempfile outfile
    local testvar <name of variable of interest>

    svy: mean `testvar', over(plat) level(90)
    preserve
        clear
        matrix fu = e(b)'
        local vplatnames: rowfullnames fu
        local plats: subinstr local vplatnames "`testvar':" "", all
        svmat fu, names(col) 
        rename y1 `testvar'_mean
        format `testvar'_mean %9.3f
        gen int plat=.
        local i 0
        foreach plat of local plats {
            replace plat = `plat' in `++i'
        }
        save `outfile', replace

        clear
        matrix var = vecdiag(e(V))'
        local vplatnames: rowfullnames var
        local plats: subinstr local vplatnames "`testvar':" "", all
        svmat var, names(col)
        replace r1 = sqrt(r1) 
        rename r1 `testvar'_sd
        format `testvar'_sd %9.6f
        gen int plat=.
        local i 0
        foreach plat of local plats {
            replace plat = `plat' in `++i'
        }
        merge 1:1 plat using `outfile', nogenerate 
        save `outfile', replace
    restore


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index