MetricsWeighted

Michael Mayer

2019-07-24

library(MetricsWeighted)

Introduction

The R package MetricsWeighted provides weighted versions of different machine learning metrics and performance measures as well as tools to use it within a dplyr chain.

Installation

From CRAN:

install.packages("MetricsWeighted")

Latest version from github:

library(devtools)
install_github("mayer79/MetricsWeighted")

Illustration

Metrics and Performance Measures

Currently, the following metrics and performance measures are available.

They all take four arguments:

  1. actual: Actual observed values.

  2. predicted: Predicted values.

  3. w: Optional vector with case weights.

  4. ...: Further arguments.

Notable exception is deviance_tweedie that requires the Tweedie power as additional parameter. The value 0 corresponds to the Gaussian/normal deviance, the value 1 to the Poisson deviance and the value 2 to the Gamma deviance, see e.g. [1]. For fixed actual and predicted values, the Tweedie deviance is continuous with respect to this parameter.

Generalized R-squared

Furthermore, we provide a generalization of R-squared, defined as the proportion of deviance explained, i.e. one minus the ratio of residual deviance and intercept-only deviance, see e.g. [2]. By default, it calculates the ordinary R-squared, i.e. proportion of normal deviance (mean-squared error) explained. However, you can specify any different deviance function, e.g. deviance_tweedie with paramter 1.5 or the deviance of the logistic regression (deviance_bernoulli).

Tidyverse

In order to facilitate the use of these metrics in a dplyr chain, you can try out the function performance: Starting from a data set with actual and predicted values (and optional case weights), it calculates one or more metrics. The resulting values are returned as a data.frame. Stratified performance calculations can e.g. be done by using do from dplyr.

Examples

require(dplyr)

# Regression with `Sepal.Length` as response
iris %>% 
  mutate(pred = predict(fit_num, data = .)) %>% 
  performance("Sepal.Length", "pred")
#>   metric    value
#> 1   rmse 0.300627

# Same
iris %>% 
  mutate(pred = predict(fit_num, data = .)) %>% 
  performance("Sepal.Length", "pred", metrics = rmse)
#>   metric    value
#> 1   rmse 0.300627

# Grouped by Species
iris %>% 
  mutate(pred = predict(fit_num, data = .)) %>% 
  group_by(Species) %>% 
  do(performance(data = ., actual = "Sepal.Length", predicted = "pred"))
#> # A tibble: 3 x 3
#> # Groups:   Species [3]
#>   Species    metric value
#>   <fct>      <fct>  <dbl>
#> 1 setosa     rmse   0.254
#> 2 versicolor rmse   0.329
#> 3 virginica  rmse   0.313

# Customized output
iris %>% 
  mutate(pred = predict(fit_num, data = .)) %>% 
  performance("Sepal.Length", "pred", value = "performance",
              metrics = list(`root-mean-squared error` = rmse))
#>                    metric performance
#> 1 root-mean-squared error    0.300627

# Multiple measures
iris %>% 
  mutate(pred = predict(fit_num, data = .)) %>% 
  performance("Sepal.Length", "pred",
              metrics = list(rmse = rmse, mae = mae, `R-squared` = r_squared))
#>      metric     value
#> 1      rmse 0.3006270
#> 2       mae 0.2428628
#> 3 R-squared 0.8673123

# Grouped by Species
iris %>% 
  mutate(pred = predict(fit_num, data = .)) %>% 
  group_by(Species) %>% 
  do(performance(., "Sepal.Length", "pred",
                 metrics = list(rmse = rmse, mae = mae, `R-squared` = r_squared)))
#> # A tibble: 9 x 3
#> # Groups:   Species [3]
#>   Species    metric    value
#>   <fct>      <fct>     <dbl>
#> 1 setosa     rmse      0.254
#> 2 setosa     mae       0.201
#> 3 setosa     R-squared 0.469
#> 4 versicolor rmse      0.329
#> 5 versicolor mae       0.276
#> 6 versicolor R-squared 0.585
#> 7 virginica  rmse      0.313
#> 8 virginica  mae       0.252
#> 9 virginica  R-squared 0.752

# Passing extra argument (Tweedie p)
iris %>% 
  mutate(pred = predict(fit_num, data = .)) %>% 
  performance("Sepal.Length", "pred",
              metrics = list(`normal deviance` = deviance_normal, 
                             `Tweedie with p = 0` = deviance_tweedie),
              tweedie_p = 0)
#>               metric      value
#> 1    normal deviance 0.09037657
#> 2 Tweedie with p = 0 0.09037657

References

[1] Ohlsson E. and Johansson B. (2015). Non-Life Insurance Pricing with Generalized Linear Models. Springer Nature EN. ISBN 978-3642107900.

[2] Cohen, Jacob. et al. (2002). Applied Multiple Regression/Correlation Analysis for the Behavioral Sciences (3rd ed.). Routledge. ISBN 978-0805822236.