Book Companion
Applied Statistical Modelling for Ecologists
Edition 1
Welcome to the Companion Site for Applied Statistical Modelling for Ecologists: A Practical Guide to Bayesian and Likelihood Inference Using R, JAGS, NIMBLE, Stan and TMB, 1st Edition.
Welcome to the Companion Site for Applied Statistical Modelling for Ecologists: A Practical Guide to Bayesian and Likelihood Inference Using R, JAGS, NIMBLE, Stan and TMB, 1st Edition.
Applied Statistical Modelling for Ecologists provides a gentle introduction to the essential models of applied statistics: linear models, generalized linear models, mixed and hierarchical models. In addition, it introduces integrated models, which have become a mega-trend in statistical ecology during the last 30 years. All models are fit with both a likelihood and a Bayesian approach, using several powerful software packages widely used in research publications: JAGS, NIMBLE, Stan, and TMB. In addition, the foundational method of maximum likelihood is explained in a manner that ecologists can really understand, and every model is also fit by numerical maximization of a custom-written likelihood function in R. Finally, the ASM book contains a comprehensive chapter on model building, checking and selection, all of which topics that are challenging for budding ecological modelers.
This book is the successor of the widely used Introduction to WinBUGS for Ecologists (Kéry, Academic Press, 2010). Like its parent, it is extremely effective for both classroom use and self-study, allowing students and researchers alike to quickly learn, understand, and carry out a very wide range of statistical modelling tasks.
The examples in Applied Statistical Modelling for Ecologists come from ecology and the environmental sciences, but the underlying statistical models are very widely used by scientists across many disciplines. This book will be useful for anybody who needs to learn and quickly become proficient in statistical modelling, with either a likelihood or a Bayesian focus, and in the model-fitting engines covered, including the three latest packages NIMBLE, Stan, and TMB.
Buy the book from Elsevier opens in new tab/window.
About this Companion Site
This site contains supplemental materials and additional resources to accompany Applied Statistical Modelling for Ecologists. These supplemental materials include:
About the authors
About the book
Downloadable code – here you can download all code from the book either by chapter or in a single zipped file - includes code for the Bonus Chapter 19B that is not in the printed book!
R package ASMbook (https://cran.r-project.org/package=ASMbook opens in new tab/window) – the accompanying R package for this book is available for download on CRAN. The package contains utility functions needed for the book and functions for simulating and customizing the datasets used for the example models in each chapter, summarizing output from model fitting engines, and running custom Markov chain Monte Carlo.
Hierarchical models – explains how the content of the ASM book relates to the vast topic of hierarchical models and how it fits into a loose cluster of similar books on hierarchical models in ecology
Bonus Chapter 19B opens in new tab/window: Binomial N-mixture models – this free, online-only bonus chapter introduces the binomial N-mixture model, which is the 'sister model' of the occupancy model featured in Chapter 19 of the printed book. Two additional topics highlighted in detail are: (1) the use of the delta method and of the parametric bootstrap to compute standard errors associated with a function of basic model parameters estimated with maximum likelihood and (2) computation of spatial predictions for the production of species distribution maps. Although not included in the printed book, this chapter is an integral part of the book, and hence all code is available for download here opens in new tab/window, and the R package ASMbook contains a data simulation function also for this chapter.
Errata – any errors or clarifications related to the content in the book will be posted here, as needed.
Materials for instructors and students – advice for how to best use the book in class or for self-study, alongside other resources including a Google group mailing list, and a summary on the data simulation / data analysis dualism, and the ‘experimental approach’ to statistics.
About the Authors
Marc Kéry is a Senior Scientist with the Swiss Ornithological Institute, Sempach, Switzerland. He has published 140 peer-reviewed journal articles and authored six textbooks on applied statistical modelling. During the last 15 years, Marc has taught about 60 week-long training workshops on statistical modelling all over the world.
Kenneth F. Kellner is an Assistant Research Professor in the Department of Fisheries and Wildlife, Michigan State University, East Lansing, MI, USA. He has published more than 60 peer-reviewed journal articles and one textbook on applied statistical modelling. In addition, Ken has developed or contributed to several software packages that are widely used by ecologists and featured in several textbooks, including the R packages jagsUI, unmarked, and ubms.
About the Book
Target audience for Applied Statistical Modelling for Ecologists
We wrote this book with multiple audiences in mind. On the one hand are novices to statistical modeling who need to learn about t-test, regression, analysis of variance and analysis of covariance models, i.e., the bread and butter in much of applied statistical modeling. Our goal is to clarify the unity of all these techniques as specific instances of linear models. Then, we present the even greater unifying role of the generalized linear model (GLM), and finally the concept of random effects, which lead to mixed and hierarchical models. These three basic concepts, linear modeling of covariate effects, GLMs and random effects/mixed models, are often very confusing to novices. The approach in the ASM book to explain them will be extremely useful to these budding ecological modellers.
A second audience of the book are more experienced researchers and managers. They may have been using canned functions in R such as lm(), glm(), lmer(), glmmTMB() or brms() for their statistical analyses for many years. But now they want to get a better understanding of what actually goes on under the hood when using maximum likelihood or Bayesian inference. Working through this book should help them understand much better the workings of these and many similar functions and consequently lead to their improved and more informed use.
A third audience also consists of more seasoned applied statistical modellers who want to learn the JAGS, NIMBLE, Stan or TMB model-fitting engines. These are often used for far more complex models than those in this book, but it would be very hard to learn these engines at the same time as trying to understand such complex models. In contrast, our approach of introducing JAGS, NIMBLE, Stan and TMB for mostly very simple statistical models should make it very easy for you to learn to "walk" in any of these engines. In addition, our ASM book adopts a "Rosetta stone approach", where we show the code for all these engines side by side. If you know one engine already, then learning another one is typically much easier in this comparative setting.
Finally, a fourth audience is also more advanced and wants to solidify and progress in their understanding of hierarchical models (Royle & Dorazio 2008, Cressie et al. 2009, Kéry & Schaub 2012, Hobbs & Hooten 2015, Hooten & Hefley 2019, Kéry & Royle 2016, 2021). If you belong to this group, you know already quite well how to fit and interpret GLMs and traditional mixed models. Most hierarchical models that you see in current research papers, and which you typically see people fit with one of the above model-fitting engines or with custom MCMC code (Hooten & Hefley 2019), can be understood conceptually as not much more than a variant of GLM. Only, there is not a single GLM, but there are two or more linked GLMs. Thus, for this type of audience, we aim to explain clearly how the building blocks (i.e., GLMs with linear models for covariate effects) of such more complex hierarchical models are constructed.
Downloadable code
Below you can download all code from the book, including the code for the bonus, online-only Chapter 19B. Additionally, there is some extended code for Chapters 2 and 18.
All code for the book is available in a single ZIP file here opens in new tab/window. Alternatively, the code for each chapter can be downloaded individually by clicking on the chapter number below (Chapters 1 and 21 do not contain any code): Chapter 2 opens in new tab/window Chapter 3 opens in new tab/window Chapter 4 opens in new tab/window Chapter 5 opens in new tab/window Chapter 6 opens in new tab/window Chapter 7 opens in new tab/window Chapter 8 opens in new tab/window Chapter 9 opens in new tab/window Chapter 10 opens in new tab/window Chapter 11 opens in new tab/window Chapter 12 opens in new tab/window Chapter 13 opens in new tab/window Chapter 14 opens in new tab/window Chapter 15 opens in new tab/window Chapter 16 opens in new tab/window Chapter 17 opens in new tab/window Chapter 18 opens in new tab/window Chapter 19 opens in new tab/window Chapter 19B opens in new tab/window Chapter 20 opens in new tab/window
Errata
Here is a full list of all the errors detected, in order of appearance in the book. If you find any errors that are not listed, please send us an email; thank you!
Currently, no errors have been discovered.
Materials for instructors and students
How to use Applied Statistical Modelling for Ecologists in classes and for self-study
Due to the highly modular structure of most chapters with multiple 'layers' or subsections, we can envision plenty of different ways in which you can use the ASM book either for self-study or to teach a course. While we believe the 'Rosetta stone approach' is a great benefit for learning a new computer language, you can also just pick one particular model-fitting engine and work through all its examples, ignoring the others. Thus, the book may either be used as a whole or else you can choose different layers, corresponding to topics or engines, to give you exactly what you need. For instance,
If you want to learn or improve your understanding of linear models in terms of how we specify the effects of continuous explanatory variables and of factors, you can study chapter 3 and then one of the layers in most chapters in the rest of the book, especially in chapters 4–9.
If you need an introduction to Bayesian inference and Bayesian statistical modeling, you can read most of chapter 2 and then select one of the Bayesian layers (typically JAGS or NIMBLE because they're the easiest; see Section 4.9) in most other chapters. And when you do this, don't forget the important chapter 18.
If you want to learn program NIMBLE, just take the NIMBLE layer in all chapters and add to it chapters 2 and 18, depending on whether you do or don't already have experience in Bayesian inference.
Similarly, if you want to learn program Stan, just take the Stan layer in every chapter.
If you want to learn how to get likelihood inference for very general models, read chapter 2 and then work through all DIY-MLE and TMB layers in the other chapters (again not forgetting chapter 18).
If you want to learn simple traditional statistics with linear models, GLMs, and mixed models with standard functions in R, do the non-Bayesian parts of chapter 2, all of chapter 3 and then the 'canned function' layers in all other chapters.
If you were never sure about the meaning of the parameter estimates that R functions lm() and glm() (and many others, too) produce, especially for factors: work through chapters 3 and 6–8.
If you were never quite sure what a link function was: read chapters 2, 3 and then 11–17.
If you never really understood what 'random effects' meant: read section 3.5 and chapters 7, 10, 14, 17, 19 and 19B.
If you were always puzzled by what 'hierarchical model' meant: again read sections 3.5, and chapters 7, 10, 14, 19, and 19B, especially the first and the last two.
If you were always confused by what exactly the response variable in a logistic regression was (i.e., is it the probability or is it a count? Or two counts ?), read chapters 15–17 and 19.
If you always wanted to know what a GLMM was, but never dared to ask: read sections 3.5 and chapters 14 and 17, and probably also chapter 10.
If you always felt intimidated by likelihood functions: read chapter 2 and then work through the DIY-MLE layers in most other chapters, especially for the simpler, non-hierarchical models in chapters 4–9, 11–13, 15 and 16.
Likewise, if you were wondering what these 'densities' were that statisticians always talk about: read chapters 2 and 18.
If you wanted to understand Bayesian information criteria (DIC and WAIC) and were wondering how they are related to the frequentist Akaike information criterion (AIC), read Section 18.6
And if you want to read all of what Ken and Marc would have liked to have in an introductory stats book when they were in grad school: read the whole book and work through all the examples.
We emphasize that by far the most effective manner to work through this book is of course to read the text, but especially to sit at your computer and run all the analyses. You can download all the R and other computer code from the book website; see above.
Owing to its extremely gentle style, we believe that the ASM book may also be used as a useful starter for more advanced courses on ecological modeling, e.g. when using maximum likelihood either from scratch (i.e., by the 'DIY-MLE engine' in our book) or when using package TMB, for instance in a course based on the recent book on spatio-temporal ecological models by Thorson and Kristensen (CRC Press, 2024). Alternatively, the ASM book may serve as a starter for more advanced courses on ecological modeling that use custom-built MCMC algorithms, such as in Hooten & Hefley (CRC Press, 2019) and Zhao (Springer, 2024). And finally, the ASM book will complement in an almost ideal manner most of the other books that we, Andy Royle, Bob Dorazio, Michael Schaub, Richard Chandler, Beth Gardner and Rahel Sollmann have written on hierarchical models in ecology; see the section below on 'Hierarchical models'.
We don't have any exercises in the book, but we encourage you to invent your own --- this is actually quite easy! You can do so by what we like to call 'addition and deletion exercises'. For instance, you can fit a new model which lacks one of the covariates that are included in a particular model in the book. On the other hand you can invent a covariate that has the right format (e.g., is a vector of the right length) and fill it with random numbers in R (e.g., using rnorm()) and then add it as another covariate into one of the models in the book. Of course, the resulting estimates for such a noise variable will not be meaningful. Nevertheless, this approach lets you learn how to fit a progression of models, starting from the large set of models for which code is provided in the book. In another type of exercise you may change the distribution assumed for a response, e.g., to turn a Poisson GLM into a normal-response GLM or vice versa. By inventing your own exercises in this way, you can greatly boost the learning effect produced by working through the ASM book.
Of course, you can also add the effect(s) of one or more covariates in the data simulation code already, rather than just in an enlarged model with an additional noise covariate. And you can tune the sample sizes so that they match the study that you have in mind or that you have already conducted. In this way, taking data simulation code in the book then leads directly to what we call 'experimental statistics'. That is, where you repeatedly simulate and analyse data sets to learn about study design, a model, whether and how well you can estimate its parameters, and many more; see below.
Google group mailing list
Our colleague Andy Royle hosts the HMecology Google group mailing list where you can ask questions about the contents of the book: https://groups.google.com/g/hmecology opens in new tab/window. This is a general list for questions about hierarchical models especially of the occupancy and N-mixture type, but also about demographic models such as survival or integrated population models, and about their implementation with JAGS or NIMBLE. The scope of this list is somewhat defined by the contents of the books of Royle, Dorazio, Kéry, and Schaub. Hence, this list is suitable also for far more complex models than those in the ASM book as long as they are implemented in these engines.
For questions about more advanced use of the model-fitting engines NIMBLE, Stan and TMB, their dedicated user group lists are your best bet at getting your problem solved:
The data simulation / data analysis dualism, and the ‘experimental approach to statistics’
Data simulation and data analysis are virtually the same thing, only they use a statistical model in opposite directions. On the one hand, when we analyse a data set, we assume a model and then deduce features of the model from the data; most importantly, we estimate the 'best values' for the model's parameters. On the other hand, when we simulate data under a model, we pick values for the parameters and possibly for covariates that affect the parameters, and then generate one or more data sets that this data-generating mechanism could produce. That is, when we use the model for data simulation, in essence we use it as a specialized sort of random number generator. There is a host of goals for which we can and should use data simulation in applied statistical modeling, ranging from questions about statistical power and sampling design, parameter identifiability, variance estimation, and assessment of bias and precision of the estimates under a model for a given design.
Arguably, one of the most important goals of data simulation is educational: we are convinced that writing computer code to simulate data under a certain model is the best way for you to prove to yourself that you have really understood a model. We think that if you can't simulate data under your model, then you probably haven't fully understood that model. Likewise, we think that R code to simulate data under a model serves as an extremely intuitive manner to explain and indeed to define a statistical model. Thus, we are convinced that the value of data simulation for applied statistical modeling can hardly be overstated.
One way to view this approach of simulating data sets under a model and then doing useful things with these data sets is by calling it 'the experimental approach to statistics'. We believe that the experimental approach to statistics can very often help mitigate to some degree the often limited formal training in probability and statistics that we ecologists have. So, if you fail to understand something in statistics, then why don't you simply try it out and 'see how things are' by simulating suitable data sets? Perhaps, in this way you can build an intuition and start to understand something which you may not be able to grasp by staring at the equations alone.
Note that we are *not* saying that it is a good thing that many of us ecologists have what turns out to be an insufficient training in mathematics and statistics. Rather, in an ideal world, we'd all receive a solid grounding in calculus, linear algebra, probability and statistics. However, we don't currently live in that ideal world. So, we suggest that as a band-aid experimental statistics can go a long way to help us understand some things to which we might otherwise not have any access in terms of understanding.
We'd like to note also that nothing of what we say here, and in the ASM book, about the use of simulated data sets is novel. Statisticians have used simulated data sets for many decades, and the use of simulated data sets is increasing also in ecology. But as ecologists we don't yet use data simulation, i.e., the experimental approach to statistics, as naturally in our daily work as we believe we should. That's why we put an incredible emphasis on this topic in the ASM book, where, with one exception in chapter 2, we only ever use simulated data.
We here provide a set of slides on the experimental approach to statistics, and on data simulation, which we have used in our courses. Along with it come three R code files that illustrate the use of data simulation for understanding a difficult statistical concept, for assessing whether a parameter in a model is identifiable, and for gauging the statistical power of different study designs opens in new tab/window.
Some slides and additional R code based on Applied Statistical Modelling for Ecologists
We here provide three sets of slides on different topics in the book, which we have also used in courses that we taught for this and other books.
Chapter 2: Here is a set of slides that we have used to teach the material in ASM book chapter 2, which is statistical inference opens in new tab/window. The chapter, and the slides, use functions in the ASMbook R package. In addition, we refer to another set of demo R code for Markov chain Monte Carlo (MCMC) which is applied to a logistic regression and which can be downloaded here opens in new tab/window.
Chapters 19 and 19B: This opens in new tab/windowis a set of slides that we use in our courses to introduce the modeling of distribution and abundance, i.e., the conceptual basis for much of what is done in the species distribution modeling (SDM) world. A simple statistical model adopted for distribution (or presence/absence) and abundance, on top of which we add a customized measurement error model, leads directly to the canonical hierarchical models that are so widely used now when we model distribution and abundance, while correcting for imperfect detection: the occupancy model in Chapter 19 of the book and the N-mixture model in Chapter 19B.
Chapter 20: Here opens in new tab/windowis a set of slides that accompany the material on integrated models in Chapter 20 of the book. These slides extend the content of the chapter in the printed book in one important way and show how a sequential Bayesian analysis is equivalent to the simultaneous analysis of all three data sets at once in an integrated model. In a sequential analysis, we first analyse the first data set using vague priors and then cycle through the other data sets, using the posterior distribution from one analysis as an informative prior in the next, and so forth, until we're through all the data sets. This additional material, which is also shown in the accompanying R code opens in new tab/window, can thus provide a new understanding of integrated models in general. In addition, it shows for the first time in the book how we can 'distil' information coming from another data set into an informative prior in a Bayesian analysis. Throughout the ASM book, we use vague priors only. These priors try to minimize the amount of information they contain and that goes towards the estimation, resulting in estimates that are mostly influenced by the information contained in the analysed data set. As we see throughout the book, in this case the resulting Bayesian estimates will numerically be very similar to maximum likelihood estimates in most cases (e.g., unless sample sizes are very small). However, one big advantage of Bayesian analysis is the relative ease with which we can incorporate external information into the estimation when fitting a model to a data set. So, we think that it is really useful to show at least once in our introductory book about Bayesian analysis how this can be achieved.
Hierarchical models
Hierarchical models describe a complex stochastic process as a sequence of sub-processes that are linked to each other by the rules of conditional probability. Technically, they represent the factorization of a complex likelihood into a sequence of usually much simpler conditional probability expressions. Typically, the statistical model for the observed data is written as a sequence of simpler submodels that embody subject-matter knowledge about the processes that underlie the observed data. Arguably the archetype of such a hierarchical model in the ASM book is the occupancy model (Chapter 19). It expresses the observed detection/nondetection data as a function of a partially latent stochastic process that determines whether each site is occupied or not, and then another, linked stochastic process that determines whether the target species is detected during a visit at an occupied site or not. It is partially latent in the sense that (under the assumptions of the model) an observed detection indicates presence of the species – so you have some observations of the true presence state – but an observed non-detection is completely ambiguous. The second process is an explicit measurement error model.
Hierarchical models have many advantages, but the biggest two are arguably:
They often make the fitting of a complex model easier.
They represent a principled approach to statistical modeling where, instead of doing relatively brain-free curve-fitting exercises, you think about the processes that likely gave rise to your data set and then represent them in your model. The result is often a more distinctly science-based model. And the act of hierarchical modeling almost always enforces a much clearer manner of thinking about a scientific problem than does the application of some out-of-the-box statistical procedure.
Only a minority of the models featured in the ASM book are hierarchical models: all the mixed models and then the occupancy and the N-mixture model. However, the ASM is an introductory book, and for many real-world statistical analyses with parametric statistical models, some form of hierarchical model is nowadays typically employed. Indeed, as we say in the concluding Chapter 21, perhaps the main direction that your statistical modeling may take after working through the ASM book is to delve much more seriously into some of the literature on hierarchical models.
There are now many good resources to learn hierarchical modeling. However, perhaps not surprisingly, we believe that the books that some colleagues and us have written since 2008 are particularly useful when you want to follow up your study of the ASM book with much more in-depth treatments of many different classes of hierarchical models for species distribution and abundance and population demography. Our books apply the principles of hierarchical modeling to a large range of problems and provide countless worked example analyses using both likelihood and Bayesian inference, and for the latter using the highly popular Bayesian BUGS modeling software (originally WinBUGS and then mostly JAGS).
See the landing page for our hierarchical modeling book cluster project for more information about hierarchical models in general and about our books in particular: https://www.mbr-pwrc.usgs.gov/pubanalysis/roylebook/ opens in new tab/window
Shop for books, journals, and more.
Discover over 2,960 journals, 48,300 books, and many iconic reference works.