Learn R Programming

RcppML (version 0.3.7)

mse: Mean Squared Error loss of a factor model

Description

MSE of factor models w and h given sparse matrix \(A\)

Usage

mse(A, w, d = NULL, h, mask_zeros = FALSE)

Arguments

A

matrix of features-by-samples in dense or sparse format (preferred classes are "matrix" or "Matrix::dgCMatrix", respectively). Prefer sparse storage when more than half of all values are zero.

w

dense matrix of class matrix with factors (columns) by features (rows)

d

diagonal scaling vector of rank length

h

dense matrix of class matrix with samples (columns) by factors (rows)

mask_zeros

handle zeros as missing values, available only when A is sparse

Value

mean squared error of the factorization model

Details

Mean squared error of a matrix factorization of the form \(A = wdh\) is given by $$\frac{\sum_{i,j}{(A - wdh)^2}}{ij}$$ where \(i\) and \(j\) are the number of rows and columns in \(A\).

Thus, this function simply calculates the cross-product of \(wh\) or \(wdh\) (if \(d\) is specified), subtracts that from \(A\), squares the result, and calculates the mean of all values.

If no diagonal scaling vector is present in the model, input d = rep(1, k) where k is the rank of the model.

Parallelization. Calculation of mean squared error is performed in parallel across columns in A using the number of threads set by setRcppMLthreads. By default, all available threads are used, see getRcppMLthreads.

Examples

Run this code
# NOT RUN {
library(Matrix)
A <- Matrix::rsparsematrix(1000, 1000, 0.1)
model <- nmf(A, k = 10, tol = 0.01)
c_mse <- mse(A, model$w, model$d, model$h)
R_mse <- mean((A - model$w %*% Diagonal(x = model$d) %*% model$h)^2)
all.equal(c_mse, R_mse)
# }

Run the code above in your browser using DataLab