Skip to content

Instantly share code, notes, and snippets.

@schnee
Created May 5, 2019 18:08
Show Gist options
  • Save schnee/22ba28327d361e9dfe1ca73bb85c398c to your computer and use it in GitHub Desktop.
Save schnee/22ba28327d361e9dfe1ca73bb85c398c to your computer and use it in GitHub Desktop.
What considerations are important in choosing between the two styles below? Interpretability, maintainability, speed, other?
library(purrr)
y_test <- sample(0:9, 2000, replace = TRUE)
pred1 <- sample(0:9, 2000, replace= TRUE)
pred2 <- sample(0:9, 2000, replace= TRUE)
preds <- list(pred1, pred2)
# want to apply the following to preds
# sum(diag(table(y_test, pred)))/length(y_test)
#
# this
#
preds %>%
map(table, y_test) %>%
map(diag) %>%
map(sum) %>%
map_dbl(.f = function(x){x/length(y_test)})
#
# or this
#
preds %>%
map_dbl(~{sum(diag(table(y_test, .x)))/length(y_test)})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment