Genotypes are from Brenna Forester tutorial
Original wolf data are from Schweizer et al. (2015) Molecular Ecology, Dryad c9b25

Please see README.md at https://github.com/z0on/RDA-forest for RDAforest package installation instructions and overview of functions.

Loading packages:

library(RDAforest)
library(terra)
library(viridis)
# rendering copy: rnaturalearth unavailable here, so the same Natural Earth
# country outlines are read from a local shapefile (1:110m rather than 1:50m)
landscape=sf::st_read("/home/claude/work/ne/geopandas/datasets/naturalearth_lowres/naturalearth_lowres.shp",quiet=TRUE)
landscape=landscape[landscape$continent=="North America",]

Loading all data:

ll=load("./wolf_v5.RData")
ll
## [1] "geno"     "env"      "latlon"   "envc"     "envf"     "ecotype"  "env.full"

What are those dataframes?

geno: genotypes of 94 wolves at sites with allele freq >0.05, in 0,1,2 format
env: subset of environmental variables that we will use for modeling, for the 94 wolves
latlon: coordinates of sampled wolves
envc: raster of coordinates and variable values for today
envf: raster of coordinates and variable values for future (2041-2060)
ecotype: table of sample names and their designated ecotype, according to Schweizer et al., 2015
env.full: all bioclimatic variables (+ treecover) for the 94 wolves. Will not use it here (for consistency with the RDA-forest paper), but you can try exploring how it will work if we use all 19 variables instead of a small subset.

Data exploration

Let’s compute genetic distances based on genotypic correlation, and make an unconstrained ordination:

# distances:
cordist=1-cor(t(geno))
# ordination:
ord=capscale(cordist~1)

Proportion of variance explained by PCs (“MDS” is the mathematically identical to principal components so we will call them PCs):

plot(ord$CA$eig/sum(ord$CA$eig),xlab="PC",ylab="proportion of variance explained")

So we clearly have some very interesting leading principal components, perhaps 7-10.
Extracting scores, plotting ordination:

so=data.frame(scores(ord,scaling=1,display="sites"))
ggplot(so,aes(MDS1,MDS2,color=ecotype$ecotype))+geom_point()+coord_equal()+theme_bw()

This looks pretty but the question is, how much of this structure is due to just isolation-by-distance (IBD)? Meaning, the fact that wolves found near each other are more similar genetically?

Exploring IBD

Plotting genetic distance against geographic distance to see if there is a sloping cline (or multiple sloping clines):

# converting lat, lon to great circle distances
GCD=gcd.dist(latlon)
latlon.gcd=GCD[[1]]
distGCD=GCD[[2]]

plot(as.dist(cordist)~distGCD,pch=16,cex=0.6,col=rgb(0,0,0,alpha=0.2))

# Alternative: use Universal Transverse Mercator coordinates instead of great circle-based 
# (for situations when you want to keep spatial coordinates among predictors and then use them for inferring adaptation on landscape)
# epsg.code=epsg.maker(mean(range(latlon$lon)),mean(range(latlon$lat)))
# latlon.utm=latlon2UTM(latlon, epsg.code)

Looks like there is a sloping cline! To make it more formal, let’s test for significance of correlation between genetic and geographic distances:

protest(capscale(distGCD~1),capscale(cordist~1))
## 
## Call:
## protest(X = capscale(distGCD ~ 1), Y = capscale(cordist ~ 1)) 
## 
## Procrustes Sum of Squares (m12 squared):        0.402 
## Correlation in a symmetric Procrustes rotation: 0.7733 
## Significance:  0.001 
## 
## Permutation: free
## Number of permutations: 999

Definitely a highly significant correlation. We must regress latitude and longitude out of genetic data when computing ordination. Let’s see how it will look then:

latlon.gcd=GCD[[1]]
ord1=capscale(cordist~1+Condition(as.matrix(latlon.gcd)))
plot(ord1$CA$eig/sum(ord1$CA$eig),xlab="PC",ylab="proportion of variance explained")

so1=data.frame(scores(ord1,scaling=1,display="sites"))
ggplot(so1,aes(MDS1,MDS2,color=ecotype$ecotype))+geom_point()+coord_equal()+theme_bw()

