1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/mse.R
\name{mse}
\alias{mse}
\title{Mean Squared Error loss of a factor model}
\usage{
mse(A, w, d = NULL, h, mask_zeros = FALSE)
}
\arguments{
\item{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.}
\item{w}{dense matrix of class \code{matrix} with factors (columns) by features (rows)}
\item{d}{diagonal scaling vector of rank length}
\item{h}{dense matrix of class \code{matrix} with samples (columns) by factors (rows)}
\item{mask_zeros}{handle zeros as missing values, available only when \code{A} is sparse}
}
\value{
mean squared error of the factorization model
}
\description{
MSE of factor models \code{w} and \code{h} given sparse matrix \eqn{A}
}
\details{
Mean squared error of a matrix factorization of the form \eqn{A = wdh} is given by \deqn{\frac{\sum_{i,j}{(A - wdh)^2}}{ij}} where \eqn{i} and \eqn{j} are the number of rows and columns in \eqn{A}.
Thus, this function simply calculates the cross-product of \eqn{wh} or \eqn{wdh} (if \eqn{d} is specified),
subtracts that from \eqn{A}, squares the result, and calculates the mean of all values.
If no diagonal scaling vector is present in the model, input \code{d = rep(1, k)} where \code{k} is the rank of the model.
\strong{Parallelization.} Calculation of mean squared error is performed in parallel across columns in \code{A} using the number of threads set by \code{\link{setRcppMLthreads}}.
By default, all available threads are used, see \code{\link{getRcppMLthreads}}.
}
\examples{
\dontrun{
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)
}
}
\author{
Zach DeBruine
}
|