How RRPP evolves?

An examination with the pairwise function

code
analysis
news
Author

Mike Collyer

Published

October 16, 2026

Conjecture: RRPP functions are fast.

Whether one thinks RRPP functions are fast depends on one’s experience with them. We attempt to make them fast but sometimes, new data challenges expose computational slogs! In the previous post, I discovered a computational slog, even though I did not reveal it in the post. As a reminder, here is what I presented:

Data Example

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))
})

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.164   0.014   0.184 
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.07374 0.0081938 0.07558 0.8176 -0.38792  0.649
Residuals 90 0.90194 0.0100216 0.92442                       
Total     99 0.97569                                         

Call: lm.rrpp(f1 = Y ~ group, iter = 999, 
turbo = TRUE, Parallel = 1)

The exact results are not the same because I did not attempt to control the random seed, but the system time needed to perform the analysis is about the same. What I did not reveal was what would happen if one wanted to perform pairwise comparisons among means. (It was not the intention, but I still felt slimy about it.) That could be accomplished, thusly:

system.time(PW <- pairwise(fit1, 
                groups = group))
   user  system elapsed 
  4.839   0.854   5.947 
summary(PW)

Pairwise comparisons

Groups: 1 2 3 4 5 6 7 8 9 10 

RRPP: 1000 permutations

LS means:
Vectors hidden (use show.vectors = TRUE to view)

Pairwise distances between means, plus statistics
               d  UCL (95%)           Z Pr > d
1:2  0.032362279 0.07436192 -0.18703518  0.573
1:3  0.047134314 0.07749052  0.50922920  0.318
1:4  0.005152141 0.07466898 -2.25153380  0.991
1:5  0.048862239 0.07562997  0.57480055  0.287
1:6  0.044166492 0.07373773  0.41008245  0.342
1:7  0.020946169 0.07253489 -0.80217161  0.773
1:8  0.052422049 0.07746128  0.71404619  0.248
1:9  0.036820023 0.07698093  0.06805409  0.480
1:10 0.051968340 0.07509386  0.73112395  0.244
2:3  0.046039909 0.07804305  0.46948958  0.314
2:4  0.027540468 0.07494099 -0.46491472  0.674
2:5  0.039953063 0.07444551  0.16082116  0.452
2:6  0.046979220 0.07551147  0.50704472  0.313
2:7  0.022565025 0.07640062 -0.71936322  0.763
2:8  0.031340641 0.07353090 -0.30008515  0.622
2:9  0.035011498 0.07703547 -0.03935591  0.514
2:10 0.023360367 0.07809807 -0.69824646  0.757
3:4  0.043920280 0.07549248  0.36078646  0.383
3:5  0.011343118 0.07317293 -1.61925967  0.946
3:6  0.005669076 0.07510590 -2.10269091  0.983
3:7  0.029259206 0.07769441 -0.34204202  0.635
3:8  0.077366185 0.07352634  1.68884226  0.038
3:9  0.011655644 0.07601432 -1.50747166  0.933
3:10 0.042152306 0.07810935  0.24206791  0.415
4:5  0.044767743 0.07540118  0.38028081  0.367
4:6  0.041425387 0.07600736  0.22331114  0.420
4:7  0.016418982 0.07540245 -1.17219473  0.870
4:8  0.049488914 0.07688782  0.55941193  0.311
4:9  0.033133724 0.07989156 -0.16929141  0.559
4:10 0.046817332 0.07651459  0.46131499  0.341
5:6  0.016407388 0.07518374 -1.11332646  0.848
5:7  0.028529601 0.07519160 -0.42027977  0.659
5:8  0.070687695 0.07584168  1.46959348  0.075
5:9  0.012820227 0.07540261 -1.43931357  0.925
5:10 0.031828563 0.07510221 -0.27575682  0.606
6:7  0.028065621 0.07343718 -0.36065048  0.646
6:8  0.078258149 0.07727222  1.66494712  0.049
6:9  0.012015152 0.07646138 -1.47205125  0.920
6:10 0.045743501 0.07484992  0.42478958  0.348
7:8  0.052213012 0.07744154  0.72263149  0.232
7:9  0.017761852 0.07375396 -1.00293603  0.831
7:10 0.034410777 0.07691515 -0.04931888  0.538
8:9  0.066327369 0.07709423  1.24764105  0.106
8:10 0.045607213 0.07692679  0.38523422  0.355
9:10 0.035193459 0.07504982 -0.08343500  0.556

Awwwww WTF?

