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

## ----setup--------------------------------------------------------------------
library(predmicror)

## -----------------------------------------------------------------------------
predmicror_models("cardinal")

## -----------------------------------------------------------------------------
safe_fit <- function(expr) {
  withCallingHandlers(
    tryCatch(
      expr,
      error = function(e) {
        message("Model fit failed: ", conditionMessage(e))
        NULL
      }
    ),
    warning = function(w) {
      message("Model fit warning: ", conditionMessage(w))
      invokeRestart("muffleWarning")
    }
  )
}

## -----------------------------------------------------------------------------
data(salmonella)
head(salmonella)

## -----------------------------------------------------------------------------
temp_fit <- safe_fit(
  fit_cardinal(
    salmonella,
    model = "CMTI",
    x = "Temp",
    response = "sqrtGR",
    start = list(Tmax = 42, Tmin = 1, MUopt = 1, Topt = 37)
  )
)

temp_fit

## -----------------------------------------------------------------------------
if (!is.null(temp_fit)) {
  fit_metrics(temp_fit)
}

## ----fig.alt="Observed and fitted square-root growth rates for the cardinal temperature model."----
if (!is.null(temp_fit)) {
  grid <- data.frame(
    Temp = seq(min(salmonella$Temp), max(salmonella$Temp), length.out = 100)
  )
  pred <- predmicror_augment(temp_fit, newdata = grid)

  plot(
    sqrtGR ~ Temp,
    data = salmonella,
    xlab = "Temperature",
    ylab = "Square root growth rate",
    pch = 19
  )
  if (".fitted" %in% names(pred)) {
    lines(pred$Temp, pred[[".fitted"]], lwd = 2)
  }
}

## -----------------------------------------------------------------------------
data(ph)
head(ph)

## -----------------------------------------------------------------------------
ph_fit <- safe_fit(
  fit_cardinal(
    ph,
    model = "CMPH",
    x = "pH",
    response = "sqrtGR",
    start = list(pHmax = 9.5, pHmin = 3.5, MUopt = 1, pHopt = 6.8)
  )
)

ph_fit

## -----------------------------------------------------------------------------
if (!is.null(ph_fit)) {
  fit_metrics(ph_fit)
}

## ----fig.alt="Observed and fitted square-root growth rates for the cardinal pH model."----
if (!is.null(ph_fit)) {
  grid <- data.frame(pH = seq(min(ph$pH), max(ph$pH), length.out = 100))
  pred <- predmicror_augment(ph_fit, newdata = grid)

  plot(
    sqrtGR ~ pH,
    data = ph,
    xlab = "pH",
    ylab = "Square root growth rate",
    pch = 19
  )
  if (".fitted" %in% names(pred)) {
    lines(pred$pH, pred[[".fitted"]], lwd = 2)
  }
}

## -----------------------------------------------------------------------------
data(aw)
head(aw)

## -----------------------------------------------------------------------------
aw_fit <- safe_fit(
  fit_cardinal(
    aw,
    model = "CMAW",
    x = "aw",
    response = "sqrtGR",
    start = list(AWmin = 0.90, MUopt = 1, AWopt = 0.995)
  )
)

aw_fit

## -----------------------------------------------------------------------------
if (!is.null(aw_fit)) {
  fit_metrics(aw_fit)
}

## ----fig.alt="Observed and fitted square-root growth rates for the cardinal water activity model."----
if (!is.null(aw_fit)) {
  grid <- data.frame(aw = seq(min(aw$aw), max(aw$aw), length.out = 100))
  pred <- predmicror_augment(aw_fit, newdata = grid)

  plot(
    sqrtGR ~ aw,
    data = aw,
    xlab = "Water activity",
    ylab = "Square root growth rate",
    pch = 19
  )
  if (".fitted" %in% names(pred)) {
    lines(pred$aw, pred[[".fitted"]], lwd = 2)
  }
}

## -----------------------------------------------------------------------------
data(inh)
head(inh)

## -----------------------------------------------------------------------------
inh_fit <- safe_fit(
  fit_cardinal(
    inh,
    model = "CMInh",
    x = "Conce",
    response = "sqrtGR",
    start = list(MIC = max(inh$Conce) * 1.2, MUopt = 1, alpha = 1)
  )
)

inh_fit

## -----------------------------------------------------------------------------
if (!is.null(inh_fit)) {
  fit_metrics(inh_fit)
}

## ----fig.alt="Observed and fitted square-root growth rates for the cardinal inhibitor model."----
if (!is.null(inh_fit)) {
  grid <- data.frame(Conce = seq(min(inh$Conce), max(inh$Conce), length.out = 100))
  pred <- predmicror_augment(inh_fit, newdata = grid)

  plot(
    sqrtGR ~ Conce,
    data = inh,
    xlab = "Inhibitor concentration",
    ylab = "Square root growth rate",
    pch = 19
  )
  if (".fitted" %in% names(pred)) {
    lines(pred$Conce, pred[[".fitted"]], lwd = 2)
  }
}

## -----------------------------------------------------------------------------
cardinal_fits <- Filter(Negate(is.null), list(
  temperature = temp_fit,
  pH = ph_fit,
  water_activity = aw_fit,
  inhibitor = inh_fit
))

if (length(cardinal_fits) > 0L) {
  diagnostics <- do.call(
    rbind,
    Map(function(name, fit) {
      out <- fit_metrics(fit)
      out$workflow <- name
      out
    }, names(cardinal_fits), cardinal_fits)
  )
  rownames(diagnostics) <- NULL
  diagnostics
}

