inst/doc/case-study-suzukii.R

## ----include=FALSE------------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 7,
                      fig.height = 4.5, dpi = 96)

## ----setup--------------------------------------------------------------------
library(freqTLS)
data(dsuzukii)

## ----data---------------------------------------------------------------------
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_std <- standardize_data(
  mort, temp = "temp", duration = "time",
  n_total = "n_total", n_surv = "n_surv", duration_unit = "minutes"
)

## ----separate-----------------------------------------------------------------
sex_fits <- lapply(split(mort, mort$sex), function(d) {
  d_std <- standardize_data(
    d, temp = "temp", duration = "time",
    n_total = "n_total", n_surv = "n_surv", duration_unit = "minutes"
  )
  fit_4pl(
    d_std, ctmax = ~ 1, z = ~ 1,
    low = ~ temp_c, up = ~ temp_c, k = ~ temp_c,
    family = "beta_binomial", t_ref = 240,
    method = "wald", quiet = TRUE
  )
})
lapply(sex_fits, diagnose_tdt_fit)
separate_table <- do.call(rbind, lapply(names(sex_fits), function(sex_level) {
  ans <- tls(sex_fits[[sex_level]], lethal = FALSE, method = "wald")$summary
  ans$sex <- sex_level
  names(ans)[names(ans) == "median"] <- "estimate"
  ans[c("sex", "quantity", "estimate", "lower", "upper")]
}))
separate_table

## ----joint--------------------------------------------------------------------
mort_fit <- fit_4pl(
  mort_std,
  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
)
diagnose_tdt_fit(mort_fit)
check_tls(mort_fit)

## ----shape-identification-----------------------------------------------------
mort_shape <- subset(
  mort_fit$fit$estimates,
  grepl("^(low|up|k):", parameter),
  select = c(parameter, estimate, std.error)
)
mort_shape

## ----quantities---------------------------------------------------------------
mort_relative <- tls(mort_fit, by = "sex", lethal = FALSE, method = "wald")$summary
names(mort_relative)[names(mort_relative) == "median"] <- "estimate"
mort_relative

## ----absolute-----------------------------------------------------------------
mort_lt50 <- data.frame(
  sex = mort_fit$fit$group_levels,
  LT50_240_min = vapply(mort_fit$fit$group_levels, function(sex_level) {
    objective <- function(temp) {
      predict(
        mort_fit,
        data.frame(temp = temp, duration = 240, group = sex_level),
        type = "survival"
      ) - 0.5
    }
    uniroot(objective, range(mort$temp), tol = 1e-10)$root
  }, numeric(1)),
  interval_method = "not computed in this vignette"
)
mort_lt50

## ----fitted-curves, fig.alt="Observed survival proportions and fitted Drosophila suzukii mortality curves, separated by sex and assay temperature."----
plot_survival_curves(mort_fit)

Try the freqTLS package in your browser

Any scripts or data that you put into this service are public.

freqTLS documentation built on Dec. 4, 2026, 5:11 p.m.