Cleaning predictors

Let’s see if any of our predictors (bioclimatic variables + tree cover) are super correlated with each other

pc=hclust(as.dist(1-cor(env)))
plot(pc)
abline(h=0.1, col="red")

Variables below red line are correlated with r > 0.9! Still let’s keep all of them for now, for consistency with the RDA-forest paper.

Exploratory RDA-forest analysis

We will use all predictors and more leading PCs (40, specified by the option pcs2keep=c(1:40)) that we will likely need, just to see how it looks. Note that we are using ord1 ordination object here, the one with lat and lon regressed out.

gf=makeGF(ord1,env,pcs2keep=c(1:40))
## Data appear to contain multiple samples per site. Such samples will be represented as their genetic median; see option site.repeats for alternatives.
##   (94 samples in 86 sites of 1 to 4 samples, identified by identical predictor rows)

Note the message: some wolves were sampled at the same place and share all their environmental values (94 samples in 86 sites). By default such samples are represented by their genetic median, one synthetic individual per site; see site.repeats for alternatives. Here the sites are recognised by identical predictor rows; to recognise them by coordinates instead, pass coords=latlon. The same detection runs again in mtrySelJack() and ordinationJackknife() below, where the progress messages are hidden.

Which PCs are predicted?

# predicted PCs, and how well they are predicted (per-PC R2s)
gf$result
##        MDS1        MDS2        MDS3        MDS4        MDS5        MDS6 
## 0.519871858 0.826956293 0.508968548 0.379356841 0.298219829 0.384054193 
##        MDS7        MDS8        MDS9       MDS12       MDS15       MDS17 
## 0.191390732 0.012436886 0.054604321 0.030182645 0.058977855 0.009816913 
##       MDS21 
## 0.025680855

These values are cross-validation R-squares (R2), the proportion of variance explained along each PC. Some are pretty high!
Looks like we must keep the first 15 MDSes .

So how much total variance does our model capture?

# rescaling to proportion of total variance (based on eigenvalues in ord1)
eigen.var=(ord1$CA$eig/sum(ord1$CA$eig))[names(gf$result)]
# total variance explained by model
sum(eigen.var*gf$result)
## [1] 0.1172054

about 12.5%, it looks like.

Let’s plot importances of all predictors. These are properly scaled to correspond to the proportion of variance explained by the predictor in the whole dataset.

# setting the number of PCs to keep
tokeep=15
# computing properly scaled importances:
imps=data.frame(importance_RDAforest(gf,ord1))
# some data frame housekeeping...
names(imps)="R2"
imps$var=row.names(imps)
# reordering predictors by their importances:
imps$var=factor(imps$var,levels=imps$var[order(imps$R2)])
# plotting
ggplot(imps,aes(var,R2))+geom_bar(stat="identity")+coord_flip()+theme_bw()

R2 values above may seem low to people used to linear regressions. The main reason is that, unlike in linear regressions, the R2 here is computed on samples that were not used for model building (“hold-out” samples). So this is a true predictive power. In linear regression, the model fit is tested on the data used to build the same model, which always over-estimates the predictive power of the model (I wonder why people keep doing this).

Turnover curves

Turnover curves is the central concept of the gradient forest method, Ellis et al 2012, greatly helping interpretation of random forest models. These curves reflect how much the predicted variable (y axis) changes as you move along the range of predictor (x axis). Importantly, although turnover curves look like monotonous accumulation of difference across the predictor scale, this may not necessarily be the case. The key meaningful information in these plots are boundaries between distinct states, which look like steps, and the magnitude of transitions, which are heights of those steps. Remember that multiple subsequent steps may lead back to the same state.

Let’s plot turnover curves for the top 6 predictors:

plot_gf_turnovers(gf,imps$var[1:6])

