
# TITLE: "Replication of the "Reference Model", described in the manuscript submitted to the European Journal of Health Economics in November 2014: 
# "Economic evaluation in chronic pain: A systematic review and de novo flexible economic model"

# WHEN VIEWED IN RStudio
# TO VIEW THE CODE IN COLLAPSED SECTIONS:
# Edit>Folding>Collapse All
# Click on margin arrows to show section headings and contained code
#Running order:

#Description
#Controls
#Source data
#Model parameters
#Input matrices for model
#Patient flow calculations
#Summary results

############
#DESCRIPTION
############

#This model is the 'Reference Model' submitted as an appendix to the manuscript "Economic evaluation in chronic pain: A systematic review and de novo flexible economic model"
#submitted to the European Journal of Health Economics in January 2015 

#The model compares two treatments for chronic musculoskeletal pain: (i) oral morphine (though the model has the flexibility to select oxycodone data at 
#first-line instead) (Treatment 1 / Tx1) and (ii) a hypothetical novel formulation (Treatment 2 /Tx2).  The model structure is identical across treatment arms.

#The model has two absorbing states to capture outcomes for those who have discontinued treatment or moved on to subsequent treatment following two failed 
#attempts at therapy: 'treatment discontinuation' and 'subsequent treatment'.  The other states in the model are states to capture outcomes while on treatment, 
#or transitory states to capture temporary outcomes following withdrawal from treatment.

#Patients enter the model and begin treatment. While on treatment, patients may experience treatment-emergent tolerable adverse events (AEs) (constipation and 
#nausea/vomiting); the simulated cohort can therefore be separated into two AE-related Markov states within 1st-line treatment. HRQL consequences of differences 
#in analgesic effects across treatments are captured across treatment arms.

#Patients may withdraw from 1st-line treatment and move to either 'withdrawal, AEs' or 'withdrawal, other (including pain)' for one model cycle to accrue the 
#temporary dip in HRQL and cost outcomes which patients experience immediately following treatment failure.  These patients then either:
# . forgo further therapy and move to the absorbing state 'treatment discontinuation, or 
# . attempt 2nd-line therapy.  
#The Markov states for 2nd-line therapy are identical to those for 1st-line therapy.  Patients may withdraw from 2nd-line therapy for identical reasons as for 
#1st-line therapy.  Outcomes following withdrawal from 2nd-line therapy are assumed to be captured by the two absorbing states in the model.

#Different structural and data assumptions can be tested by adjusting settings in #CONTROLs
#The scenarios tested in the manuscript are detailed within #CONTROLS

#Treatment 1 - Comparator
#Treatment 2 - Novel therapy

#########
#CONTROLS
#########

#MODEL SETTINGS - the user can adjust these to explore different parameter and structural assumptions
#Base case settings stored below
cont.standarderror<-0.1
cont.utilitypercimp_NovelTherap<-0
cont.costmultiplier_noveltherapy<-6
cont.prob.discont.after.failed.1L<-0.05
cont.prob.discont.after.failed.2L<-0.1
cont.PatPathway<-"Discontinuation"
cont.titration<-"no adjustment for first 4 weeks"
cont.Tx1Arm_1LTxoptions<-"Morphine"
cont.Tx1Arm_2LTxoptions<-"Oxycodone"
cont.Tx2Arm_2LTxoptions<-"Oxycodone"
cont.withd_mult_init<-2
cont.doseadj_init<-0.65
cont.prop_improv_AE_noveltherapy<-0.3
cont.prop_disc_GP<-0.5
cont.prop_naus_GP<-0.2
cont.umult_failed1L<-0.9
cont.umult_failed2L<-0.8
#cycle length 1 week
cont.CycleProp<-7/365.25
cont.TimeHorizon<-52


# MODEL SETTINGS - BASE CASE and ##RESTRICTIONS

