mse: Mean Squared Error loss of a factor model

Description Usage Arguments Details Value Author(s) Examples

View source: R/mse.R

Description

MSE of factor models w and h given sparse matrix A

Usage

1
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

Details

Mean squared error of a matrix factorization of the form A = wdh is given by

\frac{∑_{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.

Value

mean squared error of the factorization model

Author(s)

Zach DeBruine

Examples

1
2
3
4
5
6
7
8
9
## 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)

## End(Not run)

RcppML documentation built on Sept. 22, 2021, 1:06 a.m.