# Fake data
R <- 4 # number of sites
J <- 3 # number of secondary sampling occasions
T <- 2 # number of primary periods
y <- matrix(c(
1,1,0, 0,0,0,
0,0,0, 0,0,0,
1,1,1, 1,1,0,
1,0,1, 0,0,1), nrow=R, ncol=J*T, byrow=TRUE)
y
site.covs <- data.frame(x1=1:4, x2=factor(c('A','B','A','B')))
site.covs
yearly.site.covs <- list(
year = matrix(c(
'year1', 'year2',
'year1', 'year2',
'year1', 'year2',
'year1', 'year2'), nrow=R, ncol=T, byrow=TRUE)
)
yearly.site.covs
obs.covs <- list(
x4 = matrix(c(
-1,0,1, -1,1,1,
-2,0,0, 0,0,2,
-3,1,0, 1,1,2,
0,0,0, 0,1,-1), nrow=R, ncol=J*T, byrow=TRUE),
x5 = matrix(c(
'a','b','c', 'a','b','c',
'd','b','a', 'd','b','a',
'a','a','c', 'd','b','a',
'a','b','a', 'd','b','a'), nrow=R, ncol=J*T, byrow=TRUE))
obs.covs
umf <- unmarkedMultFrame(y=y, siteCovs=site.covs,
yearlySiteCovs=yearly.site.covs, obsCovs=obs.covs,
numPrimary=2) # organize data
umf # look at data
summary(umf) # summarize
fm <- colext(~1, ~1, ~1, ~1, umf) # fit a model
fm
if (FALSE) {
# Real data
data(frogs)
umf <- formatMult(masspcru)
obsCovs(umf) <- scale(obsCovs(umf))
## Use 1/4 of data just for run speed in example
umf <- umf[which((1:numSites(umf)) %% 4 == 0),]
## constant transition rates
(fm <- colext(psiformula = ~ 1,
gammaformula = ~ 1,
epsilonformula = ~ 1,
pformula = ~ JulianDate + I(JulianDate^2), umf, control = list(trace=1, maxit=1e4)))
## get the trajectory estimates
smoothed(fm)
projected(fm)
# Empirical Bayes estimates of number of sites occupied in each year
re <- ranef(fm)
modes <- colSums(bup(re, stat="mode"))
plot(1:7, modes, xlab="Year", ylab="Sites occupied", ylim=c(0, 70))
## Find bootstrap standard errors for smoothed trajectory
fm <- nonparboot(fm, B = 100) # This takes a while!
fm@smoothed.mean.bsse
## try yearly transition rates
yearlySiteCovs(umf) <- data.frame(year = factor(rep(1:7, numSites(umf))))
(fm.yearly <- colext(psiformula = ~ 1,
gammaformula = ~ year,
epsilonformula = ~ year,
pformula = ~ JulianDate + I(JulianDate^2), umf,
control = list(trace=1, maxit=1e4)))
}
Run the code above in your browser using DataLab