# cont.standarderror=0.1                              ##input a value between 0 and 0.5, inclusive
# cont.utilitypercimp_NovelTherap<-0                  ##input a value between 0 and 0.2, inclusive - scenario 0.05
# cont.costmultiplier_noveltherapy<-4                 ##input a positive number
# cont.prob.discont.after.failed.1L<-0.05             ##input a value between 0 and 1, inclusive
# cont.prob.discont.after.failed.2L<-0.1              ##input a value between 0 and 1, inclusive
# cont.PatPathway<-"Discontinuation"                  ##"Discontinuation" or "Some enter subsequent treatment"
# cont.titration<-"no adjustment for first 4 weeks"   ##"no adjustment for first 4 weeks"  or "User-defined dose and withdrawal rate adjustment for first 4 weeks"
# cont.Tx1Arm_1LTxoptions<-"Morphine"                 ##"Morphine" or "Oxycodone"
# cont.Tx1Arm_2LTxoptions<-"Oxycodone"                ##"Oxycodone" or "Morphine"
# cont.Tx2Arm_2LTxoptions<-"Oxycodone"                ##"Oxycodone" or "Morphine"
# cont.withd_mult_init<-2                             ##input a positive value
# cont.doseadj_init<-0.65                             ##input a value between 0 and 1, inclusive
# cont.prop_improv_AE_noveltherapy<-0.3               ##input a positive value
# cont.prop_disc_GP<-0.5                              ##input a value between 0 and 1, inclusive
# cont.prop_naus_GP<-0.2                              ##input a value between 0 and 1, inclusive
# cont.umult_failed1L<-0.9                            ##input a value between 0 and 1, inclusive
# cont.umult_failed2L<-0.8                            ##input a value between 0 and 1, inclusive
# cont.TimeHorizon<-52                                ##input a time horizon up to 104 (weeks)

# SCENARIOS

# Scenario 1 - Base case settings
# Scenario 2 - Base case settings, but cont.PatPathway<-"Some enter subsequent treatment"
# Scenario 3 - Base case settings, but cont.Tx2Arm_2LTxoptions<-"Morphine"
# Scenario 4 - Base case settings, but cont.titration<-"User-defined dose and withdrawal rate adjustment for first 4 weeks"
# Scenario 5 - Base case settings, but cont.utilitypercimp_NovelTherap<-0.05
# Scenario 6 - Base case settings, but cont.TimeHorizon<-104
# Scenario 7 - Base case settings, but cont.umult_failed1L<-1 and cont.umult_failed2L<-1

############
#SOURCE DATA
############

#Cost source data
#Much of source cost data are 2009/10 data - Curtis et al Inflation Factor update to 2012/2013 cost year
PriceIndex0910<-268.6
PriceIndex1213<-289.1
InfFactor<-PriceIndex1213/PriceIndex0910
#Morphine cost from BNF 67, dose from NICE 2012 study
c_cycle_Tx_morphine<-2.632729166666670000
#Morphine comed costs, withdrawal and constipation AE costs from NICE 2012 study
c_cycle_comed_morphine<-2.1*InfFactor
c_withdrawal<-99.33*InfFactor
c_constipation<-4.68*InfFactor
#Oxycodone cost from BNF 67, dose from Dunlop et al 2012 study
c_cycle_Tx_oxycodone<-9.2011500000
#Oxycodone comed costs from Dunlop et al 2012 study
c_cycle_comed_oxycodone<-0.04*InfFactor
#GP visit cost from PSSRU 2013 (Applied to a proportion of nausea AE patients) 
c_GP<-37

#Utility source data
#Utility data from Ikenberg et al 2012
u_OnTx<-0.695
u_disc_AE<-0.503
u_disc_efficacy<-0.405
u_OnTx_AE<-0.583

#Cycle (7 day) probability source data
#Morphine data from Grenier et al 2006 28-day probability data - assume 7 day nausea/vomitting duration and continuous constipation duration
#nausea/vomiting
M28DNVprob<-0.387
McontinNVprob<-(-log(1-M28DNVprob))/28
Mcycle_prob_nausea<-1-exp(-McontinNVprob*7)
#constipation
Mcycle_prob_const<-0.321
#Tolerable AE cycle prob
cycle_prob_morphine_tolerableAE<-Mcycle_prob_nausea+Mcycle_prob_const
#withdrawal due to AEs
M28DWITHDAEprob<-0.205
McontinWITHDAEprob<-(-log(1-M28DWITHDAEprob))/28
cycle_prob_morphine_withd_AEs<-1-exp(-McontinWITHDAEprob*7)
#withdrawal due to other reason
M28DWITHDOTHERprob<-0.050
McontinWITHDOTHERprob<-(-log(1-M28DWITHDOTHERprob))/28
cycle_prob_morphine_withd_other<-1-exp(-McontinWITHDOTHERprob*7)

#Oxycodone data from Ikenberg et al 2012 105-day probability data - did not report tolerable AEs by each AE, assume continuous AE duration
#tolerable AEs
cycle_prob_oxy_tolerableAE<-0.4635
#withdrawal due to AEs
O105DWITHDAEprob<-0.3936
OcontinWITHDAEprob<-(-log(1-O105DWITHDAEprob))/105
cycle_prob_oxy_withdrawAE<-1-exp(-OcontinWITHDAEprob*7)
#withdrawal due to other reason
O105DWITHDOTHERprob<-0.034
OcontinWITHDOTHERprob<-(-log(1-O105DWITHDOTHERprob))/105
cycle_prob_oxy_withdrawother<-1-exp(-OcontinWITHDOTHERprob*7)

#################
#MODEL PARAMETERS
#################
#costs
c_cycle_tx_tx1<-ifelse(cont.Tx1Arm_1LTxoptions=="Morphine",c_cycle_Tx_morphine,c_cycle_Tx_oxycodone)
c_cycle_tx_tx2<-c_cycle_Tx_oxycodone*cont.costmultiplier_noveltherapy
c_cycle_comed_tx1<-ifelse(cont.Tx1Arm_1LTxoptions=="Morphine",c_cycle_comed_morphine,c_cycle_comed_oxycodone)
c_cycle_comed_tx2<-c_cycle_comed_oxycodone*(1-cont.prop_improv_AE_noveltherapy)
c_cycle_ae<-(Mcycle_prob_nausea/cycle_prob_morphine_tolerableAE)*cont.prop_naus_GP*c_GP+c_constipation
c_cycle_withd_AE<-c_withdrawal
c_cycle_withd_other<-c_withdrawal
c_cycle_2Ltx_tx1arm<-ifelse(cont.Tx1Arm_2LTxoptions=="Morphine",c_cycle_Tx_morphine+c_cycle_comed_morphine,c_cycle_Tx_oxycodone+c_cycle_comed_oxycodone)
c_cycle_2Ltx_tx2arm<-ifelse(cont.Tx2Arm_2LTxoptions=="Morphine",c_cycle_Tx_morphine+c_cycle_comed_morphine,c_cycle_Tx_oxycodone+c_cycle_comed_oxycodone)
c_cycle_3Ltx_tx1modelarm<-c_cycle_Tx_morphine+c_cycle_comed_morphine
c_cycle_3Ltx_tx2modelarm<-c_cycle_Tx_morphine+c_cycle_comed_morphine
c_cycle_discontinued<-c_GP*cont.prop_disc_GP
c_cycle_tx_tx1_init<-c_cycle_tx_tx1*cont.doseadj_init
c_cycle_tx_tx2_init<-c_cycle_tx_tx1*cont.costmultiplier_noveltherapy*cont.doseadj_init
#utilities
u_on1LTx_Tx1_noAE<-u_OnTx
u_on1LTx_Tx1_AE<-u_OnTx_AE
u_on1LTx_Tx2_noAE<-u_OnTx*(1+cont.utilitypercimp_NovelTherap)
u_on1LTx_Tx2_AE<-u_OnTx_AE*(1+cont.utilitypercimp_NovelTherap)
u_withdrawn1L_AE<-u_disc_AE
u_withdrawn1L_other<-u_disc_efficacy
u_on2LTx_noAE<-u_OnTx*cont.umult_failed1L
u_on2LTx_AE<-u_OnTx_AE*cont.umult_failed1L
u_withdrawn2L_AE<-u_disc_AE*cont.umult_failed1L
u_withdrawn2L_other<-u_disc_efficacy*cont.umult_failed1L
u_3L<-((u_OnTx+u_OnTx_AE+u_disc_AE+u_disc_efficacy)/4)*cont.umult_failed2L
u_discontinuation<-u_disc_efficacy*cont.umult_failed2L
#cycle probabilities
prob_AE_Tx1arm_1L<-ifelse(cont.Tx1Arm_1LTxoptions=="Morphine",cycle_prob_morphine_tolerableAE,cycle_prob_oxy_tolerableAE)
prob_AE_Tx2arm_1L<-cycle_prob_oxy_tolerableAE*(1-cont.prop_improv_AE_noveltherapy)
prob_withdAE_Tx1arm_1L<-ifelse(cont.Tx1Arm_1LTxoptions=="Morphine",cycle_prob_morphine_withd_AEs,cycle_prob_oxy_withdrawAE)
prob_withdAE_Tx2arm_1L<-cycle_prob_oxy_withdrawAE*(1-cont.prop_improv_AE_noveltherapy)
prob_withdother_Tx1arm_1L<-ifelse(cont.Tx1Arm_1LTxoptions=="Morphine",cycle_prob_morphine_withd_other,cycle_prob_oxy_withdrawother)
prob_withdother_Tx2arm_1L<-cycle_prob_oxy_withdrawother*(1-cont.prop_improv_AE_noveltherapy)
prob_AE_Tx1arm_2L<-ifelse(cont.Tx1Arm_2LTxoptions=="Morphine",cycle_prob_morphine_tolerableAE,cycle_prob_oxy_tolerableAE)
prob_AE_Tx2arm_2L<-ifelse(cont.Tx2Arm_2LTxoptions=="Morphine",cycle_prob_morphine_tolerableAE,cycle_prob_oxy_tolerableAE)
prob_withdAE_Tx1arm_2L<-ifelse(cont.Tx1Arm_2LTxoptions=="Morphine",cycle_prob_morphine_withd_AEs,cycle_prob_oxy_withdrawAE)
prob_withdAE_Tx2arm_2L<-ifelse(cont.Tx2Arm_2LTxoptions=="Morphine",cycle_prob_morphine_withd_AEs,cycle_prob_oxy_withdrawAE)
prob_withdother_Tx1arm_2L<-ifelse(cont.Tx1Arm_2LTxoptions=="Morphine",cycle_prob_morphine_withd_other,cycle_prob_oxy_withdrawother)
prob_withdother_Tx2arm_2L<-ifelse(cont.Tx2Arm_2LTxoptions=="Morphine",cycle_prob_morphine_withd_other,cycle_prob_oxy_withdrawother)
prob_discontafter1L<-cont.prob.discont.after.failed.1L
prob_discontafter2L<-cont.prob.discont.after.failed.2L
prob_withdAE_Tx1arm_1L_init<-prob_withdAE_Tx1arm_1L*cont.withd_mult_init
prob_withdAE_Tx2arm_1L_init<-prob_withdAE_Tx2arm_1L*cont.withd_mult_init
prob_withdother_Tx1arm_1L_init<-prob_withdother_Tx1arm_1L*cont.withd_mult_init
prob_withdother_Tx2arm_1L_init<-prob_withdother_Tx2arm_1L*cont.withd_mult_init

