drizopoulos / JMbayes2

Extended Joint Models for Longitudinal and Survival Data
https://drizopoulos.github.io/JMbayes2/
78 stars 22 forks source link

predict() fails if longitudinal data are not pre-sorted by both ID and time_var #33

Closed AdamMS closed 2 years ago

AdamMS commented 2 years ago

Here is an example adapted from the JMBayes2 vignettes:

fm1 <- lme(log(serBilir) ~ ns(year, 3) * sex, data = pbc2, random = ~ ns(year, 3) | id, control = lmeControl(opt = 'optim'))

fm2 <- lme(prothrombin ~ ns(year, 2) * sex, data = pbc2, random = ~ ns(year, 2) | id, control = lmeControl(opt = 'optim'))

fm3 <- mixed_model(ascites ~ year * sex, data = pbc2, random = ~ year | id, family = binomial())

pbc2.id$event <- as.numeric(pbc2.id$status != "alive") CoxFit <- coxph(Surv(years, event) ~ drug + age, data = pbc2.id)

jointFit <- jm(CoxFit, list(fm1, fm2, fm3), time_var = "year", control = list(n_iter = 1000L, # TOTAL number of iterations (includes n_burnin) n_burnin = 700L, # Burn-in iterations n_thin = 10L))

t0 <- 5 ND <- pbc2[pbc2$id %in% c(25, 93), ] ND <- ND[ND$year <= t0, ] ND$status2 <- 0 ND$years <- t0

## Scramble the rows in 'ND' ... if we skip this step, predict() works as intended ND <- ND[sample(1:nrow(ND), nrow(ND), FALSE),]

predLong1 <- predict(jointFit, newdata = ND, type = "mean_subject", type_pred = "response", return_newdata = TRUE, n_samples = 15)

Error in simulate_REs(Data, mcmc, control) : Mat::elem(): incompatible matrix dimensions: 2x1 and 5x1

drizopoulos commented 2 years ago

Thanks for reporting this. It should be resolved in the current development version on GitHub.