The colored plots are turnovers of individual PCs (from MDS1 to MDS8), and the black-and-white plots are their averages. Colored plots don’t have predictor labels below x-axes, but their order corresponds to the black and white plots, which are labeled. You can see three things:
- which PC is affected by which variable. For example, PC1 (MDS1, red line on colored plots) is strongly affected by MaxT_WarmM (and a couple other variables).
- where in the predictor range the community transitions to a different state. For example, for MinT_ColdM the largest transition is at about -13 degrees C (black and white plot in the middle of upper row).
- scale of the y axes shows how much variance is explained by each predictor for each PC (colored plots), and average across all PCs (back and white curves). Proportion of total data variance explained is lower because each PC explains a different fraction of it; we will have properly scaled turnover curves later on.

Variable selection

To discard predictors that only appear important because they are correlated with some truly important variables, we use the mtry criterion. mtry is the number of randomly chosen predictors to find the next split in the tree. With higher mtry there is a higher chance that the actual driver is chosen together with the non-influential correlated variable and is then used for the split. As a result, the correlated variable is used less often, which drives its importance down, as observed in simulations by Strobl et al 2008. So, we fit two models with different mtry settings to each ordination jackknife replicate. Predictors consistently showing diminished raw importance at the higher mtry setting are then discarded.

This procedure can be made less strict (i.e. retain more predictors) by setting prop.positive.cutoff, the proportion of replicates in which importance has to increase under higher mtry, to something less than 0.333 (the default). Another way to achieve less strict selection is to manually specify mintry = 3, maxtry = 6 (these would be used by default for situations with less than 12 predictors). 
Note: this step will take a bit.

mm=mtrySelJack(Y=cordist,X=env,covariates=latlon.gcd,nreps=31, prop.positive.cutoff=0.5,top.pcs=tokeep)

Which environmental variables pass the selection process?

mm$goodvars
## [1] "MeanT_DryQ" "MaxT_WarmM" "T_Season"

Dropped variables that are strongly correlated (|r| >= 0.7, set by report.cor) with retained ones. The mtry test treats these as proxies of the retained variables, which is usually right; but two variables that are both real drivers and correlated can lose to each other the same way, so it is worth a look whether any of these is plausibly a driver in its own right:

mm$dropped.pairs
##      dropped       kept          r  importance prop.positive    reason
## 1 MinT_ColdM MeanT_DryQ  0.9174740 0.058962766    0.41935484 mtry test
## 2  Ann_meanT MeanT_DryQ  0.8885876 0.035012041    0.03225806 mtry test
## 3 MinT_ColdM   T_Season -0.8845405 0.058962766    0.41935484 mtry test
## 4 Prec_ColdQ   T_Season -0.7538575 0.030969327    0.12903226 mtry test
## 5 Prec_WarmQ   T_Season -0.7518116 0.036838779    0.38709677 mtry test
## 6  Prec_DryQ   T_Season -0.7415069 0.026448518    0.12903226 mtry test
## 7  Ann_meanT MaxT_WarmM  0.7358923 0.035012041    0.03225806 mtry test
## 8 Prec_WarmQ MeanT_DryQ  0.7172637 0.036838779    0.38709677 mtry test
## 9      trees MaxT_WarmM  0.7099947 0.006806463    0.06451613 mtry test

Boxplot of predictor importance change depending on mtry:

ggplot(mm$delta,aes(var,values))+
  geom_boxplot(outlier.shape = NA)+
  coord_flip()+
  geom_hline(yintercept=0,col="red")

Bar chart of proportion of positive change in response to higher mtry. Good variables would be the ones above the red line (change yintersept setting here to match prop.positive.cutoff in the call to mtrySelJack) :

ggplot(mm$prop.positive,aes(var,prop.positive))+
  geom_bar(stat="identity")+
  coord_flip()+
  geom_hline(yintercept=0.5,col="red")

Assessing confidence in importances; forming predictions

We are only using the variables we selected above. Note that we are still using latlon.gcd as a covariate, to account for IBD. newX here is a dataframe of environmenal conditions across the landscape where adaptation is to be predicted. This data table must contain all the predictors that the model uses.

We will make predictions for the present-day environment, and for the future environment.

