## Non-Gaussian Joint Models with JMbayes2

Taking advantage of the versatility of the GLMMadaptive package, JMbayes2 can fit joint models with several different types of mixed-effects models. The following examples illustrate these capabilities. All examples have the same structure, namely, first, a short motivation for each mixed-model is given, followed by a piece of R code simulating data from a joint model with the respective mixed-effects sub-model, closing by the syntax to fit the joint model. In this last part, the main difference per example is the call to mixed_model().

### Beta mixed models

With very few exceptions, continuous outcomes that we wish to analyze have some natural bounds. For example, the levels of blood biomarker for a set of patients. However, often, the majority of the observations are located far away from these natural bounds, and an assumption of a normal distribution for the outcome can be safely done. In some settings though, we can have outcomes for which a substantial percentage of the observations are located near the boundaries leading to skewed or U-shaped distributions. For such longitudinal outcomes, a linear mixed model with normal error terms often does not provide a good fit. A natural alternative is to select a distribution that respects the bounded nature of the outcome. The most well-known distribution for such outcomes is the Beta distribution defined in the $$(0, 1)$$ interval (note: a bounded outcome $$Y^*$$ in the $$(a, b)$$ interval can be transformed to the $$Y = (Y^* - a) / (b - a)$$ in the $$(0, 1)$$ interval).

The following piece of code illustrates how to simulate data from a joint model with a Beta mixed effects model. The default functional form is assumed, i.e., that the linear predictor $$\eta(t)$$ of the mixed model is associated with the hazard of an event at time $$t$$. The linear predictor is related to the mean $$\mu(t)$$ of the Beta distribution under the logit link function, i.e., $$\log[\mu(t) / \{1 - \mu(t)\}] = \eta(t)$$.

set.seed(1234)
n <- 200 # number of subjects
K <- 8 # number of measurements per subject
t_max <- 10 # maximum follow-up time

# we construct a data frame with the design:
# everyone has a baseline measurement, and then measurements at random
# follow-up times up to t_max
DF <- data.frame(id = rep(seq_len(n), each = K),
time = c(replicate(n, c(0, sort(runif(K - 1, 0, t_max))))),
sex = rep(gl(2, n/2, labels = c("male", "female")), each = K))

# design matrices for the fixed and random effects
X <- model.matrix(~ sex * time, data = DF)
Z <- model.matrix(~ time, data = DF)

betas <- c(-2.2, -0.25, 0.24, -0.05) # fixed effects coefficients
phi <- 5 # precision parameter of the Beta distribution
D11 <- 1.0 # variance of random intercepts
D22 <- 0.5 # variance of random slopes

# we simulate random effects
b <- cbind(rnorm(n, sd = sqrt(D11)), rnorm(n, sd = sqrt(D22)))
# linear predictor
eta_y <- as.vector(X %*% betas + rowSums(Z * b[DF$id, ])) # mean of the Beta distribution mu_y <- plogis(eta_y) # plogis(eta_y) = exp(eta_y) / (1 + exp(eta_y)) # we simulate Beta longitudinal data DF$y <- rbeta(n * K, shape1 = mu_y * phi, shape2 = phi * (1 - mu_y))
# we transform to (0, 1)
DF$y <- (DF$y * (nrow(DF) - 1) + 0.5) / nrow(DF)

