# NOT RUN {
library(dplyr)
# ---------------------------------------------------------------------------
# Two class example
data(two_class_example)
# Assuming `Class1` is our "event", this penalizes false positives heavily
costs1 <- tribble(
  ~truth,   ~estimate, ~cost,
  "Class1", "Class2",  1,
  "Class2", "Class1",  2
)
# Assuming `Class1` is our "event", this penalizes false negatives heavily
costs2 <- tribble(
  ~truth,   ~estimate, ~cost,
  "Class1", "Class2",  2,
  "Class2", "Class1",  1
)
classification_cost(two_class_example, truth, Class1, costs = costs1)
classification_cost(two_class_example, truth, Class1, costs = costs2)
# ---------------------------------------------------------------------------
# Multiclass
data(hpc_cv)
# Define cost matrix from Kuhn and Johnson (2013)
hpc_costs <- tribble(
   ~estimate, ~truth, ~cost,
   "VF",   "VF",     0,
   "VF",    "F",     1,
   "VF",    "M",     5,
   "VF",    "L",    10,
   "F",    "VF",     1,
   "F",     "F",     0,
   "F",     "M",     5,
   "F",     "L",     5,
   "M",    "VF",     1,
   "M",     "F",     1,
   "M",     "M",     0,
   "M",     "L",     1,
   "L",    "VF",     1,
   "L",     "F",     1,
   "L",     "M",     1,
   "L",     "L",     0
)
# You can use the col1:colN tidyselect syntax
hpc_cv %>%
  filter(Resample == "Fold01") %>%
  classification_cost(obs, VF:L, costs = hpc_costs)
# Groups are respected
hpc_cv %>%
  group_by(Resample) %>%
  classification_cost(obs, VF:L, costs = hpc_costs)
# }
Run the code above in your browser using DataLab