Both environments are scored in one run: newX is given as a named list, and every jackknife replicate scores both of them before its models are discarded. This matters for the genetic offset computed further below: the offset is the difference between the two predictions, so if each were made by its own ordinationJackknife() run, that difference would carry the sampling noise of two independent sets of forests on top of the environmental change we actually want to measure. Scoring both through the same replicates removes it, halves the running time, and, because no replicate’s models are kept once they have been used, keeps the memory flat. Some places on the landscape, especially in the future, have environments unlike any where wolves were sampled, and predictions there are extrapolations. Two tests decide how far is too far, and a place is predicted only if it passes both. Both measure distance in the space of the model’s predictors, counting each predictor by its importance in the fit and taking their correlations into account (see ?envNovelty):
- sigma (default 4): distance from the centre of the sampled environments, in standard deviations of a single variable, so sigma = 4 means “as unusual as a 4-SD value” whatever the number of predictors. It catches values beyond the sampled range and combinations of values the samples never showed.
- nn.max (default 3): distance to the nearest sampled environments (the mean over the 5 nearest), in units of how far apart the sampled environments typically are. It catches what the distance from the centre misses when the samples are lopsided, as here: wolves were sampled at the cool end of the summer-temperature gradient, so places much hotter than any sampled wolf can still be close to the centre. nn.max = "auto" sets a stricter, data-driven limit.
Either test can be switched off with Inf, and each can take one value per environment. To use the rule of earlier versions instead, give extra (and optionally extra.npred): for example extra = c(0.1, 0.2) lets each predictor reach 10% (present) or 20% (future) of its span beyond the sampled range.

ncores runs replicates in parallel. Each worker needs its own memory (roughly 0.8 GB here), so set it to what your RAM allows, not just to the number of cores; a worker that runs out of memory is killed and the function stops saying so.

res=ordinationJackknife(Y=cordist,X=env[,mm$goodvars],covariates=latlon.gcd,nreps=25,top.pcs=tokeep,
                        newX=list(present=envc,future=envf),sigma=4,nn.max=3,ncores=1)
oj=res$present   # present-day
ojf=res$future   # future (2041-2060)
# save(mm,gf,oj,ojf,file="models_wolf.RData")

To compare several future scenarios, add them to the same list (for example newX=list(present=envc,ssp245=envf245,ssp585=envf585)): every scenario is then scored through the same replicates, at hardly any extra cost.

To score scenarios that are not yet at hand, run with keep.models=TRUE and score them later with ojPredict(res, newX=...), through the same replicates. The kept models take about 1.2 GB here.

Where are the predictions extrapolations?

Every result carries the two novelty measures for each location, $novelty$sigma and $novelty$nn, the limits used, $limits, and $goodrows, which says which locations passed both tests and were predicted. Mapping each measure as a fraction of its limit shows where the model is on firm ground (light), where it is stretched (dark), and which places were left out (red):

lim=res$present$limits
nov=do.call(rbind,lapply(c("present","future"),function(w){
  xy=(if(w=="present") envc else envf)[,c("x","y")]
  rbind(data.frame(xy,when=w,test="distance from centre (sigma)",rel=res[[w]]$novelty$sigma/lim["sigma"]),
        data.frame(xy,when=w,test="distance from nearest samples (nn)",rel=res[[w]]$novelty$nn/lim["nn.max"]))
}))
nov$when=factor(nov$when,levels=c("present","future"))
ggplot()+geom_sf(data=landscape)+
  xlim(range(nov$x))+ylim(range(nov$y))+
  theme_minimal()+
  geom_raster(data=nov,aes(x=x,y=y,fill=pmin(rel,1)))+
  scale_fill_viridis(option="mako",direction=-1,name="novelty / limit")+
  geom_raster(data=subset(nov,rel>1),aes(x=x,y=y),fill="red")+
  facet_grid(when~test)

Plotting predicted turnover curves for the top three predictors. This time the numbers on y axis are meaningful - they reflect the proportion of total variance explained by the predictor. Vertical grey stripes indicate where the original data points used to build the model fall.

vars=names(env)[names(env) %in% mm$goodvars]
for(i in 1:length(vars)){
 plot_turnover(oj,envc[oj$goodrows,],vars[i],envdata=env[,vars[i]])
}

Plotting boxplots of importance (proportion of total variance explained)

