test_that("fit_gamm validates formula", { skip_if_not_installed("mgcv") dat <- data.frame( y = rnorm(20), x = rnorm(20), id = factor(rep(1:5, each = 4)) ) expect_error( fit_gamm( "y ~ s(x)", data = dat, random = list(id = ~1) ) ) }) test_that("fit_gamm validates data", { skip_if_not_installed("mgcv") expect_error( fit_gamm( y ~ s(x), data = list( y = rnorm(20), x = rnorm(20) ), random = list(id = ~1) ) ) }) test_that("GAMM fits without random effects", { skip_if_not_installed("mgcv") set.seed(123) dat <- data.frame( x = seq(0, 10, length.out = 50) ) dat$y <- sin(dat$x) + rnorm(50, 0, 0.1) m <- fit_gamm( formula = y ~ s(x, k = 5), data = dat ) expect_s3_class(m, "biomix_gamm") expect_s3_class(m, "biomix") expect_s3_class(m$fit, "gam") expect_null(m$random) expect_equal( m$model_type, "Generalized additive mixed model" ) }) test_that("GAMM handles supplied random argument", { skip_if_not_installed("mgcv") dat <- data.frame( id = factor(rep(1:5, each = 10)), x = rep(seq(0, 1, length.out = 10), 5) ) dat$y <- sin(dat$x) + rnorm(50, 0, 0.1) expect_error( fit_gamm( formula = y ~ s(x, k = 5), data = dat, random = ~ 1 | id ) ) }) test_that("GAMM gives helpful error for excessive basis dimension", { skip_if_not_installed("mgcv") dat <- data.frame( x = c(1, 2, 3, 1, 2, 3, 1, 2, 3), y = rnorm(9) ) expect_error( fit_gamm( formula = y ~ s(x, k = 10), data = dat ), paste0( "The GAM smooth has too many basis dimensions for the ", "number of unique covariate combinations" ) ) }) test_that("GAMM validates formula", { skip_if_not_installed("mgcv") dat <- data.frame( x = rnorm(10), y = rnorm(10) ) expect_error( fit_gamm( formula = "y ~ x", data = dat ), "formula must be a formula" ) }) test_that("GAMM validates data", { skip_if_not_installed("mgcv") expect_error( fit_gamm( formula = y ~ x, data = list( x = rnorm(10), y = rnorm(10) ) ), "data must be a data frame" ) }) test_that("GAMM stores random-effects specification", { skip_if_not_installed("mgcv") dat <- data.frame( id = factor(rep(1:5, each = 10)), x = rep(seq(0, 1, length.out = 10), 5) ) dat$y <- sin(dat$x) + rnorm(50, 0, 0.1) random_spec <- list( id = ~ 1 ) # This test should be used after changing the implementation # to use mgcv::gamm() for random effects. m <- fit_gamm( formula = y ~ s(x, k = 5), data = dat, random = random_spec ) expect_equal(m$random, random_spec) })