Nothing
## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.width = 6,
fig.height = 3.6,
dpi = 96
)
## ----setup--------------------------------------------------------------------
library(freqTLS)
## ----crossed-data-------------------------------------------------------------
set.seed(81)
crossed <- expand.grid(
plant = factor(sprintf("P%02d", 1:12)),
meas_day = factor(sprintf("D%02d", 1:8)),
temp = c(33, 35, 37, 39),
duration = c(30, 60, 120, 240),
KEEP.OUT.ATTRS = FALSE
)
crossed$ctmax_i <- 36 + rnorm(12, 0, 0.8)[crossed$plant] +
rnorm(8, 0, 0.35)[crossed$meas_day]
mid <- log10(60) - (crossed$temp - crossed$ctmax_i) / 4
crossed$prob <- plogis(-5 * (log10(crossed$duration) - mid))
crossed$total <- 12L
crossed$survived <- rbinom(nrow(crossed), crossed$total, crossed$prob)
crossed_fit <- fit_tls(
tls_bf(survived | trials(total) ~ time(duration) + temp(temp),
CTmax ~ 1 + (1 | plant) + (1 | meas_day)),
data = crossed, family = "binomial", tref = 60, quiet = TRUE
)
crossed_fit$estimates[
crossed_fit$estimates$parameter %in% c("CTmax", "sigma_CTmax:plant",
"sigma_CTmax:meas_day"),
c("parameter", "estimate", "std.error")
]
## ----crossed-prediction-------------------------------------------------------
new_crossed <- data.frame(
temp = 36, duration = 60,
plant = as.character(crossed$plant[1]),
meas_day = as.character(crossed$meas_day[1])
)
data.frame(
prediction = c("population", "plant and day conditional"),
survival = c(
predict(crossed_fit, new_crossed, type = "survival", re.form = "population"),
predict(crossed_fit, new_crossed, type = "survival", re.form = "conditional")
)
)
## ----ctmax-re-----------------------------------------------------------------
set.seed(1)
d <- simulate_tls(family = "binomial", CTmax = 36, z = 4,
re_sd = 1.5, n_re_groups = 14, seed = 1)
fit <- fit_tls(
tls_bf(survived | trials(total) ~ time(duration) + temp(temp),
CTmax ~ 1 + (1 | colony)),
data = d, family = "binomial", tref = 1, quiet = TRUE)
# The estimates table carries the population CTmax / z and the between-colony SD.
est <- fit$estimates
est[est$parameter %in% c("CTmax", "z", "sigma_CTmax"),
c("parameter", "estimate", "std.error")]
## ----re-recovery, echo = FALSE------------------------------------------------
rr_path <- system.file("extdata", "re_recovery_results.rds", package = "freqTLS")
if (nzchar(rr_path) && file.exists(rr_path)) {
rr <- readRDS(rr_path)
knitr::kable(
rr$re_recovery[, c("n_groups", "mean_sigma", "rel_bias", "CTmax_wald_coverage")],
col.names = c("n groups", "mean sigma_CTmax", "rel. bias", "CTmax 95% coverage"),
caption = sprintf(
paste("Random-intercept recovery vs number of groups (true sigma_CTmax = %.1f;",
"%d sims/cell). The ML SD and fixed-effect CTmax Wald coverage are",
"shown for this simulation design."),
rr$meta$truth_sigma, rr$meta$nsim),
digits = 3)
}
## ----ranef--------------------------------------------------------------------
head(ranef(fit), 4)
## ----re-predict---------------------------------------------------------------
colony_1 <- as.character(ranef(fit)$group[1])
new_colony <- data.frame(temp = 36, duration = 2, colony = colony_1)
data.frame(
target = c("population", "colony"),
survival = c(
predict(fit, new_colony, re.form = "population"),
predict(fit, new_colony, re.form = "conditional")
)
)
## ----ctmax-ci-----------------------------------------------------------------
suppressMessages(confint(fit, "sigma_CTmax", method = "wald"))[
, c("parameter", "conf.low", "conf.high")]
suppressMessages(confint(fit, "CTmax", method = "profile", npoints = 8))[
, c("parameter", "conf.low", "conf.high", "method")]
## ----eye, fig.alt = "Confidence Eye for the population CTmax of a random-intercept fit: a pale outlined confidence lens with a dark centre mark and hollow estimate near 36 degrees Celsius, drawn with a Wald interval; a freqTLS uncertainty display, not a posterior density."----
suppressMessages(plot_confidence_eye(fit, parm = "CTmax"))
## ----logz-re------------------------------------------------------------------
set.seed(2)
dz <- simulate_tls(family = "binomial", CTmax = 36, z = 4,
times = c(30, 60, 120, 240, 480), tref = 60,
re_sd_z = 0.3, n_re_groups = 14, seed = 2)
fit_z <- fit_tls(
tls_bf(survived | trials(total) ~ time(duration) + temp(temp),
log_z ~ 1 + (1 | colony)),
data = dz, family = "binomial", tref = 60, quiet = TRUE)
sg <- fit_z$estimates$estimate[fit_z$estimates$parameter == "sigma_logz"]
c(sigma_logz = round(sg, 3),
approx_fold_spread_in_z = round(exp(sg), 3))
## ----combined-----------------------------------------------------------------
set.seed(3)
db <- simulate_tls(family = "binomial", CTmax = 36, z = 4,
times = c(30, 60, 120, 240, 480), tref = 60,
re_sd = 1.2, re_sd_z = 0.3, n_re_groups = 14, seed = 3)
# Both random intercepts on the same `colony` grouping. This fits, but emits a
# warning that the two variances are independent (no correlation term); we pass
# `quiet = TRUE` to silence it here and discuss it below.
fit_both <- fit_tls(
tls_bf(survived | trials(total) ~ time(duration) + temp(temp),
CTmax ~ 1 + (1 | colony), log_z ~ 1 + (1 | colony)),
data = db, family = "binomial", tref = 60, quiet = TRUE)
fit_both$estimates$parameter[grepl("^sigma", fit_both$estimates$parameter)]
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.