aaronrudkin / autumn Goto Github PK
View Code? Open in Web Editor NEWautumn: Fast, Modern, and Tidy-Friendly Iterative Raking in R.
License: Other
autumn: Fast, Modern, and Tidy-Friendly Iterative Raking in R.
License: Other
I have some variables in a data set the a want to weight. However, not all levels are present in the data set.
library(dplyr)
library(autumn)
harvest(d, weights)
So I get this error:
Error in check_any_data_issues(data, target, weights) : Errors detected in data. Some variables have values in the weight targets which are not present in the data:`
Here is a dput of the quotes
dput(weights)
list(Rec_Age = c(`1` = 0, `2` = 0.181, `3` = 0.2877, `4` = 0.3311,
`5` = 0.2001), Rec_Income = c(`1` = 0.1105, `2` = 0.2852, `3` = 0.2343,
`4` = 0.3699), Q6 = c(`1` = 0.067, `2` = 0.3409, `3` = 0.592),
RECQ5_1 = c(`1` = 0.4099, `2` = 0.5239, `3` = 0.0662), RECQ5_2 = c(`1` = 0.1621,
`2` = 0.3803, `3` = 0.4576), RECQ5_3 = c(`1` = 0.0508, `2` = 0.294,
`3` = 0.6551), RECQ5_4 = c(`1` = 0.103, `2` = 0.4864, `3` = 0.4106
))
and the data:
dput(d)
structure(list(RESPID = structure(c(459, 311, 223, 60, 613, 495,
300, 273, 78, 170, 217, 61, 175, 619, 270, 218, 453, 492, 23,
65, 33, 113, 532, 26, 119, 49, 208, 102, 200, 165, 435, 298,
593, 220, 111, 53, 494, 271, 305, 420, 323, 607, 105, 19, 426,
171, 330, 201, 332, 277), label = "RESPID - Respondent ID", format.spss = "F10.0", display_width = 0L),
Rec_Age = structure(c(4, 2, 4, 3, 4, 4, 4, 3, 2, 2, 3, 2,
3, 4, 4, 2, 4, 4, 2, 3, 2, 2, 2, 3, 3, 2, 2, 2, 2, 3, 2,
3, 2, 3, 4, 3, 4, 3, 2, 3, 3, 3, 4, 4, 4, 2, 2, 3, 4, 3), label = "Rec_Age - Recode Age", format.spss = "F1.0", display_width = 0L),
Rec_Income = structure(c(3, 1, 2, 1, 1, 2, 2, 3, 2, 1, 2,
2, 2, 1, 1, 2, 2, 3, 3, 2, 2, 2, 2, 3, 2, 3, 2, 2, 1, 2,
2, 2, 1, 3, 1, 1, 1, 1, 1, 3, 3, 2, 3, 3, 2, 2, 2, 2, 2,
2), label = "Rec_Income - Recode Income", format.spss = "F1.0", display_width = 0L),
Q6 = structure(c(2, 1, 2, 3, 2, 3, 2, 1, 3, 2, 2, 3, 3, 3,
2, 2, 3, 3, 2, 1, 2, 3, 3, 2, 2, 2, 1, 2, 1, 2, 2, 3, 3,
2, 3, 2, 3, 2, 2, 1, 3, 2, 2, 2, 3, 2, 2, 1, 3, 2), label = "Q6 - Wie stark interessieren Sie sich für Bekleidung und Mode?", format.spss = "F1.0", display_width = 0L),
RECQ5_1 = c(1, 1, 2, 2, 2, 1, 1, 1, 2, 1, 2, 2, 2, 2, 1,
1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 1, 1, 1, 2, 1, 1, 2, 2,
3, 1, 1, 2, 1, 1, 2, 1, 2, 1, 1, 1, 1, 2, 1, 1), RECQ5_2 = c(2,
2, 3, 3, 3, 2, 3, 1, 3, 3, 3, 3, 3, 3, 3, 2, 3, 3, 1, 3,
2, 3, 2, 2, 2, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 3, 3, 2, 1,
1, 3, 3, 2, 1, 3, 1, 2, 1, 3, 2), RECQ5_3 = c(3, 1, 3, 3,
3, 3, 2, 2, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 1, 2, 2, 3, 3,
2, 2, 2, 1, 3, 3, 2, 2, 3, 2, 3, 3, 2, 3, 3, 2, 1, 3, 3,
3, 2, 3, 1, 3, 3, 3, 2), RECQ5_4 = c(1, 2, 2, 2, 2, 2, 1,
1, 3, 2, 2, 3, 3, 3, 1, 1, 2, 3, 1, 1, 1, 3, 2, 1, 2, 1,
1, 1, 1, 2, 1, 3, 3, 3, 2, 1, 2, 2, 1, 1, 2, 1, 2, 1, 2,
1, 2, 3, 2, 2)), row.names = c(NA, -50L), class = "data.frame")
It has been brought up that a useful flag for harvest would be the ability to automatically collapse target levels (either to remove smaller levels and improve convergence, or to solve the problem of missing interaction levels). I'm not fully sure how I'd implement this, but I think it's a good idea.
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.
Things to do before v0.10:
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.