Dear Statalist members,
I have Stata code I used during grad school to simulate power for my dissertation project. A simplified version of the code is pasted below. I included a simplified version since my objective is to estimate a different treatment effect for my current project. I tested the simplified version of the code today; it works fine. You should be able to copy/paste the code below into two separate .do files.
The code is divided into two separate .do files (referred to as outer and inner) . The outer .do file runs the inner .do file with the specified arguments. If there's a better/simpler approach, as opposed to using two separate .do files, I would be grateful if you would suggest a more elegant solution.
That said, my primary request for assistance is to convert the power calculation simulation, with a known sample size from a feasibility assessment, to a power calculation across an array of sample sizes from 1,000-10,000 patients in intervals of 1,000 patients. Ultimately, I intend to run 1,000 reps to estimate power at each sample size and ultimately plot the estimated empirical power across the array of sample sizes.
Note: I have not included the variance inflation factor I will be using for the final power calculations - since I have not yet figured out how to establish the VIF or design effect for this study using MSM with IPTW and IPCW. I intend to add this later once the method to run the simulation is established.
Your assistance is greatly appreciated! I hope this post is appropriate and meets community standards. If not, please let me know.
Chris
Outer code .do file "simpowerouter_v2"
clear all
cd "C:\Users\Chris"
pwd
do simpower_v2.do
capture log close
log using simpower_v2.log, replace
set seed 04301971
#delimit ;
set more off;
simul "simpower_v2 10000 -2.5 0.4"
* ba1 is the beta coefficient of interest
ba1=r(ba1)
var_ba1=r(var_ba1)
ep_ba1 = r(ep_ba1)
,
reps(1000);
di "simpower_v2";
summ ep*;
summ ba1;
summ var*;
#delimit cr;
capture log close
exit
Inner code .do file "simpower_v2"
capture program drop _all
program define simpower_v2, rclass
version 16.1
set more off
* the statement below calls on the arguments established in the outer .do file
args ss ycons yba1
drop _all
* gen # observations
set obs `ss'
gen id = _n
* gen person-years
gen pyears=rpoisson(2.2)
replace pyears=pyears+0.1
* gen exposure var - assuming 5% exposed 95% unexposed
capture drop a1
gen a1 = uniform()
replace a1 = 1 if a1<0.05
replace a1=0 if a1>=0.05 & a1!=1
* gen outcome var - assuming cumulative incidence of 10%
capture drop proby
gen proby = exp(`ycons' + `yba1'*a1)/(1 + exp(`ycons' + `yba1'*a1))
capture drop random3
gen random3 = uniform()
capture drop y
gen y = 1 if random3<= proby
replace y = 0 if y==.
drop random3
tab a1 y, row
* st set data and run cox model
stset pyears, id(id) failure(y==1)
stcox a1, nohr
* obtain beta coeficient of exposure parameter
matrix beta = get(_b)
scalar ba1 = beta[1,1]
return scalar ba1 = scalar(ba1)
* obtain variance of exposure parameter
matrix v0=(e(V))
scalar var_ba1=v0[1,1]
return scalar var_ba1 = scalar(var_ba1)
* gen 95% ci lcl and ucl
capture drop lcl_ba1 ucl_ba1
gen lcl_ba1=scalar(ba1) - (1.96*(sqrt(scalar(var_ba1))))
gen ucl_ba1=scalar(ba1) + (1.96*(sqrt(scalar(var_ba1))))
* gen empiric power based on the 95% CI
gen ep_ba1=0 if lcl_ba1<=0
replace ep_ba1=1 if ep_ba1==.
scalar ep_ba1=ep_ba1
return scalar ep_ba1 = scalar(ep_ba1)
end
exit
I have Stata code I used during grad school to simulate power for my dissertation project. A simplified version of the code is pasted below. I included a simplified version since my objective is to estimate a different treatment effect for my current project. I tested the simplified version of the code today; it works fine. You should be able to copy/paste the code below into two separate .do files.
The code is divided into two separate .do files (referred to as outer and inner) . The outer .do file runs the inner .do file with the specified arguments. If there's a better/simpler approach, as opposed to using two separate .do files, I would be grateful if you would suggest a more elegant solution.
That said, my primary request for assistance is to convert the power calculation simulation, with a known sample size from a feasibility assessment, to a power calculation across an array of sample sizes from 1,000-10,000 patients in intervals of 1,000 patients. Ultimately, I intend to run 1,000 reps to estimate power at each sample size and ultimately plot the estimated empirical power across the array of sample sizes.
Note: I have not included the variance inflation factor I will be using for the final power calculations - since I have not yet figured out how to establish the VIF or design effect for this study using MSM with IPTW and IPCW. I intend to add this later once the method to run the simulation is established.
Your assistance is greatly appreciated! I hope this post is appropriate and meets community standards. If not, please let me know.
Chris
Outer code .do file "simpowerouter_v2"
clear all
cd "C:\Users\Chris"
pwd
do simpower_v2.do
capture log close
log using simpower_v2.log, replace
set seed 04301971
#delimit ;
set more off;
simul "simpower_v2 10000 -2.5 0.4"
* ba1 is the beta coefficient of interest
ba1=r(ba1)
var_ba1=r(var_ba1)
ep_ba1 = r(ep_ba1)
,
reps(1000);
di "simpower_v2";
summ ep*;
summ ba1;
summ var*;
#delimit cr;
capture log close
exit
Inner code .do file "simpower_v2"
capture program drop _all
program define simpower_v2, rclass
version 16.1
set more off
* the statement below calls on the arguments established in the outer .do file
args ss ycons yba1
drop _all
* gen # observations
set obs `ss'
gen id = _n
* gen person-years
gen pyears=rpoisson(2.2)
replace pyears=pyears+0.1
* gen exposure var - assuming 5% exposed 95% unexposed
capture drop a1
gen a1 = uniform()
replace a1 = 1 if a1<0.05
replace a1=0 if a1>=0.05 & a1!=1
* gen outcome var - assuming cumulative incidence of 10%
capture drop proby
gen proby = exp(`ycons' + `yba1'*a1)/(1 + exp(`ycons' + `yba1'*a1))
capture drop random3
gen random3 = uniform()
capture drop y
gen y = 1 if random3<= proby
replace y = 0 if y==.
drop random3
tab a1 y, row
* st set data and run cox model
stset pyears, id(id) failure(y==1)
stcox a1, nohr
* obtain beta coeficient of exposure parameter
matrix beta = get(_b)
scalar ba1 = beta[1,1]
return scalar ba1 = scalar(ba1)
* obtain variance of exposure parameter
matrix v0=(e(V))
scalar var_ba1=v0[1,1]
return scalar var_ba1 = scalar(var_ba1)
* gen 95% ci lcl and ucl
capture drop lcl_ba1 ucl_ba1
gen lcl_ba1=scalar(ba1) - (1.96*(sqrt(scalar(var_ba1))))
gen ucl_ba1=scalar(ba1) + (1.96*(sqrt(scalar(var_ba1))))
* gen empiric power based on the 95% CI
gen ep_ba1=0 if lcl_ba1<=0
replace ep_ba1=1 if ep_ba1==.
scalar ep_ba1=ep_ba1
return scalar ep_ba1 = scalar(ep_ba1)
end
exit
Comment