test_that("nlmm function validates package and inputs", { skip_if_not_installed("nlme") dat <- data.frame( id = factor(rep(1:5, each = 4)), x = rep(1:4, 5), y = rnorm(20) ) expect_error( fit_nlmm( y ~ x, data = dat ), regexp = "nonlinear|random|start", ignore.case = TRUE ) }) test_that("nlmm validates model", { skip_if_not_installed("nlme") dat <- data.frame( id = factor(rep(1:5, each = 4)), x = rep(0:3, 5), y = rnorm(20) ) expect_error( fit_nlmm( model = "y ~ x", data = dat, fixed = x ~ 1, random = x ~ 1 | id ), "'model' must be a formula" ) }) test_that("nlmm validates data", { skip_if_not_installed("nlme") expect_error( fit_nlmm( model = y ~ x, data = list(y = 1:5, x = 1:5), fixed = x ~ 1, random = x ~ 1 | id ), "'data' must be a data frame" ) }) test_that("nlmm requires fixed effects", { skip_if_not_installed("nlme") dat <- data.frame( id = factor(rep(1:5, each = 4)), x = rep(0:3, 5), y = rnorm(20) ) expect_error( fit_nlmm( model = y ~ SSasymp(x, Asym, R0, lrc), data = dat, random = Asym ~ 1 | id ), "'fixed' must be supplied" ) }) test_that("nlmm requires random effects", { skip_if_not_installed("nlme") dat <- data.frame( id = factor(rep(1:5, each = 4)), x = rep(0:3, 5), y = rnorm(20) ) expect_error( fit_nlmm( model = y ~ SSasymp(x, Asym, R0, lrc), data = dat, fixed = Asym + R0 + lrc ~ 1 ), "'random' must be supplied" ) }) test_that("nlmm validates estimation method", { skip_if_not_installed("nlme") dat <- data.frame( id = factor(rep(1:5, each = 4)), x = rep(0:3, 5), y = rnorm(20) ) expect_error( fit_nlmm( model = y ~ x, data = dat, fixed = x ~ 1, random = x ~ 1 | id, method = "INVALID" ), "should be one of" ) }) test_that("fit_nlmm validates model formula", { skip_if_not_installed("nlme") dat <- data.frame( y = rnorm(10), x = rnorm(10), id = factor(rep(1:5, each = 2)) ) expect_error( fit_nlmm( model = "y ~ x", data = dat, fixed = x ~ 1, random = x ~ 1 | id ), "'model' must be a formula" ) }) test_that("fit_nlmm validates data", { skip_if_not_installed("nlme") expect_error( fit_nlmm( model = y ~ x, data = list(y = 1:5, x = 1:5), fixed = x ~ 1, random = x ~ 1 ), "'data' must be a data frame" ) }) test_that("fit_nlmm requires random effects", { skip_if_not_installed("nlme") dat <- data.frame( y = rnorm(10), x = rnorm(10) ) expect_error( fit_nlmm( model = y ~ x, data = dat, fixed = x ~ 1 ), "'random' must be supplied" ) }) test_that("fit_nlmm rejects invalid method", { skip_if_not_installed("nlme") dat <- data.frame( y = rnorm(10), x = rnorm(10), id = factor(rep(1:5, each = 2)) ) expect_error( fit_nlmm( model = y ~ x, data = dat, fixed = x ~ 1, random = x ~ 1 | id, method = "INVALID" ) ) })