library(geomorph)
# make a latticed cube
vert <- 8
template <- expand.grid(1:vert, 1:vert, 1:vert)
colnames(template) <- c("x", "y", "z")
# Groups
ng <- 10
nw <- 10
group <- factor(rep(1:ng, each = nw))
n <- length(group)
# add points for n specimens
LMlist <- lapply(seq_len(n), function(.){
as.matrix(template + rnorm(length(template),
mean = 0, sd = 0.3))
})High-dimensional Procrustes ANOVA
If you have high-dimensional shape data, Procrustes ANOVA to compare group means is probably pretty common. Becuase a Procrustes ANOVA requires a permutation procedure, with thousands of permutations, it can take a long time.
But not all functions are the same! Here we compare several that might be used by researchers who have high-dimensional (HD) shape data: RRPP::lm.rrpp & RRPP::anova.lm.rrpp, vegan::adonis2, and PERMANOVA::PERMANOVA.
Criteria
- Single core analysis. Not every function has parallel options, so for functions where it is an option, they will be constrained to use 1 core.
- 1,000 random permutations. Except in one case we only use 10, and it will be obvious why.
- ANOVA must be performed. This means for some functions first fitting many linear models then using
anova(). Others perform ANOVA inherently. - Data formatting. Any formatting is done prior to analysis but not counted as part of the analysis for measuring computation time.
Data
GPA
LMa <- simplify2array(LMlist)
GPA <- gpagen(LMa, print.progress = FALSE)
plot(GPA)RRPP
Y <- two.d.array(GPA$coords)
dim(Y)[1] 100 1536
system.time(
{fit1 <- lm.rrpp(Y ~ group,
iter = 999,
Parallel = 1,
turbo = TRUE);
AOV1 <- anova(fit1)}
) user system elapsed
0.172 0.015 0.196
AOV1
Analysis of Variance, using Residual Randomization
Permutation procedure: Randomization of null model residuals
Number of permutations: 1000
Estimation method: Ordinary Least Squares
Sums of Squares and Cross-products: Type I
Effect sizes (Z) based on F distributions
Df SS MS Rsq F Z Pr(>F)
group 9 0.06745 0.0074949 0.0688 0.7388 -0.75699 0.768
Residuals 90 0.91299 0.0101444 0.9312
Total 99 0.98045
Call: lm.rrpp(f1 = Y ~ group, iter = 999,
turbo = TRUE, Parallel = 1)
vegan
library(vegan)
D <- dist(Y)
system.time(AOV2 <- adonis2(D ~ group, permutations = 999, parallel = 1)) user system elapsed
0.284 0.000 0.285
AOV2Permutation test for adonis under reduced model
Permutation: free
Number of permutations: 999
adonis2(formula = D ~ group, permutations = 999, parallel = 1)
Df SumOfSqs R2 F Pr(>F)
Model 9 0.06745 0.0688 0.7388 0.767
Residual 90 0.91299 0.9312
Total 99 0.98045 1.0000
mvabund
library(mvabund)
system.time({
fit3 <- manylm(Y ~ group);
AOV3 <- anova(fit3, nBoot = 10)
}) user system elapsed
1.148 0.014 1.166
AOV3Analysis of Variance Table
Model: manylm(formula = Y ~ group)
Overall test for all response variables
Test statistics:
Res.Df Df.diff val(F) Pr(>F)
(Intercept) 99
group 90 9 1154 0.9
Arguments:
Test statistics calculated assuming uncorrelated response (for faster computation)
P-value calculated using 9 iterations via residual (without replacement) resampling.
PERMANOVA
library(PERMANOVA)
D2 <- DistContinuous(Y)
system.time(AOV4 <- PERMANOVA(D2, group, nperm = 999)) user system elapsed
2.572 0.013 2.585
summary(AOV4) ###### PERMANOVA Analysis #######
Call
PERMANOVA(Distance = D2, group = group, nperm = 999)
________________________________________________
Contrast Matrix
1 2 3 4 5 6 7 8 9 10
C 1 1 0 0 0 0 0 0 0 0 0
C 2 0 1 0 0 0 0 0 0 0 0
C 3 0 0 1 0 0 0 0 0 0 0
C 4 0 0 0 1 0 0 0 0 0 0
C 5 0 0 0 0 1 0 0 0 0 0
C 6 0 0 0 0 0 1 0 0 0 0
C 7 0 0 0 0 0 0 1 0 0 0
C 8 0 0 0 0 0 0 0 1 0 0
C 9 0 0 0 0 0 0 0 0 1 0
C 10 0 0 0 0 0 0 0 0 0 1
________________________________________________
PerMANOVA
Explained Residual df Num df Denom F-exp p-value p-value adj.
Total 0.06745442 0.9129917 9 90 0.7388284 0.781 0.781
________________________________________________
Contrasts
Explained Residual df Num df Denom F-exp p-value p-value adj.
C 1 0.0009017027 0.9129917 1 90 0.08888717 0.900 1.000
C 2 0.0043928641 0.9129917 1 90 0.43303545 0.630 1.000
C 3 0.0111176929 0.9129917 1 90 1.09594903 0.293 1.000
C 4 0.0035862398 0.9129917 1 90 0.35352083 0.706 1.000
C 5 0.0112344570 0.9129917 1 90 1.10745929 0.305 1.000
C 6 0.0074215398 0.9129917 1 90 0.73159328 0.456 1.000
C 7 0.0194521682 0.9129917 1 90 1.91753676 0.123 1.000
C 8 0.0005342433 0.9129917 1 90 0.05266412 0.945 1.000
C 9 0.0058249932 0.9129917 1 90 0.57421047 0.512 1.000
C 10 0.0029885175 0.9129917 1 90 0.29459915 0.710 1.000
Total 0.0674544185 0.9129917 9 90 0.73882839 0.781 0.781
________________________________________________
Initial Conclusions
- All results are similar. The
mvabund\(F\)-statistics are different because they are like the sum of \(p\) \(F\)-statistics rather than a single \(F\)-statistic calculated from the sum of \(p\) \(SS\) values. RRPPandveganare faster than the others.- It probably is not fair to include
mvabund::anova.manylmbecause it perfoms more of a MANOVA analysis, which requires inverting residual covariance matrices. - Regardless,
PERMANOVA::PERMANOVAis comparatively much slower.
Computation time profiles (increasing \(n\))
Let’s see how the top two functions (in terms of computational efficiency) perform as we increase the amount of data.
nscape <- seq(10, 50, 10)
Result <- sapply(nscape, function(ng){
group <- factor(rep(1:ng, each = nw))
n <- length(group)
LMlist <- lapply(seq_len(n), function(.){
as.matrix(template +
rnorm(length(template),
mean = 0, sd = 0.3))
})
LMa <- simplify2array(LMlist)
GPA <- gpagen(LMa,
print.progress = FALSE)
Y <- two.d.array(GPA$coords)
D <- dist(Y)
df <- rrpp.data.frame(group = group,
Y = Y)
t_RRPP <- system.time(
{fit1 <- lm.rrpp(Y ~ group,
iter = 999,
Parallel = 1,
turbo = TRUE,
data = df);
AOV1 <- anova(fit1)}
)[[3]]
t_adonis <- system.time(
AOV2 <- adonis2(D ~ group,
permutations = 999,
parallel = 1))[[3]]
c(n = n, t_RRPP = t_RRPP,
t_adonis = t_adonis)
})
plot(Result[1, ], Result[3,],
type = "o", pch = 16, col = 2,
lwd = 2,
xlab = "n", ylab = "Time, s")
points(Result[1, ], Result[2,], lwd = 2,
type = "o", pch = 16, col = 4)
legend("topleft", c("vegan", "RRPP"),
lty = 1, lwd = 2,
pch = 16, col = c(2, 4))
This is not surprising because vegan::adonis2 uses \(n \times n\) distance matrices as data and \(n \times n\) hat matrices for estimates. Even though it also uses Hadamard products (element-wise products between squared, centered distance and hat matrices), which are computationally quicker than matrix products, the matrices grow exponentially with a linear increase in \(n\). By contrast, RRPP::lm.rrpp uses sparse-matrix forms of \(\mathbf{Q}\) matrices from \(\mathbf{QR}\) decomposition of a linear model design matrix, \(\mathbf{X}\) and obtains the matrix product, \(\mathbf{Q}^T\mathbf{Y}\). The sum of squared values from this product is the same as the summed squared values from the Hadamard products, but fewer calculations are needed, especially, comparatively, as \(n\) increases.