ggplot(oj$all.importances,aes(variable,importance))+geom_boxplot(outlier.shape = NA)+coord_flip()

Plotting maps of predicted adaptive neighborhoods

Forming predictions based on averaging replicates from ordinationJackknife:

# spots on the map that are within modeled parameter range:
goods=oj$goodrows
# predictor data restricted to only those spots:
ras2=envc[which(goods),]
xy2=envc[goods,c("x","y")]
names(xy2)=c("lon","lat")
rfpreds=oj$predictions.direct
turnovers=oj$predictions.turnover
bests=head(names(oj$median.importance),4)  # up to four top predictors

Maps with PCAs and clustering info

First, we plot unclustered random forest predictions. This will produce two plots:
- PCA plot of predicted values with arrows showing how the top (up to four) variables that passed mtrySelJack procedure fit to it (by linear regression, RDA-style). Number of predictor arrows shown can be changed in the chunk above. The proportions of that plot are controlled by three parameters:
- rangeExp : increasing this value will make the PCA plot smaller relative to the plotting area
- scal : increasing this value will make the arrows shorter
- jitscale : increasing this value will increase the distance from arrow tips to arrow labels. The labels are jittered somewhat every time, so you might want to rerun the same command several times until you get them positioned nicely.
- Actual map of predicted adaptations, in square coordinates (we will replot it later in “nice” coordinates). Similar colors indicate similar adaptations. To change colors on this map (and on the PCA plot), try varying color.scheme option to plot_adaptation, like “001”,“100”,“010”,“111” etc. The saturation of the colors is controlled by lighten option; setting it to zero will produce maximum saturation.

pa0=plot_adaptation(rfpreds,ras2[,bests],xy2,main="unclustered",
                    # options affecting PCA plot:
                    rangeExp=1.5,
                    scal=10,
                    jitscale=0.05,
                    # options affecting colors:
                    color.scheme="001",
                    lighten=0.8
                    )

Clustering into “adaptive neighborhoods”.

Now our goal is to break the continuous colors in the map above into “adaptive neighborhoods” - bounded areas likely to contain similarly-adapted organisms. There are two ways doing it. One way is to simply cluster spatial points based on random forest predictions, corresponding to colors on the map above. We will ask for more clusters than we expect (based on the number of wolf ecotypes, which is six) with the idea that we will then merge the clusters that are too similar. There are two options affecting this process:
- nclust : number of initial clusters;
- cluster.merge.level : threshold for merging, as a fraction of the maximum dissimilarity observed between clusters.
With these options to plot_adaptation, an additional plot will be produced, showing hierarchical clustering tree of the spatial clusters, with the red line showing the merging threshold. This can be used as the guide to adjust nclust and cluster.merge.threshold, although there are no formal criteria how to do it.
Also note that the map now has numbers on it - these correspond to merged clusters in the tree plot.

pa1=plot_adaptation(rfpreds,ras2[,bests],xy2,main="direct preds",
                    # options affecting PCA plot:
                    rangeExp=1.5,
                    scal=10,
                    jitscale=0.05,
                    # options affecting map and PCA colors:
                    color.scheme="001",
                    lighten=0.8,
                    # options affecting clustering:
                    cluster.guide = NULL,
                    nclust=15,
                    cluster.merge.level=0.333 # the default
                    )

main title reflects clustering mode usedmain title reflects clustering mode usedmain title reflects clustering mode used

The second way of clustering is to form clusters based on turnover curves, but merge them according to similarity of direct random forest predictions within them. In simulations this mode generates less noisy adaptive neighborhood maps than clustering based on predictions themselves. This is done by including additional option, cluster.guide:

pa2=plot_adaptation(rfpreds,ras2[,bests],xy2,main="turnovers",
                    # options affecting PCA plot:
                    rangeExp=1.5,
                    scal=10,
                    jitscale=0.05,
                    # options affecting map and PCA colors:
                    color.scheme="001",
                    lighten=0.8,
                    # options affecting clustering:
                    cluster.guide = turnovers,
                    nclust=12,
                    cluster.merge.level=0.25
                    )

