## Not run:
#
# ## Don't run specified because these examples can take > 10 seconds.
#
# ## Simulate constant model, and analyze
#
#
# ns <- 10
# N <- 100
# sim.list <- F.cjs.simulate( rep(0.3,ns), rep(0.9,ns-1), N1=N, R=100 )
#
# f.analyze <- function(x){
# fit <- F.cjs.estim( ~1, ~1, x$hists, control=mra.control(maxfn=200, cov.meth=2) )
# if( fit$exit.code == 1 ){
# return( fit$n.hat )
# } else {
# return( rep(NA,ncol(x$hists)) )
# }
# }
# results <- t(sapply(sim.list, f.analyze))
# plot( 1:10, colMeans(results, na.rm=TRUE), xlab="Occasion",
# ylab="Mean population estimate", col="red", type="b")
# abline( h=N )
#
# ## Plot RMSE by occasion
# std <- apply(results, 2, sd, na.rm=TRUE)
# bias <- apply(results - N, 2, mean, na.rm=TRUE)
# plot( std, bias, type="n" )
# text( std, bias, 2:10 )
# abline(h=0)
# title(main="RMSE by Sample Occasion")
#
# ## Show bias when heterogeneity is present
# sim.list <- F.cjs.simulate( matrix(c(0.3,.7,.7,.7),4,ns), rep(0.9,ns-1), N1=N, R=100 )
# results <- t(sapply(sim.list, f.analyze))
# mean.N <- colMeans(results, na.rm=TRUE)
# plot( 1:length(mean.N), mean.N, ylim=range(c(mean.N,N),na.rm=TRUE),
# xlab="Occasion", ylab="Mean population estimate", col="red", type="b")
# abline( h=N )
# abline( h=mean(mean.N), col="red", lty=2)
# title(main="Heterogeniety causes negative bias")
#
#
# ## Simulate CJS model, first estimate one
# data(dipper.histories)
# ct <- as.factor( paste("T",1:ncol(dipper.histories), sep=""))
# attr(ct,"nan")<-nrow(dipper.histories)
# dipper.cjs <- F.cjs.estim( ~tvar(ct,drop=c(1,2)), ~tvar(ct,drop=c(1,6,7)), dipper.histories )
#
# ## Now generate histories from it.
# sim.list <- F.cjs.simulate( fit=dipper.cjs, N1=100, birth.rate=rep(1,6), R=100 )
#
# ## Now analyze generated histories using lapply or sapply. Can fit any model.
# ## Here we fit the correct model.
# f.analyze <- function(x){
# # write a counter to console, this is not necessary
# i <- get("i", env=.GlobalEnv) + 1
# cat(paste("Iteration", i, "\n"))
# assign("i",i,env=.GlobalEnv)
#
# ct <- as.factor( 1:ncol(x$hists) )
# fit <- F.cjs.estim( ~tvar(ct,nan=nrow(x$hists),drop=c(1,2)),
# ~tvar(ct,nan=nrow(x$hists),drop=c(1,6,7)),
# x$hists, control=mra.control(maxfn=200, cov.meth=2) )
# if( fit$exit.code == 1 ){
# return( fit$n.hat )
# } else {
# return( rep(NA,ncol(x$hists)) )
# }
# }
# i <- 0
# results <- t(sapply(sim.list, f.analyze))
# mean.N <- colMeans(results, na.rm=TRUE)
# plot( 1:length(mean.N), mean.N, ylim=range(c(mean.N,N),na.rm=TRUE),
# xlab="Occasion", ylab="Mean population estimate", col="red", type="b")
# abline( h=N )
# title(main="Time varying CJS model")
# ## End(Not run)
Run the code above in your browser using DataLab