To be fair, pairwise should be slower than lm.rrpp. There are several reasons for this. First, the function does more than obtain coefficients or fitted values, many times. It uses coefficients to estimate attributes (means, slopes) and then calculates (perhaps many) statistics that compare these estimates every permutation. The summary table above reveals that in this case, that means \(10 \times 9 / 2 = 45\) statistics, each permutation. Second, ANOVA can benefit from operating in fewer dimensions. For example, the data matrix, \(\mathbf{Y}\), is a \(100 \times 1,536\) matrix; i.e., high-dimensional data. ANOVA can be performed on a \(100 \times 99\) matrix of principal component scores, where the \(99\) PC variables encapsulate the same variation as the \(1,536\) original variables. What a computational boon! But because the pairwise function (at least, currently) uses means and vectors in the original data space. Finally, in this example, we used turbo = TRUE in the lm.rrpp fit, which skips recording all coefficients in the \(1,000\) random permutations. Therefore we asked paiwise to recalculate these. What would happen if we change that?

system.time(
  fit1 <- lm.rrpp(Y ~ group, 
                   iter = 999,
                   Parallel = 1,
                   turbo = FALSE,
                  verbose = TRUE))
   user  system elapsed 
  0.664   0.184   0.904 

OK, that made the analysis a few milliseconds slower, so it does not explain the multi-second slog. Additionally, because the coefficients were estimated for the original \(1,536\) variables, it casts doubt that the slow down was just because of the data dimensionality.

What does pairwise do?

Well, it does a lot of things but the one thing it does, consistently, is use the coefficients estimated from RRPP-generated data to estimate values that would be needed for statistics. In this example, that means estimating LS means, and then finding distances among means. To demonstrate the principle, let’s use the first two principal components of the data so that results are penetrable.

PCA <- ordinate(Y)
P <- PCA$x[, 1:2]
fit <- lm.rrpp(P ~ group, 
               verbose = TRUE, 
               turbo = FALSE)

Now, for just the observed data, let’s consider the following: these two things are the same:

(fitted(fit))[1:5,]
        Comp1       Comp2
1 -0.02725754 0.002698345
2 -0.02725754 0.002698345
3 -0.02725754 0.002698345
4 -0.02725754 0.002698345
5 -0.02725754 0.002698345
X <- model.matrix(fit)
B <- coef(fit)
(X %*% B)[1:5,]
        Comp1       Comp2
1 -0.02725754 0.002698345
2 -0.02725754 0.002698345
3 -0.02725754 0.002698345
4 -0.02725754 0.002698345
5 -0.02725754 0.002698345

Over many iterations, \(\mathbf{X}\) will be the same; \(\mathbf{B}\) will change. Thus, \(\mathbf{X}\) only needs to be estimated once. Also, if there were covariates in the model, their parameter values in \(\mathbf{X}\) would be replaced with the mean value rather than their original value, meaning the rows of \(\mathbf{X}\) are not unique. In the case that LS means are estimated, a second model matrix could be used on fitted values, \(\mathbf{L}\). This model design has no intercept and is simply intended to find means. There is a computational trick for estimating coefficients, found via \(\mathbf{QR}\) decomposition,

\[ \mathbf{L} = \mathbf{QR}. \]

Let \(\mathbf{H}_{\mathbf{B}} = \left(\mathbf{R}^{-1}\mathbf{Q}\right)^T\). Then,

\[ \mathbf{B}_{LS} = \mathbf{H}_{\mathbf{B}}\mathbf{\hat{Y}}, \]

where the coefficients are actually LS means, and \(\mathbf{\hat{Y}}\) are the fitted values, as found in every RRPP permutation. So, let’s make a function that resembles pairwise, which obtains LS means for every random permutations (and which takes advantage of the sparse nature of the model matrix).

library(Matrix)
fast.ginv <- RRPP:::fast.ginv
fast.solve <- RRPP:::fast.solve

getMeans1 <- function(fit, group){
  X <- Matrix(model.matrix(fit),
              sparse = TRUE)
  Bs <- fit$LM$random.coef
  k <- length(Bs)
  Bs <- Bs[[k]]
  Fitted <- lapply(Bs, 
                   function(b) X %*% b)
  L <- model.matrix(~group + 0)
  QR <- QRforX(L)
  Hb <- tcrossprod(
    fast.solve(QR$R), QR$Q)
  Means <- lapply(Fitted, function(y){
    Hb %*% y
  })
  Means
}

Now try it.

system.time(M1 <- getMeans1(fit, group))
   user  system elapsed 
  0.013   0.000   0.013 
M1[[1]]
10 x 2 Matrix of class "dgeMatrix"
                Comp1        Comp2
