Hi, I've written a function to simulate data involving multiple species, all declining in occupancy over time. The data it's outputting looks correct to me, but when I try to fit the model to it I get a "fatal error session aborted" message. It outputs "Constructing atomic D_lgamma" just before it aborts.
It runs fine, but if I change it to a different number (for example 123) I get the error.
Here's all the code (sorry, I can't figure out how to just upload the simulated data).
set.seed(1234)
library(secr)
library(occuR)
library(data.table)
library(dplyr)
library(secr)
library(occuR)
library(data.table)
library(dplyr)
# Function: multispecies_sim
# Purpose: Simulate hoverfly data with multiple species, absences only when other species are detected
# All species have the same occupancy probability, but can have different detection probability
# Occupancy probably depends on habitat and occasion
# Inputs: nsites - number of sites
# nspecies - number of species
# noccasion - number of occasions
# mean_visits - mean visits per site per occasion (generated as a random poisson)
# p - vector of detection probability of each species
# beta - [b1, b2, b3, b4] where where logit(psi) = b1 + b2*Urban + b3*Woodland + b4*occasion
# species - species of interest (the one whose observations get returned) - later will make it so they all get returned
# Output: list(visit, site), visit = visit_data and site = site_data
#
multispecies_sim <- function(nsites, nspecies, noccasion, mean_visits, p, beta, species){
# Random number of visits to each site in each occasion
visits <- matrix(rpois(nsites*noccasion, mean_visits), nrow = noccasion, ncol = nsites)
# Simulate some covariates
temp <- do.call("c", sapply(visits, FUN = function(v) {rnorm(v, 10, 5)}))
habitat_options <- c("arable", "woodland", "urban")
habitat <- sample(habitat_options, nsites, replace = TRUE)
hab_list <- rep(habitat, each=noccasion)
#occasion <- do.call("c", apply(visits, 2, FUN = function(v) {rep(1:noccasion, v)}))
occasion <- rep(1:noccasion, nsites)
# Occupancy probability
psi <- rep(0.7, nspecies*noccasion)
#psi <- invlogit(beta[1] + beta[2]*(hab_list == "urban") + beta[3]*(hab_list == "woodland") + beta[4]*occasion)
# Whether sites are occupied or not
occupied <- array(NA, dim=c(nsites*noccasion, nspecies))
for(i in 1:nspecies){
occupied[,i] <- rbinom(nsites*noccasion, 1, psi)
} #Could this be done more efficiently?
# Observations for each species, in order of site, then occasion, then visit
rows <- rep(1:nrow(occupied), as.numeric(visits))
obs <- apply(occupied[rows,], 1, FUN = function(x){rbinom(nspecies, 1, p*x)})
#This switches the dimensions of the matrix - is it wrong?
#obs <- matrix(NA, nrow = nsites*)
#row_ticker <- 1
#for(i in 1:nsites){
# for(j in 1:noccasion){
# if(visits[j, i] != 0){
# for(k in 1:visits[j,i]){
# detec <- occupied[((i-1)*noccasion + j),]*rbinom(nspecies,1,p)
# if(sum(detec) != 0){
# obs[row_ticker,] <- detec
# row_ticker <- row_ticker + 1
# }
# }
# }
# }
#}
tokeep <- which(colSums(obs) != 0)
visit_data <- data.table(site = rep(1:nsites, each = noccasion),
occasion = rep(1:noccasion),
hab = hab_list,
occupied = occupied[,species])
nrep <- rep(1:nrow(visit_data), as.numeric(visits))
visit_data <- visit_data[nrep,]
#visit_data$visit <- do.call("c", apply(visits, c(2,1), FUN = function(v) {1:v}))
visit_data$temp <- temp
visit_data$obs <- obs[species,]
visit_data <- visit_data[tokeep,]
visit_data$visit <- visit_data[,.(visit = 1:.N), .(site, occasion)]$visit
# Construct site data
# x and y coordinates are irrelevant right now, os can be random.
# will need to sort this out in the future
x_coor <- rnorm(nsites,0,1)
y_coor <- rnorm(nsites, 0, 1)
site_data <- data.table(site = rep(1:nsites, each = noccasion),
occasion = 1:noccasion,
hab = rep(habitat, each = noccasion),
x = rep(x_coor, each = noccasion),
y = rep(y_coor, each = noccasion))
return(list(visit = visit_data, site = site_data))
}
x <- multispecies_sim(nsites = 50, nspecies = 5, noccasion = 10,
mean_visits = 10, p = rep(0.7, 5),
beta = c(2, -1, 0.4, -1), species = 1)
fit1 <- fit_occu(list(psi ~ 1, p ~ 1), visit_data = x$visit, site_data = x$site)