DOI: 10.6084/m9.figshare.3568977
title: Supplement 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.

description: File List fitness.r (MD5: 970fccf39d71a26e27dffc1d6446a9c7)Description The code is presented in nine sections described below. <b>Section 0: Functions and libraries</b> 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. <b>Section 1: Population simulation</b> 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. <b>Section 2: Generation of fitting data frames</b> 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. <b>Section 3: Spatial model fitting</b> 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. <b>Section 4: Some exploratory plots</b> 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). <b>Section 5: Calculation of constructed covariates for spatial population model</b> 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 <i>φ</i> expressions in Eq. 31 of the main paper are calculated. <b>Section 6: Fitting the population models</b> 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. <b>Section 7: Goodness-of-fit plots</b> 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. <b>Section 8: Prediction plots</b> 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.