# cormack-jolly-seber model
# fit cjs models with crm, and compare
setwd("~/Dropbox/Sp16EcoSeminar-R/Day 5")
#call library, attach the package
library(marked)
#attach data
data(dipper)
dipper
#understand capture history notation, 0=not seen 1=seen, 7 events
dipper.proc=process.data(dipper,model="cjs",begin.time=1)
#dipper.proc=process.data(dipper)
#combines deplicated records, if possible
dipper.ddl=make.design.data(dipper.proc)
fit.models=function()
  {
  Phi.sex=list(formula=~sex)
  Phi.time=list(formula=~time)
  p.sex=list(formula=~sex)
  p.dot=list(formula=~1)
  cml=create.model.list(c("Phi","p"))
  #crm vector of length=nrow(dm) dm=design matrix, capture recapture model
  results=crm.wrapper(cml,data=dipper.proc, ddl=dipper.ddl,
      external=FALSE,accumulate=FALSE)
    return(results)
}
#fits model and reports likelihood, -2lnl is the total sum of squares in regression.
#repeated models with better fit would have lower -2lnl values
dipper.models=fit.models()
#The model selection table is displayed with:
dipper.models
#AIC outputs
#Call for individual outputs
dipper.models[[1]]
dipper.models[["Phi.sex.p.dot"]]
