Hello,
I want to assign observations to groups within strata. I want specific and uneven group sizes. Such as, 400 obs in group 1, 180 obs in group 2 etc. I have more observations than the combined number of observations in all groups.
My existing code:
************************************************** *************
set seed 12345 /* Seed for random number*/
bys strata: gen rand = uniform() if !missing(strata) /* Generate random number within strata*/
egen N_r=total(sample==1) /*Total number of obs in sample*/
gen share_r1=(400/N_r) /*400 obs in group 1*/
gen share_r2=(180/N_r) /*180 obs in group 2*/
* Assign treatments:
replace T_group="1" if rand> (0) & !missing(rand)
replace T_group="2" if rand> (0+share_r1) & !missing(rand)
replace T_group="Rest " if rand> (0+share_r1+share_r2) & !missing(rand)
************************************************** *************
With this method I get almost the right number of observations per group, but not exactly, I guess since the random numbers are not exactly evenly spread in [0,1].
I also tried the command randomtag, but I don't get it to work within strata.
I am using Stata 14.
Thanks a lot for helping!
Best,
Louise
I want to assign observations to groups within strata. I want specific and uneven group sizes. Such as, 400 obs in group 1, 180 obs in group 2 etc. I have more observations than the combined number of observations in all groups.
My existing code:
************************************************** *************
set seed 12345 /* Seed for random number*/
bys strata: gen rand = uniform() if !missing(strata) /* Generate random number within strata*/
egen N_r=total(sample==1) /*Total number of obs in sample*/
gen share_r1=(400/N_r) /*400 obs in group 1*/
gen share_r2=(180/N_r) /*180 obs in group 2*/
* Assign treatments:
replace T_group="1" if rand> (0) & !missing(rand)
replace T_group="2" if rand> (0+share_r1) & !missing(rand)
replace T_group="Rest " if rand> (0+share_r1+share_r2) & !missing(rand)
************************************************** *************
With this method I get almost the right number of observations per group, but not exactly, I guess since the random numbers are not exactly evenly spread in [0,1].
I also tried the command randomtag, but I don't get it to work within strata.
I am using Stata 14.
Thanks a lot for helping!
Best,
Louise
Comment