upp_Cens <- 15 # fixed Type I censoring time
shape_wb <- 5 # shape Weibull
alpha <- 0.8 # association coefficients
gammas <- c("(Intercept)" = -9, "sex" = 0.5)
W <- model.matrix(~ sex, data = DF[!duplicated(DF$id), ]) # linear predictor for the survival model eta_t <- as.vector(W %*% gammas) # to simulate event times we use inverse transform sampling # (https://en.wikipedia.org/wiki/Inverse_transform_sampling). Namely, we want # to find t, such that S(t) = u, where S(.) is the survival function, and u a # number from the Unif(0, 1) distribution. The function below calculates # log(u) - log(S(t)), and for a given u, we want to find t for which it equals # zero. We do that below using the uniroot() function invS <- function (t, i) { # i denotes the subject sex_i <- W[i, 2L] # h() is the hazard function and we assume a Weibull baseline hazard h <- function (s) { X_at_s <- cbind(1, sex_i, s, sex_i * s) Z_at_s <- cbind(1, s) # the linear predictor from the mixed model evaluated at time s f <- as.vector(X_at_s %*% betas + rowSums(Z_at_s * b[rep(i, nrow(Z_at_s)), ])) exp(log(shape_wb) + (shape_wb - 1) * log(s) + eta_t[i] + f * alpha) } # -log(S(t)) = H(t), where H(t) is the cumulative hazard function integrate(h, lower = 0, upper = t)$value + log(u[i])
}
# we simulate the event times
u <- runif(n)
trueTimes <- numeric(n)
for (i in seq_len(n)) {
Up <- 100
Root <- try(uniroot(invS, interval = c(1e-05, Up), i = i)$root, TRUE) trueTimes[i] <- if (!inherits(Root, "try-error")) Root else 150 } # we use fixed Type I right censoring denoting the end of the trial. Ctimes <- upp_Cens Time <- pmin(trueTimes, Ctimes) event <- as.numeric(trueTimes <= Ctimes) # event indicator # we keep the longitudinal measurements before the event times DF$Time <- Time[DF$id] DF$event <- event[DF$id] DF <- DF[DF$time <= DF$Time, ] To fit the corresponding joint model, we fit first a Beta mixed model using the beta.fam() family object into the call of mixed_model(): DF_id <- DF[!duplicated(DF$id), ]
Cox_fit <- coxph(Surv(Time, event) ~ sex, data = DF_id)
Beta_MixMod <- mixed_model(y ~ sex * time, random = ~ time | id, data = DF,
family = beta.fam())

jointFit <- jm(Cox_fit, Beta_MixMod, time_var = "time")
summary(jointFit)
#>
#> Call:
#> jm(Surv_object = Cox_fit, Mixed_objects = Beta_MixMod, time_var = "time")
#>
#> Data Descriptives:
#> Number of Groups: 200        Number of events: 158 (79%)
#> Number of Observations:
#>   y: 1182
#>
#>                   DIC      WAIC     LPML
#> marginal    -4019.446 -4062.396 1762.158
#> conditional -3836.014 -3938.658 1812.803
#>
#> Random-effects covariance matrix:
#>
#>        StdDev   Corr
#> (Intr) 0.8508 (Intr)
#> time   0.4668 0.0807
#>
#> Survival Outcome:
#>             Mean  StDev    2.5%  97.5%      P   Rhat
#> sexfemale 0.2172 0.2927 -0.3623 0.7681 0.4631 1.0098
#> value(y)  1.0409 0.0960  0.8763 1.2603 0.0000 1.0665
#>
#> Longitudinal Outcome: y (family = beta, link = logit)
#>                   Mean  StDev    2.5%   97.5%      P   Rhat
#> (Intercept)    -2.3457 0.1111 -2.5659 -2.1304 0.0000 1.0123
#> sexfemale      -0.0629 0.1503 -0.3543  0.2285 0.6753 1.0067
#> time            0.3275 0.0516  0.2276  0.4303 0.0000 1.0036
#> sexfemale:time -0.0644 0.0723 -0.2051  0.0785 0.3678 1.0044
#> sigma           6.3251 0.3848  5.6079  7.1273 0.0000 1.0470
#>
#> MCMC summary:
#> chains: 3
#> iterations per chain: 3500
#> burn-in per chain: 500
#> thinning: 1
#> time: 22 sec

### Censored linear mixed models

Some continuous longitudinal outcomes may have a censored nature. A typical example of such outcomes is when we have limit of detection issues. That is, the values of the outcome cannot be detected below a specified threshold having to do with the (laboratory) equipment used to determine the measurements. In these settings and even if the complete data follow a normal distribution the observed censored data cannot be analyzed with a standard mixed model. The mixed_model() function can accommodate such outcomes using the censored.normal() family object.

