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: Paste values of scalar in observation (bootstrap)


From   Camila Mendes <[email protected]>
To   [email protected]
Subject   Re: st: Paste values of scalar in observation (bootstrap)
Date   Wed, 16 Oct 2013 13:11:36 -0300

Hi all,

Nick Cox provided me an answer on Stack Overflow's website and now it
works perfectly:

clear all
forvalues j = 0/1{
    foreach l in P2 P3 P4 {
        use `l'_per_`j', clear
        set seed 1
        reg estimate xaxis
        predict yhat
        gen yhat_bs = yhat
        gen ll_95per = .
        gen ul_95per = .
        forval i = 1/11 { //number of time periods to be predicted
            bootstrap pred=(_b[_cons] + _b[xaxis]*`i'), reps(1000)
seed(1): reg estimate xaxis
            matrix b = e(b)
            matrix ci_normal = e(ci_normal)
            replace yhat_bs = b[1,1] if xaxis == `i'
            replace ll_95per = ci_normal[1,1] if xaxis == `i'
            replace ul_95per = ci_normal[2,1] if xaxis == `i'
        }
        save `l'_per_`j'_lin_trend, replace
    }
}

Thanks,

Camila

On Wed, Oct 16, 2013 at 10:02 AM, Camila Mendes <[email protected]> wrote:
> Hi everyone:
>
> I need help to paste bootstraped coefficients and their lower and
> upper 95% CI in new variables to produce graphs.
>
> I tried the code below. It looks like scalars are retrieved with
> sucess, but I am not able to paste them into specific observations
> when using replace.
>
> Can anyone help me?
>
> Thanks,
>
> Camila
>
> ---- begin of do-file ----
> clear all
> forvalues j=0/1{
> foreach l in P2 P3 P4 {
> use `l'_per_`j', clear
> set seed 1
> reg estimate xaxis
> predict yhat
> gen yhat_bs=yhat
> gen ll_95per=.
> gen ul_95per=.
> local N = _N
> foreach i in 1/11 { //number of time periods to be predicted
> bootstrap pred=(_b[_cons] + _b[xaxis]*`i'), reps(1000) seed(1): reg
> estimate xaxis
> matrix b=e(b)
> scalar sb=b[1,1]
> matrix ci_normal=e(ci_normal)
> local par=b[1,1]
> local ll=ci_normal[1,1]
> local ul=ci_normal[2,1]
> replace yhat_bs=`par' if xaxis==`i'
> replace ll_95per=`ll' if xaxis==`i'
> replace ul_95per=`ul' if xaxis==`i'
> }
> save `l'_per_`j'_lin_trend, replace
> }
> }
> ---- end of do-file ----
> *
> *   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/
*
*   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