# Simulate data
set.seed(35)
nSites <- 100
nVisits <- 3
x <- rnorm(nSites) # a covariate
beta0 <- 0
beta1 <- 1
lambda <- exp(beta0 + beta1*x) # expected counts at each site
N <- rpois(nSites, lambda) # latent abundance
y <- matrix(NA, nSites, nVisits)
p <- c(0.3, 0.6, 0.8) # detection prob for each visit
for(j in 1:nVisits) {
y[,j] <- rbinom(nSites, N, p[j])
}
# Organize data
visitMat <- matrix(as.character(1:nVisits), nSites, nVisits, byrow=TRUE)
umf <- unmarkedFramePCount(y=y, siteCovs=data.frame(x=x),
obsCovs=list(visit=visitMat))
summary(umf)
# Fit a model
fm1 <- pcount(~visit-1 ~ x, umf, K=50)
fm1
plogis(coef(fm1, type="det")) # Should be close to p
# Empirical Bayes estimation of random effects
(fm1re <- ranef(fm1))
plot(fm1re, subset=site %in% 1:25, xlim=c(-1,40))
sum(bup(fm1re)) # Estimated population size
sum(N) # Actual population size
## Not run:
#
# # Real data
# data(mallard)
# mallardUMF <- unmarkedFramePCount(mallard.y, siteCovs = mallard.site,
# obsCovs = mallard.obs)
# (fm.mallard <- pcount(~ ivel+ date + I(date^2) ~ length + elev + forest, mallardUMF, K=30))
# (fm.mallard.nb <- pcount(~ date + I(date^2) ~ length + elev, mixture = "NB", mallardUMF, K=30))
#
# ## End(Not run)
Run the code above in your browser using DataLab