##modified example from ?pcount
if (FALSE) {
if(require(unmarked)){
##Simulate data
set.seed(3)
nSites <- 100
nVisits <- 3
##covariate
x <- rnorm(nSites)
beta0 <- 0
beta1 <- 1
##expected counts
lambda <- exp(beta0 + beta1*x)
N <- rpois(nSites, lambda)
y <- matrix(NA, nSites, nVisits)
p <- c(0.3, 0.6, 0.8)
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))
## Fit model
fm1 <- pcount(~ visit ~ 1, umf, K=50)
covDiag(fm1)
##sparser data
p <- c(0.01, 0.001, 0.01)
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))
## Fit model
fm.sparse <- pcount(~ visit ~ 1, umf, K=50)
covDiag(fm.sparse)
}
}
Run the code above in your browser using DataLab