The following piece of code simulates data from a joint model with a linear mixed model for the longitudinal outcomes, but applies censoring in the realized longitudinal observations.

set.seed(1234)
n <- 200 # number of subjects
K <- 12 # number of measurements per subject
t_max <- 14 # maximum follow-up time

# we construct a data frame with the design:
# everyone has a baseline measurement, and then measurements at random
# follow-up times up to t_max
DF <- data.frame(id = rep(seq_len(n), each = K),
time = c(replicate(n, c(0, sort(runif(K - 1, 0, t_max))))),
sex = rep(gl(2, n/2, labels = c("male", "female")), each = K))

# design matrices for the fixed and random effects
X <- model.matrix(~ sex * time, data = DF)
Z <- model.matrix(~ time, data = DF)

betas <- c(-2.2, -0.25, 0.24, -0.05) # fixed effects coefficients
sigma <- 0.5 # errors' standard deviation
D11 <- 1.0 # variance of random intercepts
D22 <- 0.5 # variance of random slopes

# we simulate random effects
b <- cbind(rnorm(n, sd = sqrt(D11)), rnorm(n, sd = sqrt(D22)))
# linear predictor
eta_y <- as.vector(X %*% betas + rowSums(Z * b[DF$id, ])) # we simulate normal longitudinal data DF$y <- rnorm(n * K, mean = eta_y, sd = sigma)
# we assume that values below -4 are not observed, and set equal to -4
DF$ind <- as.numeric(DF$y < -4)
DF$y <- pmax(DF$y, -4)

upp_Cens <- 15 # fixed Type I censoring time
shape_wb <- 5 # shape Weibull
alpha <- 0.8 # association coefficients
gammas <- c("(Intercept)" = -9, "sex" = 0.5)
W <- model.matrix(~ sex, data = DF[!duplicated(DF$id), ]) # linear predictor for the survival model eta_t <- as.vector(W %*% gammas) # to simulate event times we use inverse transform sampling # (https://en.wikipedia.org/wiki/Inverse_transform_sampling). Namely, we want # to find t, such that S(t) = u, where S(.) is the survival function, and u a # number from the Unif(0, 1) distribution. The function below calculates # log(u) - log(S(t)), and for a given u, we want to find t for which it equals # zero. We do that below using the uniroot() function invS <- function (t, i) { # i denotes the subject sex_i <- W[i, 2L] # h() is the hazard function and we assume a Weibull baseline hazard h <- function (s) { X_at_s <- cbind(1, sex_i, s, sex_i * s) Z_at_s <- cbind(1, s) # the linear predictor from the mixed model evaluated at time s f <- as.vector(X_at_s %*% betas + rowSums(Z_at_s * b[rep(i, nrow(Z_at_s)), ])) exp(log(shape_wb) + (shape_wb - 1) * log(s) + eta_t[i] + f * alpha) } # -log(S(t)) = H(t), where H(t) is the cumulative hazard function integrate(h, lower = 0, upper = t)$value + log(u[i])
}
# we simulate the event times
u <- runif(n)
trueTimes <- numeric(n)
for (i in seq_len(n)) {
Up <- 100
Root <- try(uniroot(invS, interval = c(1e-05, Up), i = i)$root, TRUE) trueTimes[i] <- if (!inherits(Root, "try-error")) Root else 150 } # we use fixed Type I right censoring denoting the end of the trial. Ctimes <- upp_Cens Time <- pmin(trueTimes, Ctimes) event <- as.numeric(trueTimes <= Ctimes) # event indicator # we keep the longitudinal measurements before the event times DF$Time <- Time[DF$id] DF$event <- event[DF$id] DF <- DF[DF$time <= DF$Time, ] The corresponding joint model is fitted with the following syntax: DF_id <- DF[!duplicated(DF$id), ]
Cox_fit <- coxph(Surv(Time, event) ~ sex, data = DF_id)
CensNorm_MixMod <-
mixed_model(cbind(y, ind) ~ sex * time, random = ~ time | id, data = DF,
family = censored.normal())

jointFit <- jm(Cox_fit, CensNorm_MixMod, time_var = "time")
summary(jointFit)
#>
#> Call:
#> jm(Surv_object = Cox_fit, Mixed_objects = CensNorm_MixMod, time_var = "time")
#>
#> Data Descriptives:
#> Number of Groups: 200        Number of events: 165 (82.5%)
#> Number of Observations:
#>   cbind(y, ind): 1346
#>
#>                  DIC     WAIC      LPML
#> marginal    3714.326 4630.438 -3419.221
#> conditional 3333.870 3224.001 -1765.534
#>
#> Random-effects covariance matrix:
#>
#>        StdDev   Corr
#> (Intr) 0.9488 (Intr)
#> time   0.6664 0.1445
#>
#> Survival Outcome:
#>                        Mean  StDev   2.5%  97.5%      P   Rhat
#> sexfemale            0.5922 0.2431 0.1225 1.0651 0.0158 1.0040
#> value(cbind(y, ind)) 0.8540 0.0628 0.7360 0.9866 0.0000 1.0279
#>
#> Longitudinal Outcome: cbind(y, ind) (family = censored normal, link = identity)
#>                   Mean  StDev    2.5%   97.5%      P   Rhat
#> (Intercept)    -2.2601 0.1017 -2.4616 -2.0576 0.0000 1.0023
#> sexfemale      -0.0279 0.1423 -0.3097  0.2494 0.8427 1.0023
#> time            0.3332 0.0694  0.1986  0.4716 0.0000 1.0002
#> sexfemale:time -0.1173 0.0980 -0.3093  0.0749 0.2264 0.9998
#> sigma           0.4945 0.0141  0.4676  0.5237 0.0000 1.0037
#>
#> MCMC summary:
#> chains: 3
#> iterations per chain: 3500
#> burn-in per chain: 500
#> thinning: 1
#> time: 16 sec

### Students’s-t mixed models

Outlying observations is a common occurring issue in practice. Several methods have been proposed in the literature for identifying such observations in the context of longitudinal data. However, removing such values from the analysis is in general not recommended unless we also have external information why these values are outlying. Hence, in these settings we would need to fit mixed models to accommodate these observations. A well-known approach to achieve this is to replace the normal distribution for the error terms in the linear mixed model with a Student’s-t distribution that has heavier tails.

The following syntax simulates data from a joint model with a Student’s-t mixed effects model:

set.seed(1234)
n <- 200 # number of subjects
K <- 12 # number of measurements per subject
t_max <- 14 # maximum follow-up time

# we construct a data frame with the design:
# everyone has a baseline measurement, and then measurements at random
# follow-up times up to t_max
DF <- data.frame(id = rep(seq_len(n), each = K),
time = c(replicate(n, c(0, sort(runif(K - 1, 0, t_max))))),
sex = rep(gl(2, n/2, labels = c("male", "female")), each = K))

# design matrices for the fixed and random effects
X <- model.matrix(~ sex * time, data = DF)
Z <- model.matrix(~ time, data = DF)

betas <- c(-2.2, -0.25, 0.24, -0.05) # fixed effects coefficients
sigma <- 0.5 # error standard deviation
D11 <- 1.0 # variance of random intercepts
D22 <- 0.5 # variance of random slopes

# we simulate random effects
b <- cbind(rnorm(n, sd = sqrt(D11)), rnorm(n, sd = sqrt(D22)))
# linear predictor
eta_y <- as.vector(X %*% betas + rowSums(Z * b[DF$id, ])) # we simulate Student's-t longitudinal data DF$y <- eta_y + sigma * rt(n * K, df = 4)

upp_Cens <- 15 # fixed Type I censoring time
shape_wb <- 5 # shape Weibull
alpha <- 0.8 # association coefficients
gammas <- c("(Intercept)" = -9, "sex" = 0.5)
W <- model.matrix(~ sex, data = DF[!duplicated(DF$id), ]) # linear predictor for the survival model eta_t <- as.vector(W %*% gammas) # to simulate event times we use inverse transform sampling # (https://en.wikipedia.org/wiki/Inverse_transform_sampling). Namely, we want # to find t, such that S(t) = u, where S(.) is the survival function, and u a # number from the Unif(0, 1) distribution. The function below calculates # log(u) - log(S(t)), and for a given u, we want to find t for which it equals # zero. We do that below using the uniroot() function invS <- function (t, i) { # i denotes the subject sex_i <- W[i, 2L] # h() is the hazard function and we assume a Weibull baseline hazard h <- function (s) { X_at_s <- cbind(1, sex_i, s, sex_i * s) Z_at_s <- cbind(1, s) # the linear predictor from the mixed model evaluated at time s f <- as.vector(X_at_s %*% betas + rowSums(Z_at_s * b[rep(i, nrow(Z_at_s)), ])) exp(log(shape_wb) + (shape_wb - 1) * log(s) + eta_t[i] + f * alpha) } # -log(S(t)) = H(t), where H(t) is the cumulative hazard function integrate(h, lower = 0, upper = t)$value + log(u[i])
}
# we simulate the event times
u <- runif(n)
trueTimes <- numeric(n)
for (i in seq_len(n)) {
Up <- 100
Root <- try(uniroot(invS, interval = c(1e-05, Up), i = i)$root, TRUE) trueTimes[i] <- if (!inherits(Root, "try-error")) Root else 150 } # we use fixed Type I right censoring denoting the end of the trial. Ctimes <- upp_Cens Time <- pmin(trueTimes, Ctimes) event <- as.numeric(trueTimes <= Ctimes) # event indicator # we keep the longitudinal measurements before the event times DF$Time <- Time[DF$id] DF$event <- event[DF$id] DF <- DF[DF$time <= DF$Time, ] To fit the corresponding joint model we use the students.t() family object in the call to mixed_model(): DF_id <- DF[!duplicated(DF$id), ]
Cox_fit <- coxph(Surv(Time, event) ~ sex, data = DF_id)
Stdt_MixMod <-
mixed_model(y ~ sex * time, random = ~ time | id, data = DF,
family = students.t(df = 4))

jointFit <- jm(Cox_fit, Stdt_MixMod, time_var = "time")
summary(jointFit)
#>
#> Call:
#> jm(Surv_object = Cox_fit, Mixed_objects = Stdt_MixMod, time_var = "time")
#>
#> Data Descriptives:
#> Number of Groups: 200        Number of events: 165 (82.5%)
#> Number of Observations:
#>   y: 1347
#>
#>                  DIC     WAIC      LPML
#> marginal    5188.101 6789.630 -3773.608
#> conditional 4446.703 4349.913 -2354.946
#>
#> Random-effects covariance matrix:
#>
#>        StdDev   Corr
#> (Intr) 0.9767 (Intr)
#> time   0.6746 0.1189
#>
#> Survival Outcome:
#>             Mean  StDev    2.5%  97.5%      P   Rhat
#> sexfemale 0.0155 0.2473 -0.4476 0.4884 0.9407 1.0004
#> value(y)  0.7929 0.0639  0.6709 0.9233 0.0000 1.0303
#>
#> Longitudinal Outcome: y (family = Student's-t, link = identity)
#>                   Mean  StDev    2.5%   97.5%      P   Rhat
#> (Intercept)    -2.2433 0.1067 -2.4526 -2.0282 0.0000 1.0039
#> sexfemale      -0.1242 0.1487 -0.4206  0.1641 0.4013 1.0008
#> time            0.2801 0.0705  0.1424  0.4200 0.0000 1.0008
#> sexfemale:time -0.0325 0.0986 -0.2252  0.1599 0.7431 1.0009
#>
#> MCMC summary:
#> chains: 3
#> iterations per chain: 3500
#> burn-in per chain: 500
#> thinning: 1
#> time: 16 sec

### Negative binomial mixed models

Count longitudinal outcomes are typically modeled with the Poisson distribution. However, often these outcomes exhibit more variance than what is allowed from the Poisson distribution leading to the well-known problem of over-dispersion. To accommodate this over-dispersion typically the Negative Binomial distribution is used.

The following piece of code simulates data from a joint model for count longitudinal data that follow the Negative Binomial distribution:

set.seed(1234)
n <- 500 # number of subjects
K <- 10 # number of measurements per subject
t_max <- 5 # maximum follow-up time

# we construct a data frame with the design:
# everyone has a baseline measurement, and then measurements at random
# follow-up times up to t_max
DF <- data.frame(id = rep(seq_len(n), each = K),
time = c(replicate(n, c(0, sort(runif(K - 1, 0, t_max))))),
sex = rep(gl(2, n/2, labels = c("male", "female")), each = K))

# design matrices for the fixed and random effects
X <- model.matrix(~ sex * time, data = DF)
Z <- model.matrix(~ time, data = DF)

betas <- c(0.8, -0.5, 0.8, -0.5) # fixed effects coefficients
shape <- 2 # shape/size parameter of the negative binomial distribution
D11 <- 1.0 # variance of random intercepts
D22 <- 0.3 # variance of random slopes

# we simulate random effects
b <- cbind(rnorm(n, sd = sqrt(D11)), rnorm(n, sd = sqrt(D22)))
# linear predictor
eta_y <- as.vector(X %*% betas + rowSums(Z * b[DF$id, ])) # mean of the Beta distribution mu_y <- plogis(eta_y) # plogis(eta_y) = exp(eta_y) / (1 + exp(eta_y)) # we simulate negative binomial longitudinal data DF$y <- rnbinom(n * K, size = shape, mu = exp(eta_y))

# simulate event times
upp_Cens <- 5 # fixed Type I censoring time
shape_wb <- 5 # shape Weibull
alpha <- 0.8 # association coefficient
gammas <- c("(Intercept)" = -9, "sex" = 0.5)
W <- model.matrix(~ sex, data = DF[!duplicated(DF$id), ]) # linear predictor for the survival model eta_t <- as.vector(W %*% gammas) # to simulate event times we use inverse transform sampling # (https://en.wikipedia.org/wiki/Inverse_transform_sampling). Namely, we want # to find t, such that S(t) = u, where S(.) is the survival function, and u a # number from the Unif(0, 1) distribution. The function below calculates # log(u) - log(S(t)), and for a given u, we want to find t for which it equals # zero. We do that below using the uniroot() function invS <- function (t, i) { # i denotes the subject sex_i <- W[i, 2L] # h() is the hazard function and we assume a Weibull baseline hazard h <- function (s) { X_at_s <- cbind(1, sex_i, s, sex_i * s) Z_at_s <- cbind(1, s) # the linear predictor from the mixed model evaluated at time s f <- as.vector(X_at_s %*% betas + rowSums(Z_at_s * b[rep(i, nrow(Z_at_s)), ])) exp(log(shape_wb) + (shape_wb - 1) * log(s) + eta_t[i] + f * alpha) } # -log(S(t)) = H(t), where H(t) is the cumulative hazard function integrate(h, lower = 0, upper = t)$value + log(u[i])
}
# we simulate the event times
u <- runif(n)
trueTimes <- numeric(n)
for (i in seq_len(n)) {
Up <- 100
Root <- try(uniroot(invS, interval = c(1e-05, Up), i = i)$root, TRUE) trueTimes[i] <- if (!inherits(Root, "try-error")) Root else 150 } # we use fixed Type I right censoring denoting the end of the trial. Ctimes <- upp_Cens Time <- pmin(trueTimes, Ctimes) event <- as.numeric(trueTimes <= Ctimes) # event indicator # we keep the longitudinal measurements before the event times DF$Time <- Time[DF$id] DF$event <- event[DF$id] DF <- DF[DF$time <= DF$Time, ] The corresponding joint model is the fitted using the following syntax: DF_id <- DF[!duplicated(DF$id), ]
Cox_fit <- coxph(Surv(Time, event) ~ sex, data = DF_id)
NB_MixMod <- mixed_model(y ~ sex * time, random = ~ time | id, data = DF,
family = negative.binomial())

jointFit <- jm(Cox_fit, NB_MixMod, time_var = "time")
summary(jointFit)
#>
#> Call:
#> jm(Surv_object = Cox_fit, Mixed_objects = NB_MixMod, time_var = "time")
#>
#> Data Descriptives:
#> Number of Groups: 500        Number of events: 409 (81.8%)
#> Number of Observations:
#>   y: 3842
#>
#>                  DIC     WAIC      LPML
#> marginal    21347.87 21301.19 -10650.71
#> conditional 22641.05 22355.86 -11567.90
#>
#> Random-effects covariance matrix:
#>
#>        StdDev   Corr
#> (Intr) 1.0472 (Intr)
#> time   0.5362 -0.0512
#>
#> Survival Outcome:
#>             Mean  StDev   2.5%  97.5%     P   Rhat
#> sexfemale 0.6011 0.1626 0.2920 0.9221 2e-04 1.0391
#> value(y)  0.8061 0.0545 0.7023 0.9194 0e+00 1.0626
#>
#> Longitudinal Outcome: y (family = negative binomial, link = log)
#>                   Mean  StDev    2.5%   97.5% P   Rhat
#> (Intercept)     0.8408 0.0786  0.6880  0.9930 0 1.0188
#> sexfemale      -0.5848 0.1144 -0.8084 -0.3619 0 1.0023
#> time            0.8611 0.0415  0.7805  0.9442 0 1.0155
#> sexfemale:time -0.5139 0.0585 -0.6283 -0.4015 0 1.0025
#> sigma           1.9910 0.0837  1.8299  2.1585 0 1.0021
#>
#> MCMC summary:
#> chains: 3
#> iterations per chain: 3500
#> burn-in per chain: 500
#> thinning: 1
#> time: 31 sec

### Beta-binomial longitudinal outcomes

As for count data also for Binomial data we may have the over-dispersion problem. In this case, we can change to standard Binomial distribution to a Beta-Binomial one to accommodate this.

The following piece of code simulates data from a joint model for binomial longitudinal data that follow the Beta-Binomial distribution:

set.seed(1234)
n <- 500 # number of subjects
K <- 8 # number of measurements per subject
t_max <- 10 # maximum follow-up time

# we construct a data frame with the design:
# everyone has a baseline measurement, and then measurements at random
# follow-up times up to t_max
DF <- data.frame(id = rep(seq_len(n), each = K),
time = c(replicate(n, c(0, sort(runif(K - 1, 0, t_max))))),
sex = rep(gl(2, n/2, labels = c("male", "female")), each = K))

# design matrices for the fixed and random effects
X <- model.matrix(~ sex * time, data = DF)
Z <- model.matrix(~ time, data = DF)

betas <- c(-2.2, -0.25, 0.24, -0.05) # fixed effects coefficients
phi <- 5 # precision parameter of the Beta distribution
D11 <- 1.0 # variance of random intercepts
D22 <- 0.5 # variance of random slopes

# we simulate random effects
b <- cbind(rnorm(n, sd = sqrt(D11)), rnorm(n, sd = sqrt(D22)))
# linear predictor
eta_y <- as.vector(X %*% betas + rowSums(Z * b[DF$id, ])) # mean of the Beta distribution mu_y <- plogis(eta_y) # plogis(eta_y) = exp(eta_y) / (1 + exp(eta_y)) # we simulate probabilities from the Beta distribution probs <- rbeta(n * K, shape1 = mu_y * phi, shape2 = phi * (1 - mu_y)) # we transform to (0, 1) probs <- (probs * (nrow(DF) - 1) + 0.5) / nrow(DF) # we simulate binomial data use the probs DF$y <- rbinom(n * K, size = 20, prob = probs)