“Nice” maps: the same maps in Universal Transverse Mercator (UTM) coordinates

adding “overlay.points” of original samples, colored by ecotype.
Unclustered:

plot_nice_map(xy2,mapdata=landscape,map.on.top=F,size=0.8,cols=pa0$colors,overlay.points = latlon,size.points=2,cols.points = ecotype$ecotype) 

Direct clustering based on RF predictions.

plot_nice_map(xy2,mapdata=landscape,map.on.top=F,cols=pa1$colors,size=0.8,overlay.points = latlon,cols.points = ecotype$ecotype,size.points=2) 

That this map nicely separates ecotypes except it is having a bit of difficulty with “Arctic” - “High Arctic” distinction. Note that this map suggests that “West forest” and “Atlantic forest” ecotypes are similar, adaptation-wise.

Finally, clustering based on turnover curves.

plot_nice_map(xy2,mapdata=landscape,map.on.top=F,size=0.8,cols=pa2$colors,overlay.points = latlon,cols.points = ecotype$ecotype,size.points=2) 

For this dataset, the maps based on different clustering look quite similar. They generally recapture designated ecotypes and identify major boundaries between arctic, mid-continent, and the British Columbia environments. There remains some uncertainty about boundaries between ecotypes that are more similar to each other: the boundary between Arctic and High Arctic and the boundary between Boreal Forest and other two forest ecotypes.

Genetic offset: where wolves are the most endangered by climate change

Genetic offset is simply the Euclidean distance between future and present-day gPC predictions for each point on the landscape. To make it more interpretable, we scale it to the 90%-th quantile of the difference observed between present-day gPC predictions across landscape: then the offset of 1 would mean that survival at this specific location in the future will require almost as much adaptation as currently observed across the whole study area.

We have already generated future predictions (ojf), so let’s compare them to the present-day (oj). Both were scored through the same jackknife replicates, so what follows is the environmental difference alone, with no contribution from two models having been fitted on different random draws. The function gen_offset_oj() requires four arguments: the two prediction objects for present-day and future, and two sets of spatial coordinates, for present-day and future predictions (for aligning the two).

OFFS=gen_offset_oj(X=oj,Y=ojf,sx=envc[,1:2],sy=envf[,1:2])

# plotting the offset as a raster on the map (square coordinates):

# # simple way
# plot(terra::rast(OFFS),col=rev(map.pal("inferno")))

# fancy way
#pdf("wolves_gen_offset.pdf",height=5, width=7)
gg=ggplot()+geom_sf(data=landscape)+
  xlim(min(OFFS$x),max(OFFS$x))+
  ylim(min(OFFS$y), max(OFFS$y))+
  theme_minimal()+
  geom_raster(data=OFFS,aes(x=x,y=y,fill=offset))+scale_fill_viridis(option="inferno",direction = -1)
plot(gg)

#dev.off()

Here (and in the subsequent maps) darker color means worse. We see that East and West coast wolves are not very endangered, Arctic wolves are in real trouble, and mid-continent wolves are in between the two extremes.

Assisted gene flow planning

Let’s imagine we are managing a wildlife park, and we want to breed local wolves with wolves from elsewhere to bring genetic variants that would promote adaptation to future conditions. Where should we get such wolves from? From a location where wolf adaptation now is similar to what will be needed in our park in the future. In other words, we need to find where present-day gPCs predictions are similar to the future gPCs predictions at our park’s location. Function env_mismatch() does that:

# lat, lon coordinates of our park (somewhere in the middle of Canada)
park=c(-107, 55)
# raster of future gPCs
rfut=terra::rast(data.frame(cbind(envf[ojf$goodrows,1:2],ojf$predictions.direct)))
# extracting future adaptation needed in our park
park.fut=terra::extract(x = rfut, y = data.frame(rbind(park)), method="bilinear" )[-1]
# rescaling factor: 90% quantile of present-day adaptation disparity
sc=adapt_scale(oj$predictions.direct)[2]

# computing distances between future-needed and present-day adaptation
agf=env_mismatch(X=park.fut,Y=oj,sy=envc[,1:2],sc=sc)

