Skip to content

Commit

Permalink
Add power analysis tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kenkellner committed Jul 10, 2024
1 parent d85700d commit 5dd3fb7
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 3 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: unmarked
Version: 1.4.1.9004
Date: 2024-07-09
Version: 1.4.1.9005
Date: 2024-07-10
Type: Package
Title: Models for Data from Unmarked Animals
Authors@R: c(
Expand Down
1 change: 0 additions & 1 deletion R/power.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ powerAnalysis_internal <- function(object, model, data_sims,
fun <- get_fitting_function(object, model)
test_fit <- get_fit(data_sims[[1]], model, ...)
modname <- test_fit@fitType
modname <- "test"

if(is.null(nulls)){
nulls <- effects
Expand Down
81 changes: 81 additions & 0 deletions tests/testthat/test_powerAnalysis.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
context("powerAnalysis method")
skip_on_cran()

temp <- unmarkedFrameOccu(y=matrix(NA, 300, 8),
siteCovs=data.frame(elev=rnorm(300),
group=factor(sample(letters[1:20], 300, replace=TRUE))))

test_that("powerAnalysis method works",{

# When no effect sizes provided
nul <- capture.output(expect_error(powerAnalysis(temp, model=occu, formula=~1~elev)))

ef <- list(state=c(intercept=0, elev=-0.4), det=c(intercept=0))

nul <- capture_output({

set.seed(123)
pa <- powerAnalysis(temp, model=occu, formula=~1~elev, effects=ef, nsim=10)
expect_is(pa, "unmarkedPower")
s <- summary(pa)$Power
expect_true(s>0.5)

# output printout
out <- capture.output(pa)
expect_equal(out[7], "Power Statistics:")

# set null
set.seed(123)
nul <- list(state=c(intercept=5, elev=0), det=c(intercept=0))
pa2 <- powerAnalysis(temp, model=occu, formula=~1~elev, effects=ef, nulls=nul, nsim=10)
s <- summary(pa2, showIntercepts=TRUE)
expect_equivalent(s$Null, c(5,0,0))

# list
pl <- unmarkedPowerList(pa, pa2)
expect_is(pl, "unmarkedPowerList")
s <- summary(pl)
expect_is(s, "data.frame")

pdf(NULL)
pl_plot <- plot(pl)
expect_is(pl_plot,"list")
dev.off()

# With random effect
set.seed(123)
ef <- list(state=c(0, -0.4, 1), det=0)
pa3 <- powerAnalysis(temp, model=occu, formula=~1~elev+(1|group), effects=ef, nsim=10)
s <- summary(pa3, showIntercepts=TRUE)
expect_equal(nrow(s), 3)

# Only one random effect allowed
expect_error(powerAnalysis(temp, model=occu, formula=~1~elev+(elev||group), effects=ef, nsim=10))
})
})

test_that("custom datasets can be passed to powerAnalysis",{
set.seed(123)
ef <- list(state=c(0, -0.4), det=0)
s <- simulate(temp, model=occu, formula=~1~elev, nsim=10, coefs=ef, quiet=TRUE)

nul <- capture_output({
pa <- powerAnalysis(s, model=occu, formula=~1~elev, effects=ef)
expect_is(pa, "unmarkedPower")
expect_equal(length(pa@estimates), length(s))
})
})

test_that("powerAnalysis can be run in parallel",{
skip_on_cran()
skip_on_ci()

set.seed(123)
ef <- list(state=c(0, -0.4), det=0)

nul <- capture_output({
pa <- powerAnalysis(temp, model=occu, formula=~1~elev, effects=ef, parallel=TRUE, nsim=2)
expect_is(pa, "unmarkedPower")
expect_equal(length(pa@estimates), 2)
})
})

0 comments on commit 5dd3fb7

Please sign in to comment.