代码如下
clear
set type double
set seed 348871
*Obtain random seeds to start each simulated dataset
forvalues i = 1/1000 {
local S`i' = floor(runiform() * 1000000+1000000*(`i'-1))
}
local sim_number=1
*****************************************************************************
***********
***********Set Up Values
***********
*****************************************************************************
*Change values here to change simulation scenario
*Strength of Effect
*0 = none
*1 = strong
local strength=0
*Effect-Modification by Time-Varying Covariates
*0 = none
*1 = linear
local inter=1
*Long-Term Treatment Effect
*0 = Treatment Effect does not change over time
*1 = Treatment effect decreases over time
local long=0
*Direction of Causal Pathway
*1 = iv -> da
*2 = da -> iv
local path=1
*Censoring
*0=No Censoring
*1=Censoring
local cens=0
*****************************************************************************
***********
***********Scenario Settings
***********
*****************************************************************************
*Code below sets up treatment effects depending on the scenario given above
if `strength'==0 {
local a1=0
local b1=0
local b2=0
local c1=0
local c2=0
local c3=0
local a2=0
local b3=0
local b4=0
local c4=0
local c5=0
local c6=0
local a3=0
local b5=0
local c7=0
local c8=0
}
else if `strength' ==1 & ` inter'==0 & `long'==0 {
local a1=2 //Exac
local b1=0 //Exac Int Exac
local b2=0 //Exac Int IV
local c1=0
local c2=0
local c3=0
local a2=0.8 //IV
local b3=0 //IV Int Exac
local b4=0 //IV Int IV
local c4=0
local c5=0
local c6=0
local a3=4 //FEV
local b5=0 //FEV Int
local c7=0
local c8=0
}
else if `strength' ==1 & ` inter'==0 & `long'==1 {
local a1=2 //Exac
local b1=0 //Exac Int Exac
local b2=0 //Exac Int IV
local c1=0.25
local c2=0
local c3=0
local a2=0.8 //IV
local b3=0 //IV Int Exac
local b4=0 //IV Int IV
local c4=0.1
local c5=0
local c6=0
local a3=4 //FEV
local b5=0 //FEV Int
local c7=0.5
local c8=0
}
else if `strength' ==1 & ` inter'==1 & `long'==0 {
local a1=0.8 //Exac
local b1=0.4 //Exac Int Exac
local b2=0.02 //Exac Int IV
local c1=0
local c2=0
local c3=0
local a2=0.2 //IV
local b3=0.2 //IV Int Exac
local b4=0.02 //IV Int IV
local c4=0
local c5=0
local c6=0
local a3=8 //FEV
local b5=0.08 //FEV Int
local c7=0
local c8=0
}
else if `strength' ==1 & ` inter'==1 & `long'==1 {
local a1=0.8 //Exac
local b1=0.4 //Exac Int Exac
local b2=0.02 //Exac Int IV
local c1=0.1
local c2=0.05
local c3=0.0025
local a2=0.2 //IV
local b3=0.2 //IV Int Exac
local b4=0.02 //IV Int IV
local c4=0.025
local c5=0.025
local c6=0.0025
local a3=8 //FEV
local b5=0.8 //FEV Int
local c7=1
local c8=0.01
}
*****************************************************************************
***********
***********Simulate Data
***********
*****************************************************************************
forv m=1/1000 {
set seed `S`sim_number''
*7500 individuals with 6 visits each
set obs 45000
gen id=ceil(_n/6)
bysort id: gen visit=_n
***************
* FIRST VISIT *
if `cens'==0 {
gen year=2007 if visit==1
}
else {
gen temp=uniform()
gen year=cond(temp<0.4,2007,cond(temp<0.6,2008,cond(temp<0.75,2009, ///
cond(temp<0.85,2010,cond(temp<0.95,2011,2012)))))
drop temp
}
gen Bage=6+84*rbeta(1.1,5) if visit==1 & year==2007
replace Bage=6+84*rbeta(0.65,3.5) if visit==1 & year!=2007
gen censor=0 if visit==1
gen iv_days=min(365,(uniform()<invlogit(-0.6+0.004*Bage))
rpoisson(rgamma(2,0.5*exp(3.4-0.002*Bage)))) if visit==1
gen exac=0 if iv_days==0
replace exac=1 if iv_days>0 & iv_days!=.
gen dnase=0 if visit==1
gen cum_dnase=dnase
gen fev1=max(10,rnormal(95-0.65*Bage-5.8*exac-0.3*iv_days,20)) if visit==1
*********************
* Subsequent Visits *
*Use Age At First Visit for all Visits*
by id: replace Bage=Bage[_n-1] if _n!=1
*Simulate Dornase Alfa & FEV1 Sequentially through time *
qui forv n=2/6 {
replace year=year[_n-1]+1 if visit==`n'
if `path'==1 {
replace iv_days=(uniform()<invlogit(1-`a1'*dnase[_n-1] ///
-`b1'*dnase[_n-1]*exac[_n-1]-`b2'*dnase[_n-1]*iv_days[_n-1] ///
+`c1'*cum_dnase[_n-1] ///
+`c2'*cum_dnase[_n-1]*exac[_n-1] ///
+`c3'*cum_dnase[_n-1]*iv_days[_n-1] ///
-0.025*fev1[_n-1]+exac[_n-1]+0.05*iv_days[_n-1]-0.02*Bage))* ///
rpoisson(rgamma(1/0.3,0.3*exp(3.6-`a2'*dnase[_n-1] ///
-`b3'*dnase[_n-1]*exac[_n-1]-` b4'*dnase[_n-1]*iv_days[_n-1] ///
+`c4'*cum_dnase[_n-1] ///
+`c5'*cum_dnase[_n-1]*exac[_n-1] ///
+`c6'*cum_dnase[_n-1]*iv_days[_n-1] ///
-0.0075*fev1[_n-1]+0.1*exac[_n-1]+0.01*iv_days[_n-1] ///
-0.003*Bage))) if visit==`n'
replace exac=0 if iv_days==0 & visit==`n'
replace exac=1 if iv_days>0 & iv_days!=. & visit==`n'
replace dnase=uniform()<invlogit(-0.4+4*dnase[_n-1] ///
-0.01*fev1[_n-1]+0.7*exac+0.001*iv_days-0.02*Bage) if visit==`n'
replace cum_dnase=dnase+cum_dnase[_n-1] if visit==`n'
}
else if `path'==2 {
replace dnase=uniform()<invlogit(-0.4+4*dnase[_n-1] ///
-0.01*fev1[_n-1]+0.7*exac[_n-1]+0.001*iv_days[_n-1] ///
-0.02*Bage) if visit==`n'
replace cum_dnase=dnase+cum_dnase[_n-1] if visit==`n'
replace iv_days=(uniform()<invlogit(1-`a1'*dnase ///
-`b1'*dnase*exac[_n-1] -`b2'*dnase*iv_days[_n-1] ///
+`c1'*cum_dnase ///
+`c2'*cum_dnase*exac[_n-1] ///
+`c3'*cum_dnase*iv_days[_n-1] ///
-0.025*fev1[_n-1]+exac[_n-1]+0.05*iv_days[_n-1]-0.02*Bage))* ///
rpoisson(rgamma(1/0.3,0.3*exp(3.6-`a2'*dnase ///
-`b3'*dnase*exac[_n-1]-`b4'*dnase*iv_days[_n-1] ///
+`c4'*cum_dnase ///
+`c5'*cum_dnase*exac[_n-1] ///
+`c6'*cum_dnase*iv_days[_n-1] ///
-0.0075*fev1[_n-1]+0.1*exac[_n-1]+0.01*iv_days[_n-1] ///
-0.003*Bage))) if visit==`n'
replace exac=0 if iv_days==0 & visit==`n'
replace exac=1 if iv_days>0 & iv_days!=. & visit==`n'
}
replace fev1=rnormal(10+`a3'*dnase-`b5'*dnase*fev1[_n-1] ///
-`c7'*cum_dnase + `c8'*cum_dnase*fev1[_n-1] ///
+0.9*fev1[_n-1]-0.7*exac-0.06*iv_days-0.08*Bage,10) if visit==`n'
if `cens'==0 {
replace iv_days=365 if iv_days>365 & visit==`n'
replace fev1=10 if fev1<10 & visit==`n'
replace censor=0 if visit==`n'
}
else if `cens'==1 {
replace censor=year>2012|fev1<10|iv_days>365 | ///
uniform()<invlogit(0.02*Bage-0.03*fev1-0.9*exac+0.02*iv_days ///
-0.1*dnase-2) if visit==`n'
replace iv_days=. if visit==`n' & censor==1
replace exac=. if visit==`n' & censor==1
replace dnase=. if visit==`n' & censor==1
replace cum_dnase=. if visit==`n' & censor==1
replace fev1=. if visit==`n' & censor==1
}
}
*Make Lagged Variables
qui makelag dnase fev1 cum_dnase exac iv_days, firstvis(1) visit(visit)
qui makelag dnase fev1 cum_dnase exac iv_days Ldnase Lcum_dnase, firstvis(1) visit(visit)
drop if censor==1
drop censor
gen censor=0
sort id year
bysort id: replace censor=1 if year!=2012 & _n==_N
*Save Simulated Dataset
save "Scenario1/sim`m'", replace
clear
local sim_number=`sim_number'+1
}