
Predict site-level responses for residents and simulated invaders
Source:R/predict_invader_response.R
predict_invader_response.Rd
Builds a complete site × species prediction grid by crossing a species-trait table
(residents + simulated invaders) with a site-environment table, then calls the fitted
model’s predict()
to obtain expected responses (e.g., abundance). The function is
robust to factor level issues and mirrors a typical expand_grid()
+ left-join workflow.
Usage
predict_invader_response(
model,
species_traits,
site_env,
species_col = "species",
site_col = "site_id",
response_type = "response",
include_random = FALSE,
site_aug = NULL,
species_aug = NULL
)
Arguments
- model
Fitted model object (glmmTMB, lme4, mgcv, or base stats).
- species_traits
data.frame
. Species traits (one row per species) containingspecies_col
and all trait variables referenced in the model’s fixed effects.- site_env
data.frame
. Site predictors (one row per site) containingsite_col
and all environmental variables referenced in the model’s fixed effects.- species_col
character
. Species ID column inspecies_traits
. Default"species"
.- site_col
character
. Site ID column insite_env
. Default"site_id"
.- response_type
character
. Prediction scale forpredict()
where applicable (e.g.,"response"
or"link"
). Default"response"
.- include_random
logical
. IfFALSE
(default), compute population-level predictions (exclude random effects). IfTRUE
, compute conditional predictions (include random effects; allow new levels where supported).- site_aug
NULL
ordata.frame
. Optional site-level augmentation table (must containsite_col
) to be left-joined intosite_env
before prediction.- species_aug
NULL
ordata.frame
. Optional species-level augmentation table (must containspecies_col
) to be left-joined intospecies_traits
.
Value
A list with:
newdata
: the site × species table used inpredict()
.predictions
: long table with columnssite_col
,species_col
, andpred
.prediction_matrix
: wide matrix (sites × species) of predicted values (rows ordered by site ID, columns by species ID).
Details
Model classes supported
glmmTMB: uses
type
andre.form
. Population-level predictions setre.form = ~ 0
(default,include_random = FALSE
); conditional predictions usere.form = NULL
withallow.new.levels = TRUE
.lme4::merMod (
lmer
/glmer
): population-level usesre.form = NA
; conditional usesre.form = NULL
withallow.new.levels = TRUE
.mgcv::gam: passes
type
through (e.g.,"response"
).stats::glm / stats::lm: standard
predict()
;type
for GLM, none for LM.
Why population-level predictions by default? For novel invaders, random-effect levels (e.g., species/site intercepts) are unknown. Excluding random effects yields fixed-effects expectations driven by traits, environments, and their interactions — appropriate for invasion screening and ranking.
Augmentation inputs. site_aug
and/or species_aug
let you add extra predictors
(e.g., obs_sum
, spp_rich
) prior to prediction, replicating the common
left_join()
pattern used in analysis notebooks.
Examples
if (FALSE) { # \dontrun{
# Residents + invaders (traits), sites (env), fitted Tweedie glmmTMB model:
all_traits <- rbind(spp_trait, inv_traits)
out <- predict_invader_response(
model = mod,
species_traits = all_traits,
site_env = site_env,
species_col = "species",
site_col = "site_id",
response_type = "response",
include_random = FALSE, # population-level
site_aug = dplyr::select(spp_rich_obs, site_id, obs_sum, spp_rich)
)
head(out$predictions)
dim(out$prediction_matrix)
} # }