usgs icon pwrc icon

Hierarchical Modeling and Inference in Ecology:
The Analysis of Data from Populations, Metapopulations and Communities
J. Andrew Royle and Robert M. Dorazio


arrowreturn to main page

Below, you'll find 'R' code and data described in the book.
You can download the free programming package 'R' at:http://www.r-project.org
Chapter 1Conceptual and Philosophical Considerations in Ecology and Statistics
Chapter 2Essentials of Statistical Inference
psi.MLE.RMLE of psi (Panel 2.2)
mothMortality.Bayes.RLogistic regression of moth mortalities: Bayesian model selection
fishSurvival.Bayes.RPosterior distribution of larval fish survival (Panel 2.8)
muConfidenceInterval.RConfidence interval for normal mean (Panel 2.7)
logitpsiConfidenceInterval.RConfidence Interval for psi (Panel 2.6)
psiConfidenceInterval.RConfidence Interval for psi (Panel 2.5)
normalObs.MLE.RMLE for the normal distribution (Panel 2.3)
bruteForcePsi.MLE.RMLE of psi by brute force (Panel 2.1)
Chapter 3Modeling Occupancy and Occurrence Probability
wtmatrix.csvWillow tit data from the Swiss MHB program. These data are used in many of the analyses in Chs. 3 and 8.
utilfns.RdBasic logit utility functions
panel3pt1.fn.RdR function for executing code in panel 3.1
panel3pt2.fn.RdLikelihood functions
panel3pt6.fn.RdR function for executing code in panel 3.6
panel3pt8.fn.RdR wrapper for fitting occupancy model to the willow tit data using the likelihood specification given in Panel 3.8. This fits an occupancy model with covariates in both detection and occurrence probability using the zero-inflated binomial representation. Requires the willow tit data and utility functions
panel3pt8bayes.fn.R R wrapper for fitting occupancy model with detection and abundance covariates to the willow tit data in WinBUGS. Requires the willow tit data and utility functions.
Chapter 4Occupancy and Abundance
RNmodelWinBUGS.Rsimulates data under the model described in Royle and Nichols (2004) and fits that model in WinBUGS.
panel4pt5.fnABUNDANCE MIXTURE/Royle-Nichols Model w/overdispersion: R script for fitting RN model with over-dispersed abundance
panel4pt4.fnR code for RN model with quasi-binomial detection (Panel 4.4)
catbird.yCatbird detection frequency data for a BBS route (50 stops) based on J=11 surveys of the route
panel4pt1.fnR code for fitting Royle-Nichols model (Panel 4.1)
panel4pt3.fnR code for RN model with covariates, fitted to the willow tit data (Panel 4.3)
panel4pt2.fnR code for fitting 2-state occupancy model, RN formulation (Panel 4.2)
Chapter 5Inference in Closed Populations
panel5pt6.fnData Augmentation/Behavioral Response - R script for fitting behavioral responses model to the Microtus data in WinBUGS by data augmentation.
panel5pt5.fnData Augmentation/Model M0 - R script for fitting Model M0 to the Microtus data in WinBUGS by data augmentation.
panel5pt4.fnClosed Population/Double Observer - R function for fitting double observer model to waterfowl survey data (Panel 5.4)
panel5pt3.fnClosed Population/Model Mt - R function for fitting Model Mt (Panel 5.3)
panel5pt2.fnClosed Population/Model M0 - R function for fitting Model M0 to Microtus data (Panel 5.2)
microtus.data.RMicrotus data (thanks to J.D. Nichols)
Chapter 6Models with Individual Effects
malldata.RMallard Data used in panel6pt5.fn
panel6pt5.fn.RDOUBLE OBSERVER MODEL W/INDIVIDUAL COVARIATE - DATA AUGMENTATION: Aerial waterfowl survey data collected using two observers. Model allows detection probability to vary as a function of group size.
panel6pt4.fn.RCLOSED POPULATION MODEL - Behavioral response + Individual covariate - DATA AUGMENTATION IN WINBUGS. Microtus data, with behavioral response and body mass individual covariate (Panel 6.4). Requires R object 'microtus.data' (see Chapter 5 R files for data)
panel6pt3.fn.RCLOSED POPULATION MODEL W/INDIVIDUAL COVARIATE - DATA AUGMENTATION IN WINBUGS. Microtus data, with body mass individual covariate (Panel 6.3, Table 6.4). Requires R object 'microtus.data' (see Chapter 5 R files for data)
panel6pt2.fn.RCLOSED POPULATION MODEL W/HETEROGENEITY - DATA AUGMENTATION IN WINBUGS. Model Mh, logit-normal version, for Breeding Bird Survey Data (Panel 6.2)
panel6pt1.fn.RCLOSED POPULATION MODEL W/HETEROGENEITY: Model Mh, logit-normal version, fitted to horned-lizard data (Panel 6.1)
bbsdata.RSPECIES RICHNESS DATA: Breeding Bird Survey data from 1997-2001 (detection frequencies) analyzed in Section 6.3. See also Dorazio and Royle (2003).
Chapter 7Spatial Capture-Recapture Models
SCRmultinomial.fn.RSPATIAL CAPTURE-RECAPTURE, R script for generating spatial capture recapture data under a multinomial observation model (Section 7.4), and then fitting the model to the simulated data using WinBUGS. The model is similar to that described in Chapter 7 but this is from Gardner et al. (2009; Ecology). More details are given in the header of the R script.
SCR.fn.RSPATIAL CAPTURE-RECAPTURE - Binomial observation model: R script for simulating data and fitting a variation of the model described in Section 7.4. This model differs by the manner in which the probability of capture relates to individual activity center, and the observation model is binomial instead of multinomial. See comment in R script. This comes from Royle and Gardner(2008).
panel7pt1B.fn.RDISTANCE SAMPLING, MLE from integrated likelihood under DATA AUGMENTATION (Section 7.1.6). Analysis of Impala data.
panel7pt1.fn.RDISTANCE SAMPLING, BAYESIAN ANALYSIS in WINBUGS using DATA AUGMENTATION (Panel 7.1)
impala.RDISTANCE SAMPLING: impala data (from K. Burnham). Used in Panel 7.1. To read into R type: source("impala.R") at command prompt.
Chapter 8Metapopulation Models of Abundance
table8pt2.fnBinomial Mixture Model: R script for fitting the binomial/Poisson mixture model to the willow tit data. Produces output given in top half of Table 8.2. See Chapter 4 materials (panel4pt3.fn) for R script to prodce the bottom half of the table.
salamanderRemovals.Bayes.RBayesian analysis of salamander removal counts
salamanderRemovals2001.csvRemoval counts of salamanders
manateeCounts.Bayes.RBayesian analysis of manatee double-observer counts
manateeCounts.MLE.RFrequentist analysis of manatee double-observer counts
manatees.txtDouble-observer counts of manatees
wtitCounts.Bayes2.RBayesian analysis of willow tit terrritory counts (overdispersed Poisson regression model of territories)
wtitCounts.Bayes.RBayesian analysis of willow tit territory counts (Poisson regression model of territories)
ovenCounts.Bayes.RBayesian analysis of ovenbird point counts
ovenCounts.MLE.RFrequentist analysis of ovenbird point counts
oven.txtOvenbird point counts
Chapter 9Occupancy Dynamics
panel9pt4.fn.RAUTOLOGISTIC MODEL IN WINBUGS -- R script to fit spatial occupancy model with p < 1 in WinBUGS (Panel 9.4). Uses the simulated data img.data
panel9pt2.fn.RDYNAMIC OCCUPANCY MODEL -- R script to fit dynamic two-state occupancy model to the Crossbill data using WinBUGS. This model
panel9pt3.fn.RAUTOLOGISTIC MODEL IN WINBUGS -- R script to fit spatial occupancy model (classical autologistic) in WinBUGS (Panel 9.3) . This version assumes p = 1.
panel9pt1.fn.RDYNAMIC OCCUPANCY MODEL -- R script to fit dynamic two-state occupancy model to the Crossbill data using WinBUGS (Panel 9.1). This model assumes that p = 1. i.e., the occupancy state variable is observed perfectly.
img.data.RAUTOLOGISTIC MODEL IN WINBUGS -- simulated data. This is an R list containing 4 elements which are used in panel9pt3.fn. Source this function into R using the command> source("img.data.R")
crossbill.data.RCrossbill data from the Swiss Bird Survey. This is an R list. The first element "data" is 266 (sites) x 3 (reps) x 4 (years)
Chapter 10Modeling Population Dynamics
panel10pt2.fn.RJolly-Seber Model w/Individual Heterogeneity -- This R script fits the occupancy formulation of the Jolly-Seber model induced by data augmentation to the dipper data, and allows for individual heterogeneity in survival probability. This script produced the results in Table 10.3. The Markov chains mix slowly, so a long run is necessary to reduce Monte Carlo error.
table10pt2.fn.RJolly-Seber Model - Schwarz-Arnason parameterization by data augmentation. This model produced the results in Table 10.2.
panel10pt1B.fn.RJolly-Seber Model -- occupancy formulation induced by data augmentation -- using the alternative prior specification (see Section 10.3.7). Produces results consistent with those in Table 10.1 ("prior 2").
panel10pt1.fn.RJolly-Seber Model -- occupancy formulation induced by data augmentation. R script for fitting model to the dipper data. Requires the file dipper.data -- load it into your R workspace using source("dipper.data.R"). Results are consistent with "prior 1" results in Table 10.1. There is a slight difference because M was set too low for the results reported in Table 10.1
dipper.data.R Dipper data used in Panel 10.1
Chapter 11Modeling Survival
mapsdataspatial.list.R DATA FILE: Monitoring Avian Productivity and Survivorship (MAPS) data for the spatial model described in Panel 11.6
dipper.data.R Dipper data used for fitting model in Panel 11.7.
panel11pt7.fn.R R script containing WinBUGS specification of CJS model with individual heterogeneity in detection and survival probabilities. Model is fitted to the Dipper data. Results should be consistent with Table 11.6 in the text. The MCMC algorithm should be run for 100k iterations or more due to slow mixing. From Royle (2008; Biometrics 64:364-370).
panel11pt6.fn.RCormack-Jolly-Seber (CJS) model - SPATIAL VERSION: R script to fit CJS model to MAPS warbler data. This is a spatial version of the model in which the survival parameter is spatially indexed and the model contains a spatially correlated random effect. This requires the data file mapsdataspatial.list which is a very large file. Consult the WinBUGS manual for information about the data required to fit the CAR model.
mapsdata.list.R DATA FILE: Monitoring Avian Productivity and Survivorship (MAPS) data used in Panel 11.5
panel11pt5.fn.RCormack-Jolly-Seber (CJS) model with TRANSIENTS -- R script to fit CJS model with transients in WinBUGS. This is a simple model with year-specific detection and survival probabilities and a constant residency probability. Requires data file 'mapsdata.list' which can be sourced into your R workspace.
panel11pt4.fn.RCormack-Jolly-Seber (CJS) model -- R script to fit hierarchical formulation of the CJS model in WinBUGS. The small yellow warbler data set (Table 11.3) is contained within the R script. Results should coincide with Table 11.4.
panel11pt3.fn.RBayesian Nest survival analysis -- R script for fitting the nest survival model described in Panel 11.3 in WinBUGS. Requires nestdata.list. Results should coincide with Table 11.2.
nestdata.list.RNest survival data. American redstart data used in the analysis in Panel 11.2 -- a simple nest survival model. Data courtesy of Beth Hahn (U.S. Forest Service).
panel11pt2.fn.RNest survival analysis -- R script for optimizing the nest survival model likelihood shown in Panel 9.2 for the American redstart data. The model is a simple constant + AGE effect model. Requires nestdata.list
Chapter 12Models of Community Composition and Dynamics
detectionFreq.NH17.csvDetections of species observed in the North American Breeding Bird Survey in 1991 (Route 017, New Hampshire)
MultiSpeciesOcc.RModel of species occurrence in a community of unknown species richness
MultiSpeciesOcc.knownN.RModel of species occurrence in a community of known species richness
Chapter 13Looking Back and Ahead

download all in a single zipfile

arrowreturn to main page