Returns (weighted) proportion of deviance explained, see reference below. For the mean-squared error as deviance, this equals the usual (weighted) R-squared. The higher, the better.
The convenience functions
r_squared_poisson()
,
r_squared_gamma()
, and
r_squared_bernoulli()
call the function r_squared(..., deviance_function = fun)
with the right deviance
function.
r_squared(
actual,
predicted,
w = NULL,
deviance_function = mse,
reference_mean = NULL,
...
)
r_squared_poisson(actual, predicted, w = NULL, reference_mean = NULL, ...)
r_squared_gamma(actual, predicted, w = NULL, reference_mean = NULL, ...)
r_squared_bernoulli(actual, predicted, w = NULL, reference_mean = NULL, ...)
Observed values.
Predicted values.
Optional case weights.
A positive (deviance) function taking four arguments:
"actual", "predicted", "w" and "...". The default is mse()
, which equals
the average normal deviance.
An optional reference mean used to derive the null deviance. Recommended in out-of-sample applications.
Further arguments passed to weighted_mean()
and deviance_function()
.
A numeric vector of length one.
The deviance gain is calculated regarding the null model derived from the actual
values. While fine for in-sample considerations, this is only an approximation
for out-of-sample considerations. There, it is recommended to calculate null
deviance regarding the in-sample (weighted) mean. This value can be passed by
the argument reference_mean
.
Cohen, Jacob. et al. (2002). Applied Multiple Regression/Correlation Analysis for the Behavioral Sciences (3rd ed.). Routledge. ISBN 978-0805822236.
y <- 1:10
pred <- c(1, 1:9)
w <- 1:10
r_squared(y, pred)
#> [1] 0.8909091
r_squared(y, pred, w = w)
#> [1] 0.8363636
r_squared(y, pred, w = w, deviance_function = deviance_gamma)
#> [1] 0.6752588
r_squared_gamma(y, pred, w = w)
#> [1] 0.6752588
# Poisson situation
y2 <- 0:2
pred2 <- c(0.1, 1, 2)
r_squared(y2, pred2, deviance_function = deviance_poisson)
#> [1] 0.9278652
r_squared_poisson(y2, pred2)
#> [1] 0.9278652
# Binary (probabilistic) classification
y3 <- c(0, 0, 1, 1)
pred3 <- c(0.1, 0.1, 0.9, 0.8)
r_squared_bernoulli(y3, pred3, w = 1:4)
#> [1] 0.7503967
# With respect to 'own' deviance formula
myTweedie <- function(actual, predicted, w = NULL, ...) {
deviance_tweedie(actual, predicted, w, tweedie_p = 1.5, ...)
}
r_squared(y, pred, deviance_function = myTweedie)
#> [1] 0.8067623