Here is my sample code to fit a random occupancy model using the occu() function. The test data is a 3 seasons of 2 visits/season as indicated by the visits vector in the code. The code assumes that a
is available for the input history -- it fails if there is no row name, so you should add a simple
library(unmarked)
history <-read.csv('test-random.csv', header=TRUE,
as.is=TRUE, row.names=1)
head(history)
# We need to split the data into separate sets of sites for each season
# and put all NA's before and after the season of interest
# COnsider a study with 3 seasons and 2 visits/season
# The history 101101 is broken into 3 pseudo-histories
# 10....
# ..11..
# ....01
# Then to allow a separate psi for each season, you add a site covariate to give
# SiteCov History
# T1 10....
# T2 ..11..
# T3 ....01
# and then fit the model with psi~SiteCov
visits <- c(2,2,2) # 3 seasons with 2 visits per season
start.visit <- cumsum(c(0,visits[-length(visits)]))+1 # when does season start
end.visit <- cumsum(visits) # when does season end
rev.history <- NULL # revised history that isolates each season with NA in front and after the season's data
for(i in 1:length(visits)){
season.history <- cbind(matrix(NA,nrow=nrow(history),ncol=max(0,start.visit[i]-1)),
history[, start.visit[i]:end.visit[i]],
matrix(NA,nrow=nrow(history),ncol=max(0,end.visit[length(end.visit)]-end.visit[i])))
row.names(season.history) <- paste("SEAS.", formatC(i, digits=2, flag='0'),'.',row.names(season.history),sep="")
names(season.history) <- names(history) # fix up the visit names
rev.history<- rbind(rev.history,season.history)
}
# create a site covariate that reflects the different seasons for the rev.history
rev.history.site.cov <- data.frame(Season=rep(paste("SEAS.", formatC(1:length(visits), digits=2, flag='0'),sep=""), each=nrow(history)))
library(unmarked)
history <-read.csv('test-random.csv', header=TRUE,
as.is=TRUE, row.names=1)
head(history)
# We need to split the data into separate sets of sites for each season
# and put all NA's before and after the season of interest
# COnsider a study with 3 seasons and 2 visits/season
# The history 101101 is broken into 3 pseudo-histories
# 10....
# ..11..
# ....01
# Then to allow a separate psi for each season, you add a site covariate to give
# SiteCov History
# T1 10....
# T2 ..11..
# T3 ....01
# and then fit the model with psi~SiteCov
visits <- c(2,2,2) # 3 seasons with 2 visits per season
start.visit <- cumsum(c(0,visits[-length(visits)]))+1 # when does season start
end.visit <- cumsum(visits) # when does season end
rev.history <- NULL # revised history that isolates each season with NA in front and after the season's data
for(i in 1:length(visits)){
season.history <- cbind(matrix(NA,nrow=nrow(history),ncol=max(0,start.visit[i]-1)),
history[, start.visit[i]:end.visit[i]],
matrix(NA,nrow=nrow(history),ncol=max(0,end.visit[length(end.visit)]-end.visit[i])))
row.names(season.history) <- paste("SEAS.", formatC(i, digits=2, flag='0'),'.',row.names(season.history),sep="")
names(season.history) <- names(history) # fix up the visit names
rev.history<- rbind(rev.history,season.history)
}
# create a site covariate that reflects the different seasons for the rev.history
rev.history.site.cov <- data.frame(Season=rep(paste("SEAS.", formatC(1:length(visits), digits=2, flag='0'),sep=""), each=nrow(history)))
rev.history.site.cov$Time <- rep(1:length(visits), each=nrow(history))
Seasons <- matrix( rep(c("T1","T2","T3"),each=2), nrow=nrow(rev.history), ncol=ncol(rev.history), byrow=TRUE)
occ.UMF <- unmarkedFrameOccu(
y = rev.history,
obsCovs=list(Season=Seasons),
siteCovs=rev.history.site.cov)
occ.UMF
summary(occ.UMF)
plot(occ.UMF, panels=1)
# Now to fit a model for random occupancy with a separate psi for each season
mod <- occu( ~1 ~Season, data=occ.UMF)
mod
psi.pred.cov <- data.frame(Season=unique(rev.history.site.cov$Season))
psi.pred <- predict(mod, type="state", newdata=psi.pred.cov)
cbind(psi.pred.cov, psi.pred)
p.pred.cov <- data.frame(one=1)
p.pred <- predict(mod, type="det", newdata=p.pred.cov)
cbind(p.pred.cov, p.pred)
Seasons <- matrix( rep(c("T1","T2","T3"),each=2), nrow=nrow(rev.history), ncol=ncol(rev.history), byrow=TRUE)
occ.UMF <- unmarkedFrameOccu(
y = rev.history,
obsCovs=list(Season=Seasons),
siteCovs=rev.history.site.cov)
occ.UMF
summary(occ.UMF)
plot(occ.UMF, panels=1)
# Now to fit a model for random occupancy with a separate psi for each season but a common p over all season.
mod <- occu( ~1 ~Season, data=occ.UMF)
mod
psi.pred.cov <- data.frame(Season=unique(rev.history.site.cov$Season))
psi.pred <- predict(mod, type="state", newdata=psi.pred.cov)
cbind(psi.pred.cov, psi.pred)
p.pred.cov <- data.frame(one=1)
p.pred <- predict(mod, type="det", newdata=p.pred.cov)
cbind(p.pred.cov, p.pred)
# Now to fit a model for random occupancy with a time trend across seasons but a common p across all seasons
mod2 <- occu( ~1 ~Time, data=occ.UMF)
mod2
psi.pred.cov <- data.frame(Time=1:length(visits))
psi.pred <- predict(mod2, type="state", newdata=psi.pred.cov)
cbind(psi.pred.cov, psi.pred)
p.pred.cov <- data.frame(one=1)
p.pred <- predict(mod2, type="det", newdata=p.pred.cov)
cbind(p.pred.cov, p.pred)
# Now to fit a model for random occupancy with a time trend across seasons and a separate p for each season.
mod3 <- occu( ~Season ~Time, data=occ.UMF)
mod3
psi.pred.cov <- data.frame(Time=1:length(visits))
psi.pred <- predict(mod3, type="state", newdata=psi.pred.cov)
cbind(psi.pred.cov, psi.pred)
p.pred.cov <- data.frame(Season=c("T1","T2","T3"))
p.pred <- predict(mod3, type="det", newdata=p.pred.cov)
cbind(p.pred.cov, p.pred)