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]

RE: st: how to store matrix in a data file


From   "rasool.bux" <[email protected]>
To   "[email protected]" <[email protected]>
Subject   RE: st: how to store matrix in a data file
Date   Wed, 24 Oct 2012 05:22:15 +0000

Thank you very much for sending me the nice code. But there is a problem in it. This is storing missing values (.) for displacement and gear_ratio column even they are included in the model.

I have compared the stored results by removing capture from the sw command. I think we have to write full variable names in the varlist vars then it is working fine i.e. displacement instead of displ, gear_ratio instead of gear.

Any suggestions.
Thanks
Rasool Bux



-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Maarten Buis
Sent: Tuesday, October 23, 2012 12:10 PM
To: [email protected]
Subject: Re: st: how to store matrix in a data file

On Tue, Oct 23, 2012 at 7:57 AM, rasool.bux wrote:
> I want to store coefficients of each model in a separate data set by running the following code. Is there any solution?
>
> sysuse auto,clear
> forv i=1/5 {
>        use "$S_FN",clear
>         generate weight2 = weight^2
>         bsample _N
>         stepwise, pr(.2): regress mpg weight weight2 displ gear turn headroom foreign price
>          mat b = e(b)
> }
>


*---------------------------- begin example ----------------------------- // data preparation sysuse auto, clear sum weight, meanonly gen c_weight = (weight - r(mean))/2000 label var c_weight "weight centered at mean (short tons)"
sum price, meanonly
gen c_price = (price - r(mean))/1000
label var c_price "price centered at mean (1000s $)"
gen c_weight2 = c_weight*c_weight
tempfile orig
save `orig'

// preparations for the loop
tempfile results
tempname b memhold
local vars "c_weight c_weight2 displ gear turn headroom foreign c_price"
postfile `memhold' `vars' using `results'

// k (in this case 20) bootstrap samples local k = 20 _dots 0, title(Selecting varialbes in bootstrap samples) reps(`k') forvalues i = 1/`k' {
        // get the bootstrap sample and estimate model
        qui use `orig', clear
        bsample
        capture stepwise, pr(.2): regress mpg `vars'

        // gives you something to look at while the loop is running
        nois _dots `i' `=_rc'

        // get names of included variables
        matrix `b' = e(b)
        local incl : colnames `b'

        // reset local res to empty
        local res ""

        // a variable gets coefficient if included and missing when not
        foreach var of local vars {
                if `: list var in incl' {
                        local res "`res' (_b[`var'])"
                }
                else {
                        local res "`res' (.)"
                }
        }

        // post those results
        post `memhold' `res'
}
postclose `memhold'

// look at the results
use `results', clear
sum
list
*----------------------------- end example ------------------------------ (For more on examples I sent to the Statalist see:
http://www.maartenbuis.nl/example_faq )

Hope this helps,
Maarten

---------------------------------
Maarten L. Buis
WZB
Reichpietschufer 50
10785 Berlin
Germany

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

-----
No virus found in this message.
Checked by AVG - www.avg.com
Version: 2013.0.2741 / Virus Database: 2616/5846 - Release Date: 10/21/12

________________________________

This e-mail may contain information that is privileged or confidential. If you are not the intended recipient, please delete the e-mail and any attachments and notify us immediately.


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


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