Hi, I have a large dataset on course enrollment. Individual students take courses in different semesters. Observations are unique at the individual-semester-coursenum level. Individuals also have different graduation years ("cohort"). I would like to choose, for each individual, a random sample of individuals in their cohort of a different size ("total") that is different for each individual. The best possible way I can think of is to loop through the individual observations and use the randomtag command, and create a unique identifer for each value of random tag (possibly the unique identifer of the student) - so for example, I could use the following commands:
preserve
keep id cohort total
duplicates drop /* We now have one observation per individual */
sort id
local N=_N
set seed 1357
forvalues i = 1/`N' {
local id = id[`i']
local year = cohort[`i']
local groupsize = total[`i']
randomtag if cohort == `year', count(`groupsize') g(selected)
g randomgroup = .
replace randomgroup = `id'*selected
}
sort id
save randomgroups.dta
restore
sort id
merge id using randomgroups.dta
I'm wondering if there is a faster way to do this, rather than looping over individual observations to generate random samples one at a time. Thank you for your suggestions.
Related Posts with Drawing random sample from a large data set for each observation
marginsplot mlabel format valuesDear Statalister, my first post, so forgive me if I get something wrong. My problem is this. By usin…
How to model persistence using survival analysis techniquesDear members, I am trying to model persistence in exporting on a sample of new firms using survival…
SVAR estimation with growth and inflation - Impulse responsesHello I am estimating an SVAR model with growth, taken as the first difference of log(GDP) and infl…
variance ratio test resultArray Hi.I did the variance ratio test after two samples using groups t test and the result is as se…
how to do fixed effects regressionHey my regression does not work, how do I do a fund and time fixed effectes regression? What do I ha…
Subscribe to:
Post Comments (Atom)
0 Response to Drawing random sample from a large data set for each observation
Post a Comment