Skip to content

Commit

Permalink
Fix bug in cAIC code #387
Browse files Browse the repository at this point in the history
And increase precision on unit test
  • Loading branch information
seananderson committed Dec 12, 2024
1 parent 173078d commit 66f7f73
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: sdmTMB
Title: Spatial and Spatiotemporal SPDE-Based GLMMs with 'TMB'
Version: 0.6.0.9016
Version: 0.6.0.9017
Authors@R: c(
person(c("Sean", "C."), "Anderson", , "[email protected]",
role = c("aut", "cre"),
Expand Down
6 changes: 3 additions & 3 deletions R/caic.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ cAIC.sdmTMB <- function(object, what = c("cAIC", "EDF"), ...) {
par <- obj$env$parList()
parDataMode <- obj$env$last.par
indx <- obj$env$lrandom()
q <- length(indx)
q <- sum(indx)
p <- length(object$model$par)

## use '-' for Hess because model returns negative loglikelihood
Expand All @@ -115,8 +115,8 @@ cAIC.sdmTMB <- function(object, what = c("cAIC", "EDF"), ...) {
if (what == "caic") {
jnll <- obj$env$f(parDataMode)
cnll <- jnll - obj_new$env$f(parDataMode)
cAIC <- 2 * cnll + 2 * (p + q) - 2 * sum(negEDF)
return(cAIC)
cAIC_out <- 2 * cnll + 2 * (p + q) - 2 * sum(negEDF)
return(cAIC_out)
} else if (what == "edf") {
## Figure out group for each random-effect coefficient
group <- factor(names(object$last.par.best[obj$env$random]))
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-cAIC.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ test_that("cAIC and EDF work", {
offset = log(dogfish$area_swept)
)
)
expect_equal(AIC(fit), 12192.9613, tolerance = 1e-1)
expect_equal(cAIC(fit), 12089.4289, tolerance = 1e-1)
expect_equal(AIC(fit), 12192.9613, tolerance = 1e-4)
expect_equal(cAIC(fit), 12071.4289, tolerance = 1e-4)
edf <- cAIC(fit, what = "EDF")
expect_equal(sum(edf), 54.3870, tolerance = 1e-2)
expect_equal(sum(edf), 54.3870623, tolerance = 1e-4)
})

0 comments on commit 66f7f73

Please sign in to comment.