hesim-dev / hesim

Health economic simulation modeling and decision analysis
https://hesim-dev.github.io/hesim/
62 stars 15 forks source link

simulate time inhomogeneous CTMC or semi-markov #90

Closed esther1262 closed 2 years ago

esther1262 commented 2 years ago

Hi, I want to simulate a time inhomogeneous 3-state CTMC or semi-markov, whereas the transition rates are in terms of the hazard function of log-logistic distribution with shape=delta, scale = exp(-delta*b'Z). Covariates effects b are the same across all 6 transitions.

I'm new to hesim package and thus I have few questions.

Can I simulate a markov chain using the package? without first fitting a flexsurvreg model? using create_IndivCtstmTrans?

I read your some guidelines and wrote some codes, but something went wrong.


# Treatment strategies (I dont have it, so put a constant?)
strategies <- data.table(
  strategy_id = 1
)
n_strategies <- nrow(strategies)

# Patients
n_patients <- 100
patients <- data.table(
  patient_id = 1:n_patients,
  z2 = rnorm(n_patients,0,1),
  z1 = rbinom(n_patients, 1, 0.5)
)

# States
states <- data.table( # Non-death health states
  state_id = 1:3,
  state_name = c("first", "second", "third")
) 
n_states <- nrow(states)

# transitions
tmat <- rbind(c(NA, 1, 2),
              c(3, NA, 4),
              c(NA, 5, 6))
transitions <- create_trans_dt(tmat)
transitions[, trans := factor(transition_id)]

hesim_dat <- hesim_data(strategies = strategies,
                        patients = patients, 
                        states = states,
                        transitions = transitions)

transmod_data <- expand(hesim_dat, by = c("strategies","patients","transitions"))

n_samples <- 500

Regards

dincerti commented 2 years ago

Hi, the problem with your code is that z1 and z2 were never defined. They are only available as columns with the patients data table.

esther1262 commented 2 years ago

Hi, errors still popped up for the codes below. Do you have any examples with simulation using parameters directly? The references I found online are all based on fitted models. Thanks

# define true parameters: deltas=shape for each transition; gammas = covariates coefficients
deltas <- matrix(c(1.8, 2.3, 1.3, 2.8, 3, 2.4), nrow=3, byrow=T)
gammas <- c(-1.5, -0.6, 0.3)

# dataframe to store scale parameters adjusted by different covariates
scale_frame <- data.frame(z1=rnorm(n_samples, 0, 1),  z2=rbinom(n_samples, 1, 0.5))
scale_frame$logscale <- apply(scale_frame, 1, function(x) gammas %*%c(1,x) )

# parameters for trans 1 to 6
transmod_params <- params_surv_list(
  params_surv(coefs = list(shape=rep(log(deltas[1,1]), n_samples), scale=scale_frame), dist = "llogis"), 
  params_surv(coefs = list(shape=rep(log(deltas[1,2]), n_samples), scale=scale_frame), dist = "llogis"), 
  params_surv(coefs = list(shape=rep(log(deltas[2,1]), n_samples), scale=scale_frame), dist = "llogis"), 
  params_surv(coefs = list(shape=rep(log(deltas[2,2]), n_samples), scale=scale_frame), dist = "llogis"), 
  params_surv(coefs = list(shape=rep(log(deltas[3,1]), n_samples), scale=scale_frame), dist = "llogis"), 
  params_surv(coefs = list(shape=rep(log(deltas[3,2]), n_samples), scale=scale_frame), dist = "llogis")
)

transmod_cr <- create_IndivCtstmTrans(transmod_params, transmod_data,
                                      trans_mat = tmat, n = n_samples,
                                      clock = "reset")

Error: Not all variables in 'object' are contained in 'input_data'.
dincerti commented 2 years ago

You can look at the vignette here for an example. It seems your transmod_data object is missing columns for logscale (presumably a column of 1's) and x1.