This is an annotated version of the R code used to perform the examples in my chapter Analogue Methods in Palaeolimnology . The latest version of the script can be found on github .
## merge the data to common set of taxa...
dat <- join ( swapdiat , rlgh , verbose = TRUE )
## ...and split apart converting to %
swapdiat <- dat $ swapdiat / 100
rlgh <- dat $ rlgh / 100
## Fit the MAT model using Chord distance
swap.mat <- mat ( swapdiat , swappH , method = "chord" )
## Bootstrap the model
set.seed ( 1234 ) ## make this reproducible
swap.boot <- bootstrap ( swap.mat , n.boot = 1000 )
swap.boot
## RMSEP of the model
RMSEP ( swap.boot )
## Optimal value of `k` derived from bootstrap
K <- getK ( swap.boot )
K
## get predictions for the RLGH data for the optimal `k` using
## the MAT model...
rlgh.mat <- predict ( swap.mat , rlgh , k = K )
## ..and bootstrap derived sample-specific predictions
rlgh.boot <- predict ( swap.mat , rlgh , k = K , bootstrap = TRUE )
## produce fig 1 - MAT reconstruction with sample-specific errors on
## age and depth scales -----------------------------------------------
reconPlot ( rlgh.boot , depths = age , ylab = "pH" , xlab = "Age (yr. BP)" ,
display.error = "bars" , predictions = "bootstrap" )
par ( new = TRUE )
depths <- as.numeric ( rownames ( rlgh ))
plot ( rlgh.mat $ predictions $ model $ predicted [ K , ] ~ depths , type = "n" ,
axes = FALSE , ann = FALSE , xlim = rev ( range ( depths )))
axis ( 3 , at = pretty ( depths ), labels = pretty ( depths ))
mtext ( "Depth (cm.)" , side = 3 , line = 2.5 )
## --------------------------------------------------------------------
## reproduce figure 2 - left hand panel -------------------------------
## form a new data object on fusion of RLGH and SWAP
dat <- join ( swapdiat , rlgh , split = FALSE )
max.abb <- apply ( dat , 2 , max ) ## maximum abundance per species
n.occ <- colSums ( dat > 0 ) ## number of occurrences per taxon
## select only taxa with max abundance greater than 5% and present
## in 40 or more samples
spp.want <- which ( max.abb >= 0.05 & n.occ >= 40 )
## subset out this smaller data set
diat2 <- swapdiat [, spp.want ]
## extract the dissimilarity matrix for the full SWAP MAT model
Dij <- as.dist ( swap.mat $ Dij )
## compute the kernel density estimate of the dissimilarities
dens <- density ( Dij , from = 0 , to = sqrt ( 2 ))
## extract the low quantiles of this density estimate
quant <- quantile ( Dij , probs = c ( 0.01 , 0.02 , 0.025 , 0.05 , 0.1 ))
## Fit a MAT to this smaller data set
swap.mat2 <- mat ( diat2 , swappH , method = "chord" )
## extract the dissimilarity matrix from the MAT model
Dij2 <- as.dist ( swap.mat2 $ Dij )
## compute the kernel density estimate of the dissimilarities
## for the smaller data set
dens2 <- density ( Dij2 , from = 0 , to = sqrt ( 2 ))
## extract the low quantiles of this new density estimate
quant2 <- quantile ( Dij2 , probs = c ( 0.01 , 0.02 , 0.025 , 0.05 , 0.1 ))
## Do the plotting
plot ( dens , main = "" , type = "n" , bty = "n" , xlab = "" )
abline ( h = 0 , col = "grey" )
lines ( dens , col = "red" , lwd = 2 )
lines ( dens2 , col = "blue" , lwd = 2 , lty = "dashed" )
rug ( dens $ x [ which.max ( dens $ y )], col = "red" , lwd = 2 ,
side = 1 , ticksize = 0.03 )
rug ( dens $ x [ which.max ( dens $ y )], col = "red" , lwd = 2 ,
side = 3 , ticksize = 0.02 )
rug ( dens2 $ x [ which.max ( dens2 $ y )], col = "blue" , lwd = 2 ,
side = 1 , ticksize = 0.03 )
rug ( dens2 $ x [ which.max ( dens2 $ y )], col = "blue" , lwd = 2 ,
side = 3 , ticksize = 0.02 )
abline ( v = quant [ 1 ], lwd = 2 , col = "red" )
abline ( v = quant2 [ 1 ], lwd = 2 , col = "blue" , lty = "dashed" )
legend ( "topleft" , legend = paste ( "m =" , c ( NCOL ( swapdiat ), length ( spp.want ))),
lty = c ( "solid" , "dashed" ), lwd = 2 , col = c ( "red" , "blue" ),
bty = "n" , inset = 0.02 )
axis ( 3 , at = quant [ 1 ], label = round ( quant [ 1 ], 3 ), las = 2 )
axis ( 3 , at = quant2 [ 1 ], label = round ( quant2 [ 1 ], 3 ), las = 2 )
box ()
## --------------------------------------------------------------------
## reproduce right hand panel of figure 2 -----------------------------
## uses objects computed above
plot ( dens , main = "" , type = "n" )
abline ( h = 0 , col = "grey" )
abline ( v = quant [ -2 ], lty = "dashed" , lwd = 2 )
lines ( dens , col = "red" , lwd = 2 )
axis ( 3 , at = quant [ -2 ], labels = names ( quant [ -2 ]), las = 2 )
## --------------------------------------------------------------------
## reproduce figure 3
## Screeplot of LOO and bootstrap RMSEP as a function of the number of
## `k` closest analogue used
screeplot ( swap.boot , sub = "" , ylab = "RMSEP" , main = "" ,
col = rep ( "black" , 2 ))
## Monte Carlo resampling, via
## * usual pairwise random selections
swap.paired.mc <- mcarlo ( swap.mat , nsamp = 10000 , type = "paired" )
## * pairwise sampling but with replacement
swap.boot.mc <- mcarlo ( swap.mat , nsamp = 10000 ,
type = "paired" , replace = TRUE )
## plot the observed and resampled distributions
plot ( swap.paired.mc , which = 1 )
lines ( dens , col = "red" , lwd = 2 , lty = "dashed" )
legend ( "topleft" , legend = c ( "Observed" , "Resampled" ),
lty = c ( "dashed" , "solid" ), lwd = 2 : 1 , col = c ( "red" , "black" ),
bty = "n" , inset = 0.02 )
## Reproduce Figure 4 in its entirety ---------------------------------
layout ( matrix ( 1 : 2 , ncol = 2 ))
plot ( swap.boot.mc , which = 1 , caption = "" )
lines ( dens , col = "black" , lwd = 2 , lty = "dashed" )
legend ( "topleft" , legend = c ( "Observed" , "Resampled" ),
lty = c ( "dashed" , "solid" ), lwd = 2 : 1 , col = rep ( "black" , 2 ),
bty = "n" , inset = 0.02 )
crit <- plot ( swap.boot.mc , which = 2 , caption = "" , alpha = 0.05 )
legend ( "topleft" , legend = expression ( alpha == 0.05 ),
fill = "grey" , bty = "n" , inset = 0.02 , box.lwd = 0 )
layout ( 1 )
## --------------------------------------------------------------------