set.seed(123)
options(max.print = 50)
# We simulate data in 100 sites with 3 observations of 7 days per site.
nSites <- 100
nObs <- 3
# For an occupancy covariate, we associate each site to a land-use category.
landuse <- sample(factor(c("Forest", "Grassland", "City"), ordered = TRUE),
size = nSites, replace = TRUE)
simul_psi <- ifelse(landuse == "Forest", 0.8,
ifelse(landuse == "Grassland", 0.4, 0.1))
z <- rbinom(n = nSites, size = 1, prob = simul_psi)
# For a detection covariate, we create a fake wind variable.
wind <- matrix(rexp(n = nSites * nObs), nrow = nSites, ncol = nObs)
simul_lambda <- wind / 5
L = matrix(7, nrow = nSites, ncol = nObs)
# We now simulate count detection data
y <- matrix(rpois(n = nSites * nObs, lambda = simul_lambda * L),
nrow = nSites, ncol = nObs) * z
# We create our unmarkedFrameOccuCOP object
umf <- unmarkedFrameOccuCOP(
y = y,
L = L,
siteCovs = data.frame("landuse" = landuse),
obsCovs = list("wind" = wind)
)
print(umf)
# We fit our model without covariates
fitNull <- occuCOP(data = umf)
print(fitNull)
# We fit our model with covariates
fitCov <- occuCOP(data = umf, psiformula = ~ landuse, lambdaformula = ~ wind)
print(fitCov)
# We back-transform the parameter's estimates
## Back-transformed occupancy probability with no covariates
backTransform(fitNull, "psi")
## Back-transformed occupancy probability depending on habitat use
predict(fitCov,
"psi",
newdata = data.frame("landuse" = c("Forest", "Grassland", "City")),
appendData = TRUE)
## Back-transformed detection rate with no covariates
backTransform(fitNull, "lambda")
## Back-transformed detection rate depending on wind
predict(fitCov,
"lambda",
appendData = TRUE)
## This is not easily readable. We can show the results in a clearer way, by:
## - adding the site and observation
## - printing only the wind covariate used to get the predicted lambda
cbind(
data.frame(
"site" = rep(1:nSites, each = nObs),
"observation" = rep(1:nObs, times = nSites),
"wind" = getData(fitCov)@obsCovs
),
predict(fitCov, "lambda", appendData = FALSE)
)
# We can choose the initial parameters when fitting our model.
# For psi, intituively, the initial value can be the proportion of sites
# in which we have observations.
(psi_init <- mean(rowSums(y) > 0))
# For lambda, the initial value can be the mean count of detection events
# in sites in which there was at least one observation.
(lambda_init <- mean(y[rowSums(y) > 0, ]))
# We have to transform them.
occuCOP(
data = umf,
psiformula = ~ 1,
lambdaformula = ~ 1,
psistarts = qlogis(psi_init),
lambdastarts = log(lambda_init)
)
# If we have covariates, we need to have the right length for the start vectors.
# psi ~ landuse --> 3 param to estimate: Intercept, landuseForest, landuseGrassland
# lambda ~ wind --> 2 param to estimate: Intercept, wind
occuCOP(
data = umf,
psiformula = ~ landuse,
lambdaformula = ~ wind,
psistarts = rep(qlogis(psi_init), 3),
lambdastarts = rep(log(lambda_init), 2)
)
# And with covariates, we could have chosen better initial values, such as the
# proportion of sites in which we have observations per land-use category.
(psi_init_covs <- c(
"City" = mean(rowSums(y[landuse == "City", ]) > 0),
"Forest" = mean(rowSums(y[landuse == "Forest", ]) > 0),
"Grassland" = mean(rowSums(y[landuse == "Grassland", ]) > 0)
))
occuCOP(
data = umf,
psiformula = ~ landuse,
lambdaformula = ~ wind,
psistarts = qlogis(psi_init_covs))
# We can fit our model with a different optimisation algorithm.
occuCOP(data = umf, method = "Nelder-Mead")
# We can run our model with a C++ or with a R likelihood function.
## They give the same result.
occuCOP(data = umf, engine = "C", psistarts = 0, lambdastarts = 0)
occuCOP(data = umf, engine = "R", psistarts = 0, lambdastarts = 0)
## The C++ (the default) is faster.
system.time(occuCOP(data = umf, engine = "C", psistarts = 0, lambdastarts = 0))
system.time(occuCOP(data = umf, engine = "R", psistarts = 0, lambdastarts = 0))
## However, if you want to understand how the likelihood is calculated,
## you can easily access the R likelihood function.
print(occuCOP(data = umf, engine = "R", psistarts = 0, lambdastarts = 0)@nllFun)
# Finally, if you do not want to fit your model but only get the likelihood,
# you can get the negative log-likelihood for a given set of parameters.
occuCOP(data = umf, return.negloglik = list(
c("psi" = qlogis(0.25), "lambda" = log(2)),
c("psi" = qlogis(0.5), "lambda" = log(1)),
c("psi" = qlogis(0.75), "lambda" = log(0.5))
))
Run the code above in your browser using DataLab