# MinutePlot validation -- Dose-response regression, four-parameter log-logistic (generated 4PL data)
# Install once (skip on later runs):
#   install.packages(c("drc"))   # exactly what THIS script uses

# Load packages (every session):
library(drc)

# Versions (compare against those stated on this page):
cat(R.version.string, "\n")
for (p in c("drc")) cat(p, as.character(packageVersion(p)), "\n")

# Load data:
d <- read.csv("doseresponse_4pl.csv")   # same folder as script (dose, response; 7 doses x 3 replicates, fixed seed)

# Analysis (one print per table shown on the page, in page order):
fit <- drm(response ~ dose, data = d, fct = LL.4(names = c("Slope", "Lower", "Upper", "ED50")))
print(summary(fit))                                     # parameter estimates and SE
print(ED(fit, 50, interval = "delta"))                  # ED50 with CI
cat("Residual SE:", sqrt(sum(residuals(fit)^2) / df.residual(fit)), "\n")

# ryegrass (drc's built-in; the CSV is the same data): all 24 observations, and the 18 with conc > 0 -- MinutePlot fits the latter
data(ryegrass)
fit_rg <- drm(rootl ~ conc, data = ryegrass, fct = LL.4(names = c("Slope", "Lower", "Upper", "ED50")))
print(summary(fit_rg)); print(ED(fit_rg, 50, interval = "delta"))
fit_rg0 <- drm(rootl ~ conc, data = subset(ryegrass, conc > 0), fct = LL.4(names = c("Slope", "Lower", "Upper", "ED50")))
print(summary(fit_rg0)); print(ED(fit_rg0, 50, interval = "delta"))     # the like-for-like run for MinutePlot's dosed-only fit
