Hello Statalisters,
I'm attempting to run some simulations that alternate the number of stratification factors within a randomisation plan, and have approached a cross-roads as to which direction I continue coding. I won't bore you with all the code, but essentially i'm trying to find a better way than tuples (ssc install tuples) / nested loops to alternate the first variable in the local. As way of an example this is some skeleton code that runs with tuples
If you run it you'll see that the first variable only changes when it needs to be dropped from the macro in order to form a new combination. Otherwise, the first item in the string lhs is v1. The next snippet is even less effective, but shows another line of my thinking.
I would like to arrive at a place where I can alternate the first item within the macro tuples in the first instance or strata in the second. Perhaps there's a way of reordering locals on the fly, much like order(varname), after(othervar) for variables.
Many thanks in advance!
I'm attempting to run some simulations that alternate the number of stratification factors within a randomisation plan, and have approached a cross-roads as to which direction I continue coding. I won't bore you with all the code, but essentially i'm trying to find a better way than tuples (ssc install tuples) / nested loops to alternate the first variable in the local. As way of an example this is some skeleton code that runs with tuples
Code:
clear all set obs 10 cd "/Users/Desktop/" postfile buffer pvaltre pvalcon str50 lhs using "tuplesimtest.dta", replace forvalues i = 1/10{ gen v`i' = rbeta(3,1) } gen binary = mod(_n,2) ds binary, not skip(1) local stratlist "`r(varlist)'" tuples `stratlist' forvalues i = 1/`ntuples'{ reg `tuple`i'' binary, r matrix A = r(table) post buffer (A[4,1]) (A[4,2]) ("`tuple`i''") } postclose buffer use tuplesimtest.dta, clear sort lhs
Code:
clear all set obs 10 cd "/Users/Desktop/" local strata = "" postfile buffer pvaltre pvalcon str50 lhs using "nestedsimtest.dta", replace forvalues i = 1/10{ gen v`i' = rbeta(3,1) } gen binary = mod(_n,2) ds binary, not skip(1) local stratlist "`r(varlist)'" local strata forval stratnum = 1/9{ local strat: word `stratnum' of `stratlist' local strata `"`strata' `strat'"' qui egen strata=group(`strata') foreach var of local strata{ qui reg `var' binary, r matrix A = r(table) post buffer (A[4,1]) (A[4,2]) ("`strata'") } qui drop strata } postclose buffer use nestedsimtest.dta, clear
Many thanks in advance!
Comment