Getting Started With mvrsquared

Tommy Jones



Welcome to the mvrsquared package! This package does one thing: calculate the coefficient of determination or R-squared. However, this implementation is different from what you may be familiar with. In addition to the standard R-squared used frequently in linear regression, mvrsquared calculates R-squared for multivariate outcomes. (This is why there is an ‘mv’ in mvrsquared).

mvrsquared implements R-squared based on a derivation in this paper. It’s the same definition of R-squared you’re probably familiar with (\(1 - \frac{SSE}{SST}\)) but generalized to n-dimensions.

In the standard case, your outcome \(y\) and prediction \(\hat{y}\) are vectors. In other words, each observation is a single number. This is fine if you are predicting a single variable. But what if you are predicting multiple variables at once? In that case, \(y\) and \(\hat{y}\) are matrices. This situation occurs frequently in topic modeling or simultaneous equation modeling.

Below are some examples of using mvrsquared for calculating R-squared in the traditional context and for multivariate outcomes.

Traditional R-squared

Below is a simple example comparing the R-squared calculated from a linear model fit by lm in the stats package and the R-squared calculated in the mvrsquared package. (Spoiler alert: they’re identical.) Why do this? If mvrsquared can’t get the same calculation, you probably shouldn’t trust it. (Spoiler alert again: it can and you should.)



# fit a linear model
f <- lm(mpg ~ cyl + disp + hp + wt, data = mtcars)

# extract r-squared 
f_summary <- summary(f)

r2_lm <- f_summary$r.squared

#> [1] 0.8486348

# calculate univariate r-squared using mvrsquared
r2_mv <- calc_rsquared(y = mtcars$mpg, yhat = f$fitted.values)

#> [1] 0.8486348

# just to be 100% sure...
r2_lm == r2_mv
#> [1] TRUE

You can also calculate R-squared by passing your data and your linear weights. (That’s probably less useful for univariate outcome models. But, as you’ll see below, it’s very important for latent variable models like topic models.)

x <- cbind(1, f$model[, -1]) # note, you have to add 1's for the intercept and
                             # I'm removing the first column of f$model as it
                             # is the outcome we are predicting

x <- as.matrix(x) # x needs to be a matrix, not a data.frame or tibble for now

w <- matrix(f$coefficients, ncol = 1) # w also has to be a matrix

# this calculates yhat as the dot product x %*% w
r2_mv2 <- calc_rsquared(y = mtcars$mpg, 
                        yhat = list(x = x,
                                    w = w))

#> [1] 0.8486348

Calculating R-squared this way does lead to a tiny difference in calculation due to numeric precision.

r2_mv2 == r2_lm
#> [1] FALSE

However, the difference is tiny. Below demonstrates that they are the same up to 14 decimal places in this example.

round(r2_mv2, 14) == round(r2_lm, 14)
#> [1] TRUE

Multivariate R-squared

Multivariate prediction

A silly example to prove a point

Below, just to show you I’m not blowing smoke, is a silly example that demonstrates that you don’t get any crazy results just because your outcome is a matrix. If we make the columns of \(y\) the same values (and ditto for \(\hat{y}\)), you actually get the same R-squared as above.

Same as above, no?

Probabilistic topic modeling

The paper (as of this writing still in progress) that derives the multivariate R-squared is focused on probabilistic topic modeling. Here’s an example using Latent Dirichlet Allocation.

Below are preliminaries of loading data and getting the model.

The paper notes that for a probabilistic topic model, \(\hat{y} = \vec{n} \odot \Theta \cdot \Phi\), where \(\vec{n}\) is a vector of document lengths, \(\odot\) is elementwise multiplication, \(\Theta\) is a matrix of topic distributions over documents, and \(\Phi\) is a matrix of token distributions over topics.

Doing this multiplication explicitly in R could lead you to run out of RAM for a decently-sized corpus. (This toy example won’t.) Luckily, we can use the ability to pass a list x and w to calc_rsquared to do that multiplication more efficiently. In that case, we set x \(= \vec{n}\odot\Theta\) and w \(= \Phi\).

(Note that you can pass a sparse dgCMatrix from the Matrix or something coercible to one directly to calc_rsquared.)

Latent semantic analysis topic modeling

Latent semantic analysis (LSA) uses a single value decomposition of a document term matrix as a non-probabilistic topic model.

Note the example below doesn’t use TF-IDF even though most folks recommend you do. For a more thorough example of LSA (and LDA) see textmineR’s vignette on topic modeling.

Parallelization/distributed computing for large data sets

You say you have BIG DATA?!?! Below is an example of how to chop up a calculation and have r-squared built in parallel in a distributed setting. mvrsquared has a parallel back end using RcppThread and controlled by the threads argument. However, if that isn’t enough, you can bring your own distributed parallel framework. The example below uses parallel::mclapply which will not parallelize on Windows, but R has other options.

mvrsquared has the option to return only the sums of squares (specifically SSE and SST). You can calculate these in parallel/distributed and combine them by adding post-hoc. (That’s a little algorithm called “map-reduce” for those in the know.) Once combined, you can calculate R-squared yourself with the canonical formula \(1 - \frac{SSE}{SST}\).

Note: You must calculate the mean of your outcome, \(\bar{y}\), first and hand it off to calc_rsquared. Otherwise, your calculations for SST on a given core will be based only on the observations sent to that core, not the whole data set. (If you do this, calc_rsquared will give you a WARNING.)

Below is a toy example using parallel::mclapply to run parallel computations calculating R-squared for our LDA model, above. (Yes, this is overkill for our toy problem and it will actually be slower due to overhead. But imagine the calculations for a corpus of ~1 million documents with hundreds-of-thousands or millions of unique tokens.)

Because the below example uses parallel execution, which can be tricky when working with CRAN, I’m not actually evaluating it in this vignette. However, you can run the code yourself.


batch_size <- 10

batches <- mclapply(X = seq(1, nrow(dtm), by = batch_size),
                    FUN = function(b){
                      # rows to select on
                      rows <- b:min(b + batch_size - 1, nrow(dtm))
                      # rows of the dtm
                      y_batch <- dtm[rows, ]
                      # rows of theta multiplied by document length
                      x_batch <- rowSums(y_batch) * lda$theta[rows, ]
                      list(y = y_batch,
                           x = x_batch)
                    }, mc.cores = 2)

# calculate ybar for the data
# in this case, lazily doing colMeans, but you could divide this problem up too
ybar <- colMeans(dtm)

# MAP: calculate sums of squares
ss <- mclapply(X = batches,
               FUN = function(batch){
                 calc_rsquared(y = batch$y,
                               yhat = list(x = batch$x, w = lda$phi),
                               ybar = ybar,
                               return_ss_only = TRUE)
               }, mc.cores = 2)

# REDUCE: get SST and SSE by summation
ss <-, ss) %>% colSums()

r2_mapreduce <- 1 - ss["sse"] / ss["sst"]

# should be the same as above