
Prepare trait space and resident crowding (no site-z)
Source:R/prepare_trait_space.R
prepare_trait_space.RdOrchestrates the traits → space → centrality/hull → crowding pipeline for
residents and invaders. Optionally standardises trait inputs, constructs a
joint trait space, computes convex hull / centroid / centrality, and derives
raw resident crowding \(C_{js}\) using a Gaussian kernel (no per-site
standardisation here). Row-wise z-scoring is deferred to
model_residents().
Plot objects are always created and returned; they are only displayed when
show_plots = TRUE.
Usage
prepare_trait_space(
fit,
traits_inv,
crowding_sigma = NULL,
show_plots = TRUE,
do_standardise = TRUE,
row_z = FALSE
)Arguments
- fit
An
invasimapr_fitproduced byprepare_inputs()/assemble_matrices(). Must containfit$inputs$traits_res,fit$inputs$comm_res, and (optionally)fit$inputs$site_df.- traits_inv
Data frame (or matrix) of raw invader traits (rows = invaders; columns aligned to resident trait columns).
- crowding_sigma
Optional numeric bandwidth for the resident crowding kernel. If
NULL, a default/optimised value is chosen insidecompute_resident_crowding().- show_plots
Logical. If
TRUE, display plots as they are created. IfFALSE, plots are still created/returned but not shown. DefaultTRUE.- do_standardise
Logical. If
TRUEandstandardise_model_inputs()exists, standardise resident and invader trait inputs (environment handled later). DefaultTRUE.- row_z
Logical. Whether to perform row-wise (site) z-scoring inside
compute_resident_crowding(). LeaveFALSEhere to keep rawC_js; row-z is typically applied inmodel_residents().
Value
The input invasimapr_fit with updated components:
$traitsList with
gower,Q_res,Q_inv,hull,centroid,density, stored plots (plots_ts,plots_ch), centrality table, and hull vertices.$crowdingList with
W_site,D_res,sigma_alpha,K_res_res, and rawC_js(unlessrow_z = TRUE).$metaLightweight cache of
residents,sites,invaders, andn_invaders.
Details
Pipeline
(Optional) Standardise
traits_res/traits_invviastandardise_model_inputs()when available; otherwise pass through raw traits.Build a joint trait space with
compute_trait_space()(returns Gower distances, ordination scores for residentsQ_resand invadersQ_inv, a density surface, dendrograms, etc.). Plots are requested but may be hidden depending onshow_plots.Compute centrality & hull (resident convex hull, centroid, centrality scores) using
compute_centrality_hull().Compute resident crowding with
compute_resident_crowding(), returning kernel weightsK_res_res, distancesD_res, site kernelsW_site, chosensigma_alpha, and rawC_js(unlessrow_z = TRUEis requested).
Display control
To ensure reproducible plot objects without cluttering the console,
the function temporarily opens a null graphics device when show_plots = FALSE.
Assumptions & safeguards
Requires resident community and trait tables in
fit$inputs.If standardisation fails or is unavailable, the function proceeds with raw traits.
Site-wise z-scoring is intentionally not applied here by default.
Examples
if (FALSE) { # \dontrun{
# Compute all plots but do not display them
fit2 <- prepare_trait_space(fit, traits_inv, show_plots = FALSE)
# Display plots during construction
fit3 <- prepare_trait_space(fit, traits_inv, show_plots = TRUE)
# Use a fixed kernel bandwidth and request row-z inside the crowding step
fit4 <- prepare_trait_space(fit, traits_inv, crowding_sigma = 0.35, row_z = TRUE)
} # }