## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE, comment = "#>", fig.width = 6.5, fig.height = 4.5,
  dev = "svglite", fig.ext = "svg", dev.args = list(pointsize = 14)
)
has_ggplot <- requireNamespace("ggplot2", quietly = TRUE)

## ----install-cran, eval = FALSE-----------------------------------------------
# install.packages("spacc")

## ----install-dev, eval = FALSE------------------------------------------------
# # install.packages("pak")
# pak::pak("gcol33/spacc")

## ----simulate-----------------------------------------------------------------
library(spacc)
set.seed(42)
n_sites <- 80
n_species <- 40
coords <- data.frame(x = runif(n_sites, 0, 100), y = runif(n_sites, 0, 100))
species <- matrix(0L, n_sites, n_species)
for (j in seq_len(n_species)) {
  cx <- runif(1, 20, 80)
  cy <- runif(1, 20, 80)
  p <- exp(-0.001 * ((coords$x - cx)^2 + (coords$y - cy)^2))
  species[, j] <- rbinom(n_sites, 1, p)
}
colnames(species) <- paste0("sp", seq_len(n_species))

## ----first-call---------------------------------------------------------------
sac <- spacc(species, coords, n_seeds = 30, progress = FALSE)
sac

## ----curves-------------------------------------------------------------------
dim(sac$curves)
sac$curves[1:3, 1:8]

## ----summary------------------------------------------------------------------
summary(sac)

## ----plot-sac, eval = has_ggplot, fig.alt = "Spatial accumulation curve: the mean number of species rises from about ten at one site to forty at eighty sites, with a shaded band that is widest in the first third of the curve."----
plot(sac)

## ----as-df--------------------------------------------------------------------
head(as.data.frame(sac), 3)

## ----random-------------------------------------------------------------------
rnd <- spacc(species, coords, n_seeds = 30, method = "random",
             progress = FALSE)
both <- c(spatial = sac, random = rnd)

## ----plot-both, eval = has_ggplot, fig.alt = "Two accumulation curves on the same axes: the random-order curve rises faster than the spatial curve and both reach forty species at eighty sites."----
plot(both)

## ----compare------------------------------------------------------------------
set.seed(1)
compare(sac, rnd, n_perm = 199)

## ----write-csv----------------------------------------------------------------
field <- data.frame(plot = sprintf("P%02d", seq_len(n_sites)),
                    easting = coords$x, northing = coords$y, species)
path <- tempfile(fileext = ".csv")
write.csv(field, path, row.names = FALSE)

## ----read-csv-----------------------------------------------------------------
dat <- read.csv(path)
sp_cols <- setdiff(names(dat), c("plot", "easting", "northing"))
x <- as.matrix(dat[, sp_cols])
xy <- data.frame(x = dat$easting, y = dat$northing)
spacc(x, xy, n_seeds = 30, progress = FALSE)

## ----err-numeric, error = TRUE------------------------------------------------
try({
spacc(dat[, c("plot", sp_cols)], xy, progress = FALSE)
})

## ----fix-numeric--------------------------------------------------------------
spacc(dat[, sp_cols], xy, n_seeds = 30, progress = FALSE)

## ----err-coords, error = TRUE-------------------------------------------------
try({
spacc(x, dat[, c("easting", "northing")], progress = FALSE)
})

## ----fix-coords---------------------------------------------------------------
names(xy)
spacc(x, xy, n_seeds = 30, progress = FALSE)

## ----err-rows, error = TRUE---------------------------------------------------
try({
keep <- dat$easting < 50
spacc(x[keep, ], xy, progress = FALSE)
})

## ----fix-rows-----------------------------------------------------------------
spacc(x[keep, ], xy[keep, ], n_seeds = 30, progress = FALSE)

## ----abund--------------------------------------------------------------------
set.seed(7)
abund <- matrix(rpois(n_sites * n_species, lambda = 2), n_sites, n_species)
colnames(abund) <- colnames(species)

## ----hill, eval = has_ggplot, fig.alt = "Three Hill-number accumulation curves: q = 0 highest, q = 1 in the middle, q = 2 lowest."----
hill <- spaccHill(abund, coords, q = c(0, 1, 2), n_seeds = 20,
                  progress = FALSE)
plot(hill)

## ----beta---------------------------------------------------------------------
beta <- spaccBeta(species, coords, n_seeds = 20, progress = FALSE)
as.data.frame(beta)[c(1, 5, 20), 1:4]

## ----coverage-----------------------------------------------------------------
cov <- spaccCoverage(abund, coords, n_seeds = 20, progress = FALSE)
colMeans(interpolateCoverage(cov, target = c(0.90, 0.95)))

## ----chao2--------------------------------------------------------------------
chao2(species)

## ----extrapolate--------------------------------------------------------------
fit <- extrapolate(sac, model = "michaelis-menten")
fit

## ----decay--------------------------------------------------------------------
betaDecay(species, coords, model = "exponential", progress = FALSE)

## ----metrics------------------------------------------------------------------
m <- spaccMetrics(species, coords, metrics = c("slope_10", "auc"),
                  progress = FALSE)
head(as.data.frame(m), 3)

## ----subset-------------------------------------------------------------------
sac[1:5]

## ----autoplot, eval = has_ggplot, fig.alt = "The spatial accumulation curve with a horizontal dashed line at forty species."----
ggplot2::autoplot(sac) +
  ggplot2::geom_hline(yintercept = n_species, linetype = "dashed")

