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: Re:Assigning Students Randomly to Classes of Heterogeneous  Sizes
From 
 
Mike Lacy <[email protected]> 
To 
 
[email protected] 
Subject 
 
st: Re:Assigning Students Randomly to Classes of Heterogeneous  Sizes 
Date 
 
Sun, 24 Apr 2011 12:29:34 -0600 
Andy Baxter <[email protected]> wrote:
>I am using Stata 11 on a dataset of students and their class
>assignments that is structured like this:
>
>_studentid  _year _school _grade _classid
>
>Now, I want to randomly assign the students to classes--within the
>same year, school, and grade--while keeping the existing class sizes
>the same.
>
>I would be grateful for any code that would accomplish this.
>
>Thanks,
>
If I understand the question correctly, this can be accomplished by 
permuting the class listings
within year, school, and grade, and merging them back onto the original data.
// example data
clear
set obs 60
gen int id = _n
gen byte year = 1 + int(2 * runiform())
gen byte school = 1 + int(2 * runiform())
gen byte grade = 1 + int(3 * runiform())
gen byte class = 1 + int(5 * runiform())
//
//  A little program to save the data that saves the data that 
-permute- feeds it.
cap prog drop saveit
prog saveit, rclass
  args file
  keep id class
  rename class newclass
  save `file', replace
  return scalar something = -1  // permute wants something
end
//
tempfile temp
permute class r(something), reps(1) strata(year school grade): saveit `temp'
merge 1:1 id using `temp'
// eyeball to check
sort year school grade
edit
Regards,
=-=-=-=-=-=-=-=-=-=-=-=-=
Mike Lacy, Assoc. Prof.
Soc. Dept., Colo. State. Univ.
Fort Collins CO 80523 USA
(970)-491-6721   
*
*   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/