psi <- function(expected, actual, bins = 10) {
# bin edges from the reference data (deciles)
cuts <- unique(quantile(expected, seq(0, 1, length.out = bins + 1)))
cuts[c(1, length(cuts))] <- c(-Inf, Inf)
e <- tabulate(cut(expected, cuts), length(cuts) - 1) / length(expected)
a <- tabulate(cut(actual, cuts), length(cuts) - 1) / length(actual)
e <- pmax(e, 1e-4); a <- pmax(a, 1e-4) # avoid log(0)
sum((a - e) * log(a / e))
}
psi(training$n_exclaim, later$n_exclaim)[1] 0.1766474