######################## 
#INPUT MATRICES FOR MODEL
########################
#INITIAL VECTOR
InitVector<-c(1,0,0,0,0,
              0,0,0,0,0)

#UTILITY VECTORS
UtilityVector_Comp<-c(u_on1LTx_Tx1_noAE,u_on1LTx_Tx1_AE,
                      u_withdrawn1L_AE,u_withdrawn1L_other,
                      u_on2LTx_noAE,u_on2LTx_AE,
                      u_withdrawn2L_AE,u_withdrawn2L_other,
                      u_3L,u_discontinuation)*cont.CycleProp
UtilityVector_Novel<-UtilityVector_Comp
UtilityVector_Novel[1]<-u_on1LTx_Tx2_noAE*cont.CycleProp
UtilityVector_Novel[2]<-u_on1LTx_Tx2_AE*cont.CycleProp

#set Cost Matrix and Trace/Trans Matrices length to 105 - maximum time horizon tested+1
costANDtracematrices_nrows<-105

#COST MATRICES
CostMatrix_Comp<-matrix(nrow=costANDtracematrices_nrows,ncol=10)
CostMatrix_Comp[,1]<-c_cycle_tx_tx1+c_cycle_comed_tx1
CostMatrix_Comp[,2]<-c_cycle_tx_tx1+c_cycle_comed_tx1+c_cycle_ae
CostMatrix_Comp[,3]<-c_cycle_withd_AE
CostMatrix_Comp[,4]<-c_cycle_withd_other
CostMatrix_Comp[,5]<-c_cycle_2Ltx_tx1arm
CostMatrix_Comp[,6]<-c_cycle_2Ltx_tx1arm+c_cycle_ae
CostMatrix_Comp[,7]<-c_cycle_withd_AE
CostMatrix_Comp[,8]<-c_cycle_withd_other
CostMatrix_Comp[,9]<-c_cycle_3Ltx_tx1modelarm
CostMatrix_Comp[,10]<-c_cycle_discontinued

