Nothing
## ----include=FALSE------------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 7,
fig.height = 4.5, dpi = 96)
library(freqTLS)
cache_path <- system.file(
"extdata", "canonical_bayesTLS_cache.rds", package = "freqTLS"
)
stopifnot(nzchar(cache_path), file.exists(cache_path))
bayes_cache <- readRDS(cache_path)
stopifnot(isTRUE(bayes_cache$meta$diagnostic_all_pass))
## ----provenance---------------------------------------------------------------
data.frame(
item = c(
"bayesTLS", "freqTLS cache-build source", "CmdStan", "R", "built (UTC)",
"OpenBLAS threads", "bounded cores"
),
recorded_value = c(
paste0(bayes_cache$meta$bayesTLS_version, " @ ",
substr(bayes_cache$meta$bayesTLS_git_sha, 1, 12)),
paste0("pre-release source @ ",
substr(bayes_cache$meta$freqTLS_git_sha, 1, 12)),
bayes_cache$meta$cmdstan_version,
bayes_cache$meta$R_version,
bayes_cache$meta$date_built_utc,
bayes_cache$meta$openblas_num_threads,
bayes_cache$meta$bounded_cores
),
check.names = FALSE
)
## ----bayes-diagnostics--------------------------------------------------------
bayes_cache$diagnostics[c(
"case_id", "rhat_max", "ess_bulk_min", "ess_tail_min", "divergences",
"treedepth_hits", "bfmi_min", "all_pass"
)]
## ----freq-fits, results='hide', message=FALSE, warning=FALSE------------------
data(zebrafish_o2)
zf <- droplevels(subset(
zebrafish_o2,
ploidy == "diploid" & oxygen %in% c("normoxia", "hyperoxia")
))
zf_fit <- fit_4pl(
standardize_data(
zf, temp = "temp", duration = "duration_min",
n_total = "n_total", n_surv = "n_surv", duration_unit = "minutes"
),
ctmax = ~ 0 + oxygen, z = ~ 0 + oxygen, low = ~ 0 + oxygen,
up = ~ 1, k = ~ 1, family = "beta_binomial", t_ref = 60,
method = "wald", quiet = TRUE
)
data(aphid_tdt)
aphid6 <- droplevels(subset(aphid_tdt, branch == "heat" & age == "6"))
aphid6_fit <- fit_4pl(
standardize_data(
aphid6, temp = "temp", duration = "duration_min",
n_total = "n_total", n_surv = "n_surv", duration_unit = "minutes"
),
ctmax = ~ 0 + species, z = ~ 0 + species, low = ~ 1, up = ~ 1,
k = ~ temp_c, family = "beta_binomial", t_ref = 60,
method = "wald", quiet = TRUE
)
aphid_all <- droplevels(subset(aphid_tdt, branch == "heat"))
aphid_all_std <- standardize_data(
aphid_all, temp = "temp", duration = "duration_min",
n_total = "n_total", n_surv = "n_surv", duration_unit = "minutes"
)
aphid_all_fit <- fit_4pl(
aphid_all_std,
ctmax = ~ 1 + species * age, z = ~ 1 + species * age,
low = ~ 1, up = ~ 1, k = ~ temp_c, family = "beta_binomial",
t_ref = 60, method = "wald", quiet = TRUE
)
data(snowgum_psii)
snowgum_fit <- fit_4pl(
standardize_data(
snowgum_psii, temp = "Temp", duration = "Time",
proportion = "fvfm_prop", duration_unit = "minutes"
),
ctmax = ~ 0 + recovery + (1 | plant), z = ~ 0 + recovery,
low = ~ 1, up = ~ 1, k = ~ 1, family = "beta", t_ref = 60,
method = "wald", quiet = TRUE
)
data(dsuzukii)
mort <- stats::aggregate(
cbind(
n_dead = as.integer(dsuzukii$dead),
n_total = rep.int(1L, nrow(dsuzukii))
) ~ temp + time + sex,
data = dsuzukii,
FUN = sum
)
mort$n_surv <- mort$n_total - mort$n_dead
mort_fit <- fit_4pl(
standardize_data(
mort, temp = "temp", duration = "time",
n_total = "n_total", n_surv = "n_surv", duration_unit = "minutes"
),
ctmax = ~ 0 + sex, z = ~ 0 + sex,
low = ~ temp_c, up = ~ temp_c, k = ~ temp_c,
family = "beta_binomial", t_ref = 240, method = "wald", quiet = TRUE
)
coma_cell <- interaction(
dsuzukii$temp, dsuzukii$lvl, dsuzukii$sex, drop = TRUE
)
coma <- do.call(rbind, lapply(split(dsuzukii, coma_cell), function(d) {
data.frame(
temp = d$temp[1], lvl = d$lvl[1], sex = d$sex[1],
duration = d$time[1], n_total = nrow(d),
n_awake = sum(is.na(d$t_coma))
)
}))
coma <- droplevels(subset(coma, duration > 0))
coma_fit <- fit_4pl(
standardize_data(
coma, temp = "temp", duration = "duration",
n_total = "n_total", n_surv = "n_awake", duration_unit = "minutes"
),
ctmax = ~ sex, z = ~ sex,
low = ~ temp_c, up = ~ temp_c, k = ~ temp_c,
family = "beta_binomial", t_ref = 60, method = "wald", quiet = TRUE
)
## ----freq-diagnostics---------------------------------------------------------
fit_list <- list(
zebrafish_oxygen = zf_fit,
aphid_age6 = aphid6_fit,
aphid_all_age = aphid_all_fit,
snowgum_psii = snowgum_fit,
drosophila_mortality = mort_fit,
drosophila_awake = coma_fit
)
freq_diagnostics <- do.call(rbind, lapply(names(fit_list), function(case_id) {
ans <- diagnose_tdt_fit(fit_list[[case_id]])
data.frame(
case_id = case_id,
converged = ans$converged,
pd_hessian = ans$pd_hessian,
max_abs_gradient = ans$max_abs_gradient,
gradient_pass = ans$gradient_pass,
interval_method = if (case_id == "aphid_all_age") {
"ML point prediction"
} else if (case_id == "drosophila_mortality") {
"Wald direct coordinates; attempted bootstrap for absolute CTmax"
} else {
"Wald 95% confidence interval"
}
)
}))
rownames(freq_diagnostics) <- NULL
freq_diagnostics
## ----paired-table-------------------------------------------------------------
as_freq_rows <- function(fit, case_id, by, endpoint, t_ref) {
out <- tls(fit, by = by, lethal = FALSE, method = "wald")$summary
data.frame(
case_id = case_id,
endpoint = endpoint,
threshold = "relative",
t_ref = t_ref,
group = as.character(out[[by]]),
parameter = out$quantity,
freq_estimate = out$median,
freq_lower = out$lower,
freq_upper = out$upper,
freq_interval = "Wald 95% confidence interval"
)
}
freq_rows <- rbind(
as_freq_rows(
zf_fit, "zebrafish_oxygen", "oxygen", "survival counts", 60
),
as_freq_rows(
aphid6_fit, "aphid_age6", "species", "survival counts", 60
),
as_freq_rows(
snowgum_fit, "snowgum_psii", "recovery",
"retained PSII function proportion", 60
),
as_freq_rows(
coma_fit, "drosophila_awake", "sex",
"awake counts (no recorded t_coma)", 60
)
)
bayes_rows <- bayes_cache$summaries
# Use the current reader-facing wording before joining the pinned cache to the
# freshly refitted rows. The cache's older "missing t_coma" label denotes the
# same awake/coma endpoint.
bayes_rows$endpoint[bayes_rows$case_id == "drosophila_awake"] <-
"awake counts (no recorded t_coma)"
bayes_rows$group <- ifelse(
!is.na(bayes_rows$oxygen), as.character(bayes_rows$oxygen),
ifelse(
!is.na(bayes_rows$recovery), as.character(bayes_rows$recovery),
ifelse(
!is.na(bayes_rows$sex), as.character(bayes_rows$sex),
as.character(bayes_rows$species)
)
)
)
paired <- merge(
freq_rows,
bayes_rows[c(
"case_id", "endpoint", "threshold", "t_ref", "group", "parameter",
"median", "lower", "upper", "interval_method"
)],
by = c("case_id", "endpoint", "threshold", "t_ref", "group", "parameter"),
all = FALSE,
sort = FALSE
)
names(paired)[names(paired) == "median"] <- "bayes_median"
names(paired)[names(paired) == "lower"] <- "bayes_lower"
names(paired)[names(paired) == "upper"] <- "bayes_upper"
names(paired)[names(paired) == "interval_method"] <- "bayes_interval"
paired$difference_freq_minus_bayes <- paired$freq_estimate - paired$bayes_median
paired$freq_width <- paired$freq_upper - paired$freq_lower
paired$bayes_width <- paired$bayes_upper - paired$bayes_lower
stopifnot(nrow(paired) == 18L)
paired_display <- paired[c(
"case_id", "endpoint", "threshold", "t_ref", "group", "parameter",
"freq_estimate", "freq_lower", "freq_upper", "bayes_median",
"bayes_lower", "bayes_upper", "freq_width", "bayes_width",
"difference_freq_minus_bayes"
)]
names(paired_display) <- c(
"Case", "Endpoint", "Threshold", "tref", "Group", "Quantity",
"ML estimate", "ML CI lower", "ML CI upper", "Bayes median",
"Bayes CrI lower", "Bayes CrI upper", "ML CI width", "Bayes CrI width",
"ML minus Bayes"
)
knitr::kable(paired_display, digits = 3,
caption = "Matched quantities: ML Wald confidence intervals and Bayesian credible intervals remain labelled separately.")
## ----aphid-all-age-differences------------------------------------------------
aphid_cells <- expand.grid(
species = levels(aphid_all$species),
age = levels(aphid_all$age),
temp = mean(aphid_all_std$temp),
KEEP.OUT.ATTRS = FALSE
)
aphid_pred <- predict(aphid_all_fit, aphid_cells, type = "parameters")
aphid_freq <- rbind(
data.frame(
species = aphid_cells$species, age = aphid_cells$age,
parameter = "CTmax", freq_estimate = aphid_pred$CTmax
),
data.frame(
species = aphid_cells$species, age = aphid_cells$age,
parameter = "z", freq_estimate = aphid_pred$z
)
)
aphid_bayes <- subset(bayes_rows, case_id == "aphid_all_age")
aphid_join <- merge(
aphid_freq,
aphid_bayes[c("species", "age", "parameter", "median")],
by = c("species", "age", "parameter")
)
aphid_join$difference_freq_minus_bayes <-
aphid_join$freq_estimate - aphid_join$median
stopifnot(nrow(aphid_join) == 18L)
aggregate(
abs(difference_freq_minus_bayes) ~ parameter,
data = aphid_join,
FUN = max
)
## ----mortality-absolute-------------------------------------------------------
mort_tdt <- extract_tdt(
mort_fit, target_surv = "absolute", nboot = 50, seed = 6203
)
mort_tdt$CTmax$summary
mort_tdt$meta$inversion_counts
mort_tdt$meta$interval_counts
mort_bayes <- subset(
bayes_rows,
case_id == "drosophila_mortality" & parameter == "CTmax"
)
mort_compare <- merge(
data.frame(
group = mort_tdt$CTmax$summary$sex,
freq_estimate = mort_tdt$CTmax$summary$temp_median,
freq_lower = mort_tdt$CTmax$summary$temp_lower,
freq_upper = mort_tdt$CTmax$summary$temp_upper
),
mort_bayes[c("group", "median", "lower", "upper")],
by = "group"
)
names(mort_compare)[names(mort_compare) == "median"] <- "bayes_median"
mort_compare$difference_freq_minus_bayes <-
mort_compare$freq_estimate - mort_compare$bayes_median
stopifnot(nrow(mort_compare) == 2L)
mort_compare
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.