Learn R Programming

tensr, Because tensor was Already Taken

Description

This package contains a collection of functions for statistical analysis with tensor(array)-variate data sets.

Let $X$ be a multidimensional array (also called a tensor) of $K$ dimensions. This package provides a series of functions to perform statistical inference when $\text{vec}(X) \sim N(0,\Sigma)$, where $\Sigma$ is assumed to be Kronecker structured. That is, $\Sigma$ is the Kronecker product of $K$ covariance matrices, each of which has the interpretation of being the covariance of $X$ along its $k$th mode, or dimension.

Pay particular attention to the zero mean assumption. That is, you need to de-mean your data prior to applying these functions. If you have more than one sample, $X_i$ for $i = 1,\ldots,n$, then you can concatenate these tensors along a $(K+1)$th mode to form a new tensor $Y$ and apply the demean_tensor() function to $Y$ which will return a tensor that satisfies the mean-zero assumption.

Details of the methods may be found in Gerard & Hoff (2015) and Gerard & Hoff (2016). In particular, tensr has the following features:

  • Basic functions for handling arrays, such as vectorization, matrix unfolding, and multilinear multiplication.
  • Functions for calculating (Tucker) tensor decompositions, such as the incredible higher-order LQ decomposition (incredible HOLQ), the incredible singular value decomposition (ISVD), the incredible higher-order polar decomposition (IHOP), the higher-order singular value decomposition (HOSVD), and the low multilinear rank approximation using the higher-order orthogonal iteration (HOOI).
  • Perform likelihood inference in mean-zero Kronecker structured covariance models, such as
    • Derive the maximum likelihood estimates of the covariance matrices under the array normal model,
    • Run a likelihood ratio test in separable covariance models, and
    • Calculate AIC’s and BIC’s for separable covariance models.
  • Run a Gibbs sampler to draw from the posterior distribution of the Kronecker structured covariance matrix in the array normal model. This posterior is with respect to a (non-informative) prior induced by the right Haar measure over a product group of lower triangular matrices acting on the space of Kronecker structured covariance matrices. For any invariant loss function, any Bayes rule with respect to this prior will be the uniformly minimum risk equivariant estimator (UMREE) with respect to that loss.
  • Calculate the UMREE under a multiway generalization of Stein’s loss. This estimator dominates the maximum likelihood estimator uniformly over the entire parameter space of Kronecker structured covariance matrices.
  • Calculate a (randomized) orthogonally invariant estimator of the Kronecker structured covariance matrix. This estimator dominates the UMREE under the product group of lower triangular matrices.

This package is also published on CRAN.

Vignettes are available on Equivariant Inference and Likelihood Inference.

Installation

To install from CRAN, run in R:

install.packages("tensr")

To install the latest version from Github, run in R:

## install.packages("pak")
pak::pak("github::dcgerard/tensr")

References

Gerard, D., & Hoff, P. (2016). A higher-order LQ decomposition for separable covariance models. Linear Algebra and its Applications, 505, 57-84. doi: 10.1016/j.laa.2016.04.033

Gerard, D., & Hoff, P. (2015). Equivariant minimax dominators of the MLE in the array normal model. Journal of Multivariate Analysis, 137, 32-49. doi: 10.1016/j.jmva.2015.01.020

Copy Link

Version

Install

install.packages('tensr')

Monthly Downloads

325

Version

1.0.2

License

GPL-3

Issues

Pull Requests

Stars

Forks

Maintainer

David Gerard

Last Published

July 24th, 2025

Functions in tensr (1.0.2)

kendalltau

Kendall's tau measure of association.
hosvd

Calculate the (truncated) higher-order SVD (HOSVD).
lrt_stat

Calculate the likelihood ratio test statistic.
polar

The left polar decomposition.
mhalf

The symmetric square root of a positive definite matrix.
mat

Unfold a matrix.
lq

LQ decomposition.
multi_stein_loss

Calculate multiway Stein's loss from square root matrices.
multiway_takemura

Calculate a truncated multiway Takemura estimator.
lrt_null_dist_dim_same

Draw from null distribution of likelihood ratio test statistic.
mle_from_holq

Get MLE from output of holq.
qr2

QR Decomposition.
start_ident

Get list of identity matrices.
multi_stein_loss_cov

Calculate multiway Stein's loss from component covariance matrices.
rsan

Standard normal array.
start_resids

Sample covariance matrices for each mode.
topK

Top K elements of a vector.
sample_sig

Update for total variation parameter in equi_mcmc.
tensr-package

tensr: Covariance Inference and Decompositions for Tensor Datasets
rwish

Wishart simulation.
rmirror_wishart

Sample from the mirror-Wishart distribution.
random_ortho

Generate a list of orthogonal matrices drawn from Haar distribution.
sample_right_wishart

Gibbs update of Phi_inv.
rmvnorm

Multivariate normal simulation.
tsum

Tucker sum.
zscores

Normal scores.
trim

Truncates small numbers to 0.
tr

Trace of a matrix.
amprod

\(k\)-mode product.
collapse_mode

Collapse multiple modes into one mode.
array_bic_aic

Calculate the AIC and BIC.
arrIndices

Array indices.
atrans

Tucker product.
Kom

Commutation matrix.
demean_tensor

Demeans array data.
ldan

Log-likelihood of array normal model.
fnorm

Frobenius norm of an array.
ihop

The incredible higher-order polar decomposition (IHOP).
get_isvd

Calculate the incredible SVD (ISVD).
anorm_cd

Array normal conditional distributions.
equi_mcmc

Gibbs sampler using an invariant prior.
hooi

Calculate the higher-order orthogonal iteration (HOOI).
convert_cov

Convert the output from equi_mcmc to component covariance matrices.
holq

Calculate the incredible higher-order LQ decomposition (HOLQ).
get_equi_bayes

Get the Bayes rule under multiway Stein's loss.
listprod

Element-wise matrix products between two lists.