## Aims

By the end of this practical, you should feel comfortable:

• Fitting DSMs with multiple smooth terms in them
• Selecting smooth terms by \(p\)-values
• Using shrinkage smoothers
• Inspecting Q-Q plots select response distribution
• Selecting between models using AIC
• Investigating sensitivity and path dependence

``library(Distance)``
``## Loading required package: mrds``
``````## This is mrds 2.2.3
## Built: R 4.0.2; ; 2020-08-01 10:33:56 UTC; unix``````
``````##
## Attaching package: 'Distance'``````
``````## The following object is masked from 'package:mrds':
##
##     create.bins``````
``library(dsm)``
``## Loading required package: mgcv``
``## Loading required package: nlme``
``## This is mgcv 1.8-33. For overview type 'help("mgcv-package")'.``
``## Loading required package: numDeriv``
``````## This is dsm 2.3.0
## Built: R 4.0.2; ; 2020-07-16 23:56:50 UTC; unix``````
``````library(ggplot2)
library(patchwork)
library(knitr)``````

Load the data and the fitted detection function objects from the previous exercises:

``````load("spermwhale.RData")

## Exploratory analysis

We can plot the covariates together using the following code (don’t worry too much about understanding what that code is doing at the moment).

``````# make a list to hold our plots
p <- list()

# make a plot for each covariate
for(covname in c("Depth", "SST", "NPP", "DistToCAS", "EKE")){
# make
p[[covname]] <- ggplot() +
# covariates are plotted as tiles
geom_tile(aes_string(x="x", y="y", fill=covname), data=predgrid) +
geom_point(aes(x=x, y=y, size=size),
alpha=0.6,
data=subset(obs, size>0))+
# remove grey background etc
theme_minimal() +
# remove axis labels and fiddle with the legend
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.title.y=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
legend.position="right", legend.key.width=unit(0.005, "npc")) +
# make the fill scale be colourblind friendly
scale_fill_viridis_c()
}

# using patchwork to stick the plots together
p[["Depth"]] + p[["SST"]] + p[["NPP"]] + p[["DistToCAS"]] + p[["EKE"]]  + plot_layout(ncol = 3, nrow=3)``````