aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKen Kellner <ken@kenkellner.com>2022-04-12 12:21:37 -0400
committerKen Kellner <ken@kenkellner.com>2022-04-12 12:21:37 -0400
commit0a98a378e8ef04f2b252fbf6f556429461e8862f (patch)
tree1b726def67a21dc82d10be8d8082787f5e13e878
parent2c46ba93e8839cf5c35e71167ee3e5a7257db28a (diff)
Don't allow kfold on spatial models
-rw-r--r--R/kfold.R3
-rw-r--r--R/spatial.R4
-rw-r--r--tests/testthat/test_spatial.R13
3 files changed, 18 insertions, 2 deletions
diff --git a/R/kfold.R b/R/kfold.R
index 9f54cd5..9402bdc 100644
--- a/R/kfold.R
+++ b/R/kfold.R
@@ -23,6 +23,9 @@ setMethod("kfold", "ubmsFit", function(x, K=10, folds=NULL, quiet=FALSE, ...){
stopifnot(length(folds) == unmarked::numSites(x@data))
stopifnot(max(folds) == K)
}
+ if(has_spatial(x)){
+ stop("kfold does not work with spatial models", call.=FALSE)
+ }
op <- pbapply::pboptions()
if(quiet) pbapply::pboptions(type = "none")
diff --git a/R/spatial.R b/R/spatial.R
index a77000c..5c76f06 100644
--- a/R/spatial.R
+++ b/R/spatial.R
@@ -140,6 +140,10 @@ setMethod("has_spatial", "ubmsSubmodel", function(object, ...){
methods::.hasSlot(object, "spatial")
})
+setMethod("has_spatial", "ubmsFit", function(object, ...){
+ any(sapply(object@submodels@submodels, has_spatial))
+})
+
setClass("ubmsSubmodelSpatial", contains = "ubmsSubmodel",
slots=c(data_aug="data.frame", sites_aug="logical",
spatial="formula"))
diff --git a/tests/testthat/test_spatial.R b/tests/testthat/test_spatial.R
index 07ffc91..e4a4140 100644
--- a/tests/testthat/test_spatial.R
+++ b/tests/testthat/test_spatial.R
@@ -126,6 +126,11 @@ test_that("has_spatial works on lists of formulas", {
expect_error(has_spatial(list(det=~1,state=~RSR(x,y,1)),support=FALSE))
})
+test_that("has_spatial works on ubmsFit objects",{
+ expect_true(has_spatial(fit))
+ expect_false(has_spatial(fit2))
+})
+
test_that("construction of ubmsSubmodelSpatial objects", {
ex <- extract_missing_sites(umf)
sm <- ubmsSubmodelSpatial("Test","test", ex$umf@siteCovs, ~1+RSR(x,y,1), "plogis",
@@ -201,6 +206,10 @@ test_that("plot_spatial returns ggplot", {
test_that("extract_log_lik method works",{
ll <- extract_log_lik(fit)
expect_is(ll, "matrix")
- expect_equal(dim(ll), c(200/2 * 2, numSites(fit@data)-7))
- expect_between(sum(ll), -7000, -6500)
+ expect_equal(dim(ll), c(200/2 * 2, numSites(fit@data)-7))
+ expect_between(sum(ll), -7000, -6500)
+})
+
+test_that("kfold errors when used on spatial model",{
+ expect_error(kfold(fit))
})