On this small example data, all methods are basically equally fast, but
if we have a much larger dataset, the optimizations for the sparse data
start to show.
I generate a dataset with 10,000 rows and 50 columns that is 99% empty
As you can see sparseMatrixStats is ca. 35 times fast than
matrixStats, which in turn is 7 times faster than the apply()
version.
API
The package now supports all functions from the matrixStats API for
column sparse matrices (dgCMatrix). And thanks to the
MatrixGenerics it
can be easily integrated along-side
matrixStats and
DelayedMatrixStats.
Note that the rowXXX() functions are called by transposing the input
and calling the corresponding colXXX() function. Special optimized
implementations are available for rowSums2(), rowMeans2(), and
rowVars().
Method
matrixStats
sparseMatrixStats
Notes
colAlls()
✔
✔
colAnyMissings()
✔
❌
Not implemented because it is deprecated in favor of colAnyNAs()
colAnyNAs()
✔
✔
colAnys()
✔
✔
colAvgsPerRowSet()
✔
✔
colCollapse()
✔
✔
colCounts()
✔
✔
colCummaxs()
✔
✔
colCummins()
✔
✔
colCumprods()
✔
✔
colCumsums()
✔
✔
colDiffs()
✔
✔
colIQRDiffs()
✔
✔
colIQRs()
✔
✔
colLogSumExps()
✔
✔
colMadDiffs()
✔
✔
colMads()
✔
✔
colMaxs()
✔
✔
colMeans2()
✔
✔
colMedians()
✔
✔
colMins()
✔
✔
colOrderStats()
✔
✔
colProds()
✔
✔
colQuantiles()
✔
✔
colRanges()
✔
✔
colRanks()
✔
✔
colSdDiffs()
✔
✔
colSds()
✔
✔
colsum()
✔
❌
Base R function
colSums2()
✔
✔
colTabulates()
✔
✔
colVarDiffs()
✔
✔
colVars()
✔
✔
colWeightedMads()
✔
✔
Sparse version behaves slightly differently, because it always uses interpolate=FALSE.
colWeightedMeans()
✔
✔
colWeightedMedians()
✔
✔
Only equivalent if interpolate=FALSE
colWeightedSds()
✔
✔
colWeightedVars()
✔
✔
rowAlls()
✔
✔
rowAnyMissings()
✔
❌
Not implemented because it is deprecated in favor of rowAnyNAs()
rowAnyNAs()
✔
✔
rowAnys()
✔
✔
rowAvgsPerColSet()
✔
✔
rowCollapse()
✔
✔
rowCounts()
✔
✔
rowCummaxs()
✔
✔
rowCummins()
✔
✔
rowCumprods()
✔
✔
rowCumsums()
✔
✔
rowDiffs()
✔
✔
rowIQRDiffs()
✔
✔
rowIQRs()
✔
✔
rowLogSumExps()
✔
✔
rowMadDiffs()
✔
✔
rowMads()
✔
✔
rowMaxs()
✔
✔
rowMeans2()
✔
✔
rowMedians()
✔
✔
rowMins()
✔
✔
rowOrderStats()
✔
✔
rowProds()
✔
✔
rowQuantiles()
✔
✔
rowRanges()
✔
✔
rowRanks()
✔
✔
rowSdDiffs()
✔
✔
rowSds()
✔
✔
rowsum()
✔
❌
Base R function
rowSums2()
✔
✔
rowTabulates()
✔
✔
rowVarDiffs()
✔
✔
rowVars()
✔
✔
rowWeightedMads()
✔
✔
Sparse version behaves slightly differently, because it always uses interpolate=FALSE.
rowWeightedMeans()
✔
✔
rowWeightedMedians()
✔
✔
Only equivalent if interpolate=FALSE
rowWeightedSds()
✔
✔
rowWeightedVars()
✔
✔
Installation Problems
sparseMatrixStats uses features from C++14 and as the standard is more
than 6 years old, I thought this wouldn’t cause problems. In most
circumstances this is true, but there are reoccuring reports, that the
installation fails for some people and that is of course annoying. The
typical error message is:
Error: C++14 standard requested but CXX14 is not defined
The main reason that the installation fails is that the compiler is too
old. Sufficient support for C++14 came in
clang version 3.4
gcc version 4.9
Accordingly, you must have a compiler available that is at least that
new. If you run on the command line
$ gcc --version
and it says 4.8, you will have to install a newer compiler. At the end
of the section, I have collected a few tips to install an appropriate
version on different distributions.
If you have recent version of gcc (>=4.9) or clang (>= 3.4)
installed, but you still see the error message
Error: C++14 standard requested but CXX14 is not defined
the problem is that R doesn’t yet know about it.
The solution is to either create a ~/.R/Makevars file and define
One of the main culprits causing trouble is CentOS 7. It is popular in
scientific computing and is still supported until 2024. It does,
however, by default come with a very old version of gcc (4.8.5).
To install a more recent compiler, we can use
devtoolset.
First, we enable the Software Collection Tools and then install for
example gcc version 7:
Note, that our shenanigans are only necessary once, when we install
sparseMatrixStats. After the successful installation of the package,
we can use R as usual.
Debian
All Debian releases later than Jessie (i.e. Stretch, Buster, Bullseye)
are recent enough and should install sparseMatrixStats without problems.
I was able to install sparseMatrixStats on Debian Jessie (which comes
with gcc version 4.9.2) by providing the necessary Makefile arguments
Debian Wheezy comes with gcc 4.7, which does not support C++14. On the
other hand, the last R release that was backported to Wheezy is 3.2.5
(see information on
CRAN).
Thus, if you are still on Wheezy, I would encourage you to update your
OS.
Ubuntu
Since 16.04, Ubuntu comes with a recent enough compiler.
Ubuntu 14.04 comes with gcc 4.8.5, but updating to gcc-5 is easy:
sparseMatrixStats
The goal of
sparseMatrixStatsis to make the API of matrixStats available for sparse matrices.Installation
You can install the release version of sparseMatrixStats from BioConductor:
Alternatively, you can get the development version of the package from GitHub with:
If you have trouble with the installation, see the end of the README.
Example
The package provides an interface to quickly do common operations on the rows or columns. For example calculate the variance:
On this small example data, all methods are basically equally fast, but if we have a much larger dataset, the optimizations for the sparse data start to show.
I generate a dataset with 10,000 rows and 50 columns that is 99% empty
I use the
benchpackage to benchmark the performance difference:As you can see
sparseMatrixStatsis ca. 35 times fast thanmatrixStats, which in turn is 7 times faster than theapply()version.API
The package now supports all functions from the
matrixStatsAPI for column sparse matrices (dgCMatrix). And thanks to theMatrixGenericsit can be easily integrated along-sidematrixStatsandDelayedMatrixStats. Note that therowXXX()functions are called by transposing the input and calling the correspondingcolXXX()function. Special optimized implementations are available forrowSums2(),rowMeans2(), androwVars().colAnyNAs()interpolate=FALSE.interpolate=FALSErowAnyNAs()interpolate=FALSE.interpolate=FALSEInstallation Problems
sparseMatrixStatsuses features from C++14 and as the standard is more than 6 years old, I thought this wouldn’t cause problems. In most circumstances this is true, but there are reoccuring reports, that the installation fails for some people and that is of course annoying. The typical error message is:The main reason that the installation fails is that the compiler is too old. Sufficient support for C++14 came in
clangversion 3.4gccversion 4.9Accordingly, you must have a compiler available that is at least that new. If you run on the command line
and it says 4.8, you will have to install a newer compiler. At the end of the section, I have collected a few tips to install an appropriate version on different distributions.
If you have recent version of
gcc(>=4.9) orclang(>= 3.4) installed, but you still see the error messagethe problem is that R doesn’t yet know about it.
The solution is to either create a
~/.R/Makevarsfile and defineor simply call
Update Compiler
CentOS / Scientic Linux / RHEL
One of the main culprits causing trouble is CentOS 7. It is popular in scientific computing and is still supported until 2024. It does, however, by default come with a very old version of
gcc(4.8.5).To install a more recent compiler, we can use devtoolset. First, we enable the Software Collection Tools and then install for example
gccversion 7:We can now either activate the new compiler for an R session
and then call
or we refer to the full path of the newly installed g++ from a standard R session
Note, that our shenanigans are only necessary once, when we install
sparseMatrixStats. After the successful installation of the package, we can use R as usual.Debian
All Debian releases later than Jessie (i.e. Stretch, Buster, Bullseye) are recent enough and should install sparseMatrixStats without problems.
I was able to install
sparseMatrixStatson Debian Jessie (which comes withgccversion 4.9.2) by providing the necessary Makefile argumentsDebian Wheezy comes with
gcc4.7, which does not support C++14. On the other hand, the last R release that was backported to Wheezy is 3.2.5 (see information on CRAN). Thus, if you are still on Wheezy, I would encourage you to update your OS.Ubuntu
Since 16.04, Ubuntu comes with a recent enough compiler.
Ubuntu 14.04 comes with
gcc4.8.5, but updating togcc-5is easy:After that, you can install
sparseMatrixStatswith a custom Makevars variables that refer to the new compilerMacOS
No trouble reported so far. Just do:
Windows
It is important that you have RTools40 installed. After that, you shouldn’t have any troubles installing
sparseMatrixStatsdirectly from Bioconductor:But I still have a problems
gcc>= 4.9 andclang>= 3.4)If your problems nonetheless persist, please file an issue including the following information:
sessionInfo()~/.R/Makevarsfile and what it containssparseMatrixStatsincluding the full error message