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: Obtaining a average replications from a random draw


From   Maarten Buis <[email protected]>
To   [email protected]
Subject   Re: st: Obtaining a average replications from a random draw
Date   Fri, 14 Mar 2014 09:50:40 +0100

On Thu, Mar 13, 2014 at 9:02 PM, Theophilus Dapel wrote:
> I have a simple predicted equation for approximately 7000 observations and need to obtain the value of "y":
>
> y = a + bx + e -------------- (1)
>
> The parameters "a" and "b" are given. Values of the variable "x" are also given. To fine the value of 'y', we need the given values of ''e''.
>
> Now, I have "e" elsewhere with approximately 8000 observations. I wish to take a random draw of 7000 with replacement from the 8000 so I can substitute for "e" in (1). This is fairly simple because I was able to use bsample 7000. This is just a single replication. What if I want to have at least 300 replications (sampling "e" 300 times and substituting for "e" 300 times) and then take the average of the 300 "y's" resulting from the replication and use the average in place of "y" .

Notice that many models assume that the average of e is 0, so the
result of this entire excercise is just a + bx.

If you still want to do this, then here is how I would do it:

*------------------ begin example ------------------
//============== prepare an example
clear all
tempfile vars errs result

// the variables
set obs 10
gen x1 = rnormal()
gen x2 = rnormal()
save `vars'

// the coefficients
tempname a b c
scalar `a' =  1
scalar `b' =  2
scalar `c' = -3

// the errors
drop _all
set obs 20
gen e = rnormal()
save `errs'

//============== do the sampling
// create an id and xb variable
use `vars'
gen id = _n
gen double xb = `a' + `b'*x1 + `c'*x2
save `vars' , replace

// prepare the result datase
drop _all
gen id = .
gen y = .
save `result'

// sample the errors add them to xb and collect the results
forvalues i = 1/300 {
    use `errs'
    bsample 10
    gen id = _n
    qui merge 1:1 id using `vars'
    gen y = xb + e
    keep id y
    append using `result'
    qui save `result', replace
    _dots `i' 0
}

// compute the mean
collapse (mean) y, by(id)
*------------------- end example -------------------
* (For more on examples I sent to the Statalist see:
* http://www.maartenbuis.nl/example_faq )

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


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