inst/doc/ascent_tutorial.R

## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  warning = FALSE,
  message = FALSE,
  fig.width = 10,
  fig.height = 5.5
)

## ----data---------------------------------------------------------------------
library(ascent)
# birds_sim is loaded with the package

traits  <- birds_sim$traits    # species x traits (categorical traits as factors)
counts  <- birds_sim$counts    # surveys x species, counts of individuals
samples <- birds_sim$samples   # site and time of each survey

head(traits)
table(samples$site, samples$time)

## ----space--------------------------------------------------------------------
q <- assess_functional_space(traits)
q
summary(q)

## ----paired-------------------------------------------------------------------
res <- asc_paired(traits, counts,
                  sites = samples$site, time = samples$time,
                  ref_time = "Before")
res <- asc_null(res, n_perm = 199, seed = 1)
summary(res)

## ----trait_shifts-------------------------------------------------------------
res$trait_shifts

## ----blocks-------------------------------------------------------------------
# Suppose the k-th survey before and after the event was made in the same survey round
round <- ave(seq_len(nrow(samples)), samples$site, samples$time, FUN = seq_along)
res_b <- asc_paired(traits, counts, sites = samples$site, time = samples$time,
                    ref_time = "Before", blocks = round)
res_b <- asc_null(res_b, n_perm = 199, seed = 1)
res_b$null_settings$permutation

## ----paired_table, echo = FALSE-----------------------------------------------
nm <- res$null_models
pos <- nm[nm$Layer == "Position", c("Contrast", "Test", "SES", "P_upper", "P_lower")]

## ----transitions--------------------------------------------------------------
tr <- asc_transitions(res)
summary(tr, n_sp = 5)
plot(res, contrast = "Logged", type = "both", n_sp = 5)

## ----entities, fig.width = 7, fig.height = 5----------------------------------
ent <- asc_entities(traits, k_max = 10, B = 50, seed = 1)
summary(ent)
plot(ent)

## ----entities_use-------------------------------------------------------------
res_e <- asc_null(res, n_perm = 199, seed = 1, entities = ent)
res_e$null_models[res_e$null_models$Layer == "Position", c("Contrast", "Test", "SES", "P_upper")]
asc_transitions(res, entities = ent)$Logged$entities

## ----pairwise-----------------------------------------------------------------
pw <- asc_pairwise(traits, birds_sim$landscape)
pw <- asc_null(pw, n_perm = 199, seed = 1)
summary(pw)
plot(pw, contrast = "Primary_vs_Farmland", type = "both", n_sp = 5)

## ----baseline-----------------------------------------------------------------
summary(asc_baseline(traits, birds_sim$landscape))

Try the ascent package in your browser

Any scripts or data that you put into this service are public.

ascent documentation built on Nov. 30, 2026, 1:07 a.m.