Learn R Programming

⚠️There's a newer version (14.0.2-1) of this package.Take me there.

RcppArmadillo: R and Armadillo via Rcpp


Synopsis

RcppArmadillo provides an interface from R to and from Armadillo by utilising the Rcpp R/C++ interface library.

What is Armadillo?

Armadillo is a high-quality linear algebra library for the C++ language, aiming towards a good balance between speed and ease of use. It provides high-level syntax and functionality deliberately similar to Matlab (TM). See its website more information about Armadillo.

So give me an example!

Glad you asked. Here is a light-weight and fast implementation of linear regression:

#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]

// [[Rcpp::export]]
Rcpp::List fastLm(const arma::mat& X, const arma::colvec& y) {
    int n = X.n_rows, k = X.n_cols;

    arma::colvec coef = arma::solve(X, y);    // fit model y ~ X
    arma::colvec res  = y - X*coef;           // residuals

    // std.errors of coefficients
    double s2 = std::inner_product(res.begin(), res.end(), res.begin(), 0.0)/(n - k);

    arma::colvec std_err = arma::sqrt(s2 * arma::diagvec(arma::pinv(arma::trans(X)*X)));

    return Rcpp::List::create(Rcpp::Named("coefficients") = coef,
                              Rcpp::Named("stderr")       = std_err,
                              Rcpp::Named("df.residual")  = n - k);
}

You can Rcpp::sourceCpp() the file above to compile the function. A slightly more involved version is also included in the package as the fastLm() function.

Status

The package is under active development with releases to CRAN about once every other month, and widely-used by other CRAN packages as can be seen from the CRAN package page. As of September 2019, there are 658 CRAN packages using RcppArmadillo.

Documentation

The package contains a pdf vignette which is a pre-print of the paper by Eddelbuettel and Sanderson in CSDA (2014), as well as an introductory vignette for the sparse matrix conversions.

Installation

RcppArmadillo is a CRAN package, and lives otherwise in its own habitat on GitHub within the RcppCore GitHub organization.

Run

install.packages("RcppArmadillo")

to install from your nearest CRAN mirror.

Authors

Dirk Eddelbuettel, Romain Francois, Doug Bates and Binxiang Ni

License

GPL (>= 2)

Copy Link

Version

Install

install.packages('RcppArmadillo')

Monthly Downloads

504,011

Version

0.10.5.0.0

License

GPL (>= 2)

Issues

Pull Requests

Stars

Forks

Last Published

May 23rd, 2021

Functions in RcppArmadillo (0.10.5.0.0)

fastLm

Bare-bones linear model fitting function
RcppArmadillo-package

R and Armadillo Integration
armadillo_set_seed_random

Set the Armadillo Random Number Generator to a random value
armadillo_set_seed

Set the Armadillo Random Number Generator to the given value
RcppArmadillo.package.skeleton

Create a skeleton for a new package that intends to use RcppArmadillo
armadillo_version

Report the version of Armadillo