Skip to content

Commit

Permalink
Fix cpp pointer bug (#435)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinju authored Jan 20, 2025
1 parent 6b3cc17 commit b06d14b
Show file tree
Hide file tree
Showing 29 changed files with 214 additions and 148 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# shapr (development version)

* Fix CRAN NOTE which turned out to be bug related to returning NULL rather than integer(0) to identify (unconditional)
asymmetric causal sampling [#435](https://github.com/NorskRegnesentral/shapr/pull/435)

# shapr 1.0.1

* Spelling checking and other minor clean up [#431](https://github.com/NorskRegnesentral/shapr/pull/431))
Expand Down
7 changes: 6 additions & 1 deletion R/asymmetric_and_casual_Shapley.R
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,12 @@ get_S_causal_steps <- function(S, causal_ordering, confounding, as_string = FALS

# If confounding is FALSE, add intervened features in the same component to the `to_condition` set.
# If confounding is TRUE, then no extra conditioning.
if (!confounding[i]) to_condition <- union(intersect(causal_ordering[[i]], index_given), to_condition)

if (!confounding[i]) {
inter0 <- intersect(causal_ordering[[i]], index_given)
inter0 <- if (length(inter0) == 0) NULL else inter0
to_condition <- union(inter0, to_condition)
}

# Save Sbar and S (sorting is for the visual)
to_sample <- sort(to_sample)
Expand Down
1 change: 0 additions & 1 deletion R/explain_forecast.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
#'
#' @author Jon Lachmann, Martin Jullum
#' @examples
#'
#' \dontrun{
#' # Load example data
#' data("airquality")
Expand Down
1 change: 0 additions & 1 deletion R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@
#'
#' @export
#' @examples
#'
#' \dontrun{
#' data("airquality")
#' airquality <- airquality[complete.cases(airquality), ]
Expand Down
1 change: 0 additions & 1 deletion R/zzz.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
.onLoad <- function(libname = find.package("shapr"), pkgname = "shapr") {

# CRAN Note avoidance 1
# (as per https://stackoverflow.com/questions/77323811/r-package-to-cran-had-cpu-time-5-times-elapsed-time)
# CRAN OMP THREAD LIMIT
Expand Down
1 change: 0 additions & 1 deletion man/explain_forecast.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/plot.shapr.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions tests/testthat/_snaps/asymmetric-causal-output.md
Original file line number Diff line number Diff line change
Expand Up @@ -374,11 +374,11 @@
i Using 32 of 32 coalitions.
Output
explain_id none Solar.R Wind Temp Month Day
<int> <num> <num> <num> <num> <num> <num>
1: 1 42.44 -7.978 10.871 12.1981 -2.188 -0.3003
2: 2 42.44 3.637 -6.474 -9.6711 -1.850 0.4779
3: 3 42.44 1.926 -27.039 0.7298 1.404 5.4112
explain_id none Solar.R Wind Temp Month Day
<int> <num> <num> <num> <num> <num> <num>
1: 1 42.44 -8.606 10.387 13.963 -4.010 0.8685
2: 2 42.44 3.506 -6.071 -4.521 -6.183 -0.6104
3: 3 42.44 2.371 -26.300 2.790 1.600 1.9707

# output_sym_caus_conf_mix

Expand Down
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/testthat/test-asymmetric-causal-output.R
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ test_that("output_asym_caus_conf_TRUE", {
})



test_that("output_asym_caus_conf_FALSE", {
expect_snapshot_rds(
explain(
Expand Down Expand Up @@ -250,6 +249,7 @@ test_that("output_sym_caus_conf_TRUE", {
)
})


test_that("output_sym_caus_conf_FALSE", {
expect_snapshot_rds(
explain(
Expand Down
30 changes: 30 additions & 0 deletions tests/testthat/test-asymmetric-causal-setup.R
Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,33 @@ test_that("asymmetric erroneous input: `confounding`", {
error = TRUE
)
})



test_that("cond_sym_as_NULLconfounding", {
ex_condsym <- explain(
testing = TRUE,
model = model_lm_numeric,
x_explain = x_explain_numeric,
x_train = x_train_numeric,
approach = "gaussian",
phi0 = p0,
n_MC_samples = 5 # Just for speed
)

ex_NULLconfounding <- explain(
testing = TRUE,
model = model_lm_numeric,
x_explain = x_explain_numeric,
x_train = x_train_numeric,
approach = "gaussian",
phi0 = p0,
asymmetric = FALSE,
causal_ordering = list(1:2, 3, 4:5),
confounding = NULL,
n_MC_samples = 5 # Just for speed
)

# When confounding is NULL, causal_ordering is ignored and regular symmetric conditional shapley values is computed
expect_equal(ex_condsym$shapley_values_est, ex_NULLconfounding$shapley_values_est)
})
305 changes: 169 additions & 136 deletions vignettes/asymmetric_causal.Rmd

Large diffs are not rendered by default.

Binary file modified vignettes/figure_asymmetric_causal/compare_plots-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified vignettes/figure_asymmetric_causal/explanation_sym_con_SV-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified vignettes/figure_asymmetric_causal/group_gaussian_plot_SV-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified vignettes/figure_asymmetric_causal/n_coalitions_plot_SV-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified vignettes/figure_asymmetric_causal/n_coalitions_plot_beeswarm-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified vignettes/figure_asymmetric_causal/scatter_plots-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified vignettes/figure_asymmetric_causal/two_dates_1-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified vignettes/figure_asymmetric_causal/two_dates_2-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified vignettes/figure_asymmetric_causal/two_dates_3-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b06d14b

Please sign in to comment.