# plot agf suitability, simple way
# plot(terra::rast(agf),col=rev(map.pal("inferno")))
# points(park[1],park[2],pch=0)

# fancy way
#pdf("wolves_agf.pdf",height=5, width=7)
gg=ggplot()+geom_sf(data=landscape)+
  xlim(min(agf$x),max(agf$x))+
  ylim(min(agf$y), max(agf$y))+
  theme_minimal()+
  geom_raster(data=agf,aes(x=x,y=y,fill=env.mismatch))+scale_fill_viridis(option="inferno",direction = -1)+
  geom_point(data=data.frame(rbind(park)),aes(X1,X2),pch=0,size=2,col="cyan")
plot(gg)

#dev.off()

Our park is the little square in the middle. The preferred area for our assisted gene flow provenance is south-southeast of the park.

Habitat suitability for a given individual

Now consider a situation when we have a genotyped wolf of unknown origin, and we want to release it into the wild so it has the best chance of survival. This is the same problem that we just solved above for assisted gene flow: we need to calculate environmental mismatch across lanscape for a specific vector or gPCs, only this time this vector characterizes an individual wolf, not the future adaptation needed at our wildlife park.

So let’s pick an “lone wolf” from our pack of 94, build a ordinationJackknife model without it, and then predict where it belongs.

#  This one is High Arctic ecotype. Feel free to pick any other, it is quite fun.

i=65
#print(ecotype[i,])

# data to build a model
geno0=geno[-i,]
# our lone wolf
geno.i=geno[i,]

# distance matrix for model building
cordist0=1-cor(t(geno0))

# adding the lone wolf to the matrix, to predict its gPCs later
cordist.i=1-cor(t(rbind(geno0,geno.i)))

Building the landscape model without the lone wolf (will take a while…):

oj0=ordinationJackknife(Y=cordist0,X=env[-i,mm$goodvars],newX=envc,covariates=latlon.gcd[-i,],nreps=30,top.pcs=tokeep)

Now we place the unknown wolf on the genetic PCs of the model and calculate its mismatch with the landscape. projectOrdination() does the placing; predict(..., type = 'sp'), used in earlier versions of this script, is not suitable for this, because it projects the distances themselves rather than the double-centred squared distances the principal coordinates are made of, which stretches some axes and shrinks others:

# placing the lone wolf on the ordination of the other wolves: the same ordination
# (capscale of cordist0 conditioned on latlon.gcd[-i,]) that oj0 was built on.
# The model wolves land exactly on their own scores; the lone wolf is placed by
# Gower's add-a-point formula. Its location is supposed to be unknown, and with it
# its latlon.gcd covariates, so it is projected without the covariate adjustment.
pj=projectOrdination(cordist.i, model.rows=1:nrow(cordist0), covariates=latlon.gcd[-i,])
# the lone wolf's predicted gPCs, on the same axes as oj0's predictions
wi=pj$scores[nrow(cordist.i),1:tokeep]
sc=adapt_scale(oj0$predictions.direct)[2]

# calculating environmental mismatch
home=env_mismatch(X=wi,Y=oj0,sy=envc[,1:2],sc=sc)

# plot habitat mismatch for our wolf:

# # simple way
# plot(terra::rast(home),col=rev(map.pal("magma")))
# # this is where the wolf actually came from:
# points(latlon[i,1],latlon[i,2],pch=0)

# fancy way
#pdf("wolves_loneWolf.pdf",height=5, width=7)
gg=ggplot()+geom_sf(data=landscape)+
  xlim(min(home$x),max(home$x))+
  ylim(min(home$y), max(home$y))+
  theme_minimal()+
  geom_raster(data=home,aes(x=x,y=y,fill=env.mismatch))+scale_fill_viridis(option="inferno",direction = -1)+
  geom_point(data=data.frame(latlon[i,]),aes(lon,lat),pch=0,col="cyan",size=2)
plot(gg)

#dev.off()

Looks like our lone wolf wandered to the edge of its comfort zone - the lightest area on the map (best habitat suitability for its genotype) shows that it belongs to the High Arctic (which is the correct ecotype), but the location where it was sampled is in a darker area next to it.