autumn
autumn copied to clipboard
Floating point comparison issues with "targets that do not sum to 1" error
Great package - thanks!
I've been playing around with it, and I've encountered an issue (maybe a bug?).
It looks like the function check_any_data_issues
is looking to see if all of the weighting targets add up to 1. I have a situation where they do all add up to 1, but I'm getting the error message "Target variable ... has targets that do not sum to 1." I suspect this is a floating point comparison issue.
Here's a very small example that should be reproducible (let me know if it doesn't work for you):
atlanta <- structure(list(w_race = c("White", "White", "White", "White",
"Black", "White", "Black", "White", "Other race", "Black", "White",
"White", "White", "White", "White", "White", "White", "White",
"White", "White")), row.names = c(NA, -20L), class = c("tbl_df",
"tbl", "data.frame"))
(atlanta)
#> w_race
#> 1 White
#> 2 White
#> 3 White
#> 4 White
#> 5 Black
#> 6 White
#> 7 Black
#> 8 White
#> 9 Other race
#> 10 Black
#> 11 White
#> 12 White
#> 13 White
#> 14 White
#> 15 White
#> 16 White
#> 17 White
#> 18 White
#> 19 White
#> 20 White
targets <- structure(list(variable = c("w_race", "w_race", "w_race"), level = c("Black",
"Other race", "White"), proportion = c(0.299944881294484, 0.0993062927185731,
0.600748825986942)), row.names = c(NA, -3L), class = c("tbl_df",
"tbl", "data.frame"))
(targets)
#> variable level proportion
#> 1 w_race Black 0.29994488
#> 2 w_race Other race 0.09930629
#> 3 w_race White 0.60074883
sum(targets$proportion)
#> [1] 1
autumn::harvest(atlanta, target = targets)
#> Error in check_any_data_issues(data, target, weights): Errors detected in weight targets:
#> Target variable `w_race` has targets that do not sum to 1.
Created on 2020-07-14 by the reprex package (v0.3.0)
If I'm missing something, please let me know. Thanks again for all of your work on the package.