Hello Statalisters,
I am attempting to run some simulations, incrementing sample size and stratification factors to test for balance. The below code does most of what I need it to do.
1. It creates datasets of different sample sizes based on the values in -- local sample --
2. It creates covariate variables based on the categories in -- local cats --
3. It conducts stratified randomisation at different sizes of the dataset
The bit i'm struggling with is highlighted in red below. If you run the code you can see on the first loop that it creates what I need, iteratively adding "obs1" and then "obs1 obs2" and then "obs1 obs2 obs3", etc, until it is complete. But it then goes on because it is a nested loop. What I want to do is after each iteration stratify based just on obs1, and then obs1 and obs2, etc., until i'm stratifying my randomisation on all the observations generated in the first loop.
I've tried different things, such as moving both the bottom two snippets into the top bit of the code but to no avail. If anyone has any ideas, i'd welcome them!
I am attempting to run some simulations, incrementing sample size and stratification factors to test for balance. The below code does most of what I need it to do.
1. It creates datasets of different sample sizes based on the values in -- local sample --
2. It creates covariate variables based on the categories in -- local cats --
3. It conducts stratified randomisation at different sizes of the dataset
The bit i'm struggling with is highlighted in red below. If you run the code you can see on the first loop that it creates what I need, iteratively adding "obs1" and then "obs1 obs2" and then "obs1 obs2 obs3", etc, until it is complete. But it then goes on because it is a nested loop. What I want to do is after each iteration stratify based just on obs1, and then obs1 and obs2, etc., until i'm stratifying my randomisation on all the observations generated in the first loop.
I've tried different things, such as moving both the bottom two snippets into the top bit of the code but to no avail. If anyone has any ideas, i'd welcome them!
Code:
**Clear memory
clear
local sample 100 200 400 800 1600 3200 6400 10000 //set up local for different sample sizes
local strata = ""
foreach size of local sample{
preserve
qui set obs `size' //set different sample sizes
qui gen id = _n //generate a unique id
local cats "`"2"' `"2"' `"2"' `"2"' `"2"' `"2"'" //set up local for category numbers of strat vars
forval i = 1/6{
local catind :word `i' of `cats'
qui gen obs`i' = mod(_n,`catind') //generate 6 i number of strat vars based on values of local cats
}
qui ds obs*, skip(1)
local stratlist "`r(varlist)'" //store all strat vars in a local
*di `"`stratlist'"'{
forval stratnum = 1/6{
local strat: word `stratnum' of `stratlist'
local strata `"`strata' `strat'"'
di `"`strata'"'
}
qui egen strata=group(`strata') //gen a variable that makes unique groups based on strat vars
set seed 31540 //setting a seed for replicability
qui gen randomnum = runiform() //generating a random number
qui bysort strata: egen order=rank(randomnum) //generating a rank order var based on the random number
qui bysort strata: gen treat = (order <= _N/2) //assigning condition based on rank
foreach var of local strata{
tab `var' treat, r
}
des, s
restore
}
Comment