Skip to content

Commit

Permalink
candidate fix for #7
Browse files Browse the repository at this point in the history
  • Loading branch information
pbiecek committed Dec 8, 2017
1 parent 7005780 commit 56cab46
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions R/break.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ broken <- function(model, new_observation, ...) {
broken.lm <- function(model, new_observation, ..., baseline = 0) {
ny <- predict.lm(model, newdata = new_observation, type="terms")

# add terms with :
labels <- attr(terms(model), "term.labels")
ilabels <- grep(labels, patter=":", value = TRUE)
for (interact in ilabels) {
vars <- strsplit(interact, split = ":")[[1]]
new_observation[,interact] <- apply(new_observation[,vars], 1, paste0, collapse=":")
}

broken_obj <- data.frame(variable = paste(colnames(ny), "=",
sapply(new_observation[colnames(ny)], as.character)),
contribution = c(ny))
Expand Down Expand Up @@ -81,6 +89,14 @@ broken.lm <- function(model, new_observation, ..., baseline = 0) {
broken.glm <- function(model, new_observation, ..., baseline = 0) {
ny <- predict.glm(model, newdata = new_observation, type="terms")

# add terms with :
labels <- attr(terms(model), "term.labels")
ilabels <- grep(labels, patter=":", value = TRUE)
for (interact in ilabels) {
vars <- strsplit(interact, split = ":")[[1]]
new_observation[,interact] <- apply(new_observation[,vars], 1, paste0, collapse=":")
}

broken_obj <- data.frame(variable = paste(colnames(ny), "=",
sapply(new_observation[colnames(ny)], as.character)),
contribution = c(ny))
Expand Down

0 comments on commit 56cab46

Please sign in to comment.