nnTensor: An R package for non-negative
matrix/tensor decomposition
Koki Tsuyuzaki∗1,2and Itoshi Nikaido

1,3,41 Laboratory for Bioinformatics Research, RIKEN Center for Biosystems Dynamics Research, Japan 2
Japan Science and Technology Agency, PRESTO, Japan 3 Science Innovation (Bioinformatics), Degree
Programs in Systems and Information Engineering, Graduate School of Science and Technology),
University of Tsukuba, Japan 4 Department of Functional Genome Informatics, Division of Biological
Data Science, Medical Research Institute, Tokyo Medical and Dental University, Japan
DOI: 10.21105/joss.05015
Software• Review• Repository• ArchiveEditor: Prashant K Jha
Reviewers:• @Stat-Cook• @oneilsh
Submitted: 13 October 2022
Published: 23 April 2023
LicenseAuthors of papers retain copyrightand release the work under aCreative Commons Attribution 4.0
International License (CC BY 4.0).

Summary
Non-negative matrix factorization (NMF) is a widely used algorithm to decompose non-negative
matrix data into factor matrices. Due to the interpretability of its non-negativity and theconvenience of using decomposition results as clustering, there are many applications of NMF
in image processing, audio processing, and bioinformatics (Cichocki et al., 2009).
NMF has been applied to matrix data but there is a growing demand to apply NMF to moreheterogeneous non-negative data such as multiple matrices and tensors (high-dimensionalarrays), which are higher-order data structures than matrices (Cichocki et al., 2009). To
meet these requirements, we originally developed nnTensor, which is an R/CRAN package toperform some non-negative matrix/tensor decomposition algorithms (https://cran.r-project.
org/web/packages/nnTensor/index.html).

Statement of needIn many cases, the latest tensor decomposition algorithms are implemented in MATLAB in theoriginal papers, which means that those are not freely applicable to users’ data immediately.
To fill this gap, we originally implemented some non-negative matrix/tensor decomposition
algorithms in R language, which is one of the popular open-source programming languages.
nnTensor provides the matrix/decomposition decomposition functions as follows:• NMF: Non-negative matrix factorization for non-negative matrix (Cichocki et al., 2009)• NMTF: Non-negative matrix tri-factorization for non-negative matrix (Čopar et al., 2019)• siNMF: Simultaneous non-negative matrix factorization for non-negative matrix (Badea,2009)• jNMF: Joint non-negative matrices factorization for non-negative matrix (Yang & Michailidis, 2016)• NTF: Non-negative tensor factorization for non-negative tensor (Cichocki et al., 2009)• NTD: Non-negative Tucker decomposition for non-negative tensor (Cichocki et al., 2009)
All the objective functions are the divergence between the original matrix/tensor and thereconstructed matrix/tensor from the decomposition. Users can easily try some divergences
including Frobenius norm, Kullback–Leibler (KL) divergence, and Itakura-Saito (IS) divergenceby specifying algorithm argument. Some utility functions are available such as recTensor toreconstruct data matrix/tensor, plotTensor2D/plotTensor3D to visualize the structure of data∗ first author

Tsuyuzaki, & Nikaido. (2023). nnTensor: An R package for non-negative matrix/tensor decomposition. Journal of Open Source Software, 8 (84), 15015. https://doi.org/10.21105/joss.05015.

matrix/tensor, and toyModel to provide some toy datasets so that users can immediately checkthe operation of these functions and learn how to use them. Besides, mask matrices/tensors
can be specified in the matrix/tensor decomposition functions above to support rank estimation
with cross-validation.

ExampleThe non-negative tensor decomposition and plots in Figure Figure 1 can be easily reproducedon any machine where R is pre-installed by using the following commands in R:# Install package required (one per computer)install.packages("nnTensor")# Load required package (once per R instance)
library("nnTensor")# Load Toy data
data <- toyModel("Tucker")# Perform NTD
set.seed(1234)
out.ntd <- NTD(data, rank=c(4,5,6), algorithm="KL", num.iter=30)# Reconstruction of data tensor
rec.data <- recTensor(S=out.ntd$S, A=out.ntd$A)# Visualization
plotTensor3D(data)
plotTensor3D(rec.data)plot(out.ntd$RecError, type="b", xlab="Iteration", ylab="RecError")Figure 1: Toydata decomposed by NTD with KL-divergence

Related work
There are some versatile tools to perform tensor decomposition such as Tensorly (Python,Kossaifi et al. (2019)), TensorToolbox (MATLAB, Kolda & Bader (2009)), and TensorLab(MATLAB, Vervliet et al. (2016)). In the R language, rTensor (Li et al., 2018) is a widelyused package to perform tensor decomposition but none of the non-negative type algorithms
is implemented and nnTensor is the first package to meet the demand.

Tsuyuzaki, & Nikaido. (2023). nnTensor: An R package for non-negative matrix/tensor decomposition. Journal of Open Source Software, 8 (84), 25015. https://doi.org/10.21105/joss.05015.

References
Badea, L. (2009). Extracting Gene Expression Profiles Common to Colon and Pancreatic
Adenocarcinoma using Simultaneous nonnegative matrix factorization. Pacific Symposium
on Biocomputing 2008. https://doi.org/10.1142/9789812776136_0027
Cichocki, A., Zdunek, F., Phan, A. H., & Amari, S. (2009). Wiley. https://onlinelibrary.wiley.
com/doi/book/10.1002/9780470747278
Čopar, A., Zupan, B., & Zitnik, M. (2019). Fast Optimization of Non-Negative Matrix
Tri-Factorization. PLOS ONE. https://doi.org/10.1371/journal.pone.0217994
Kolda, T. G., & Bader, B. W. (2009). Tensor Decompositions and Applications. SIAM Review.
https://doi.org/10.1137/07070111X
Kossaifi, J., Panagakis, Y., Anandkumar, A., & Pantic, M. (2019). TensorLy: Tensor Learning inPython. Journal of Machine Learning Research. https://jmlr.org/papers/v20/18-277.html
Li, J., Bien, J., & Wells, M. T. (2018). rTensor: An R Package for Multidimensional Array(Tensor) Unfolding, Multiplication, and Decomposition. Journal of Statistical Software.
https://doi.org/10.18637/jss.v087.i10
Vervliet, N., Debals, O., & De Lathauwer, L. (2016). Tensorlab 3.0 - Numerical optimization
strategies for large-scale constrained and coupled matrix/tensor factorization. 2016 50th
Asilomar Conference on Signals, Systems and Computers. https://doi.org/10.1109/acssc.
2016.7869679
Yang, Z., & Michailidis, G. (2016). A non-negative matrix factorization method for detecting
modules in heterogeneous omics multi-modal data. Bioinformatics. https://doi.org/10.
1093/bioinformatics/btv544

Tsuyuzaki, & Nikaido. (2023). nnTensor: An R package for non-negative matrix/tensor decomposition. Journal of Open Source Software, 8 (84), 35015. https://doi.org/10.21105/joss.05015.