fastrda is a high-performance implementation of Redundancy Analysis (RDA) written in C++ with Armadillo and OpenMP. It is designed for large-scale ecological, genomic, and other multivariate datasets where computational efficiency is critical.
Key Features:
anova_fastrda() and the S3 anova() method,
supporting both overall and axis-wise tests.print(), summary(),
predict(), anova(), plot(), and
biplot()), plus fastrda_scores() for score
extraction without masking vegan::scores().keep_workspace = TRUE/FALSE trades memory for permutation
tests.biplotrda() or S3 generics plot() /
biplot().System Requirements:
To demonstrate fastrda, we will use the classic
mite (oribatid mite) ecological dataset from the
vegan package.
Load the package:
Prepare the data:
# Load real-world ecological data: Mite dataset
if (!requireNamespace("vegan", quietly = TRUE)) {
stop("The 'vegan' package is required to run this example.")
}
data(mite, package = "vegan")
data(mite.env, package = "vegan")
# Apply Hellinger standardization to species abundance (response) data
Y <- vegan::decostand(mite, "hellinger")
# Select environmental variables (e.g., Substrate Density and Water Content)
# The intercept is removed because RDA expects only explanatory variables.
X <- model.matrix(~ SubsDens + WatrCont, mite.env)[, -1]Fit a standard RDA model using fastrda:
fit <- fastrda(
response = Y,
predictors = X,
scale = FALSE, # Hellinger-transformed community data: keep the variances
axes = 2,
scaling = 2,
keep_workspace = TRUE,
threads = 1, # Increase to use multiple CPU cores
verbose = TRUE
)Inspect the results using S3 methods:
# Inspect the components of model fit
names(fit)
[1] "raw_sample_scores" "sample_scores" "response_scores"
[4] "biplot_raw" "scaling_constants" "loadings"
[7] "coefficients" "lc_coefficients_raw" "eigenvalues"
[10] "prop_total" "prop_constrained" "total_inertia"
[13] "conditioned_inertia" "constrained_inertia" "unconstrained_inertia"
[16] "R2" "adj_R2" "rank"
[19] "n_axes" "n_samples" "df_model"
[22] "pseudo_F" "df_resid" "df_cond"
[25] "X_rcond" "X_rank_deficient" "Z_rcond"
[28] "Z_rank_deficient" "scaling" "X_center"
[31] "X_scale" "Y_center" "Y_scale"
[34] "Z_center" "Z_scale" "Z_coef"
[37] "center" "scale_x" "scale_y"
[40] "workspace" "call" "pred_names"
[43] "response_names" "sample_names" "site_names"
# Print concise model output
print(fit)
Call:
fastrda(response = Y, predictors = X, scale = FALSE, axes = 2,
scaling = 2, keep_workspace = TRUE, threads = 1, verbose = TRUE)
Inertia Proportion Rank Df
Total 0.3943 1.0000 NA 69
Constrained 0.1288 0.3268 2 2
Unconstrained 0.2654 0.6732 NA 67
Eigenvalues for constrained axes:
RDA1 RDA2
0.1138 0.0150
Pseudo-F : 16.2600
Adjusted R2 : 0.3067
# Summary of ordination model
summary(fit)
Call:
fastrda(response = Y, predictors = X, scale = FALSE, axes = 2,
scaling = 2, keep_workspace = TRUE, threads = 1, verbose = TRUE)
Inertia Proportion Rank Df
Total 0.3943 1.0000 NA 69
Constrained 0.1288 0.3268 2 2
Unconstrained 0.2654 0.6732 NA 67
Eigenvalues for constrained axes:
RDA1 RDA2
0.1138 0.0150
Pseudo-F : 16.2600
Adjusted R2 : 0.3067
# Eigenvalues
fit$eigenvalues
RDA1 RDA2
0.11381840 0.01501836
# R-squared values
fit$R2
[1] 0.326768
fit$adj_R2
[1] 0.3066715
# Pre-computed sample scores
head(fit$sample_scores)
RDA1 RDA2
1 0.12362776 -0.009630045
2 0.05135241 -0.368259498
3 0.12362094 -0.168981434
4 0.16031276 -0.221578959
5 0.32314863 0.332469214
6 0.31962429 -0.448221458
# Pre-computed response scores
head(fit$response_scores)
RDA1 RDA2
Brachy 0.09921318 0.05940153
PHTH 0.15842046 -0.03622157
HPAV -0.03903237 0.12883733
RARD 0.15460632 -0.01552277
SSTR 0.03918073 -0.05316673
Protopl 0.06240529 -0.01490285
# Response loadings (unit-norm right singular vectors; not correlations)
head(fit$loadings)
RDA1 RDA2
Brachy 0.08085389 0.13326747
PHTH 0.12910494 -0.08126318
HPAV -0.03180948 0.28904684
RARD 0.12599659 -0.03482537
SSTR 0.03193038 -0.11927967
Protopl 0.05085726 -0.03343458keep_workspace is a single on/off switch. With
keep_workspace = TRUE (default) fastrda
retains the orthonormal basis Q, the projected response
QtY, the residualized response Y_res and, for
partial RDA, the covariate basis Qz and the centered
predictors Xc. These are required by anova();
predict(type = "response") needs them only without
newdata, with store_fitted = FALSE and at full
rank. With keep_workspace = FALSE nothing is retained
(lowest memory use, no permutation tests). The legacy character values
"minimal", "compact" and "full"
are accepted and behave like TRUE; "none"
behaves like FALSE.
# Workspace retained (default) - supports permutation tests and prediction
fit_ws <- fastrda(response = Y, predictors = X, scale = FALSE, keep_workspace = TRUE, verbose = FALSE)
# No workspace - lowest memory, no permutation tests
fit_none <- fastrda(response = Y, predictors = X, scale = FALSE, keep_workspace = FALSE, verbose = FALSE)Often you want to remove the effect of known covariates (e.g.,
spatial structure, microhabitat type like Shrub) before
testing predictor variables.
# Define conditioning matrix Z (e.g., Shrub presence/absence or type)
Z <- model.matrix(~ Shrub, mite.env)[, -1]
# Partial RDA: Y ~ SubsDens + WatrCont | Shrub
fit_partial <- fastrda(
response = Y,
predictors = X,
covariates = Z,
scale = FALSE,
axes = 2,
scaling = 2,
keep_workspace = TRUE,
threads = 1,
verbose = FALSE
)
print(fit_partial)
Call:
fastrda(response = Y, predictors = X, covariates = Z, scale = FALSE,
axes = 2, scaling = 2, keep_workspace = TRUE, threads = 1,
verbose = FALSE)
Inertia Proportion Rank Df
Total 0.3943 1.0000 NA 69
Conditioned 0.0907 0.2301 2 2
Constrained 0.0601 0.1525 2 2
Unconstrained 0.2434 0.6173 NA 65
Eigenvalues for constrained axes:
RDA1 RDA2
0.0477 0.0124
Pseudo-F : 8.0310
Adjusted R2 : 0.1375The conditioned inertia is the variation explained by the conditioning variables, including the part it shares with the predictors (fractions [b] + [c] of the variation partitioning); the constrained inertia is the part of the response variation explained by the predictors after removing the covariates (fraction [a]).
Assess the statistical significance of the constrained model using
anova_fastrda() or the S3 generic anova().
For models with covariates (partial RDA) the default
scheme = "permute-x" keeps the residualized response fixed
and, in every permutation, the rows of the predictors are permuted and
re-residualized on the covariates. scheme = "freedman-lane"
permutes the residualized response instead; this is conservative (low
power) when the number of responses is large. Both are approximate
tests. Note that fastrda() defaults to
scale = TRUE; for Hellinger-transformed community data, as
here, use scale = FALSE.
# 999 permutations correspond to the minimum attainable p-value of 0.001.
# Larger numbers of permutations provide finer p-value resolution at the expense of longer computation time.
res_overall <- anova(fit, permutations = 999, threads = 1)
print(res_overall)
Permutation Test for Redundancy Analysis
Model: Constrained Inertia = 0.1288 (R2 = 0.3268)
Test Statistic (Pseudo-F): 16.2600
Degrees of Freedom: Model = 2, Residuals = 67
Permutations: 999 (scheme: permute-x)
Pr(>F) / p-value: 0.0010 ***# Test each constrained axis individually
res_axis <- fastrda::anova_fastrda(
fit,
by = "axis",
permutations = 999,
threads = 1
)
print(res_axis)
Unconditional Permutation Test for Redundancy Analysis Axes
Permutations: 999
Axis Eigenvalue Variance (%) Unconditional p Sig
RDA1 0.1138 88.34% 0.0010 ***
RDA2 0.0150 11.66% 0.0010 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Disclaimer:
Axis-wise permutation p-values quantify the extremeness of each constrained eigenvalue relative to its permutation distribution.Axis-wise tests are unconditional tests of each ordered eigenvalue, not sequential tests. They should be interpreted descriptively rather than as formal significance tests for axis inclusion. Each canonical axis is evaluated against its own permutation distribution.
The scaling parameter controls how sample, response, and
predictor biplot scores are scaled:
| Scaling | Sample Scores | Response Scores | Predictor Biplot Scores | Description |
|---|---|---|---|---|
| 0 | Unscaled | Unscaled | Unscaled | Raw scores |
| 1 | slam * const |
const |
slam |
Samples scaled by slam |
| 2 | const |
slam * const |
1 |
Response scaled by slam (default) |
| 3 | sqrt(slam) * const |
sqrt(slam) * const |
sqrt(slam) |
Symmetric scaling |
The unit-norm raw sample scores and response loadings are
multiplied by these factors, where
slam = sqrt(eigenvalue / total_inertia) (per axis) and
const = ((n - 1) * total_inertia)^(0.25). Raw predictor
biplot scores (fit$biplot_raw) are the correlations between
the centered (un-residualized) predictor variables and the unit-norm
sample scores, and are scaled without const (matching
vegan).
# Compare different scaling options
# Scaling = 2 is the default because it emphasizes response variable relationships and matches
# the most commonly used scaling in ecological RDA applications.
fit0 <- fastrda(response = Y, predictors = X, scale = FALSE, scaling = 0, axes = 2, keep_workspace = FALSE, verbose = FALSE)
fit2 <- fastrda(response = Y, predictors = X, scale = FALSE, scaling = 2, axes = 2, keep_workspace = FALSE, verbose = FALSE)
head(fit2$sample_scores)
RDA1 RDA2
1 0.12362776 -0.009630045
2 0.05135241 -0.368259498
3 0.12362094 -0.168981434
4 0.16031276 -0.221578959
5 0.32314863 0.332469214
6 0.31962429 -0.448221458
head(fit2$response_scores)
RDA1 RDA2
Brachy 0.09921318 0.05940153
PHTH 0.15842046 -0.03622157
HPAV -0.03903237 0.12883733
RARD 0.15460632 -0.01552277
SSTR 0.03918073 -0.05316673
Protopl 0.06240529 -0.01490285Extract sample, response, or predictor scores using
fastrda_scores() or dedicated helper functions:
# Extract both sample and response scores
sc_both <- fastrda_scores(fit, display = "both", choices = 1:2)
head(sc_both$samples)
RDA1 RDA2
1 0.12362776 -0.009630045
2 0.05135241 -0.368259498
3 0.12362094 -0.168981434
4 0.16031276 -0.221578959
5 0.32314863 0.332469214
6 0.31962429 -0.448221458
head(sc_both$response)
RDA1 RDA2
Brachy 0.09921318 0.05940153
PHTH 0.15842046 -0.03622157
HPAV -0.03903237 0.12883733
RARD 0.15460632 -0.01552277
SSTR 0.03918073 -0.05316673
Protopl 0.06240529 -0.01490285
# Get sample scores with dynamic re-scaling (e.g., scaling = 1)
sample_sc1 <- get_sample_scores(fit, scaling = 1)
head(sample_sc1)
RDA1 RDA2
1 0.06642355 -0.001879488
2 0.02759097 -0.071872890
3 0.06641988 -0.032979961
4 0.08613391 -0.043245375
5 0.17362346 0.064887731
6 0.17172988 -0.087478997
# Get response and predictor biplot scores
bp_scores <- biplot_scores(fit, type = "both", scaling = 2)
head(bp_scores$response)
RDA1 RDA2
Brachy 0.09921318 0.05940153
PHTH 0.15842046 -0.03622157
HPAV -0.03903237 0.12883733
RARD 0.15460632 -0.01552277
SSTR 0.03918073 -0.05316673
Protopl 0.06240529 -0.01490285
head(bp_scores$predictors)
RDA1 RDA2
SubsDens -0.09673542 -0.9953101
WatrCont -0.96523730 -0.2613751Use predict.fastrda() to get linear combination (LC)
scores or fitted response values. The columns of newdata
must have the same variables, order, and encoding as the predictor
matrix used to fit the model. For a partial RDA fit, the covariate
values for the rows of newdata must also be supplied
(predict(fit_partial, newdata = new_X, covariates = new_Z));
the predictors are then residualized on the covariates exactly as in the
fit.
# In-sample LC scores
lc_scores <- predict(fit, type = "lc")
head(lc_scores)
RDA1 RDA2
1 0.12362776 -0.009630045
2 0.05135241 -0.368259498
3 0.12362094 -0.168981434
4 0.16031276 -0.221578959
5 0.32314863 0.332469214
6 0.31962429 -0.448221458
# Predict for new predictor data
new_X <- head(X, 10)
new_lc <- predict(fit, newdata = new_X, type = "lc")
head(new_lc)
RDA1 RDA2
1 0.12362776 -0.009630045
2 0.05135241 -0.368259498
3 0.12362094 -0.168981434
4 0.16031276 -0.221578959
5 0.32314863 0.332469214
6 0.31962429 -0.448221458
# Reconstruct response matrix (type = "response")
pred_resp <- predict(fit, type = "response", rank = 2)
head(pred_resp[, 1:5])
Brachy PHTH HPAV RARD SSTR
1 0.2333160 0.06887523 0.2321238 0.06351154 0.019324967
2 0.2048422 0.07041546 0.1887400 0.05790424 0.035560321
3 0.2238497 0.07464611 0.2115936 0.06598406 0.027796892
4 0.2243656 0.08236401 0.2033849 0.07247331 0.032030942
5 0.2734324 0.08809205 0.2684112 0.08904840 0.008954043
6 0.2267085 0.11581156 0.1679666 0.10062199 0.050322724fastrda provides publication-ready
ggplot2-based biplots through direct calls to
biplotrda() or S3 methods plot() and
biplot(). Predictor arrows indicate the direction of
increasing values for each explanatory variable, whereas sample and
response positions summarize their relationships in canonical space.
# Create biplot with all components using S3 plot generic
plot(fit, axes = 1:2, scaling = 2, title = "Mite RDA Biplot (S3 method)")Control the number of parallel threads used by C++ routines in
fastrda:
# Use all available cores (the default is min(2, detectCores()))
fit <- fastrda(response = Y, predictors = X, scale = FALSE, threads = parallel::detectCores())
# Use 4 threads
fit <- fastrda(response = Y, predictors = X, scale = FALSE, threads = 4)Run time depends on the data dimensions, the BLAS library and the
number of threads. Speed gains are largest for many response variables;
state the comparison method, BLAS, n, p and
threads when reporting benchmarks.
For detailed documentation, argument descriptions, and additional examples for specific functions, you can access the built-in help pages directly in R:
fastrda provides a fast, memory-efficient implementation
of redundancy analysis for large ecological, genomic, and other
multivariate datasets. The package supports partial RDA, permutation
testing, prediction, multiple scaling options, and flexible
visualization through a consistent S3 interface.