Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Floating point comparison issues with "targets that do not sum to 1" error #4

Open
dfeehan opened this issue Jul 15, 2020 · 2 comments

Comments

@dfeehan
Copy link

dfeehan commented Jul 15, 2020

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.

@DonghuiWangDemography
Copy link

I had exact the same problem where all proportions add to 1 but still got error message. Is there a solution for this?

@dfeehan
Copy link
Author

dfeehan commented Aug 30, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants