Skip to content

Plotting models

The SDMs are integrated with the Makie plotting package.

julia
using SpeciesDistributionToolkit
using CairoMakie

We will use the demonstration data from the SDeMo package:

julia
model = SDM(RawData, Logistic, SDeMo.__demodata()...)
❎  RawData → Logistic → P(x) ≥ 0.5 🗺️

Plotting instances

julia
scatter(model; axis=(; aspect=DataAspect()))

This can be coupled with information about the model itself, to provide more interesting visualisations:

julia
scatter(model; color=labels(model), axis=(; aspect=DataAspect()))

Model diagnostic plots

These plots require a trained model:

julia
train!(model)
☑️  RawData → Logistic → P(x) ≥ 0.582 🗺️

Ceteris paribus

The ceteris paribus plot allows seeing the effect of all possible values of a feature on a specific instance. For example, this is how the prediction for instance 4 is affected by a change in the BIO1 variable (mean annual temperature).

All of these plots have a feature (values of) in the x axis, and the resulting prediction on the y axis.

julia
cpplot(model, 4, 1)

This plot can be drawn as a line rather than stairs:

julia
cpplot(model, 4, 1; stairs=false)

The line attributes can be changed:

julia
cpplot(model, 4, 1; stairs=false, linewidth=2, color=:red, linestyle=:dash)

The plot can also be presented by centering the x axis to the midpoint value:

julia
cpplot(model, 4, 1; center=:midpoint)

Or to the value of the feature for this specific instance:

julia
cpplot(model, 4, 1; center=:value)

CP plots can also be produced for two variables:

julia
cpplot(model, 4, 4, 6; colormap=:YlGnBu)

Individual conditional expectations

The ICE plot is the superposition of multiple CP plots. It uses the same arguments, but the instances are given as a range or collection.

To plot all the instances, we can use:

julia
iceplot(model, 1)

Because this creates a lot of overplotting, it is a good idea to tweak the transparency:

julia
iceplot(model, 1; alpha=0.2, center=:midpoint, stairs=false)

The list of instances to use can also be given as a collection:

julia
iceplot(model, findall(labels(model)), 1; alpha=0.2, stairs=false, color=:darkgreen, label="Presence")
iceplot!(model, findall(!, labels(model)), 1; alpha=0.2, stairs=false, color=:grey50, label="Absence")
axislegend(current_axis())
current_figure()

Partial dependence

The partial dependence plot is the average of all CP plots. The instances to use in it are specified like in the ICE plots.

julia
partialdependenceplot(model, 1)

We can also pass a ribbon function to draw a band around the line:

julia
import Statistics
partialdependenceplot(model, 1; ribbon=Statistics.std, stairs=false, background=:skyblue, color=:darkblue)

We can also specify to only run the plot for some instances:

julia
partialdependenceplot(model, findall(labels(model)), 1; ribbon=Statistics.std, stairs=false, background=:grey95, color=:orange)