Hi Dr. Schechter,
I have taken a look at a previous post of yours where you simulated multi-level data and looped over combinations of parameter values as follows:
*Generate multi-level data
capture program drop swcrt
program define swcrt, rclass
version 15.1
preserve
clear
*Stepped-wedge specifications
local num_clus 3 6 9 18 36
local ICC_lvl3
local ICC_lvl2
local clussize 25
*Model specifications
local intercept
local timecoeff
local intcoeff
local sigma_u3
local sigma_u2
local sigma_error
local nrep 100
local alpha 0.05
args num_clus ICC_lvl3 ICC_lvl2 clussize intercept timecoeff intrvcoeff
assert `num_clus' > 0 `ICC_lvl3' > 0 `ICC_lvl2' > 0 `clussize' > 0 `intercept' > 0 `timecoeff' > 0 `intrvcoeff' > 0 `u_3' > 0 `u_2' > 0 `error' > 0
/*Generate simulated multi—level data*/
qui
set seed 12345
clear
set obs `num_clus'
qui gen cluster = _n
//Generate cluster-level errors //
qui gen u_3 = rnormal(0,`sigma_u3')
expand `clussize'
bysort cluster: gen individual = _n
//Generate patient-level errors //
qui gen u_2 = rnormal(0,`sigma_u2')
expand 6
//Set up time//
bysort cluster individual: gen time = _n-1
//Set up intervention variable//
gen intrv = (time>=cluster)
//Generate residual errors
qui gen error = rnormal(0,`sigma_error')
//Generate outcome y
qui gen y = `intercept' + `timecoeff'*time + `intrvcoeff'*intrv + u_3 + u_2 + error
/*Fit multi-level model to simulated dataset*/
mixed y intrv i.time ||cluster: ||individual:, covariance(unstructured) reml dfmethod(kroger)
/*Return estimated effect size, p-value, and significance dichotomy*/
tempname M
matrix `M' = r(table)
return scalar p = `M'r(p)
return scalar p_= `M'(`r(p)' < 0.05)
exit
end swcrt
*Postfile to store results
tempfile powerresults
capture postutil clear
postfile step int num_clus intrv ICC using `results'
*Loop over number of clusters, ICC
foreach x of local num_clus{
display as text "Number of clusters" as result "`c'"
foreach x of local ICC{
display as text "Intra-class correlation" as result `i'
forvalue i = 1/`nrep'{
display as text "Iterations" as result `nrep'
quietly swcrt `c' `ICC' `intrvcoeff' `u_3' `u_2' `error'
post step (`c') (`ICC') (`r(p)') (`r(p_)')
}
}
}
postclose step
*Open results, calculate power
use `powerresults', clear
levelsof num_clus, local(num_clus)
levelsof ICC, local(ICC)
matrix drop _all
*Loop over combinations of clusters and ICC
*Add power results to matrix
foreach x of local num_clus {
foreach d of local ICC {
quietly ci proportions sig if arm_size == `a' & float(delta) == float(`d') *Fix*
matrix M = nullmat(M) \ (`a', `d', `r(proportion)', `r(lb)', `r(ub)') *Fix*
}
}
*Display the matrix
matrix colnames M = arm_size delta power power_lb power_ub
matrix list M, noheader format(%3.2f)
I was wondering if you can perhaps explain to me what these lines mean:
quietly ci proportions sig if arm_size == `a' & float(delta) == float(`d') *Fix*
matrix M = nullmat(M) \ (`a', `d', `r(proportion)', `r(lb)', `r(ub)') *Fix*
What do they accomplish? How would I know what type of numeric variable (e.g. float) I should specify a variable as?
More importantly, considering that the type 1 error rate (defined as the proportion of simulations in which the p-value for the intervention effect was < 0.05 when the true treatment effect delta = 0), how would I amend the above code to determine that for each combination of num_clus and ICC? How would I do the same to calculate bias?
Your help is much appreciated.
Many thanks,
Jack
I have taken a look at a previous post of yours where you simulated multi-level data and looped over combinations of parameter values as follows:
*Generate multi-level data
capture program drop swcrt
program define swcrt, rclass
version 15.1
preserve
clear
*Stepped-wedge specifications
local num_clus 3 6 9 18 36
local ICC_lvl3
local ICC_lvl2
local clussize 25
*Model specifications
local intercept
local timecoeff
local intcoeff
local sigma_u3
local sigma_u2
local sigma_error
local nrep 100
local alpha 0.05
args num_clus ICC_lvl3 ICC_lvl2 clussize intercept timecoeff intrvcoeff
assert `num_clus' > 0 `ICC_lvl3' > 0 `ICC_lvl2' > 0 `clussize' > 0 `intercept' > 0 `timecoeff' > 0 `intrvcoeff' > 0 `u_3' > 0 `u_2' > 0 `error' > 0
/*Generate simulated multi—level data*/
qui
set seed 12345
clear
set obs `num_clus'
qui gen cluster = _n
//Generate cluster-level errors //
qui gen u_3 = rnormal(0,`sigma_u3')
expand `clussize'
bysort cluster: gen individual = _n
//Generate patient-level errors //
qui gen u_2 = rnormal(0,`sigma_u2')
expand 6
//Set up time//
bysort cluster individual: gen time = _n-1
//Set up intervention variable//
gen intrv = (time>=cluster)
//Generate residual errors
qui gen error = rnormal(0,`sigma_error')
//Generate outcome y
qui gen y = `intercept' + `timecoeff'*time + `intrvcoeff'*intrv + u_3 + u_2 + error
/*Fit multi-level model to simulated dataset*/
mixed y intrv i.time ||cluster: ||individual:, covariance(unstructured) reml dfmethod(kroger)
/*Return estimated effect size, p-value, and significance dichotomy*/
tempname M
matrix `M' = r(table)
return scalar p = `M'r(p)
return scalar p_= `M'(`r(p)' < 0.05)
exit
end swcrt
*Postfile to store results
tempfile powerresults
capture postutil clear
postfile step int num_clus intrv ICC using `results'
*Loop over number of clusters, ICC
foreach x of local num_clus{
display as text "Number of clusters" as result "`c'"
foreach x of local ICC{
display as text "Intra-class correlation" as result `i'
forvalue i = 1/`nrep'{
display as text "Iterations" as result `nrep'
quietly swcrt `c' `ICC' `intrvcoeff' `u_3' `u_2' `error'
post step (`c') (`ICC') (`r(p)') (`r(p_)')
}
}
}
postclose step
*Open results, calculate power
use `powerresults', clear
levelsof num_clus, local(num_clus)
levelsof ICC, local(ICC)
matrix drop _all
*Loop over combinations of clusters and ICC
*Add power results to matrix
foreach x of local num_clus {
foreach d of local ICC {
quietly ci proportions sig if arm_size == `a' & float(delta) == float(`d') *Fix*
matrix M = nullmat(M) \ (`a', `d', `r(proportion)', `r(lb)', `r(ub)') *Fix*
}
}
*Display the matrix
matrix colnames M = arm_size delta power power_lb power_ub
matrix list M, noheader format(%3.2f)
I was wondering if you can perhaps explain to me what these lines mean:
quietly ci proportions sig if arm_size == `a' & float(delta) == float(`d') *Fix*
matrix M = nullmat(M) \ (`a', `d', `r(proportion)', `r(lb)', `r(ub)') *Fix*
What do they accomplish? How would I know what type of numeric variable (e.g. float) I should specify a variable as?
More importantly, considering that the type 1 error rate (defined as the proportion of simulations in which the p-value for the intervention effect was < 0.05 when the true treatment effect delta = 0), how would I amend the above code to determine that for each combination of num_clus and ICC? How would I do the same to calculate bias?
Your help is much appreciated.
Many thanks,
Jack
Comment