upp_Cens <- 15 # fixed Type I censoring time
shape_wb <- 5 # shape Weibull
alpha <- 0.8 # association coefficients
gammas <- c("(Intercept)" = -9, "sex" = 0.5)
W <- model.matrix(~ sex, data = DF[!duplicated(DF$id), ]) # linear predictor for the survival model eta_t <- as.vector(W %*% gammas) # to simulate event times we use inverse transform sampling # (https://en.wikipedia.org/wiki/Inverse_transform_sampling). Namely, we want # to find t, such that S(t) = u, where S(.) is the survival function, and u a # number from the Unif(0, 1) distribution. The function below calculates # log(u) - log(S(t)), and for a given u, we want to find t for which it equals # zero. We do that below using the uniroot() function invS <- function (t, i) { # i denotes the subject sex_i <- W[i, 2L] # h() is the hazard function and we assume a Weibull baseline hazard h <- function (s) { X_at_s <- cbind(1, sex_i, s, sex_i * s) Z_at_s <- cbind(1, s) # the linear predictor from the mixed model evaluated at time s f <- as.vector(X_at_s %*% betas + rowSums(Z_at_s * b[rep(i, nrow(Z_at_s)), ])) exp(log(shape_wb) + (shape_wb - 1) * log(s) + eta_t[i] + f * alpha) } # -log(S(t)) = H(t), where H(t) is the cumulative hazard function integrate(h, lower = 0, upper = t)$value + log(u[i])
}
# we simulate the event times
u <- runif(n)
trueTimes <- numeric(n)
for (i in seq_len(n)) {
Up <- 100
Root <- try(uniroot(invS, interval = c(1e-05, Up), i = i)$root, TRUE) trueTimes[i] <- if (!inherits(Root, "try-error")) Root else 150 } # we use fixed Type I right censoring denoting the end of the trial. Ctimes <- upp_Cens Time <- pmin(trueTimes, Ctimes) event <- as.numeric(trueTimes <= Ctimes) # event indicator # we keep the longitudinal measurements before the event times DF$Time <- Time[DF$id] DF$event <- event[DF$id] DF <- DF[DF$time <= DF$Time, ] The corresponding joint model is then fitted with the syntax: DF_id <- DF[!duplicated(DF$id), ]
Cox_fit <- coxph(Surv(Time, event) ~ sex, data = DF_id)
BetaBinom_MixMod <-
mixed_model(cbind(y, 20 - y) ~ sex * time, random = ~ time | id, data = DF,
family = beta.binomial())

jointFit <- jm(Cox_fit, BetaBinom_MixMod, time_var = "time")
summary(jointFit)
#>
#> Call:
#> jm(Surv_object = Cox_fit, Mixed_objects = BetaBinom_MixMod, time_var = "time")
#>
#> Data Descriptives:
#> Number of Groups: 500        Number of events: 395 (79%)
#> Number of Observations:
#>   cbind(y, 20 - y): 2837
#>
#>                  DIC     WAIC      LPML
#> marginal    11921.79 11872.37 -5953.717
#> conditional 13489.96 13290.88 -7441.949
#>
#> Random-effects covariance matrix:
#>
#>        StdDev   Corr
#> (Intr) 1.0079 (Intr)
#> time   0.7114 -0.0012
#>
#> Survival Outcome:
#>                           Mean  StDev   2.5%  97.5%      P   Rhat
#> sexfemale               0.5743 0.1981 0.1841 0.9532 0.0051 1.0056
#> value(cbind(y, 20 - y)) 0.9284 0.0679 0.8063 1.0751 0.0000 1.0862
#>
#> Longitudinal Outcome: cbind(y, 20 - y) (family = beta binomial, link = logit)
#>                   Mean  StDev    2.5%   97.5%      P   Rhat
#> (Intercept)    -2.1558 0.0945 -2.3417 -1.9730 0.0000 1.0072
#> sexfemale      -0.2692 0.1327 -0.5300 -0.0087 0.0413 1.0139
#> time            0.3292 0.0520  0.2280  0.4314 0.0000 1.0007
#> sexfemale:time -0.1801 0.0749 -0.3281 -0.0367 0.0167 1.0030
#> sigma           4.8225 0.2847  4.2791  5.3984 0.0000 1.0158
#>
#> MCMC summary:
#> chains: 3
#> iterations per chain: 3500
#> burn-in per chain: 500
#> thinning: 1
#> time: 50 sec