This function computes the coefficient of determination (R squared).
Usage
r2_score(true, pred, weights = NULL, multi = c("average", "sum", "raw"))
Source
See Wiki page for further information.
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
Value
the R squared score, a double
or a vector of double
values,
one for each response, if response has multiple output values and multi = "raw"
Details
The coefficient of determination (R squared) is a measure of the proportion of variance in the dependent variable that is explained by the independent variables. The R2 estimated over n observations is defined as
$$R^{2}(y,\hat{y}) = 1 - \frac{\sum_{i=1}^{n} (y_{i} - \hat{y}_{i})^{2}}{\sum_{i=1}^{n} (y_{i} - \bar{y})^{2}}$$
where \(\bar{y}\) is the mean of the observed data, \(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 R squared is computed as
$$wR^{2}(w,y,\hat{y}) = 1 - \frac{\sum_{i=1}^{n} w_{i}*(y_{i} - \hat{y}_{i})^{2}}{\sum_{i=1}^{n} w_{i}*(y_{i} - \bar{y})^{2}}$$
where \(w_{i}\) is the weighting factor assigned to the \(i\)-th observation.
The best possible score is one.