## ----message=FALSE, warning=FALSE---------------------------------------------
library(ec50estimator)
library(drc)
library(ggplot2)

## -----------------------------------------------------------------------------
data(multi_isolate)

example_data <- subset(
  multi_isolate,
  isolate %in% 1:5 & fungicida == "Fungicide A"
)

head(example_data)

## ----fig.alt="Jittered raw dose-response observations for five isolates, faceted by field system."----
raw_plot_data <- subset(example_data, dose > 0)

ggplot(raw_plot_data, aes(dose, growth, color = factor(isolate))) +
  geom_jitter(width = 0.08, height = 0, alpha = 0.85, size = 2) +
  facet_wrap(~field) +
  scale_x_log10() +
  labs(x = "Dose", y = "Growth", color = "Isolate") +
  theme_light()

## -----------------------------------------------------------------------------
check_ec50_data(
  example_data,
  response = "growth",
  dose = "dose",
  isolate = "isolate",
  strata = "field"
)

## -----------------------------------------------------------------------------
fit <- ec50_multimodel(
  growth ~ dose,
  data = example_data,
  isolate_col = "isolate",
  strata_col = "field",
  fct = list(drc::LL.3(), drc::LL.4(), drc::W2.3()),
  interval = "delta"
)

head(ec50_estimates(fit))

## -----------------------------------------------------------------------------
fit_quality(fit)

fit_failures(fit)

## -----------------------------------------------------------------------------
selection <- model_selection(fit)

selection[, c("ID", "field", "model", "IC", "delta", "weight", "rank")]

## -----------------------------------------------------------------------------
best <- best_model(fit)

best[, c("ID", "field", "model", "Estimate", "Lower", "Upper", "IC")]

## ----fig.alt="Faceted dose-response plot with raw observations and all candidate fitted curves for each isolate and field."----
plot_EC50_curves(fit)

## ----fig.alt="Faceted dose-response plot with raw observations and the best-supported fitted curve for each isolate and field."----
plot_EC50_curves(fit, models = "best")

## -----------------------------------------------------------------------------
curves <- curve_data(fit)

head(curves)

## ----fig.alt="Custom ggplot2 curve plot built from fitted EC50 curve coordinates."----
ggplot(curves, aes(dose, growth, color = factor(isolate), linetype = model)) +
  geom_line(linewidth = 1) +
  facet_wrap(~field) +
  scale_x_log10() +
  labs(x = "Dose", y = "Growth", color = "Isolate") +
  theme_light()

## -----------------------------------------------------------------------------
predict_ec50(
  fit,
  dose = c(0.001, 0.01, 0.1),
  models = "best"
)

## -----------------------------------------------------------------------------
report_ec50(fit, models = "best")

## -----------------------------------------------------------------------------
head(residual_data(fit, models = "best"))

## ----fig.alt="Residual diagnostic plot for best-supported EC50 models."-------
plot_residuals(fit, models = "best")