if (cont.titration=="no adjustment for first 4 weeks"){
  print('no titration adjustment...')
} else {
  for (i in 2:5){
    CostMatrix_Comp[i,1]<-c_cycle_tx_tx1_init+c_cycle_comed_tx1
    CostMatrix_Comp[i,2]<-c_cycle_tx_tx1_init+c_cycle_comed_tx1+c_cycle_ae
  }}
#NovelTherapy arm
CostMatrix_Novel<-CostMatrix_Comp
CostMatrix_Novel[,1]<-c_cycle_tx_tx2+c_cycle_comed_tx2
CostMatrix_Novel[,2]<-c_cycle_tx_tx2+c_cycle_comed_tx2+c_cycle_ae
CostMatrix_Novel[,5]<-c_cycle_2Ltx_tx2arm
CostMatrix_Novel[,6]<-c_cycle_2Ltx_tx2arm+c_cycle_ae

if (cont.titration=="no adjustment for first 4 weeks"){
  print('no titration adjustment...')
} else {
  for (i in 2:5){
    CostMatrix_Novel[i,1]<-c_cycle_tx_tx2_init+c_cycle_comed_tx2
    CostMatrix_Novel[i,2]<-c_cycle_tx_tx2_init+c_cycle_comed_tx2+c_cycle_ae
  }}

#TRANSITION ARRAYS - 1= transitions from 1LTx NO AEs, etc
#Note - 3D arrays are required rather than 2D matrices as different transition matrices are required for early cycles if titration is considered

TransVec1_Comp<-c((1-prob_withdAE_Tx1arm_1L-prob_withdother_Tx1arm_1L)*(1-prob_AE_Tx1arm_1L),
                  (1-prob_withdAE_Tx1arm_1L-prob_withdother_Tx1arm_1L)*(prob_AE_Tx1arm_1L),
                  prob_withdAE_Tx1arm_1L,
                  prob_withdother_Tx1arm_1L,
                  0,
                  0,0,0,0,0
)
TransVec2_Comp<-c((1-prob_withdAE_Tx1arm_1L-prob_withdother_Tx1arm_1L)*(1-prob_AE_Tx1arm_1L),
                  (1-prob_withdAE_Tx1arm_1L-prob_withdother_Tx1arm_1L)*(prob_AE_Tx1arm_1L),
                  prob_withdAE_Tx1arm_1L,
                  prob_withdother_Tx1arm_1L,
                  0,
                  0,0,0,0,0
)
TransVec3_Comp<-c(0,0,0,0,
                  (1-prob_discontafter1L)*(1-prob_AE_Tx1arm_2L),
                  (1-prob_discontafter1L)*prob_AE_Tx1arm_2L,
                  0,0,0,
                  prob_discontafter1L
)
TransVec4_Comp<-c(0,0,0,0,
                  (1-prob_discontafter1L)*(1-prob_AE_Tx1arm_2L),
                  (1-prob_discontafter1L)*prob_AE_Tx1arm_2L,
                  0,0,0,
                  prob_discontafter1L
)
TransVec5_Comp<-c(0,0,0,0,
                  (1-prob_withdAE_Tx1arm_2L-prob_withdother_Tx1arm_2L)*(1-prob_AE_Tx1arm_2L),
                  (1-prob_withdAE_Tx1arm_2L-prob_withdother_Tx1arm_2L)*(prob_AE_Tx1arm_2L),
                  prob_withdAE_Tx1arm_2L,
                  prob_withdother_Tx1arm_2L,
                  0,0
)
TransVec6_Comp<-c(0,0,0,0,
                  (1-prob_withdAE_Tx1arm_2L-prob_withdother_Tx1arm_2L)*(1-prob_AE_Tx1arm_2L),
                  (1-prob_withdAE_Tx1arm_2L-prob_withdother_Tx1arm_2L)*(prob_AE_Tx1arm_2L),
                  prob_withdAE_Tx1arm_2L,
                  prob_withdother_Tx1arm_2L,
                  0,0
)
TransVec7_Comp<-c(0,0,0,0,0,
                  0,0,0,
                  ifelse(cont.PatPathway=="Discontinuation",0,1-prob_discontafter2L),
                  ifelse(cont.PatPathway=="Discontinuation",1,prob_discontafter2L)
)
TransVec8_Comp<-c(0,0,0,0,0,
                  0,0,0,
                  ifelse(cont.PatPathway=="Discontinuation",0,1-prob_discontafter2L),
                  ifelse(cont.PatPathway=="Discontinuation",1,prob_discontafter2L)
)
TransVec9_Comp<-c(0,0,0,0,0,
                  0,0,0,1,0)
