//FPM //Load the data from step 03 import delimited "pseudo_dataset.csv", clear sum dep_1_time death_time tab1 dep_1_status death_status gen casage2=case_inv*age_gr2 gen casage3=case_inv*age_gr3 gen casage4=case_inv*age_gr4 //Define the transition matrix mat tmat= (.,1\.,.) //Msset the data msset, transmatrix(tmat) id(id) times(dep_1_time ) states(dep_1_status ) gen _newt=_stop-_start //Place basic variables first order id _from _to _start _stop _status //save "${N}/01_MSM_FPM_pseudodata.dta", replace export delimited using "01_MSM_FPM_pseudodata.csv", replace //CR //Load the data from step 03 import delimited "pseudo_dataset.csv", clear sum dep_1_status dep_1_time sum death_status death_time msset, id(id) times(dep_1_time death_time) states(dep_1_status death_status) //Define Transition matrix mat tmat1 = r(transmatrix) mat li tmat1 gen _newt=_stop-_start gen casage2=case_inv*age_gr2 gen casage3=case_inv*age_gr3 gen casage4=case_inv*age_gr4 //Place basic variables first order id _from _to _start _stop _status //save "${N}/02_MSM_CR_pseudodata.dta", replace export delimited using "02_MSM_CR_pseudodata.csv", replace //MSMsimple //Load the data from step 03 //use "${N}/pseudo_dataset.dta", clear import delimited "pseudo_dataset.csv", clear sum dep_1_status dep_1_time sum death_status death_time gen casage2=case_inv*age_gr2 gen casage3=case_inv*age_gr3 gen casage4=case_inv*age_gr4 mat tmat= (.,1,2\.,.,3\.,.,.) msset, transmatrix(tmat) id(id) times(dep_1_time death_time) states(dep_1_status death_status) gen _newt=_stop-_start //Place basic variables first order id _from _to _start _stop _status //save "${N}/03_MSM_MSMsimple_pseudodata.dta", replace export delimited using "03_MSM_MSMsimple_pseudodata.csv", replace /**********************************************************************************/ /*******************MSM 4 state unidirectional structure make*****************************/ /**********************************************************************************/ //Load the data from step 08a //use "${N}/pseudo_dataset.dta", clear import delimited "pseudo_dataset.csv", clear gen casage2=case_inv*age_gr2 gen casage3=case_inv*age_gr3 gen casage4=case_inv*age_gr4 //MSM PD structure mat tmat4= (.,1,.,2\.,.,3,4\.,.,.,5\.,.,.,.) preserve keep dep_1_time pd_1_time death_time order dep_1_time pd_1_time death_time local x foreach var of varlist _all { local x `x' `var' } restore preserve keep dep_1_status pd_1_status death_status order dep_1_status pd_1_status death_status local y foreach var of varlist _all { local y `y' `var' } restore msset, transmatrix(tmat4) id(id) times("`x'") states("`y'") //msset, transmatrix(tmat4) id(lpnr) times(dep_1th_time postdep_1_time_pd death_time) states(dep_1th_status postdep_1_status_pd death_status) //Place basic variables first order id _trans _from _to _start _stop _status gen _newt=_stop-_start //save "${N}/04_MSM_MSMpd_pseudodata.dta", replace export delimited using "04_MSM_MSMpd_pseudodata.csv", replace /**********************************************************************************/ /*******************MSM bidirectional structure make*****************************/ /**********************************************************************************/ //Load the data from step 08a //use "${N}/pseudo_dataset.dta", clear import delimited "pseudo_dataset.csv", clear global nstates=13 gen casage2=case_inv*age_gr2 gen casage3=case_inv*age_gr3 gen casage4=case_inv*age_gr4 //stset with death as the failure variable and eofdate the time variable //with startdate as the left truncation variable //We will thus create the variables _t, _t0 that will used to create variables _start _stop // MSM bidirectional structure //Create list of status variables for msset preserve keep dep_*_status pd_*_status death_status order dep_1_status pd_1_status dep_2_status pd_2_status dep_3_status pd_3_status dep_4_status pd_4_status /// dep_5_status pd_5_status dep_6_status , last order death_status, last local x foreach var of varlist _all { local x `x' `var' } di "`x'" restore //Create list of status variables for msset preserve keep dep_*_time pd_*_time death_time order dep_1_time pd_1_time dep_2_time pd_2_time dep_3_time pd_3_time dep_4_time pd_4_time /// dep_5_time pd_5_time dep_6_time , last order death_time, last local y foreach var of varlist _all { local y `y' `var' } di "`y'" restore global nstates: word count of `x' di "Number of string values: $nstates" //Automatically derive tmat matrix define tmat=J(${nstates},${nstates},.) mat li tmat global nstatesminus1=$nstates -1 mat tmat[1,2]=1 local p1=1 local p2=2 forval i=1(1)$nstatesminus1 { local iplus1=`i'+1 mat tmat[`i',`iplus1']=`p1' mat tmat[`i',${nstates}]=`p2' local p1=`p1'+2 local p2=`p2'+2 } mat tmat[${nstatesminus1},${nstates} ]=`p1'-2 mat li tmat msset, transmatrix(tmat) id(id) times("`y'") states("`x'") // Rename transitions, _from and _to variables to have the bidirectional structure bysort id (_trans): replace _trans=2 if _from==1 & _to==${nstates} local p1=4 local p2=6 local p3=3 local p4=5 forval i=1(1)$nstates { bysort id (_trans): replace _trans=4 if _trans==`p1' bysort id (_trans): replace _trans=6 if _trans==`p2' bysort id (_trans): replace _trans=3 if _trans==`p3' bysort id (_trans): replace _trans=5 if _trans==`p4' local p1=`p1'+4 local p2=`p2'+4 local p3=`p3'+4 local p4=`p4'+4 } replace _from=2 if _trans==3 replace _to =3 if _trans==3 replace _from=3 if _trans==5 replace _to =2 if _trans==5 replace _from=2 if _trans==4 replace _to =4 if _trans==4 replace _from=3 if _trans==6 replace _to =4 if _trans==6 replace _to=4 if _trans==2 //Place basic variables first order id _from _to _start _stop _status _trans sort id (_start _trans) //replace _start=_start/365.25 //replace _stop=_stop/365.25 gen _newt=_stop-_start //save "${N}/05_MSM_MSMbid_pseudodata.dta", replace export delimited using "05_MSM_MSMbid_pseudodata.csv", replace /**********************************************************************************/ /*******************MSM recurrent structure make*****************************/ /**********************************************************************************/ //Load the data from step 08a //use "${N}/pseudo_dataset.dta", clear import delimited "pseudo_dataset.csv", clear gen casage2=case_inv*age_gr2 gen casage3=case_inv*age_gr3 gen casage4=case_inv*age_gr4 //stset with death as the failure variable and eofdate the time variable //with startdate as the left truncation variable //We will thus create the variables _t, _t0 that will used to create variables _start _stop global nstates=13 //MSM rec structure preserve mat tmat= (.,1,.,.,.,.,.,.,.,.,.,.,2 \ /// .,.,3,.,.,.,.,.,.,.,.,.,4 \ /// .,.,.,5,.,.,.,.,.,.,.,.,6 \ /// .,.,.,.,7,.,.,.,.,.,.,.,8 \ /// .,.,.,.,.,9,.,.,.,.,.,.,10 \ /// .,.,.,.,.,.,11,.,.,.,.,.,12 \ /// .,.,.,.,.,.,.,13,.,.,.,.,14 \ /// .,.,.,.,.,.,.,.,15,.,.,.,16 \ /// .,.,.,.,.,.,.,.,.,17,.,.,18 \ /// .,.,.,.,.,.,.,.,.,.,19,.,20 \ /// .,.,.,.,.,.,.,.,.,.,.,21,22 \ /// .,.,.,.,.,.,.,.,.,.,.,.,23 \ /// .,.,.,.,.,.,.,.,.,.,.,.,.) msset, transmatrix(tmat) id(id) /// times(dep_1_time pd_1_time dep_2_time pd_2_time dep_3_time pd_3_time dep_4_time /// pd_4_time dep_5_time pd_5_time dep_6_time death_time) /// states(dep_1_status pd_1_status dep_2_status pd_2_status dep_3_status pd_3_status /// dep_4_status pd_4_status dep_5_status pd_5_status dep_6_status death_status) //Place basic variables first order id _from _to _start _stop _status gen _newt=_stop-_start forval i=1(1)23 { qui count if _trans==`i' di "Transition `i' is populated by `r(N)' individuals" qui count if _trans==`i' & _status==1 di "In transition `i', `r(N)' individuals experience the event" } //save "${N}/06_MSM_MSMrec_pseudodata.dta", replace export delimited using "06_MSM_MSMrec_pseudodata.csv", replace /**********************************************************************************/ /*******************MSM recurrent structure make*****************************/ /**********************************************************************************/ //Load the data from step 08a //use "${N}/pseudo_dataset.dta", clear import delimited "pseudo_dataset.csv", clear gen casage2=case_inv*age_gr2 gen casage3=case_inv*age_gr3 gen casage4=case_inv*age_gr4 //stset with death as the failure variable and eofdate the time variable //with startdate as the left truncation variable //We will thus create the variables _t, _t0 that will used to create variables _start _stop global nstates=13 //MSM rec structure preserve mat tmat= (.,1,.,.,.,.,.,.,.,.,.,.,2 \ /// .,.,3,.,.,.,.,.,.,.,.,.,4 \ /// .,.,.,5,.,.,.,.,.,.,.,.,6 \ /// .,.,.,.,7,.,.,.,.,.,.,.,8 \ /// .,.,.,.,.,9,.,.,.,.,.,.,10 \ /// .,.,.,.,.,.,11,.,.,.,.,.,12 \ /// .,.,.,.,.,.,.,13,.,.,.,.,14 \ /// .,.,.,.,.,.,.,.,15,.,.,.,16 \ /// .,.,.,.,.,.,.,.,.,17,.,.,18 \ /// .,.,.,.,.,.,.,.,.,.,19,.,20 \ /// .,.,.,.,.,.,.,.,.,.,.,21,22 \ /// .,.,.,.,.,.,.,.,.,.,.,.,23 \ /// .,.,.,.,.,.,.,.,.,.,.,.,.) msset, transmatrix(tmat) id(id) /// times(dep_1_time pd_1_time dep_2_time pd_2_time dep_3_time pd_3_time dep_4_time /// pd_4_time dep_5_time pd_5_time dep_6_time death_time) /// states(dep_1_status pd_1_status dep_2_status pd_2_status dep_3_status pd_3_status /// dep_4_status pd_4_status dep_5_status pd_5_status dep_6_status death_status) //Place basic variables first order id _from _to _start _stop _status gen _newt=_stop-_start forval i=1(1)23 { qui count if _trans==`i' di "Transition `i' is populated by `r(N)' individuals" qui count if _trans==`i' & _status==1 di "In transition `i', `r(N)' individuals experience the event" } //save "${N}/07_MSM_MSMbidemul_pseudodata.dta", replace export delimited using "07_MSM_MSMbidemul_pseudodata.csv", replace