Skip to content

Commit

Permalink
Fix fitted with certain Dail-Madsen models, fixes #278
Browse files Browse the repository at this point in the history
  • Loading branch information
kenkellner committed Apr 1, 2024
1 parent 5b2b00e commit 1815977
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
6 changes: 3 additions & 3 deletions R/unmarkedFit.R
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ setMethod("fitted", "unmarkedFitDailMadsen",
if(is.null(Xom.offset)) Xom.offset <- rep(0, M*(T-1))
if(is.null(Xiota.offset)) Xiota.offset <- rep(0, M*(T-1))

lambda <- exp(Xlam %*% coef(object, 'lambda') + Xlam.offset)
lambda <- as.vector(exp(Xlam %*% coef(object, 'lambda') + Xlam.offset))
if(identical(mixture, "ZIP")) {
psi <- plogis(coef(object, type="psi"))
lambda <- (1-psi)*lambda
Expand Down Expand Up @@ -618,7 +618,7 @@ setMethod("fitted", "unmarkedFitDailMadsen",
N[i,t] <- N[i,t-1]*exp(gamma[i,t-1]*(1-N[i,t-1]/omega[i,t-1]))+
iota[i, t-1]
else if(identical(dynamics, "gompertz"))
N[i,1] <- N[i,t-1] * exp(gamma[i,t-1]*(1-log(N[i,t-1]+1)/
N[i,t] <- N[i,t-1] * exp(gamma[i,t-1]*(1-log(N[i,t-1]+1)/
log(omega[i,t-1]+1))) + iota[i, t-1]
else
N[i,t] <- N[i,t-1] * omega[i,t-1] + gamma[i,t-1]
Expand All @@ -633,7 +633,7 @@ setMethod("fitted", "unmarkedFitDailMadsen",
N[i, t] <- N[i, t] * exp(gamma[i, t-1] * (1 - N[i,t] /
omega[i,t-1]))+ iota[i, t-1]
else if(identical(dynamics, "gompertz"))
N[i, 1] <- N[i, t] * exp(gamma[i, t-1] * (1 -
N[i, t] <- N[i, t] * exp(gamma[i, t-1] * (1 -
log(N[i, t]+1) / log(omega[i, t-1] + 1))) +
iota[i, t-1]
else
Expand Down
13 changes: 13 additions & 0 deletions tests/testthat/test_pcountOpen.R
Original file line number Diff line number Diff line change
Expand Up @@ -353,11 +353,24 @@ test_that("pcountOpen can fit models with various dynamics",{
c(1.2131713, 0.7301736, 1.1949289),
tolerance=1e-5)

# issue #278
ft2 <- fitted(m2)
expect_equal(dim(ft2), c(20,5))

m3 <- pcountOpen(~1, ~1, ~1, ~1, umf, K=20, dynamics="trend")
expect_equivalent(coef(m3),
c(1.67211946, -0.06534021, 0.18287762),
tolerance=1e-5)

m4 <- pcountOpen(~1, ~1, ~1, ~1, umf, K=20, dynamics="gompertz")
expect_equivalent(coef(m3),
c(1.67211946, -0.06534021, 0.18287762),
tolerance=1e-5)

# issue #278
ft4 <- fitted(m4)
expect_true(all(!is.na(ft4)))

})


Expand Down

0 comments on commit 1815977

Please sign in to comment.