rbchan / unmarked

R package for hierarchical models in ecological research
https://rbchan.github.io/unmarked/
36 stars 25 forks source link

multmixOpen() error when "immigration = TRUE" #188

Closed dslramsey closed 4 years ago

dslramsey commented 4 years ago

running multmixOpen() with "immigration = TRUE", causes the R session to crash on ver. 1.01, WIN64, R 3.6.3. Reproducible example follows...

set.seed(123)
lambda=4; gamma=0.5; omega=0.8; p=0.5
M <- 100; T <- 5
y <- array(NA, c(M, 3, T))
N <- matrix(NA, M, T)
S <- G <- matrix(NA, M, T-1)

for(i in 1:M) {
  N[i,1] <- rpois(1, lambda)
  y[i,1,1] <- rbinom(1, N[i,1], p)    # Observe some
  Nleft1 <- N[i,1] - y[i,1,1]         # Remove them
  y[i,2,1] <- rbinom(1, Nleft1, p)   # ...
  Nleft2 <- Nleft1 - y[i,2,1]
  y[i,3,1] <- rbinom(1, Nleft2, p)

  for(t in 1:(T-1)) {
    S[i,t] <- rbinom(1, N[i,t], omega)
    G[i,t] <- rpois(1, gamma)
    N[i,t+1] <- S[i,t] + G[i,t]
    y[i,1,t+1] <- rbinom(1, N[i,t+1], p)    # Observe some
    Nleft1 <- N[i,t+1] - y[i,1,t+1]         # Remove them
    y[i,2,t+1] <- rbinom(1, Nleft1, p)   # ...
    Nleft2 <- Nleft1 - y[i,2,t+1]
    y[i,3,t+1] <- rbinom(1, Nleft2, p)
  }
}
y=matrix(y, M)

#Create some random covariate data
sc <- data.frame(x1=rnorm(100))

#Create unmarked frame
umf <- unmarkedFrameMMO(y=y, numPrimary=5, siteCovs=sc, type="removal")

#Fit model
(fit <- multmixOpen(~x1, ~1, ~1, ~1, K=30, data=umf, immigration=TRUE))

R sessionInfo()

R version 3.6.3 (2020-02-29)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale:
[1] LC_COLLATE=English_Australia.1252  LC_CTYPE=English_Australia.1252   
[3] LC_MONETARY=English_Australia.1252 LC_NUMERIC=C                      
[5] LC_TIME=English_Australia.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] unmarked_1.0.1  lattice_0.20-41

loaded via a namespace (and not attached):
 [1] MASS_7.3-51.5    compiler_3.6.3   plyr_1.8.6       parallel_3.6.3   tools_3.6.3     
 [6] sp_1.4-2         Rcpp_1.0.5       raster_3.3-7     codetools_0.2-16 grid_3.6.3
kenkellner commented 4 years ago

Thanks. This should be fixed now in 1f90b4f7. There were two problems. You actually shouldn't be able to set immigration=TRUE when fitting a model with constant or notrend dynamics, as in the example you provided. This is now prevented. There was also a bug in the C++ code which caused the crash, which should now be fixed for other models where you actually can estimate immigration. Let me know if you still run into issues.

dslramsey commented 4 years ago

Great! This seems to have resolved the issue. Thanks for getting to this so quickly.