Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


[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 <Michael.Lacy@colostate.edu>
To   statalist@hsphsun2.harvard.edu
Subject   st: Re:Assigning Students Randomly to Classes of Heterogeneous Sizes
Date   Sun, 24 Apr 2011 12:29:34 -0600


Andy Baxter <baxterassoc@gmail.com> 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/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index