我在 hesim 中遇到以下错误:ictstm$sim_disease() 函数错误:错误:期望单个值:[extent=0]

I encountered the following error in hesim: ictstm$sim_disease() function error: Error: Expecting a single value: [extent=0]

提问人:Cam McIntosh 提问时间:11/14/2023 更新时间:11/14/2023 访问量:21

问:

我正在使用人工数据进行一些 flexsurv + hesim 模拟的练习运行。一切似乎都很顺利,直到我点击 ictstm$sim_disease() 函数,我收到一个错误:

ICTSTM$sim_disease() 错误:需要单个值:[extent=0]。

我无法成功诊断问题。感谢您的任何提示!

卡梅伦

 Here is my code. I was expecting a populated disease progression data frame in the end.

 #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 T     
 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     
 AFTCLOCK-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_)
DataFrame 函数 错误处理 模拟 R-Package

评论


答: 暂无答案