mvrem fits multivariate joint models with INLA: several longitudinal markers and one or more outcomes, each with its own family, fixed effects and random effects. An outcome is linked to a marker through a scaled copy of the marker’s linear predictor: the term scaled(x) in the outcome’s formula adds \gamma \, \eta_x to it, with \eta_x the marker’s fixed + random effects evaluated at the outcome’s covariates (its time, subject, …), and \gamma estimated. The marker and the outcome need not be measured at the same times.
The whole model is written in lme4 notation and fitted with one call, jsm(); INLA’s interface (stacked data, f() terms, copies) is built and hidden by the package.
INLA is not on CRAN; install it first, then mvrem from GitHub (htmlwidgets is optional and makes the plots interactive):
Simulated data, so the estimates can be compared with the truth: 100 subjects, a Gaussian marker x1 with a correlated random intercept and slope, measured at t = 0, 0.5, 1, 1.5, and a beta-distributed outcome y measured between these visits. The outcome depends on the marker’s current (error-free) value with \gamma = 0.8.
set.seed(1)
n <- 100
ids <- sprintf("S%03d", seq_len(n))
dX <- data.frame(id = rep(ids, each = 4), t = rep(c(0, 0.5, 1, 1.5), n)) # marker visits
dY <- data.frame(id = rep(ids, each = 4), t = rep(c(0.25, 0.75, 1.25, 1.75), n)) # outcome times
z0 <- rnorm(n); z1 <- 0.6 * z0 + sqrt(1 - 0.6^2) * rnorm(n)
b0 <- 0.5 * z0; b1 <- 0.2 * z1 # random intercept (SD 0.5), slope (SD 0.2), cor 0.6
eta <- function(d) { i <- match(d$id, ids); 1 + 0.5 * d$t + b0[i] + b1[i] * d$t }
dX$x1 <- eta(dX) + rnorm(nrow(dX), sd = 0.2) # marker
mu <- plogis(-1 + 0.8 * eta(dY)) # gamma = 0.8
dY$y <- rbeta(nrow(dY), 20 * mu, 20 * (1 - mu)) # outcome, phi = 20
dY$y[sample(nrow(dY), 60)] <- NA # missing outcomes are predicted
head(dY, 3)#> id t y
#> 1 S001 0.25 0.3630654
#> 2 S001 0.75 0.6428514
#> 3 S001 1.25 0.5363252
#> id t x1
#> 1 S001 0.0 0.7686535
#> 2 S001 0.5 1.1873312
#> 3 S001 1.0 1.3296577
All formulae go in one list. A formula with a scaled() term is an outcome, one without is a marker. Random effects are written as in lme4, (1 + t | id), or with re() to set their prior; here the Wishart prior of the block is centred on an lme4 fit of the marker (scale = "data"). The data are given per response; families default to Gaussian.
m <- jsm(
list(y ~ 1 + t + scaled(x1),
x1 ~ 1 + t + re(1 + t | id, prior = wishartPrec(scale = "data"))),
data = list(y = dY, x1 = dX),
family = list(y = betaFamily()),
criteria = TRUE # DIC, WAIC and CPO for modelFit()
)
summary(m)#> Call:
#> jsm(form = list(y ~ 1 + t + scaled(x1), x1 ~ 1 + t + re(1 + t | id, prior = wishartPrec(scale = "data"))),
#> data = list(y = dY, x1 = dX), family = list(y = betaFamily()), criteria = TRUE)
#>
#> Fixed effects (posterior):
#> response term mean sd q0.025 q0.5 q0.975
#> y (Intercept) -1.06 0.0773 -1.21 -1.06 -0.906
#> y t 0.0337 0.0537 -0.0722 0.0339 0.138
#> x1 (Intercept) 1.05 0.0495 0.955 1.05 1.15
#> x1 t 0.51 0.0263 0.458 0.51 0.561
#>
#> Associations (gamma, posterior):
#> outcome longitudinal mean sd q0.025 q0.5 q0.975
#> y x1 0.829 0.0506 0.73 0.828 0.929
#>
#> Hyperparameters (posterior; precisions):
#> response parameter mean sd q0.025 q0.5 q0.975
#> y phi 19 1.49 16.2 19 22.1
#> x1 residual precision 21.6 2.01 17.8 21.5 25.8
#> x1 precision of (Intercept) | id 4.87 0.771 3.51 4.81 6.54
#> x1 precision of t | id 34 9.52 19.3 32.7 56.5
#> x1 correlation of (Intercept), t | id 0.565 0.139 0.261 0.577 0.801
True values: \gamma = 0.8, \phi = 20; y intercept -1, no effect of t; x1 intercept 1, slope 0.5, SDs 0.5 and 0.2, correlation 0.6, residual SD 0.2.
estimatesTable() collects all estimates (fixed effects, associations, SDs and correlations, family parameters) in one table; format(..., "markdown") or "latex" writes it for a report.
| response | parameter | mean (sd) | 95% interval |
|---|---|---|---|
| y | (Intercept) | -1.1 (0.077) | [-1.2, -0.91] |
| t | 0.034 (0.054) | [-0.072, 0.14] | |
| gamma (x1 -> y) | 0.83 (0.051) | [0.73, 0.93] | |
| phi | 19 (1.5) | [16, 22] | |
| x1 | (Intercept) | 1.1 (0.049) | [0.95, 1.1] |
| t | 0.51 (0.026) | [0.46, 0.56] | |
| SD (Intercept) | id | 0.46 (0.036) | [0.39, 0.53] | |
| SD t | id | 0.18 (0.024) | [0.13, 0.23] | |
| corr (Intercept), t | id | 0.57 (0.14) | [0.26, 0.8] | |
| residual SD | 0.22 (0.01) | [0.2, 0.24] |
plot() shows the data of each equation, one line per subject, and after the fit the fitted values (open circles: predictions of the missing outcomes). With htmlwidgets installed the plot is interactive: hovering a subject highlights it in all panels, a click pins it.
Further accessors: fixedEffects(), associations(), hyperparameters(), randomEffects(), varianceComponents() (SDs and correlations rather than INLA’s precisions), fittedValues(), residuals() and modelFit():
#> Variance components (posterior summaries; scale = sd)
#>
#> block parameter effect effect2 mean sd q0.025 q0.5
#> x1 | id [(Intercept), t] sd (Intercept) <NA> 0.458 0.0360 0.392 0.456
#> x1 | id [(Intercept), t] sd t <NA> 0.176 0.0238 0.133 0.175
#> x1 | id [(Intercept), t] correlation (Intercept) t 0.565 0.1390 0.261 0.577
#> residual sd residual <NA> 0.216 0.0100 0.197 0.216
#> q0.975
#> 0.533
#> 0.227
#> 0.801
#> 0.237
#>
#> x1 | id [(Intercept), t]: covariance (from posterior mean SDs and correlations)
#> (Intercept) t
#> (Intercept) 0.2090 0.0456
#> t 0.0456 0.0311
#> response dic waic logScore cpoFailures mlik
#> 1 y -540.90840 -540.73173 -270.27493 0 NA
#> 2 x1 28.11844 31.41728 25.12763 0 NA
#> 3 overall -2977.68574 -3096.90491 -1138.71994 0 -1480.783
and plots of them: plotFixedEffects(), plotAssociations(), plotRandomEffects(), plotVarianceComponents(), plotResiduals().
predict() gives the posterior of the fitted mean at new rows: new subjects (random effects from their distribution) or new times of known subjects (their own random effects).
new <- data.frame(id = rep(c("S001", "S002", "new"), each = 5),
t = rep(seq(0, 2, by = 0.5), 3))
p <- predict(m, new)
head(p, 3)#> response id t mean sd q0.025 q0.5 q0.975 mode
#> 1 y S001 0.0 0.4089410 0.02655307 0.3575947 0.4086692 0.4618372 0.4081335
#> 2 y S001 0.5 0.4497080 0.02191445 0.4065643 0.4497600 0.4925639 0.4498755
#> 3 y S001 1.0 0.4912603 0.02261553 0.4464668 0.4914072 0.5352265 0.4917123
#> Predictions by refitting with INLA; chosen automatically: the fit has no INLA configurations.
latexEquation() writes the model as LaTeX; with annotate = "tooltip" hovering a symbol shows its estimate ("inline" writes the estimates under the symbols).
\begin{aligned} \htmlData{par=response x1 (longitudinal),item=observed,mean=1.43,lo=-0.164,hi=3.66,band=range,note=400 observations}{x_{1,i}} &= \htmlData{par=fixed effect of (Intercept) in x1,mean=1.05,sd=0.0495,lo=0.955,hi=1.15}{\beta^{(x_{1})}_{0}} + \htmlData{par=fixed effect of t in x1,mean=0.51,sd=0.0263,lo=0.458,hi=0.561}{\beta^{(x_{1})}_{1}}\, \htmlData{par=variable t,item=values,mean=0.875,lo=0,hi=1.75,band=range}{t_{i}} + \htmlData{par=random effect (Intercept) | id of x1,item=SD,mean=0.458,sd=0.036,lo=0.392,hi=0.533,note=one value per level: randomEffects()}{b^{(x_{1})}_{0,\mathrm{id}[i]}} + \htmlData{par=random effect t | id of x1,item=SD,mean=0.176,sd=0.0238,lo=0.133,hi=0.227,note=one value per level: randomEffects()}{b^{(x_{1})}_{1,\mathrm{id}[i]}}\, \htmlData{par=variable t,item=values,mean=0.875,lo=0,hi=1.75,band=range}{t_{i}} + \htmlData{par=residual of x1,item=SD,mean=0.216,sd=0.01,lo=0.197,hi=0.237}{\varepsilon^{(x_{1})}_{i}} \\ 0 &= \htmlData{par=fixed effect of (Intercept) in x1,mean=1.05,sd=0.0495,lo=0.955,hi=1.15}{\beta^{(x_{1})}_{0}} + \htmlData{par=fixed effect of t in x1,mean=0.51,sd=0.0263,lo=0.458,hi=0.561}{\beta^{(x_{1})}_{1}}\, \htmlData{par=variable t,item=values,mean=0.875,lo=0,hi=1.75,band=range}{t} + \htmlData{par=random effect (Intercept) | id of x1,item=SD,mean=0.458,sd=0.036,lo=0.392,hi=0.533,note=one value per level: randomEffects()}{b^{(x_{1})}_{0,\mathrm{id}}} + \htmlData{par=random effect t | id of x1,item=SD,mean=0.176,sd=0.0238,lo=0.133,hi=0.227,note=one value per level: randomEffects()}{b^{(x_{1})}_{1,\mathrm{id}}}\, \htmlData{par=variable t,item=values,mean=0.875,lo=0,hi=1.75,band=range}{t} - \htmlData{par=zero-equation field u (x1 -> y),note=linear predictor of x1 at the 400 combinations of (id t) in the data of y. Its precision is fixed}{u^{(x_{1} \to y)}_{\mathrm{id},t}}, \quad \left(\mathrm{id}, t\right) \in \htmlData{par=combinations of (id t) in the data of y,note=400 unique combinations}{\mathcal{D}_{y}} \\ \operatorname{logit}\left(\htmlData{par=fitted mean of y (one per observation: fittedValues()),item=posterior means,mean=0.562,lo=0.26,hi=0.89,band=range}{\mu^{(y)}_{i}}\right) &= \htmlData{par=fixed effect of (Intercept) in y,mean=-1.06,sd=0.0773,lo=-1.21,hi=-0.906}{\beta^{(y)}_{0}} + \htmlData{par=fixed effect of t in y,mean=0.0337,sd=0.0537,lo=-0.0722,hi=0.138}{\beta^{(y)}_{1}}\, \htmlData{par=variable t,item=values,mean=0.875,lo=0,hi=1.75,band=range}{t_{i}} + \htmlData{par=association gamma (x1 -> y),mean=0.829,sd=0.0506,lo=0.73,hi=0.929,note=normal prior on the coefficient with mean 0 and SD 10 (default)}{\gamma_{y,x_{1}}}\,\htmlData{par=zero-equation field u (x1 -> y),note=linear predictor of x1 at the 400 combinations of (id t) in the data of y. Its precision is fixed}{u^{(x_{1} \to y)}_{\mathrm{id}[i],t_{i}}} \\ \htmlData{par=residual of x1,item=SD,mean=0.216,sd=0.01,lo=0.197,hi=0.237}{\varepsilon^{(x_{1})}_{i}} &\sim \htmlData{par=normal distribution,note=parametrised by its precision tau (variance 1/tau) as in INLA}{\mathcal{N}}\left(0, \left(\htmlData{par=residual precision of x1,mean=21.6,sd=2.01,lo=17.8,hi=25.8,note=SD 0.216 (0.01). prior: INLA default}{\tau^{(x_{1})}_{\varepsilon}}\right)^{-1}\right) \\ \left(\htmlData{par=random effect (Intercept) | id of x1,item=SD,mean=0.458,sd=0.036,lo=0.392,hi=0.533,note=one value per level: randomEffects()}{b^{(x_{1})}_{0,l}}, \htmlData{par=random effect t | id of x1,item=SD,mean=0.176,sd=0.0238,lo=0.133,hi=0.227,note=one value per level: randomEffects()}{b^{(x_{1})}_{1,l}}\right)^\top &\sim \htmlData{par=normal distribution,note=parametrised by its precision tau (variance 1/tau) as in INLA}{\mathcal{N}}\left(\mathbf{0}, \left(\htmlData{par=covariance of the random effects | id of x1,item=SD (Intercept);SD t;corr (Intercept):t,mean=0.458;0.176;0.565,sd=0.036;0.0238;0.139,lo=0.392;0.133;0.261,hi=0.533;0.227;0.801,note=Wishart prior on the precision matrix with k + 2 degrees of freedom (E[Q] is df times the scale matrix - here the precision matrix of an lme4 fit of the formula divided by df)}{\mathbf{Q}^{(x_{1})}_{\mathrm{id}}}\right)^{-1}\right), \quad l = 1, \ldots, \htmlData{par=levels of id in the data of x1,note=100 levels}{n_{\mathrm{id}}} \\ \htmlData{par=covariance of the random effects | id of x1,item=SD (Intercept);SD t;corr (Intercept):t,mean=0.458;0.176;0.565,sd=0.036;0.0238;0.139,lo=0.392;0.133;0.261,hi=0.533;0.227;0.801,note=Wishart prior on the precision matrix with k + 2 degrees of freedom (E[Q] is df times the scale matrix - here the precision matrix of an lme4 fit of the formula divided by df)}{\mathbf{Q}^{(x_{1})}_{\mathrm{id}}} &\sim \htmlData{par=Wishart prior,note=on the precision matrix Q with the degrees of freedom and scale matrix V shown: E[Q] is df V}{\mathcal{W}_{2}}\left(4, \begin{pmatrix}1.71 & -2.63 \\ -2.63 & 13.1\end{pmatrix}\right) \\ \htmlData{par=response y (outcome),item=observed,mean=0.567,lo=0.143,hi=0.892,band=range,note=400 observations (60 missing)}{y_{i}} &\sim \htmlData{par=beta distribution,note=mean mu and precision phi: variance mu(1 - mu)/(1 + phi)}{\mathrm{Beta}}\left(\htmlData{par=fitted mean of y (one per observation: fittedValues()),item=posterior means,mean=0.562,lo=0.26,hi=0.89,band=range}{\mu^{(y)}_{i}}\, \htmlData{par=phi of y,mean=19,sd=1.49,lo=16.2,hi=22.1,note=prior: INLA default}{\phi^{(y)}}, \left(1 - \htmlData{par=fitted mean of y (one per observation: fittedValues()),item=posterior means,mean=0.562,lo=0.26,hi=0.89,band=range}{\mu^{(y)}_{i}}\right) \htmlData{par=phi of y,mean=19,sd=1.49,lo=16.2,hi=22.1,note=prior: INLA default}{\phi^{(y)}}\right) \\ \htmlData{par=zero-equation field u (x1 -> y),note=linear predictor of x1 at the 400 combinations of (id t) in the data of y. Its precision is fixed}{u^{(x_{1} \to y)}_{\mathrm{id},t}} &\sim \htmlData{par=normal distribution,note=parametrised by its precision tau (variance 1/tau) as in INLA}{\mathcal{N}}\left(0, \left(\htmlData{par=precision of u (x1 -> y),note=fixed at exp(-6) (not estimated)}{\tau_{x_{1} \to y}}\right)^{-1}\right) \end{aligned}
Each response has its own family; family inputs follow the response after |: trials(n) for binomial, exposure(pt) for Poisson, event(status) for survival and weights(w) for observation weights. An outcome may be linked to several markers, and arguments of scaled() set the prior of its coefficient.
set.seed(2)
n <- 120
d <- data.frame(id = sprintf("S%03d", rep(seq_len(n), each = 5)), t = rep(0:4 / 2, n))
u <- matrix(rnorm(n * 3), n)[as.integer(factor(d$id)), ]
etaX <- 1 + 0.5 * d$t + 0.5 * u[, 1] + 0.2 * u[, 2] * d$t # Gaussian marker x1
etaC <- 0.5 + 0.2 * d$t + 0.5 * u[, 3] # count marker (log rate)
d$x1 <- etaX + rnorm(nrow(d), sd = 0.3)
d$pt <- runif(nrow(d), 0.8, 1.2)
d$cnt <- rpois(nrow(d), d$pt * exp(etaC))
muY <- plogis(-1 + 0.3 * d$t + 0.6 * (etaX - 1) + 0.3 * (etaC - 0.5))
d$y <- rbeta(nrow(d), 30 * muY, 30 * (1 - muY))
d$n <- 10
d$k <- rbinom(nrow(d), d$n, pnorm(-0.3 + 0.5 * (etaX - 1)))m2 <- jsm(
list(y ~ 1 + t + scaled(x1, prior = normalCoef(0, 1)) + scaled(cnt),
k | trials(n) ~ 1 + t + scaled(x1),
x1 ~ 1 + t + re(1 + t | id, prior = wishartPrec(scale = "data")),
cnt | exposure(pt) ~ 1 + t + (1 | id)),
data = d,
family = list(y = betaFamily(), k = binomialFamily(link = "probit"), cnt = "poisson")
)
associations(m2)#> outcome longitudinal mean sd q0.025 q0.5 q0.975 mode
#> 1 y x1 0.5632205 0.03245912 0.4990896 0.5632992 0.6268929 0.5636261
#> 2 y cnt 0.2858039 0.05807257 0.1720809 0.2855972 0.4007334 0.2847364
#> 3 k x1 0.5101047 0.03130255 0.4489577 0.5099415 0.5722068 0.5092592
True values: \gamma_{y,x_1} = 0.6, \gamma_{y,\mathit{cnt}} = 0.3, \gamma_{k,x_1} = 0.5.
A time to event with right censoring is written time | event(status) ~ ... with weibullSurvFamily() or exponentialSurvFamily(), one row per subject. The marker enters the hazard at the covariate values of the survival row: t = 0 below, the subject’s marker level at baseline. The two parts have their own data frames.
set.seed(6)
n <- 200
b0 <- rnorm(n, sd = 0.5); b1 <- rnorm(n, sd = 0.25)
etaS <- function(i, t) 1 + 0.4 * t + b0[i] + b1[i] * t
T <- (-log(runif(n)) / exp(-2.5 + 0.8 * etaS(seq_len(n), 0)))^(1 / 1.5) # Weibull, alpha 1.5
C <- runif(n, 1, 4)
dSurv <- data.frame(id = sprintf("S%03d", seq_len(n)), time = pmin(T, C),
status = as.integer(T <= C), t = 0)
dMark <- data.frame(id = rep(dSurv$id, each = 5), t = rep(c(0, 0.5, 1, 1.5, 2), n))
dMark$x1 <- etaS(rep(seq_len(n), each = 5), dMark$t) + rnorm(nrow(dMark), sd = 0.2)
dMark <- dMark[dMark$t <= rep(dSurv$time, each = 5), ] # visits before the event
m3 <- jsm(
list(time | event(status) ~ 1 + scaled(x1),
x1 ~ 1 + t + (1 + t || id)),
dataOut = dSurv, dataLong = dMark,
family = list(time = weibullSurvFamily())
)
estimatesTable(m3)#> response type parameter mean (sd) 95% interval
#> time fixed (Intercept) -2.69 (0.304) [-3.3, -2.11]
#> association gamma (x1 -> time) 0.926 (0.226) [0.483, 1.37]
#> family alpha 1.48 (0.136) [1.22, 1.75]
#> x1 fixed (Intercept) 0.975 (0.0362) [0.904, 1.05]
#> fixed t 0.378 (0.0245) [0.33, 0.426]
#> sd SD (Intercept) | id 0.485 (0.0264) [0.435, 0.539]
#> sd SD t | id 0.274 (0.0201) [0.236, 0.315]
#> residual residual SD 0.197 (0.00692) [0.184, 0.211]
True values: \gamma = 0.8 (a log hazard ratio per unit of the marker), Weibull shape \alpha = 1.5.
| Function | Gives |
|---|---|
jsm(form, data, family) |
build and fit the model (fit = FALSE: build only) |
summary(), estimatesTable() |
all estimates, readable names |
fixedEffects(), associations(), hyperparameters(), coef() |
posterior summaries |
randomEffects(), varianceComponents() |
subject effects; SDs, correlations, covariance matrices |
fittedValues(), residuals(), modelFit() |
fitted means, residuals; DIC, WAIC, log score |
predict(m, newdata) |
posterior of the mean at new rows; plot() of the result |
plot(), plotResiduals(), plotRandomEffects(), plotVarianceComponents(), plotFixedEffects(), plotAssociations() |
data and fitted values; diagnostics; posteriors |
plotModel(), latexEquation() |
diagram and equations of the model |
update(), refit() |
re-run with changed arguments / INLA options |
inlaFormula(), inlaData(), inlaArguments(), inlaFit(), formatInlaFormula() |
what was given to INLA, and its result |
Families: gaussianFamily(), studentTFamily(), lognormalFamily(), gammaFamily(), betaFamily(), poissonFamily(), negBinomialFamily(), binomialFamily(), betaBinomialFamily(), zero-inflated Poisson and negative binomial, weibullSurvFamily(), exponentialSurvFamily() and inlaFamily() for any other INLA likelihood. Priors: gammaPrec(), pcSd(), halfNormalSd(), halfCauchySd() (also scaled from the data), wishartPrec() for correlated blocks and normalCoef() for associations. See their help pages for details.