group1  -0.0272575418  0.002698345
group2   0.0006953068  0.019006482
group3   0.0100818830 -0.026066407
group4  -0.0222053372  0.003708186
group5   0.0173127611 -0.017326801
group6   0.0046853379 -0.027802990
group7  -0.0069021031 -0.002241099
group8  -0.0044854461  0.049915956
group9   0.0045851911 -0.015788256
group10  0.0234899483  0.013896584

Should be the same as:

aggregate(P ~ group, group, mean)
   group         Comp1        Comp2
1      1 -0.0272575418  0.002698345
2      2  0.0006953068  0.019006482
3      3  0.0100818830 -0.026066407
4      4 -0.0222053372  0.003708186
5      5  0.0173127611 -0.017326801
6      6  0.0046853379 -0.027802990
7      7 -0.0069021031 -0.002241099
8      8 -0.0044854461  0.049915956
9      9  0.0045851911 -0.015788256
10    10  0.0234899483  0.013896584

So for two variables, that was fast! But for the original data?

system.time(M1 <- getMeans1(fit1, group))
   user  system elapsed 
  0.660   0.212   0.976 

This helps to explain some (but not all) of the time slog! And it is stupid. It lacks memoization. Why? Because even though \(\mathbf{X}\) and \(\mathbf{L}\) are each only calculated once, they are both used once in each permutation, when their product could be calculated and it used once in each permutation. What the algorithm above does is find each time,

\[ \mathbf{H}_{\mathbf{B}}(\mathbf{LB}), \]

when we could calculate

\[ (\mathbf{H}_{\mathbf{B}}\mathbf{L})\mathbf{B}, \]

and \((\mathbf{H}_{\mathbf{B}}\mathbf{L})\) could be calculated once instead of in every permutation. Furthermore, in this case, \(\mathbf{H}_{\mathbf{B}}\) is a \(10 \times 100\) matrix and \(\mathbf{LB}\) is a \(100 \times 1,536\) matrix, and the product of these in every random permutation involves many calculations. But \(\mathbf{H}_{\mathbf{B}}\mathbf{L}\) is a \(10 \times 10\) matrix and \(\mathbf{B}\) a \(10 \times 1,536\) matrix, so their product in every random permutation would mean fewer calculations. Let’s test the veracity of this.

getMeans2 <- function(fit, group){
  X <- Matrix(model.matrix(fit),
              sparse = TRUE)
  Bs <- fit$LM$random.coef
  k <- length(Bs)
  Bs <- Bs[[k]]
  L <- model.matrix(~group + 0)
  QR <- QRforX(L)
  Hb <- tcrossprod(
    fast.solve(QR$R), QR$Q)
  HL <- Matrix(Hb %*% L, sparse = TRUE)
  
  Means <- lapply(Bs, function(b){
    HL %*% b
  })
  Means
}

system.time(M2 <- getMeans2(fit1, group))
   user  system elapsed 
  0.029   0.040   0.079 

Much better!

This does not identify and fix all analytical slogs but it helps, immensely. Profiling the pairwise function with large data sets like this example will help identify other slow-downs.

How did this happen in the first place?

Quite simply, it is difficult to identify problems until they surface. For example, if we applied the same two functions to the example with only two PCs of data,

system.time(M1 <- getMeans1(fit, group))
   user  system elapsed 
  0.010   0.001   0.011 
system.time(M2 <- getMeans2(fit, group))
   user  system elapsed 
  0.005   0.000   0.006 
M1[[2]]
10 x 2 Matrix of class "dgeMatrix"
                Comp1        Comp2
group1  -0.0135928685  0.043184581
group2  -0.0270921414  0.013824456
group3  -0.0156944519  0.044796539
group4  -0.0076115837 -0.007208750
group5   0.0069870107 -0.024756518
group6   0.0175724099 -0.017006257
group7  -0.0001412765 -0.020625526
group8   0.0185480505 -0.007356538
group9   0.0142122672 -0.013671688
group10  0.0068125837 -0.011180298
M2[[2]]
10 x 2 Matrix of class "dgeMatrix"
               Comp1        Comp2
group1  -0.013592868  0.043184581
group2  -0.013499273 -0.029360125
group3  -0.002101583  0.001611958
group4   0.005981285 -0.050393331
group5   0.020579879 -0.067941099
group6   0.031165278 -0.060190838
group7   0.013451592 -0.063810107
group8   0.032140919 -0.050541119
group9   0.027805136 -0.056856268
group10  0.020405452 -0.054364879

The difference is not profound. The issue was revealed only when data were large enough to reveal it.

Needless to say…

some updates will be coming to RRPP soon