Skip to content

Commit f535a6c

Browse files
author
Dominic Muston
committed
Calculation corrections
1 parent efc3dfe commit f535a6c

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

R/discrmd.R

+14-9
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,8 @@ drmd_psm <- function(ptdata, dpam, psmtype="simple", Ty=10, discrate=0, lifetabl
7979
)
8080
# Obtain all the hazards
8181
allh <- calc_haz_psm(timevar=ds$tmid, ptdata=ptdata, dpam=dpam, psmtype=psmtype)$adj
82-
# Derive the unconstrained mortality hazards
82+
# Derive the unconstrained PPD mortality probability
8383
ds$q_ppd <- 1-exp(-allh$ppd)
84-
ds$q_pps <- 1-exp(-allh$pps)
8584
# Derive the constrained life table
8685
ds$clx <- calc_ltsurv(convert_wks2yrs(ds$tzero), lifetable)
8786
# Other calculations on the dataset
@@ -92,15 +91,23 @@ drmd_psm <- function(ptdata, dpam, psmtype="simple", Ty=10, discrate=0, lifetabl
9291
# Derive the TTP probability (balancing item)
9392
q_pfs = 1 - dplyr::lead(u_pf)/u_pf,
9493
q_ttp = q_pfs-q_ppd,
94+
d_pf = u_pf * q_ppd,
9595
c_qpfs = q_ttp + pmax(q_ppd, cqx),
96+
# Derive the PPS mortality probability
97+
d_pfpd = u_pf + u_pd - dplyr::lead(u_pf) - dplyr::lead(u_pd),
98+
d_pps = d_pfpd - d_pf,
99+
q_pps = dplyr::if_else(u_pd==0, 0, d_pps / u_pd),
100+
# Constrained probabilities
101+
cqpfs = q_ttp + pmax(q_ppd, cqx),
102+
cqpps = pmax(q_pps, cqx),
96103
# Derive the constrained PF and PD memberships
97104
c_pf = u_pf,
98105
c_pd = u_pd,
99106
)
100107
# Derive the constrained PF and PD memberships
101108
for (t in 2:(Tw)) {
102109
ds$c_pf[t] = ds$c_pf[t-1] * (1-ds$cqpfs[t-1])
103-
ds$c_pd[t] = ds$c_pf[t-1] * ds$q_ttp[t-1] + ds$c_pd[t-1] * (1 - pmax(ds$q_pps[t-1], ds$cqx[t-1]))
110+
ds$c_pd[t] = ds$c_pf[t-1] * ds$q_ttp[t-1] + ds$c_pd[t-1] * (1 - ds$cqpps[t-1])
104111
}
105112
# The final membership probabilities are zero
106113
ds$c_pf[Tw+1] <- ds$c_pd[Tw+1] <- 0
@@ -160,13 +167,13 @@ drmd_stm_cf <- function(dpam, Ty=10, discrate=0, lifetable=NA, timestep=1) {
160167
h_ppd = calc_haz(tmid, survobj=dpam$ppd),
161168
q_ppd = 1-exp(-h_ppd),
162169
# Derive the constrained life table
163-
clx = calc_ltsurv(convert_wks2yrs(ds$tzero), lifetable),
170+
clx = calc_ltsurv(convert_wks2yrs(tzero), lifetable),
164171
# Derive the background mortality for this timepoint
165172
cqx = 1 - dplyr::lead(clx)/clx,
166173
# Derive the TTP probability (balancing item for PFS)
167174
q_pfs = 1 - dplyr::lead(u_pf)/u_pf,
168175
q_ttp = q_pfs-q_ppd,
169-
cqpfs = q_ttp + pmax(q_ppd, cqx),
176+
d_pf = u_pf * q_ppd,
170177
# Derive the PPS mortality probability
171178
d_pfpd = u_pf + u_pd - dplyr::lead(u_pf) - dplyr::lead(u_pd),
172179
d_pps = d_pfpd - d_pf,
@@ -181,7 +188,7 @@ drmd_stm_cf <- function(dpam, Ty=10, discrate=0, lifetable=NA, timestep=1) {
181188
# Derive the constrained PF and PD memberships
182189
for (t in 2:(Tw)) {
183190
ds$c_pf[t] = ds$c_pf[t-1] * (1-ds$cqpfs[t-1])
184-
ds$c_pd[t] = ds$c_pf[t-1] * ds$q_ttp[t-1] + ds$c_pd[t-1] * (1 - pmax(ds$q_pps[t-1], ds$cqx[t-1]))
191+
ds$c_pd[t] = ds$c_pf[t-1] * ds$q_ttp[t-1] + ds$c_pd[t-1] * (1 - ds$cqpps[t-1])
185192
}
186193
# The final membership probabilities are zero
187194
ds$c_pf[Tw+1] <- ds$c_pd[Tw+1] <- 0
@@ -202,8 +209,6 @@ drmd_stm_cf <- function(dpam, Ty=10, discrate=0, lifetable=NA, timestep=1) {
202209
return(list(pf=pf, pd=pd, os=pf+pd, calc=ds))
203210
}
204211

205-
206-
207212
#' Discretized Restricted Mean Duration calculation for State Transition Model Clock Reset structure
208213
#' Calculate restricted mean duration (RMD) in PF, PD and OS states under a State Transition Model Clock Reset structure.
209214
#' @inherit drmd_psm params return
@@ -246,7 +251,7 @@ drmd_stm_cr <- function(dpam, Ty=10, discrate=0, lifetable=NA, timestep=1) {
246251
h_ppd = calc_haz(tmid, survobj=dpam$ppd),
247252
q_ppd = 1-exp(-h_ppd),
248253
# Derive the constrained life table
249-
clx = calc_ltsurv(convert_wks2yrs(ds$tzero), lifetable),
254+
clx = calc_ltsurv(convert_wks2yrs(tzero), lifetable),
250255
cqx = 1 - dplyr::lead(clx)/clx,
251256
# Derive the TTP probability (balancing item for PF)
252257
q_pfs = 1 - dplyr::lead(u_pf)/u_pf,

0 commit comments

Comments
 (0)