Skip to content

Commit 8adc4e7

Browse files
authored
Merge pull request #136 from ModelOriented/check-predictions
Add checks to prediction output
2 parents 5c3914f + adff5c1 commit 8adc4e7

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

NEWS.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ This release is intended to be the last before stable version 1.0.0.
44

55
## Major changes
66

7-
- Factor-valued predictions are not anymore supported.
7+
- Factor-valued predictions are not supported anymore.
88

99
## Maintenance
1010

1111
- Fix CRAN note about unavailable link to `gam::gam()`.
1212
- Added dependency to {MASS} for calculating Moore-Penrose generalized matrix inverse.
13+
- Slight code improvements.
1314

1415
# kernelshap 0.5.0
1516

R/utils.R

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,13 @@ align_pred <- function(x) {
153153
if (is.data.frame(x) && ncol(x) == 1L) {
154154
x <- x[[1L]]
155155
}
156-
as.matrix(x)
156+
if (!is.matrix(x)) {
157+
x <- as.matrix(x)
158+
}
159+
if (!is.numeric(x) && !is.logical(x)) {
160+
stop("Predictions must be numeric!")
161+
}
162+
return(x)
157163
}
158164

159165
#' Head of List Elements

tests/testthat/test-utils.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,8 @@ test_that("wrowmean_vector() works for 1D matrices", {
8282
expect_equal(out2, cbind(a = c(a, b)))
8383
})
8484

85+
test_that("align_pred() works", {
86+
expect_error(align_pred(c("A", "B")))
87+
expect_error(align_pred(factor(c("A", "B"))))
88+
expect_equal(align_pred(1:4), as.matrix(1:4))
89+
})

0 commit comments

Comments
 (0)