This function computes the mean squared error.
Usage
mean_squared_error(
true,
pred,
weights = NULL,
multi = c("average", "sum", "raw"),
root = FALSE,
rweights = NULL
)
Arguments
- true
a vector (or a matrix) of observed values. If a matrix is provided, a multi-response is assumed
- pred
a vector (or a matrix) of predicted values
- weights
observation weights
- multi
what to do when response has multiple output values
average
errors of multiple outputs are averaged to get a single value for each observation
sum
errors of multiple outputs are summed up to get a single value for each observation
raw
returns a vector containing one error for each output
- root
logical, whether to return the MSE or the root mean squared error (RMSE)
- rweights
response weights
Value
the mean squared error, a positive double
or a vector of positive double
values,
one for each response, if response has multiple output values and multi = "raw"
Details
The mean squared error (MSE) is a measure of errors based on squared losses. Small and large errors are not treated equally, as the large errors are more heavily weighted than small ones thus skewing the measure. It is more sensitive to outliers compared to other measures (e.g. MAE). The MSE estimated over n observations is defined as
$$MSE(y,\hat{y}) = \frac{1}{n}\sum_{i=1}^{n} (y_{i} - \hat{y}_{i})^{2}$$
where \(y_{i}\) is the true value of the \(i\)-th sample and \(\hat{y}_{i}\) is the predicted value. If observation weights are provided, then the weighted mean absolute error is computed as
$$wMSE(w,y,\hat{y}) = \frac{1}{\sum_{i=1}^{n} w_{i}}\sum_{i=1}^{n} w_{i} * (y_{i} - \hat{y}_{i})^{2}$$
where \(w_{i}\) is the weighting factor assigned to the \(i\)-th observation.
The best possible score is zero.