TransVec10_Comp<-c(0,0,0,0,0,
                   0,0,0,0,1)                 

TransMat_Comp<-rbind(TransVec1_Comp,TransVec2_Comp,TransVec3_Comp,TransVec4_Comp,TransVec5_Comp,
                     TransVec6_Comp,TransVec7_Comp,TransVec8_Comp,TransVec9_Comp,TransVec10_Comp)
colnames(TransMat_Comp)<-c("NoAEs_1L","AEs_1L","WithdAEs_1L","WithdOther_1L","NoAEs_2L","AEs_2L","WithdAEs_2L","WithdOther_1L","3rdL","Discontinuation")
rownames(TransMat_Comp)<-colnames(TransMat_Comp)
rowSums(TransMat_Comp)

#Novel Therapy arm
TransVec1_Novel<-c((1-prob_withdAE_Tx2arm_1L-prob_withdother_Tx2arm_1L)*(1-prob_AE_Tx2arm_1L),
                   (1-prob_withdAE_Tx2arm_1L-prob_withdother_Tx2arm_1L)*(prob_AE_Tx2arm_1L),
                   prob_withdAE_Tx2arm_1L,
                   prob_withdother_Tx2arm_1L,
                   0,
                   0,0,0,0,0
)
TransVec2_Novel<-c((1-prob_withdAE_Tx2arm_1L-prob_withdother_Tx2arm_1L)*(1-prob_AE_Tx2arm_1L),
                   (1-prob_withdAE_Tx2arm_1L-prob_withdother_Tx2arm_1L)*(prob_AE_Tx2arm_1L),
                   prob_withdAE_Tx2arm_1L,
                   prob_withdother_Tx2arm_1L,
                   0,
                   0,0,0,0,0
)
TransVec3_Novel<-c(0,0,0,0,
                   (1-prob_discontafter1L)*(1-prob_AE_Tx2arm_2L),
                   (1-prob_discontafter1L)*prob_AE_Tx2arm_2L,
                   0,0,0,
                   prob_discontafter1L
)
TransVec4_Novel<-c(0,0,0,0,
                   (1-prob_discontafter1L)*(1-prob_AE_Tx2arm_2L),
                   (1-prob_discontafter1L)*prob_AE_Tx2arm_2L,
                   0,0,0,
                   prob_discontafter1L
)
TransVec5_Novel<-c(0,0,0,0,
                   (1-prob_withdAE_Tx2arm_2L-prob_withdother_Tx2arm_2L)*(1-prob_AE_Tx2arm_2L),
                   (1-prob_withdAE_Tx2arm_2L-prob_withdother_Tx2arm_2L)*(prob_AE_Tx2arm_2L),
                   prob_withdAE_Tx2arm_2L,
                   prob_withdother_Tx2arm_2L,
                   0,0
)
TransVec6_Novel<-c(0,0,0,0,
                   (1-prob_withdAE_Tx2arm_2L-prob_withdother_Tx2arm_2L)*(1-prob_AE_Tx2arm_2L),
                   (1-prob_withdAE_Tx2arm_2L-prob_withdother_Tx2arm_2L)*(prob_AE_Tx2arm_2L),
                   prob_withdAE_Tx2arm_2L,
                   prob_withdother_Tx2arm_2L,
                   0,0
)
TransVec7_Novel<-c(0,0,0,0,0,
                   0,0,0,
                   ifelse(cont.PatPathway=="Discontinuation",0,1-prob_discontafter2L),
                   ifelse(cont.PatPathway=="Discontinuation",1,prob_discontafter2L)
)
TransVec8_Novel<-c(0,0,0,0,0,
                   0,0,0,
                   ifelse(cont.PatPathway=="Discontinuation",0,1-prob_discontafter2L),
                   ifelse(cont.PatPathway=="Discontinuation",1,prob_discontafter2L)
)
TransVec9_Novel<-c(0,0,0,0,0,
                   0,0,0,1,0)
TransVec10_Novel<-c(0,0,0,0,0,
                    0,0,0,0,1)     

TransMat_Novel<-rbind(TransVec1_Novel,TransVec2_Novel,TransVec3_Novel,TransVec4_Novel,TransVec5_Novel,
                      TransVec6_Novel,TransVec7_Novel,TransVec8_Novel,TransVec9_Novel,TransVec10_Novel)
