# tests/testthat/test-dose_response.R make_dose_resp <- function(seed = 10) { set.seed(seed) doses <- rep(c(0, 10, 30, 100, 300, 1000, 3000), each = 4) resp <- 100 / (1 + exp(-(-1.8) * (log(pmax(doses, 0.01)) - log(80)))) + rnorm(length(doses), 0, 3) resp <- pmin(pmax(resp, 0), 100) data.frame(dose_g_ha = doses, weed_control_pct = resp) } dat_dr <- make_dose_resp() # ---- fit_dose_response ---------------------------------------------------- test_that("fit_dose_response LL.4 returns agriDRC", { skip_if_not_installed("drc") m <- fit_dose_response(dat_dr, "dose_g_ha", "weed_control_pct", fct = "LL.4") expect_s3_class(m, "agriDRC") expect_equal(m$fct, "LL.4") }) test_that("fit_dose_response LL.5 returns agriDRC", { skip_if_not_installed("drc") m <- fit_dose_response(dat_dr, "dose_g_ha", "weed_control_pct", fct = "LL.5") expect_s3_class(m, "agriDRC") }) test_that("fit_dose_response stores dose and resp column names", { skip_if_not_installed("drc") m <- fit_dose_response(dat_dr, "dose_g_ha", "weed_control_pct") expect_equal(m$dose_col, "dose_g_ha") expect_equal(m$resp_col, "weed_control_pct") }) test_that("fit_dose_response errors on missing column", { skip_if_not_installed("drc") expect_error(fit_dose_response(dat_dr, "missing_col", "weed_control_pct")) }) # ---- ed_estimates --------------------------------------------------------- test_that("ed_estimates returns matrix with ED50 row", { skip_if_not_installed("drc") m <- fit_dose_response(dat_dr, "dose_g_ha", "weed_control_pct") ed <- ed_estimates(m, respLev = c(50)) expect_true(is.matrix(ed) || is.data.frame(ed)) }) test_that("ed_estimates ED50 is in plausible range", { skip_if_not_installed("drc") m <- fit_dose_response(dat_dr, "dose_g_ha", "weed_control_pct") ed <- ed_estimates(m, respLev = 50, interval = "none") # True ED50 is ~80 g/ha; allow a wide margin for small simulated dataset expect_true(ed[1, 1] > 10 && ed[1, 1] < 500) }) # ---- print / summary ------------------------------------------------------ test_that("print.agriDRC outputs agriReg header", { skip_if_not_installed("drc") m <- fit_dose_response(dat_dr, "dose_g_ha", "weed_control_pct") expect_output(print(m), "agriReg") }) test_that("summary.agriDRC prints without error", { skip_if_not_installed("drc") m <- fit_dose_response(dat_dr, "dose_g_ha", "weed_control_pct") expect_output(summary(m), "Dose-Response") }) # ---- plot ----------------------------------------------------------------- test_that("plot.agriDRC returns ggplot", { skip_if_not_installed("drc") skip_if_not_installed("ggplot2") m <- fit_dose_response(dat_dr, "dose_g_ha", "weed_control_pct") p <- plot(m) expect_s3_class(p, "gg") }) test_that("plot.agriDRC linear scale returns ggplot", { skip_if_not_installed("drc") skip_if_not_installed("ggplot2") m <- fit_dose_response(dat_dr, "dose_g_ha", "weed_control_pct") p <- plot(m, log_dose = FALSE) expect_s3_class(p, "gg") })