Jason Matthiopoulos, John Fieberg, Geert Aarts, Hawthorne L. Beyer, Juan M. Morales, and Daniel T. Haydon. 2015. Establishing the link between habitat selection and animal population dynamics. Ecological Monographs 85:413436. http://dx.doi.org/10.1890/14-2244.1


Simulation code. Encompasses all functions and libraries required by individual based simulation, the implementation of the model fitting, as well as the goodness-of-fit and prediction experiments with graphical diagnostics.
Ecological Archives M085-016-S1.


File list (downloads)


Jason Matthiopoulos
University of Glasgow
Institute of Biodiversity, Animal Health and Comparative Medicine
College of Medical, Veterinary and Life Sciences
Graham Kerr Building
G12 8QQ, UK
E-mail: [email protected]

File list

fitness.r (MD5: 970fccf39d71a26e27dffc1d6446a9c7)


The code is presented in nine sections described below.

Section 0: Functions and libraries

This section loads all necessary R-libraries and introduces the four custom functions. Functions Espace.numer and Espace.approx are used to generate density plots of the availability of different habitats in a two-dimensional E-space. Espace.numer plots the actual availability (effectively a 2D histogram of the frequencies of different habitats). Espace.approx plots the approximate availabilities generated by the Gaussian decomposition of the library mclust. These two plots are used in the bottom row of Fig. 3 of the paper. Function environ is used to create environmental layers (e.g., distribution of food) across space. Function resCi, generates percentile confidence intervals from a distribution of residuals.

Section 1: Population simulation

This section contains the simulation of population redistribution and growth (fecundity and mortality) in response to the combined fitness afforded by the two environmental variables. At the very beginning of this section, the environmental layers are generated and the Gaussian decomposition of E-space is calculated (this step is described as Stage 1 in section 10 of the main paper). The code in this section also records the data and visualizes the outcome of each scenario graphically (example is shown in Fig. 3 in the paper). The simulation is outlined in section 11 of the paper.

Section 2: Generation of fitting data frames

The code in this section constructs the data frames used for fitting the habitat selection functions, as well as the population data to be used for the temporal dynamics model fit. Depending on the values chosen for the constants scmaxi, stp, stpc, these data can be thinned by scenario, years or spatial cells. This thinning approach was used to create the data-impoverishment experiments described in Appendix C.

Section 3: Spatial model fitting

Here, the spatial date-frame generated in Section 2 is used to fit Generalised Functional Responses to statistically describe the distribution of the animals in response to environmental covariates. This step is described as Stage 2 in section 10 of the main paper. The values for the Akaike Information Criterion of the GFR and a simple Generalised Linear Model are also compared.

Section 4: Some exploratory plots

Three composite plots can be generated here. The first compares a simple GLM Habitat Selection Function with the GFR HSF (both models are generated in the previous section). The second plot looks at a sample of spatial snapshots of usage from different scenarios and years in the simulated data, and compares those with the corresponding spatial fits generated by the GFR. The third plot looks at the collection of population trajectories from all scenarios. The red trajectories belong to the model fitting data set. The grey trajectories belong to the model validation data set (this is how Fig. 4 in the main paper is generated).

Section 5: Calculation of constructed covariates for spatial population model

This is the heart of the method. The implementation is described as stage 3 in section 10 of the main paper. Here, the values of the φ expressions in Eq. 31 of the main paper are calculated.

Section 6: Fitting the population models

The fitting part of the data is used to estimate the beta parameters of Eq. 31 in the main paper. This is the spatially explicit version of the population model. A mean field version of the model is also fitted and the AICs of the two models are compared.

Section 7: Goodness-of-fit plots

This section compares the quality of fit of the spatial and the mean field models to the data used for fitting both models. The comparison is described as simulation experiment 1 in section 12 of the main paper. This output is used in Figs 5a, 5b, 6a, 6c.

Section 8: Prediction plots

This section compares the quality of predictions from the spatial and the mean field models using the new scenarios from the validation data. The comparison is described as simulation experiments 2 and 3 in section 12 of the main paper. This output is used in Figs 5c, 5d, 5e, 5f, 6c, 6d, 6e, 6f.