Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ictstm$sim_disease() function error: Error: Expecting a single value: [extent=0]. #111

Open
mcincam100 opened this issue Nov 14, 2023 · 2 comments

Comments

@mcincam100
Copy link

Hi,
I am doing some practice runs of a flexsurv + hesim simulation, using artificial data. All seems to go well until I hit the ictstm$sim_disease() function, and I get an error:

ictstm$sim_disease()
Error: Expecting a single value: [extent=0].
My full code is below. I have been unable to successfully diagnose the issue. Thanks for any tips!
Cameron

#STEP 1: CREATING HESIM_DAT FILE FROM MULTIPLE CSVs CONTAINING PIECES OF DISEASE PROGRESSION #FRAMEWORK, TO #INITIALIZE THE SIMULATION

Load necessary libraries

library(hesim)

Write strategies.csv

strategies <- data.frame(strategy_id = c(0, 1))
write.csv(strategies, "J:/Data Development Unit/Administrative/Analytical Diversification/R Testing/hesimstrat.csv", row.names = FALSE)

Write patients.csv

set.seed(123) # for reproducibility
patients <- data.frame(patient_id = seq(1, 1000),
x1 = rbinom(1000, 1, .5),
x2 = rbinom(1000, 1, .5),
x3 = rbinom(1000, 1, .5))
write.csv(patients, "J:/Data Development Unit/Administrative/Analytical Diversification/R Testing/hesimpat.csv", row.names = FALSE)

Write states.csv

states <- data.frame(state_id = c(1, 2, 3, 4))
write.csv(states, "J:/Data Development Unit/Administrative/Analytical Diversification/R Testing/hesimstat.csv", row.names = FALSE)

Read the .csv files into R

strategies <- read.csv("J:/Data Development Unit/Administrative/Analytical Diversification/R Testing/hesimstrat.csv")
patients <- read.csv("J:/Data Development Unit/Administrative/Analytical Diversification/R Testing/hesimpat.csv")
states <- read.csv("J:/Data Development Unit/Administrative/Analytical Diversification/R Testing/hesimstat.csv")

Define transitions

tmat <- rbind(c(NA, 1, NA, NA),
c(2, NA, 3, NA),
c(4, NA, NA, 5),
c(6, 7, NA, NA))
colnames(tmat) <- rownames(tmat) <- c("nonCJS", "Police", "Courts", "Corrections")
transitions <- create_trans_dt(tmat)
transitions[, trans := factor(transition_id)]

Create the hesim_dat object

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

Print the hesim_dat object

print(hesim_dat)

#Expand the hesim_dat object into long format
transmod_data <- expand(hesim_dat,
by = c("strategies", "patients", "transitions"))

options(max.print = 10000) # Increase max.print to a large number
head(transmod_data, 14) # Now this should print 14 rows (can be changed to any desired number)

#STEP 2: CREATING FLEXSURV MSTM FIT OBJECT TO ENABLE THE DISEASE PROGRESSION MODEL IN HESIM #(WEIBULL AFT CLOCK-RESET MODEL)

Load necessary libraries

library(flexsurv)
library(survival)

Set the seed for reproducibility

set.seed(123)

Generate data for flexsurv analysis

n <- 1000 # Define the number of patients
patient_id <- rep(1:n, each = 7) # Each patient has 7 transitions
time <- rweibull(n * 7, shape = 2, scale = 1) # Modify parameters as needed
status <- rbinom(n * 7, size = 1, prob = 0.5)
trans <- rep(1:7, times = n) # 7 transitions
strategy_id <- rep(rbinom(n, size = 1, prob = 0.5), each = 7)
x1 <- rep(rbinom(n, size = 1, prob = 0.5), each = 7)
x2 <- rep(rbinom(n, size = 1, prob = 0.5), each = 7)
x3 <- rep(rbinom(n, size = 1, prob = 0.5), each = 7)

Create data frame

data <- data.frame(patient_id, time, status, trans, strategy_id, x1, x2, x3)

Write to CSV

write.csv(data, file = "J:/Data Development Unit/Administrative/Analytical Diversification/R Testing/flexsurvmod.csv", row.names = FALSE)

Define your transition data

surv_dat <- read.csv("J:/Data Development Unit/Administrative/Analytical Diversification/R Testing/flexsurvmod.csv")

Fit the survival models

fits <- vector(length = max(tmat, na.rm = TRUE), mode = "list")
for (i in 1:length(fits)){
fits[[i]] <- flexsurvreg(Surv(time, status) ~ factor(strategy_id),
data = surv_dat,
subset = (trans == i),
dist = "weibull")
}
fits <- flexsurvreg_list(fits)

#STEP 3: CREATING/RUNNING THE DISEASE PROGRESSION MODEL IN HESIM

transmod_data <- expand(hesim_dat)
transmod <- create_IndivCtstmTrans(fits, input_data = transmod_data,
trans_mat = tmat,
n = 200)
class(transmod)

ictstm <- IndivCtstm$new(trans_model = transmod,
utility_model = NULL,
cost_models = NULL)

ictstm$sim_disease()
head(ictstm$disprog_)

@dincerti
Copy link
Member

dincerti commented Nov 24, 2023

The problem is that your transition model doesn't have a death state specified. Try this:

transmod <- create_IndivCtstmTrans(
  fits,
  input_data = transmod_data,
  trans_mat = tmat,
  n = 200,
  death_state=4
)

The reason no death state is specified is because your tmat doesn't have any absorbing states so a death state cannot be automatically determined. That said, this is a bug: a much better error message should be displayed alerting you of the problem.

@mcincam100
Copy link
Author

Many thanks Devin, that worked.
Cam

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants