library(ctsem)
library(ggplot2)
set.seed(49)
n_subjects <- 20
n_obs <- 80
raw_param_names <- c("t0m", "drift", "diffusion", "mmean")
raw_cor <- matrix(c(
1.00, 0.45, -0.25, 0.35,
0.45, 1.00, -0.55, 0.25,
-0.25, -0.55, 1.00, -0.20,
0.35, 0.25, -0.20, 1.00
), nrow = 4, byrow = TRUE,
dimnames = list(raw_param_names, raw_param_names))
raw_sd <- c(t0m = .7, drift = .55, diffusion = .35, mmean = 2.45)
raw_mean <- c(t0m = 0, drift = -.4, diffusion = log(exp(.45) - 1), mmean = 0)
raw_cov <- diag(raw_sd) %*% raw_cor %*% diag(raw_sd)
dimnames(raw_cov) <- dimnames(raw_cor)
# Each subject's raw parameters are drawn from this population by ctGenerate:
# RAWPOPVAR is the covariance of the raw (pre-transform) parameters, written
# from a covariance matrix by ctCov(), and values the population means on
# the natural scale.
gm <- ctModel(
silent = TRUE,
Tpoints = n_obs,
latentNames = "eta",
manifestNames = "Y",
LAMBDA = matrix(1),
T0MEANS = matrix("t0m|param|TRUE"),
DRIFT = matrix("drift|-log1p_exp(-param)|TRUE"),
DIFFUSION = matrix("diffusion|log1p_exp(param)|TRUE"),
CINT = matrix(0),
T0VAR = matrix(0),
MANIFESTMEANS = matrix("mmean|param|TRUE"),
MANIFESTVAR = matrix(.35)
)
gm$RAWPOPVAR <- ctCov(raw_cov)
softplus <- function(x) ifelse(x > 30, x, log1p(exp(x)))
generated <- ctGenerate(gm, n = n_subjects, dtmean = .1, backend = "julia",
values = c(t0m = raw_mean[["t0m"]], drift = -softplus(-raw_mean[["drift"]]),
diffusion = softplus(raw_mean[["diffusion"]]), mmean = raw_mean[["mmean"]]))
# The parameters each subject was generated with, on the natural scale, and
# back on the raw scale through the inverse transforms.
truth <- attr(generated, "subjectpars")[, c("id", raw_param_names)]
raw_truth <- data.frame(id = truth$id, t0m = truth$t0m,
drift = -log(expm1(-truth$drift)), diffusion = log(expm1(truth$diffusion)),
mmean = truth$mmean)
dat <- data.frame(generated)[, c("id", "time", "Y")]Empirical Bayes subject-wise fitting
Source:vignettes/empirical-bayes-fitting.qmd
empirical-bayes-fitting.RmdThis document demonstrates ctEmpiricalBayesFit() for a
simple univariate continuous time process. The data contain 20 subjects,
each with 80 equally spaced observations. The generating model varies
across subjects in:
-
T0MEANS: subject-specific initial latent level -
DRIFT: subject-specific autoregressive continuous time drift -
DIFFUSION: subject-specific process noise scale -
MANIFESTMEANS: subject-specific measurement intercept
Simulate data
ggplot(dat[dat$id <= 4, ], aes(time, Y, group = id, colour = factor(id))) +
geom_line(linewidth = .35) + theme_bw()+
labs(colour = "subject")
Specify the fitted model
The same model object is used for the empirical Bayes and random-effects workflows. Random effects are requested on the four generating parameters. The empirical Bayes function internally removes those random effects for the per-subject fits, because each subject fit contains only one subject.
fit_model <- ctModel(
type = "ct",
silent = TRUE,
latentNames = "eta",
manifestNames = "Y",
LAMBDA = matrix(1),
T0MEANS = matrix("t0m||TRUE", 1, 1),
DRIFT = matrix("drift||TRUE", 1, 1),
DIFFUSION = matrix("diffusion||TRUE", 1, 1),
MANIFESTMEANS = matrix("mmean||TRUE", 1, 1),
MANIFESTVAR = matrix("merror||FALSE", 1, 1)
)Empirical Bayes subject-wise fits
ctEmpiricalBayesFit() uses two subject-wise passes by
default. First, it fits the model once per subject using the model
prior. The raw estimates from these first-pass fits define the marginal
empirical Bayes prior. Second, it rewrites the single-subject model
transforms so raw normal(0, 1) parameters imply that
empirical location and scale, then fits every subject again. The final
$fits element contains the final EB-prior fits;
$initialfits contains the model-prior fits, and
$passfits contains every pass.
The transform adjustment is on the unconstrained raw scale. If the original model transforms a raw parameter to the model parameter as
then the EB-adjusted model uses a new local raw parameter and sets
where
and
are the empirical mean and standard deviation of the subject-level raw
estimates. For example, a positive parameter with
log1p_exp(param) becomes
log1p_exp(mu + sigma * param).
If Npasses > 2, each later pass maps the local EB raw
estimates back to the original raw scale before recomputing
and
.
This gives an iterated marginal EB prior, but it still does not encode
the empirical covariance among raw parameters.
By default, the EB prior construction is robust to extreme first-pass
raw estimates: raw values are winsorized using quantile and MAD bounds,
then the prior location and scale are computed with the median and MAD.
This protects the second pass from a single subject fit with a very
unusual optimum. Set ebRobust = FALSE, or adjust
ebOutlierQuantiles, ebOutlierMAD, and
ebWinsorize, if you want different behavior.
For optimized fits, the first pass uses
optimcontrol$estonly = TRUE, because only point estimates
are needed to build the EB prior. The stochastic optimizer is disabled
by default for both passes; pass
optimcontrol = list(stochastic = TRUE) if you want to
override this.
cores=2
eb_fit <- ctEmpiricalBayesFit(
datalong = dat,
model = fit_model,
priors = TRUE,
optimize = TRUE,
cores = cores,
Npasses = 2
)
eb_summary <- summary(eb_fit, use = "rawest", sdscale = "unit")
eb_summary$initialpopmeans
#> mean sd 2.5% 50% 97.5%
#> t0m -0.0956 0.7227 -1.4298 -0.0897 1.0709
#> drift -2.2049 1.4978 -5.2270 -1.9748 -0.2941
#> diffusion 0.5402 0.3394 0.0992 0.4454 1.1333
#> merror 0.3297 0.0492 0.2028 0.3401 0.3826
#> mmean -0.6641 2.8957 -5.0221 -0.5457 4.7611
eb_summary$outliers$initial
#> param lower upper nlower nupper nchanged action
#> 1 t0m -0.1430 0.1071 1 1 2 winsorized
#> 2 drift -1.2687 0.9213 1 1 2 winsorized
#> 3 diffusion -2.3137 -1.0648 1 1 2 winsorized
#> 4 merror -1.5959 -1.2659 1 1 2 winsorized
#> 5 mmean -0.5022 0.4761 1 1 2 winsorized
eb_summary$popmeans
#> mean sd 2.5% 50% 97.5%
#> t0m -0.0816 0.6160 -1.2641 -0.0913 0.9391
#> drift -1.8890 0.9448 -3.4822 -1.7352 -0.4851
#> diffusion 0.4704 0.2270 0.1628 0.4779 0.8663
#> merror 0.3405 0.0138 0.3143 0.3413 0.3589
#> mmean -0.6564 2.8787 -4.9768 -0.5477 4.7566
eb_summary$correlations$final
#> t0m drift diffusion merror mmean
#> t0m 1.0000 0.1465 -0.2513 0.0598 0.1936
#> drift 0.1465 1.0000 -0.1264 -0.3499 -0.3247
#> diffusion -0.2513 -0.1264 1.0000 -0.2983 -0.0719
#> merror 0.0598 -0.3499 -0.2983 1.0000 -0.0614
#> mmean 0.1936 -0.3247 -0.0719 -0.0614 1.0000The summary is on the transformed parameter scale, matching the scale
users usually inspect in summary(ctFit(...)). The EB
covariance is summarized for inspection, but it is not encoded in the
adjusted model. eb_fit$adjustedmodel is the
random-effect-free single-subject model used for the final pass, with
transforms based on marginal EB means and standard deviations only. The
full subject fits and raw estimate matrices remain on
eb_fit; summary(eb_fit) only returns compact
summaries.
eb_fit$adjustedmodel$pars[
eb_fit$adjustedmodel$pars$param %in% c("t0m", "drift", "diffusion", "mmean"),
c("matrix", "param", "transform", "indvarying", "sdscale")]
#> matrix param transform indvarying sdscale
#> 1 T0MEANS t0m 0 FALSE 1
#> 3 DRIFT drift 1 FALSE 1
#> 4 DIFFUSION diffusion 1 FALSE 1
#> 6 MANIFESTMEANS mmean 0 FALSE 1Standard random-effects fit
For comparison, fit the same model as a conventional hierarchical random-effects model. This fit is independent of the empirical Bayes fits.
re_fit <- ctFit(
datalong = dat,
model = fit_model,
priors = TRUE,
cores = cores
)Compare subject-level recovery
The empirical Bayes subject estimates below are separate per-subject estimates from the second pass, using the empirical prior learned from the first pass. The random-effects estimates are posterior subject parameters from the hierarchical fit.
extract_subject_point <- function(fit){
cp <- ctSummaryMatrices(fit)
c(
t0m = cp$T0MEANS["eta", 1],
drift = cp$DRIFT["eta", "eta"],
diffusion = cp$DIFFUSION["eta", "eta"],
mmean = cp$MANIFESTMEANS["Y", 1]
)
}
initial_subject <- do.call(rbind, lapply(eb_fit$initialfits,
extract_subject_point))
eb_subject <- do.call(rbind, lapply(eb_fit$fits, extract_subject_point))
re_subject <- ctSubjectPars(re_fit, pointest = TRUE)[1, ,
c("t0m", "drift", "diffusion", "mmean")]
truth_mat <- as.matrix(truth[, c("t0m", "drift", "diffusion", "mmean")])
recovery_summary <- function(est, truth){
data.frame(
param = colnames(truth),
correlation = diag(stats::cor(est, truth)),
rmse = sqrt(colMeans((est - truth)^2)),
estimate_sd = apply(est, 2, sd),
true_sd = apply(truth, 2, sd),
row.names = NULL
)
}Initial and final EB subject recovery
The first EB pass uses the model prior separately for each subject. The final EB pass uses the empirical marginal raw prior estimated from the preceding pass. This comparison isolates the effect of the EB prior update before adding the random-effects model to the comparison.
eb_pass_recovery <- rbind(
cbind(method = "Initial model-prior fits",
recovery_summary(initial_subject, truth_mat)),
cbind(method = "Final EB-prior fits",
recovery_summary(eb_subject, truth_mat))
)
knitr::kable(eb_pass_recovery, digits = 3)| method | param | correlation | rmse | estimate_sd | true_sd |
|---|---|---|---|---|---|
| Initial model-prior fits | t0m | 0.885 | 0.333 | 0.723 | 0.699 |
| Initial model-prior fits | drift | 0.087 | 1.897 | 1.498 | 0.389 |
| Initial model-prior fits | diffusion | 0.578 | 0.276 | 0.339 | 0.175 |
| Initial model-prior fits | mmean | 0.999 | 0.147 | 2.896 | 2.844 |
| Final EB-prior fits | t0m | 0.881 | 0.323 | 0.618 | 0.699 |
| Final EB-prior fits | drift | 0.127 | 1.296 | 0.951 | 0.389 |
| Final EB-prior fits | diffusion | 0.751 | 0.147 | 0.227 | 0.175 |
| Final EB-prior fits | mmean | 0.999 | 0.147 | 2.879 | 2.844 |
eb_pass_plot_data <- rbind(
data.frame(method = "Initial model-prior fits", id = truth$id,
param = rep(colnames(truth_mat), each = n_subjects),
true = as.vector(truth_mat),
estimate = as.vector(initial_subject)),
data.frame(method = "Final EB-prior fits", id = truth$id,
param = rep(colnames(truth_mat), each = n_subjects),
true = as.vector(truth_mat),
estimate = as.vector(eb_subject))
)
ggplot(eb_pass_plot_data, aes(true, estimate, colour = method)) +
geom_abline(slope = 1, intercept = 0, linewidth = .3) +
geom_point(alpha = .55, size = 1.4) +
facet_wrap(~ param, scales = "free") +
labs(x = "Generating value", y = "Estimated subject value", colour = NULL)
Correlation Recovery
There are two distinct correlation targets. The population raw
random-effects correlation is on the pre-transformation scale, so it
should be compared with the generating raw parameter correlations and
with raw estimates. Subject-level parameter correlations are on the
actual transformed parameter scale, so they should be compared with
correlations among the generated truth values.
lower_cor_table <- function(reference, estimates){
lower <- lower.tri(reference)
pair_index <- which(lower, arr.ind = TRUE)
out <- data.frame(
pair = paste(rownames(reference)[pair_index[, 1]],
colnames(reference)[pair_index[, 2]], sep = "__"),
truth = as.vector(reference[lower]),
check.names = FALSE)
for(nm in names(estimates)){
out[[nm]] <- as.vector(estimates[[nm]][lower])
}
out
}
true_raw_cor <- stats::cor(as.matrix(raw_truth[, raw_param_names]))
initial_eb_raw_cor <- stats::cor(eb_fit$initialraw[, raw_param_names])
final_eb_raw_cor <- stats::cor(
eb_fit$passoriginalraw[[length(eb_fit$passoriginalraw)]][, raw_param_names])
re_raw_names <- ctsem:::getparnames(re_fit, reonly = TRUE)
re_rawpopcorr <- re_fit$stanfit$transformedparsfull$rawpopcorr[1, , ]
dimnames(re_rawpopcorr) <- list(re_raw_names, re_raw_names)
re_rawpopcorr <- re_rawpopcorr[raw_param_names, raw_param_names]
raw_cor_recovery <- lower_cor_table(true_raw_cor, list(
"Initial EB raw estimates" = initial_eb_raw_cor,
"Final EB raw estimates" = final_eb_raw_cor,
"Random-effects population raw" = re_rawpopcorr
))
knitr::kable(raw_cor_recovery, digits = 3)| pair | truth | Initial EB raw estimates | Final EB raw estimates | Random-effects population raw |
|---|---|---|---|---|
| drift__t0m | 0.737 | 0.184 | 0.187 | 0.111 |
| diffusion__t0m | -0.411 | -0.309 | -0.260 | -0.187 |
| mmean__t0m | 0.339 | 0.149 | 0.194 | -0.094 |
| diffusion__drift | -0.549 | -0.468 | -0.178 | 0.570 |
| mmean__drift | 0.338 | -0.402 | -0.294 | 0.049 |
| mmean__diffusion | -0.241 | -0.008 | -0.128 | -0.018 |
true_subject_cor <- stats::cor(truth_mat)
initial_subject_cor <- stats::cor(initial_subject)
eb_subject_cor <- stats::cor(eb_subject)
re_subject_cor <- stats::cor(re_subject)
subject_cor_recovery <- lower_cor_table(true_subject_cor, list(
"Initial EB subject values" = initial_subject_cor,
"Final EB subject values" = eb_subject_cor,
"Random-effects subject values" = re_subject_cor
))
knitr::kable(subject_cor_recovery, digits = 3)| pair | truth | Initial EB subject values | Final EB subject values | Random-effects subject values |
|---|---|---|---|---|
| drift__t0m | 0.709 | 0.133 | 0.136 | 0.241 |
| diffusion__t0m | -0.456 | -0.237 | -0.245 | -0.341 |
| mmean__t0m | 0.339 | 0.149 | 0.195 | 0.111 |
| diffusion__drift | -0.587 | -0.481 | -0.153 | 0.482 |
| mmean__drift | 0.335 | -0.436 | -0.332 | -0.033 |
| mmean__diffusion | -0.256 | 0.148 | -0.070 | -0.412 |
Final EB and random-effects subject recovery
This comparison uses the final EB subject fits and the hierarchical random-effects subject estimates on the transformed parameter scale.
recovery <- rbind(
cbind(method = "EB subject fits", recovery_summary(eb_subject, truth_mat)),
cbind(method = "Random effects", recovery_summary(re_subject, truth_mat))
)
knitr::kable(recovery, digits = 3)| method | param | correlation | rmse | estimate_sd | true_sd |
|---|---|---|---|---|---|
| EB subject fits | t0m | 0.881 | 0.323 | 0.618 | 0.699 |
| EB subject fits | drift | 0.127 | 1.296 | 0.951 | 0.389 |
| EB subject fits | diffusion | 0.751 | 0.147 | 0.227 | 0.175 |
| EB subject fits | mmean | 0.999 | 0.147 | 2.879 | 2.844 |
| Random effects | t0m | 0.814 | 0.399 | 0.527 | 0.699 |
| Random effects | drift | 0.063 | 1.495 | 1.502 | 0.389 |
| Random effects | diffusion | 0.506 | 0.277 | 0.221 | 0.175 |
| Random effects | mmean | 0.995 | 0.279 | 2.913 | 2.844 |
Correlation and RMSE answer different questions. Correlation is mostly about whether subjects are ranked correctly, while RMSE also penalizes bias and miscalibrated spread. The EB fits can therefore show better correlation if the empirical prior stabilizes noisy individual estimates, but worse RMSE if the subject estimates are shifted or overshrunk relative to the generating values. The random-effects fit can have lower RMSE because it estimates the population distribution and covariance jointly with the subject deviations.
plot_data <- rbind(
data.frame(method = "EB subject fits", id = truth$id,
param = rep(colnames(truth_mat), each = n_subjects),
true = as.vector(truth_mat),
estimate = as.vector(eb_subject)),
data.frame(method = "Random effects", id = truth$id,
param = rep(colnames(truth_mat), each = n_subjects),
true = as.vector(truth_mat),
estimate = as.vector(re_subject))
)
ggplot(plot_data, aes(true, estimate, colour = method)) +
geom_abline(slope = 1, intercept = 0, linewidth = .3) +
geom_point(alpha = .55, size = 1.4) +
facet_wrap(~ param, scales = "free") +
labs(x = "Generating value", y = "Estimated subject value", colour = NULL)