Skip to content

Commit

Permalink
Fix for censored states in simfitted.msm (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
chjackson committed Feb 1, 2024
1 parent 61bab53 commit a29de0b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
11 changes: 6 additions & 5 deletions R/simul.R
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ simhidden.msm <- function(state, hmodel, nstates, beta=NULL, x=NULL)
simfitted.msm <- function(x, drop.absorb=TRUE, drop.pci.imp=TRUE){
sim.df <- x$data$mf
x$data <- expand.data(x)
sim.df$"(cens)" <- ifelse(sim.df$"(state)" %in% 1:x$qmodel$nstates, 0, sim.df$"(state)") # 0 if not censored, cens indicator if censored, so that censoring is retained in simulated data. TODO used in pearson?
sim.df$"cens" <- ifelse(sim.df$"(state)" %in% 1:x$qmodel$nstates, 0, sim.df$"(state)") # 0 if not censored, cens indicator if censored, so that censoring is retained in simulated data. TODO used in pearson?
if (x$qcmodel$ncovs > 0) {
sim.df <- cbind(sim.df, x$data$mm.cov)
cov.effs <- lapply(x$Qmatrices, function(y)t(y)[t(x$qmodel$imatrix)==1])[x$qcmodel$covlabels]
Expand All @@ -548,10 +548,11 @@ simfitted.msm <- function(x, drop.absorb=TRUE, drop.pci.imp=TRUE){
sim.df <- cbind(sim.df, x$data$mm.mcov)
misccov.effs <- lapply(x$Ematrices, function(y)t(y)[t(x$emodel$imatrix)==1])[x$ecmodel$covlabels]
} else misccov.effs <- NULL
names(sim.df) <- replace(names(sim.df), match(c("(state)","(time)","(subject)"), names(sim.df)),
c("state","time","subject"))
if (any(union(names(cov.effs), names(misccov.effs)) %in% c("state","time","subject")))
stop("Not supported with covariates named \"state\", \"time\" or \"subject\"") # TODO?
names(sim.df) <- replace(names(sim.df), match(c("(time)","(subject)"), names(sim.df)),
c("time","subject"))
sim.df$state <- NULL # replace observed with simulated state
if (any(union(names(cov.effs), names(misccov.effs)) %in% c("state","time","subject","cens")))
stop("Not supported with covariates named \"state\", \"time\", \"subject\" or \"cens\"") # TODO?
boot.df <- simmulti.msm(data=sim.df,
qmatrix=qmatrix.msm(x, covariates=0, ci="none"),
covariates=cov.effs,
Expand Down
13 changes: 13 additions & 0 deletions tests/testthat/test_simul.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
test_that("simfitted.msm",{
skip_on_cran()
psor.msm <- msm(state ~ months, subject=ptnum, data=psor, qmatrix = psor.q, covariates = ~ollwsdrt)
expect_equal(simfitted.msm(psor.msm)$time[1], psor$months[1])

psorc <- psor
psorc$state[2] <- 99
psorc.msm <- msm(state ~ months, subject=ptnum, data=psorc, qmatrix = psor.q,
covariates = ~ollwsdrt, censor=99)
set.seed(1)
simdat <- simfitted.msm(psorc.msm)$state[2]
expect_equal(simdat, psorc$state[2])
})

0 comments on commit a29de0b

Please sign in to comment.