rownames(TransMat_Novel)<-rownames(TransMat_Comp)
colnames(TransMat_Novel)<-colnames(TransMat_Comp)
rowSums(TransMat_Novel)

####
#Generate alternate transition matrix if titration scenario being considered
####
if (cont.titration=="no adjustment for first 4 weeks"){
  print('no titration adjustment...')
} else {
  #Comp arm
  InitTransVec1_Comp<-c((1-prob_withdAE_Tx1arm_1L_init-prob_withdother_Tx1arm_1L_init)*(1-prob_AE_Tx1arm_1L),
                        (1-prob_withdAE_Tx1arm_1L_init-prob_withdother_Tx1arm_1L_init)*(prob_AE_Tx1arm_1L),
                        prob_withdAE_Tx1arm_1L_init,
                        prob_withdother_Tx1arm_1L_init,
                        0,
                        0,0,0,0,0
  )
  InitTransVec2_Comp<-c((1-prob_withdAE_Tx1arm_1L_init-prob_withdother_Tx1arm_1L_init)*(1-prob_AE_Tx1arm_1L),
                        (1-prob_withdAE_Tx1arm_1L_init-prob_withdother_Tx1arm_1L_init)*(prob_AE_Tx1arm_1L),
                        prob_withdAE_Tx1arm_1L_init,
                        prob_withdother_Tx1arm_1L_init,
                        0,
                        0,0,0,0,0
  )
  
  InitTransMat_Comp<-rbind(InitTransVec1_Comp,InitTransVec2_Comp,TransVec3_Comp,TransVec4_Comp,TransVec5_Comp,
                           TransVec6_Comp,TransVec7_Comp,TransVec8_Comp,TransVec9_Comp,TransVec10_Comp)
  rownames(InitTransMat_Comp)<-rownames(TransMat_Comp)
  colnames(InitTransMat_Comp)<-colnames(TransMat_Comp)
  rowSums(InitTransMat_Comp)
  #Novel arm
  InitTransVec1_Novel<-c((1-prob_withdAE_Tx2arm_1L_init-prob_withdother_Tx2arm_1L_init)*(1-prob_AE_Tx2arm_1L),
                         (1-prob_withdAE_Tx2arm_1L_init-prob_withdother_Tx2arm_1L_init)*(prob_AE_Tx2arm_1L),
                         prob_withdAE_Tx2arm_1L_init,
                         prob_withdother_Tx2arm_1L_init,
                         0,
                         0,0,0,0,0
  )
  InitTransVec2_Novel<-c((1-prob_withdAE_Tx2arm_1L_init-prob_withdother_Tx2arm_1L_init)*(1-prob_AE_Tx2arm_1L),
                         (1-prob_withdAE_Tx2arm_1L_init-prob_withdother_Tx2arm_1L_init)*(prob_AE_Tx2arm_1L),
                         prob_withdAE_Tx2arm_1L_init,
                         prob_withdother_Tx2arm_1L_init,
                         0,
                         0,0,0,0,0
  )
  InitTransMat_Novel<-rbind(InitTransVec1_Novel,InitTransVec2_Novel,TransVec3_Novel,TransVec4_Novel,TransVec5_Novel,
                            TransVec6_Novel,TransVec7_Novel,TransVec8_Novel,TransVec9_Novel,TransVec10_Novel)
  rownames(InitTransMat_Novel)<-rownames(TransMat_Novel)
  colnames(InitTransMat_Novel)<-colnames(TransMat_Novel)
  rowSums(InitTransMat_Novel)  
}

####

#create arrays of matrices to allow time-varying transition probabilities
TransArrayComp <- array(data=NA,dim=c(10,10,costANDtracematrices_nrows)) #dim=c(nrow,ncol,nmatrix)
TransArrayNovel <- array(data=NA,dim=c(10,10,costANDtracematrices_nrows)) #dim=c(nrow,ncol,nmatrix)
for (i in 1:costANDtracematrices_nrows) {
  TransArrayComp[,,i] <-TransMat_Comp
  TransArrayNovel[,,i]<-TransMat_Novel
}
#adapt array if titration is under consideration
###
if (cont.titration=="no adjustment for first 4 weeks"){
  print('no titration adjustment...')
} else {
  for (i in 1:4) {
    TransArrayComp[,,i] <-InitTransMat_Comp
    TransArrayNovel[,,i]<-InitTransMat_Novel
  }
}
###

