I'm struggeling with some similar issues and just don't understand completely what I should do. I've run a single season model, survey ran over 45 days at 13 sites (so quite small dataset), with 2 categorical covariates (habitat, saltlick present/absent) and a continuous covariate (distance to watersource).
library(unmarked)
wd <- "D:/MA/Daten/Occupancy_2"
setwd(wd)
my.files <- list.files(wd, pattern = ".csv")
#user defined function to create ordered table of aic wts and predictions
my_aic<-function(labels,aic,predictions,se_predictions) {
delta<-aic-min(aic)
wt<-exp(-delta*0.5)
wt<-wt/sum(wt)
wt
output<-cbind(labels,aic,delta,wt,predictions,se_predictions)
output<-data.frame(labels=labels,AIC=aic,Delta=delta,ModWt=wt,Prediction=predictions,SE_prediction=se_predictions)
output[order(aic) , ]
}
#create a list of labels for the models
labels<-c("p(.)psi(.)","p(.)psi(salt)","p(.)psi(distance.water)","p(.)psi(open.shrub)",
"p(.)psi(closed.shrub)","p(.)psi(watercourse)","p(.)psi(tree.shrub)")
results.aic <- c()
results.predict <-c()
results.psi <- c()
for (i in 1:length(my.files)) {
my.data <- read.csv(my.files[i], h=T, row.names=1)
#detection data rows are sites columns are detection replicates
y<-my.data[,1:45]
n<-nrow(my.data)
#site level (individual) covariates
Cov.site<-my.data[,46:51]
#put everything together in unmarked data frame
alles <- unmarkedFrameOccu(y = y, siteCovs = Cov.site)
#summary of unmarked data frame
summary(alles)
fm1<-occu(~1 ~1,alles)
occu(formula = ~1 ~ 1, data = alles)
#adding covariates to modeling
#constant detection, occupancy predicted by salt
fm2<-occu(~1 ~salt,alles)
occu(formula = ~1 ~ salt, data = alles)
#constant detection, occupancy predicted by distancewater
fm3<-occu(~1 ~distance.water,alles)
occu(formula = ~1 ~ distance.water, data = alles)
fm4<-occu(~1 ~open.shrub,alles)
occu(formula = ~1 ~ open.shrub, data = alles)
fm5<-occu(~1 ~closed.shrub,alles)
occu(formula = ~1 ~ closed.shrub, data = alles)
fm6<-occu(~1 ~watercourse,alles)
occu(formula = ~1 ~ watercourse, data = alles)
fm7<-occu(~1 ~tree.shrub,alles)
occu(formula = ~1 ~ tree.shrub, data = alles)
#summarize and compare the models
fmlist<-fitList(p_dot_psi_dot=fm1,p_dot_psi_salt=fm2,p_dot_psi_distance.water=fm3,
p_dot_psi_open.shrub=fm4,p_dot_psi_closed.shrub=fm5,p_dot_psi_watercourse=fm6,
p_dot_psi_tree.shrub=fm7)
modSel(fmlist)
pred.i <- predict(fmlist, type="state")
pred.i <- cbind(my.files[i], pred.i)
results.predict <- rbind(results.predict, pred.i)
psi.c.i <- ranef(fm1,fm2,fm3,fm4,fm5,fm6,fm7, type="state")
show(psi.c.i)
psi.c.i <- cbind(my.files[i], psi.c.i)
results.psi <- rbind(results.psi, psi.c.i)
#aic values
aic<-c(fm1@AIC,fm2@AIC,fm3@AIC,fm4@AIC,fm5@AIC,fm6@AIC,fm7@AIC)
#logit scale predictions for psi under each model
lc1<-linearComb(fm1, c(1), type="state")
lc2<-linearComb(fm2, c(1, 1), type="state")
lc3<-linearComb(fm3, c(1, 1), type="state")
lc4 <- linearComb(fm4, c(1, 1), type="state")
lc5 <- linearComb(fm5, c(1, 1), type="state")
lc6 <- linearComb(fm6, c(1, 1), type="state")
lc7 <- linearComb(fm7, c(1, 1), type="state")
#predictions and se under each model
predictions<-c(lc1@estimate,lc2@estimate,lc3@estimate,lc4@estimate,lc5@estimate,
lc6@estimate,lc7@estimate)
se_predictions<-sqrt(c(lc1@covMat,lc2@covMat,lc3@covMat,lc4@covMat,lc5@covMat,
lc6@covMat,lc7@covMat))
aic_mods<-my_aic(labels=labels,aic=aic,predictions=predictions,se_predictions=se_predictions)
#then apply linear functions to weighted average and se and backstransform
#aic_mods
results.aic.i <- cbind(my.files[i], aic_mods)
results.aic <- rbind(results.aic, results.aic.i)
}
write.csv (results.aic, "AIC.models.csv")
write.csv(results.predict, "Predictions.csv")
write.csv (results.psi, "Psi.conditional.csv")
My aim is to make a plot of the probability of occupancy for every species at every site. I'm confused about the differences with predict and ranef. Can anybody help me with that? Which values can I use to plot? The deviations are really high in predict, why are they like that?
I'm sorry, if I'm asking silly questions, but I'm just a total beginner with R.