inst/doc/model-math.R

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

## ----setup--------------------------------------------------------------------
library(freqTLS)

## ----alternative-curve-example------------------------------------------------
d_ln <- simulate_tls(
  family = "binomial", curve = "lognormal", temps = seq(32, 40, by = 2),
  times = c(15, 30, 60, 120, 240), reps = 4, n = 80,
  CTmax = 36, z = 4, tref = 60, seed = 2
)
fit_ln <- fit_lognormal(
  d_ln, y = survived, n = total, time = duration, temp = temp,
  family = "binomial", tref = 60
)
fit_ln$curve

## ----ctmax-z-properties-------------------------------------------------------
set.seed(1)
dat <- simulate_tls(
  family = "binomial", CTmax = 36, z = 4,
  times = c(30, 60, 120, 240, 480), tref = 60, seed = 1
)
fit <- fit_tls(dat, y = survived, n = total, time = duration, temp = temp,
               family = "binomial", tref = 60)
ct <- get_ctmax(fit)$estimate
zz <- get_z(fit)$estimate

# (1) midpoint at temp = CTmax equals log10(tref) = log10(60)
predict(fit, data.frame(temp = ct, duration = 60), type = "midpoint")

# (2) midpoint slope in temperature is -1/z
mids <- predict(fit, data.frame(temp = c(ct, ct + 1), duration = 60), type = "midpoint")
c(slope = diff(mids), minus_one_over_z = -1 / zz)

## ----derive-lt----------------------------------------------------------------
# Absolute 50% survival at three temperatures, in minutes
derive_lt(fit, p = 0.5, temp = c(35, 36, 37))

## ----bridge-check-------------------------------------------------------------
Tbar <- mean(c(35, 37))               # any centring temperature works
mids <- predict(fit, data.frame(temp = c(35, 37), duration = 60), type = "midpoint")
beta1 <- diff(mids) / 2               # slope in T  (= -1/z)
beta0 <- mids[1] - beta1 * (35 - Tbar) # intercept at T = Tbar

z_recovered     <- -1 / beta1
ctmax_recovered <- Tbar + (log10(fit$tref) - beta0) / beta1

stopifnot(
  isTRUE(all.equal(unname(ctmax_recovered), unname(ct), tolerance = 1e-10)),
  isTRUE(all.equal(unname(z_recovered), unname(zz), tolerance = 1e-10))
)

rbind(
  fitted    = c(CTmax = ct, z = zz),
  recovered = c(CTmax = ctmax_recovered, z = z_recovered)
)

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.