#check
rowSums(TransArrayComp[,,4])
rowSums(TransArrayComp[,,8])
rowSums(TransArrayNovel[,,4])
rowSums(TransArrayNovel[,,8])

##########################
#PATIENT FLOW CALCULATIONS
####################
#remember, costANDtracematrices_nrows<-105

#COMP
trace_matrix_Comp <- matrix(NA, nrow=costANDtracematrices_nrows, ncol=ncol(TransArrayComp))  
trace_matrix_Comp[1,] <- InitVector
trace_matrix_Comp[2,] <- InitVector %*% TransArrayComp[,,1] 
for (i in 3:nrow(trace_matrix_Comp)){
  trace_matrix_Comp[i,] <- trace_matrix_Comp[i-1,] %*% TransArrayComp[,,i-1]
}
#hcc = half cycle corrected
hcc_trace_matrix_Comp <- matrix(NA, nrow=costANDtracematrices_nrows, ncol=ncol(TransArrayComp))
hcc_trace_matrix_Comp[1,] <- 0.5*InitVector + 0.5*trace_matrix_Comp[2,]
for (i in 2:costANDtracematrices_nrows-1){
  hcc_trace_matrix_Comp[i,] <- 0.5*trace_matrix_Comp[i,] + 0.5*trace_matrix_Comp[i+1,]
}
hcc_trace_matrix_Comp[costANDtracematrices_nrows,] <- trace_matrix_Comp[costANDtracematrices_nrows,]

trace_costs_Comp <- hcc_trace_matrix_Comp*CostMatrix_Comp
trace_qalys_Comp <- sweep(hcc_trace_matrix_Comp, MARGIN=2, UtilityVector_Comp, "*")

HealthStateCosts_Comp <- colSums(trace_costs_Comp[1:cont.TimeHorizon,])
HealthStateQALYs_Comp <- colSums(trace_qalys_Comp[1:cont.TimeHorizon,])

total_costs_Comp <- sum(colSums(trace_costs_Comp[1:cont.TimeHorizon,]))
total_qalys_Comp <- sum(colSums(trace_qalys_Comp[1:cont.TimeHorizon,]))

#NOVEL
trace_matrix_Novel <- matrix(NA, nrow=costANDtracematrices_nrows, ncol=ncol(TransArrayNovel))  
trace_matrix_Novel[1,] <- InitVector
trace_matrix_Novel[2,] <- InitVector %*% TransArrayNovel[,,1] 
for (i in 3:nrow(trace_matrix_Novel)){
  trace_matrix_Novel[i,] <- trace_matrix_Novel[i-1,] %*% TransArrayNovel[,,i-1]
}
#hcc = half cycle corrected
hcc_trace_matrix_Novel <- matrix(NA, nrow=costANDtracematrices_nrows, ncol=ncol(TransArrayNovel))
hcc_trace_matrix_Novel[1,] <- 0.5*InitVector + 0.5*trace_matrix_Novel[2,]
for (i in 2:costANDtracematrices_nrows-1){
  hcc_trace_matrix_Novel[i,] <- 0.5*trace_matrix_Novel[i,] + 0.5*trace_matrix_Novel[i+1,]
}
hcc_trace_matrix_Novel[costANDtracematrices_nrows,] <- trace_matrix_Novel[costANDtracematrices_nrows,]

trace_costs_Novel <- hcc_trace_matrix_Novel*CostMatrix_Novel
trace_qalys_Novel <- sweep(hcc_trace_matrix_Novel, MARGIN=2, UtilityVector_Novel, "*")

HealthStateCosts_Novel <- colSums(trace_costs_Novel[1:cont.TimeHorizon,])
HealthStateQALYs_Novel <- colSums(trace_qalys_Novel[1:cont.TimeHorizon,])

total_costs_Novel <- sum(colSums(trace_costs_Novel[1:cont.TimeHorizon,]))
total_qalys_Novel <- sum(colSums(trace_qalys_Novel[1:cont.TimeHorizon,]))

ICER<-(total_costs_Novel-total_costs_Comp)/(total_qalys_Novel-total_qalys_Comp)

###############
#SUMMARY RESULTS
###############
total_costs_Comp
total_qalys_Comp
total_costs_Novel
total_qalys_Novel
ICER


