## ----message=FALSE, warning=FALSE---------------------------------------------
library(ec50estimator)
library(drc)
library(ggplot2)

## -----------------------------------------------------------------------------
data(multi_isolate)

example_data <- subset(
  multi_isolate,
  isolate %in% 1:6 & fungicida == "Fungicide A"
)

head(example_data)

## -----------------------------------------------------------------------------
check_ec50_data(
  example_data,
  response = "growth",
  dose = "dose",
  isolate = "isolate",
  strata = "field"
)

## -----------------------------------------------------------------------------
fit <- estimate_EC50(
  growth ~ dose,
  data = example_data,
  isolate_col = "isolate",
  strata_col = "field",
  fct = drc::LL.3(),
  interval = "delta"
)

head(fit)

## -----------------------------------------------------------------------------
ec50_estimates(fit)

## -----------------------------------------------------------------------------
ec50_metadata(fit)

fit_quality(fit)

fit_failures(fit)

## -----------------------------------------------------------------------------
models <- fitted_models(fit)

length(models)
names(models)[1:3]

## ----fig.alt="Faceted dose-response plot with raw observations and one fitted EC50 curve for each isolate and field."----
plot_EC50_curves(fit)

## -----------------------------------------------------------------------------
predict_ec50(
  fit,
  dose = c(0.001, 0.01, 0.1)
)

## -----------------------------------------------------------------------------
report_ec50(fit)

## -----------------------------------------------------------------------------
curves <- curve_data(fit)

head(curves)

## ----fig.alt="Custom EC50 estimate plot showing EC50 estimates and confidence intervals by isolate and field."----
estimates <- ec50_estimates(fit)
estimates$ID <- factor(estimates$ID, levels = sort(unique(estimates$ID)))

ggplot(estimates, aes(ID, Estimate, color = field)) +
  geom_point(size = 2) +
  geom_errorbar(aes(ymin = Lower, ymax = Upper), width = 0.15) +
  scale_y_log10() +
  labs(x = "Isolate", y = "EC50", color = "Field") +
  theme_light()

## -----------------------------------------------------------------------------
head(residual_data(fit))

## ----fig.alt="Residual diagnostic plot with residuals against fitted values for EC50 models."----
plot_residuals(fit)

