The previous post started a series of 3 posts dedicated to the package that I have developed with my colleagues. After using 3 dimensional distance matrices, we will now move to connectivity using resistance distances with spatial climate layers.
In this example we will see how to include climate as a resistance layer to connectivity. Once we have that relation established we will proceed to the interpolation of lineages using phylin. Since we are using a climate layer, we can use results from climate models or reconstructions to assess the connectivity during other periods. In this example we will use current climate and future climate predictions (2061-2080).
I’m using the term connectivity as the resistance offered by the landscape/environment to the movement of some organism. The concept is identical to circuitscape that introduced the concept of isolation by resistance. Usually the resistance is optimized for our species of interest based on landscape features such as altitude, slope, land cover and other variables that might be important to explain space usage by the species. We will be using in this example a single layer of climate. The idea behind this example is a species that has a distribution constrained to lower values of temperature.
In this example we will be using two other packages along with the last version of phylin. We will need the package raster to open the climate layers in a spatial grid format and also the package gdistance that provide us the methods to calculate a resistance distance based on spatial data.
I’ve also set the seed so you should get the same results as shown here.
We will need two different types of data for this example: the climate layers and species’ occurrence. For the first we will be using real data and for the second we will randomly generate with a few constraints.
The climate data I’m using here comes from CHELSA climate. I’ve downloaded and processed the Annual Mean Temperature for the present and for the future (2061-2080 average). I chose a single climate model (CCSM4) and a single Representative Concentration Pathway (RCP85) which is the most severe greenhouse gas emission scenario. This largely simplifies this example as many models and RCPs are used to cover a range of possible scenarios.
The rasters are originally available at 30’’ spatial resolution but I have upscaled them to 1’ (~2km). I’ve also clipped both rasters to a smaller extent. This allows faster processing and a smaller file size for storing and downloading. You can find here the processed files for present and future.
Be sure to save files on the working directory for your R session and import them:
We will need a grid that defines the area for interpolation. It is usually a good idea to have an interpolation area with a smaller extent than the one used to calculate the resistance distances. It avoids edge effects on the calculations of the distance matrix, particularly if you have samples near the edge (for example, if you use a minimum convex polygon with the samples to define the study area).
In this example I’m eliminating a border of one pixel around the original raster:
We will generate species data randomly. The idea here is to restrict the occurrence to low temperatures which are located in the elevated areas of the study region. We randomly sample 100 points from the raster where the temperature is less than 10°C. Than we use longitude to classify two lineages: lineage 1 on the west side and lineage 2 on the east.
Let’s do a fast plot just to check the data:
Now we have to create a resistance layer from the climate layer. This step is often done using some optimization procedure, niche modeling or with some expert-based decisions. Here we simplify the process: we transform the original data with a logistic function setting the inflection point to 12°C.
Although we speak about resistance, in gdistance conductance is preferred. Conductance is simply 1/resistance. The resulting raster has values of 1 (high conductance) for low temperature values and values towards 0 for higher temperatures. With increasing temperature, the conductance decreases, until we have zero conductance for high temperatures (to visualize the relation run
To calculate a distance matrix using the conductance layer we have to run two extra steps that produce a transition matrix and some geocorrections if needed. I won’t enter into details about this code here (you can find a good explanation in gdistance vignette).
Custom distance function
At this point we have what we need to calculate distances based on the climate resistance with gdistance. We still need to wrap the code in our own function with the mandatory arguments for phylin (see the previous post).
Within the function we will
- create a matrix with from coordinates above the to coordinates
- use commute distance (equivalent to the circuit theory) with both transition matrix and coordinates objects
- filter coordinates to return a matrix with from locations on rows and to locations on columns.
This is the same function as shown in the resistance vignette.
As you see, this function is slightly more complex than the one in the previous post because it needs three arguments. The coordinate matrices (‘from’ and ‘to’ arguments) that are mandatory to use in phylin plus a ‘tr’ that is the transition matrix needed for the calculation of the distances. As you will see below, we will have to pass this layer through the krig function when interpolating. This function also benefits simplicity in detriment of efficiency: it calculates many distances that are discarded in the last step. This will slow down the interpolation but it is still useful.
We can now use the function to calculate distances between samples:
We simulated the occurrences and we don’t have genetic distances. So, let’s create a simple genetic distance matrix similarly to what we did in the previous post. We derive a matrix that is linearly related to the climate resistance distanced plus a error term. Also, to simplify the variogram, genetic distances greater than 4 units of resistance are kept constant. The log makes the variogram easier to fit in this case.
Variogram and interpolation
It is time to create a variogram with our data. We will need the genetic and resistance distance matrices.
Now we can interpolate both lineages. I’m using the study area raster as a template to create another one (filled with zeros) that will be used to store the interpolation results. The loop interpolates both lineages and classify as presence pixels with prediction higher than 0.75. It stores 1 for first lineage and 2 for the second lineage.
An important detail in the code above is how we feed the transition matrix through the krig function. As said before, the distance function requires two mandatory arguments for source and target coordinates. However, our res.dist has a third argument that is the transition matrix. We have to convey this info through the krig by naming the argument. So, our distance function can have as many arguments as needed for the internal calculations but we have to be careful not to use same names as those arguments for krig function.
We can plot the raster with:
Now it is time to interpolate based on the future climate layer. For that we repeat the process using the same formula, creating a conductance for the future layer. We use this layer to build a transition matrix as well.
Now we can proceed to the interpolation directly. Notice that I’m using the same variogram model we fitted before with current climate. We assume that this relation with space (i.e. resistance distances) is stable across periods. Otherwise we could not calculate a new model as we can’t possibly now if we can find the species in the future at the same locations we have observed in the present. We proceed with the interpolation repeating the same code as before, but using a new raster to store information and feeding the new transition matrix.
We can now plot the final results.
I’m just doing a simple trick to join both maps and keep track of the information. By multiplying future predictions by 10 we have 1 and 2 for each lineage only in the present, 10 and 20 for lineage presence only in the future, and 11 and 22 for lineages in both periods. This example is quite simple, so it allows to do that.
This last figure shows that climate might force contraction of the ranges. Both lineages are affected, but lineage 2 shows larger isolated patches that suggest a possible fragmentation of the distribution.
This is a extremely simplified example just to illustrate the possible application in the context of climate change. Real cases would require, for instance, more effort in optimizing the resistance distances, likely with more variables that would alone bring more complexity to the system. I hope to have opportunity to test this with real data soon but if you try it, let me know the issues and suggestions!