matrixStats R Package

repository·develop·Indexed 42 days ago

https://github.com/henrikbengtsson/matrixstats

An R package providing highly optimized C-based functions for computing statistical summaries over the rows and columns of matrices and over vectors. It is designed to be significantly faster and more memory-efficient than standard R apply() operations, featuring functions such as rowQuantiles(), colMedians(), and logSumExp().

Tokens
5.2K
Snippets
23
Records
31
Agent score
48%

What's inside matrixStats

  1. Overview of matrixStats

    develop

    matrixStats

    matrixStats is an R package providing highly optimized functions for computing common summaries over rows and columns of matrices, as well as functions that operate on vectors.

    Key Features:

    • Performance: Implementations are mostly in C to minimize processing time and memory usage, often outperforming apply() significantly.
    • Memory Efficiency: Functions support operating on subsets of rows and columns directly, avoiding the creation of large intermediate objects.
    • Cross-Platform: Installs out-of-the-box on Linux, macOS, and Windows.
  2. Use matrixStats for high-performance matrix summaries

    develop

    The matrixStats package provides highly optimized C-based functions for computing summaries (like medians, quantiles, etc.) over the rows and columns of matrices, as well as functions for vectors. These functions are designed to be significantly faster and more memory-efficient than standard R apply() solutions.

    Key benefits:

    • Speed: Optimized C implementations outperform apply().
    • Memory Efficiency: Functions allow you to specify subsets of rows and columns directly via rows and cols arguments, avoiding the memory overhead of creating intermediate matrix subsets (e.g., x[rows, cols]).
    # Example: Calculating column medians efficiently
    x <- matrix(rnorm(20 * 500), nrow = 20, ncol = 500)
    mu <- matrixStats::colMedians(x)
  3. Resolve R documentation (Rd) mismatches and errors

    develop

    R documentation errors can prevent successful package checks. Key issues to look for:

    • Code/Documentation Mismatches: The arguments defined in the function's actual R code do not match the arguments described in the .Rd documentation file. This often manifests as "Argument names in code not in docs".
    • Undocumented Arguments: A function's \usage section includes arguments that are not documented in the rest of the .Rd file.
    • Invalid LaTeX in Rd files: Using characters like _ in documentation without proper escaping (e.g., \_) can cause checkRd errors.
    • Missing \alias entries: Functions with \usage entries must have appropriate \alias entries and all arguments must be documented.
  4. Identify missing matrixStats imports in dependent packages

    develop
    Some packages that depend on matrixStats may fail checks if matrixStats is listed in the Depends field of the DESCRIPTION file but is not explicitly imported in the NAMESPACE file. To ensure compatibility when the namespace is loaded but not attached, ensure matrixStats is included in the importFrom or import statements in your NAMESPACE file.
  5. Troubleshoot R package dependency and namespace issues

    develop

    When developing or using R packages, you may encounter several types of dependency-related warnings and notes during checks (e.g., via revdepcheck). Common issues include:

    • Unimported Dependencies: A package lists a dependency in its Depends or Imports field but fails to actually import it in the NAMESPACE file. This prevents the package from working correctly when loaded without attaching the dependency.
    • Excessive Search Path: Including many packages in the Depends field is considered excessive. It is preferable to use selective imports.
    • Unstated Vignette Dependencies: A vignette may use a library (e.g., library(calibrate)) that is not declared in the package's DESCRIPTION file.
    • Missing Global Function Definitions: If your code uses functions from other packages (e.g., stats::hclust or graphics::abline) without explicit imports, R will flag them as undefined global functions. You should resolve this by adding importFrom("package", "function") to your NAMESPACE file.
  6. Fix C++ specification and portability warnings in R packages

    develop

    When writing compiled code (C/C++) for R packages, be aware of the following:

    • C++ Standard: Avoid specifying a specific C++ standard (like C++11) in your configuration unless it is strictly essential, as R prefers more flexible specifications.
    • Portability and System Calls: Compiled code should avoid calling entry points that might terminate R, writing to stdout/stderr instead of the R console, using Fortran I/O, using system RNGs, or using [v]sprintf. Use portable methods to ensure the package works across different environments.
  7. Install revdepcheck for R package testing

    develop

    To set up the environment for running revdepcheck, configure R to use multiple CPUs for parallel processing and install the remotes package to fetch revdepcheck from GitHub.

    options(Ncpus = 6L)
    install.packages("remotes")
    remotes::install_github("r-lib/revdepcheck")
  8. Get detailed reverse dependency information with revdepcheck

    develop

    To investigate specific issues or get more detailed information about a package's reverse dependency status, use the revdep_details function from the revdepcheck package. This is useful when you encounter notes or warnings during dependency checks and need a deeper dive into the package's environment or recursive dependencies.

    Example usage:

    revdepcheck::revdep_details(, "PackageName")
    revdepcheck::revdep_details(, "EstimationTools")
  9. Use matrixStats for optimized matrix summaries

    develop

    The matrixStats package provides highly optimized C-based functions for computing summaries over rows and columns of matrices (e.g., rowQuantiles(), colMedians()) and functions for vectors (e.g., logSumExp()).

    These functions are designed to be significantly faster and more memory-efficient than standard R apply() solutions. You can also perform calculations on specific subsets of rows and columns directly within the function call to avoid the memory overhead of creating intermediate matrix subsets.

    # Setup a sample matrix
    x <- matrix(rnorm(20 * 500), nrow = 20, ncol = 500)
    
    # Optimized column medians (much faster than apply(x, 2, median))
    mu <- matrixStats::colMedians(x)
    
    # Optimized calculation on a subset of rows and columns
    # This is more efficient than apply(x[rows, cols], 2, median)
    mu_subset <- colMedians(x, rows = 33:158, cols = 1001:3000)
  10. Calculate summaries on matrix subsets efficiently

    develop

    When performing calculations on a subset of rows or columns, do not use R's subsetting syntax (e.g., x[rows, cols]) inside an apply() call, as this creates a temporary copy of the data in memory. Instead, use the rows and cols arguments provided by matrixStats functions. This approach is both faster and more memory-efficient.

    # Efficient way: Use the rows and cols arguments directly
    mu <- matrixStats::colMedians(x, rows = 33:158, cols = 1001:3000)
    
    # Inefficient way: Subsetting creates a copy in memory
    mu <- apply(x[33:158, 1001:3000], MARGIN = 2, FUN = median)
  11. Handle compiler requirements for specific packages during pre-installation

    develop

    Some packages require specific GCC versions during the pre-installation phase. Use scl enable to switch to the appropriate devtoolset before running the revdep/run.R --preinstall command.

    • OpenMx (needed by semtree): Requires gcc >= 4.9.3 (use devtoolset-6).
    • Rfast (needed by IMIFA and obfuscatoR): Requires gcc 6.3.1 (use devtoolset-6).
    • RcppAlgos (needed by ImpactEffectsize): Requires gcc 5.3.1 (use devtoolset-4).
    ## OpenMx (gcc >= 4.9.3)
    scl enable devtoolset-6 "revdep/run.R --preinstall OpenMx"
    
    ## Rfast (gcc 6.3.1)
    scl enable devtoolset-6 "revdep/run.R --preinstall Rfast"
    
    ## RccpAlgos (gcc 5.3.1)
    scl enable devtoolset-4 "revdep/run.R --preinstall RcppAlgos"