-
Notifications
You must be signed in to change notification settings - Fork 29
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
Add filtering joins, with documentation and tests. #120
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
#' @export | ||
semi_join.tbl_svy <- function( | ||
x, | ||
y, | ||
by = NULL, | ||
copy = FALSE, | ||
..., | ||
na_matches = c("na", "never") | ||
) { | ||
|
||
if (inherits(y, "tbl_svy")) { | ||
y <- y$variables | ||
} | ||
|
||
x <- mutate(x, `___row_number` = dplyr::row_number()) | ||
|
||
filtered_vars <- semi_join(x = x$variables, | ||
y = y, | ||
by = by, | ||
copy = copy, | ||
na_matches = na_matches, | ||
...) | ||
|
||
x <- mutate(x, `___retained` = `___row_number` %in% filtered_vars[['___row_number']]) | ||
x <- filter(x, .data$`___retained`) | ||
x <- select(x, -.data$`___retained`) | ||
|
||
x | ||
|
||
} | ||
|
||
#' @export | ||
anti_join.tbl_svy <- function( | ||
x, | ||
y, | ||
by = NULL, | ||
copy = FALSE, | ||
..., | ||
na_matches = c("na", "never") | ||
) { | ||
|
||
if (inherits(y, "tbl_svy")) { | ||
y <- y$variables | ||
} | ||
|
||
x <- mutate(x, `___row_number` = dplyr::row_number()) | ||
|
||
filtered_vars <- anti_join(x = x$variables, | ||
y = y, | ||
by = by, | ||
copy = copy, | ||
na_matches = na_matches, | ||
...) | ||
|
||
x <- mutate(x, `___retained` = `___row_number` %in% filtered_vars[['___row_number']]) | ||
x <- filter(x, `___retained`) | ||
x <- select(x, -`___retained`) | ||
|
||
x | ||
|
||
} | ||
|
||
# Import + export generics from dplyr and tidyr | ||
#' Filtering joins from dplyr | ||
#' | ||
#' These are data manipulation functions designed to work on a \code{tbl_svy} object | ||
#' and another data frame or \code{tbl_svy} object. | ||
#' | ||
#' \code{semi_join} and \code{anti_join} filter certain observations from a \code{tbl_svy} | ||
#' depending on the presence or absence of matches in another table. | ||
#' See \code{\link[dplyr]{filter-joins}} for more details. | ||
#' | ||
#' Mutating joins (\code{full_join}, \code{left_join}, etc.) are not implemented | ||
#' for any \code{tbl_svy} objects. These data manipulations | ||
#' may require modifications to the survey variable specifications and so | ||
#' cannot be done automatically. Instead, use dplyr to perform them while the | ||
#' data is still stored in data.frames. | ||
#' @name dplyr_filter_joins | ||
NULL | ||
|
||
#' @name semi_join | ||
#' @export | ||
#' @importFrom dplyr semi_join | ||
#' @rdname dplyr_filter_joins | ||
NULL | ||
|
||
#' @name anti_join | ||
#' @export | ||
#' @importFrom dplyr anti_join | ||
#' @rdname dplyr_filter_joins | ||
NULL |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,229 @@ | ||
context("filtering joins (semi_join and anti_join) work") | ||
|
||
suppressPackageStartupMessages({ | ||
library(survey) | ||
library(srvyr) | ||
library(dplyr) | ||
}) | ||
|
||
source("utilities.R") | ||
|
||
# Set up example data ---- | ||
|
||
data(api) | ||
|
||
##_ Create simple stratified survey design object ---- | ||
stratified_design <- apistrat %>% | ||
as_survey_design(strata = stype, weights = pw) | ||
|
||
##_ Create clustered survey design object ---- | ||
cluster_design <- as_survey_design( | ||
.data = apiclus1, | ||
id = dnum, | ||
weights = pw, | ||
fpc = fpc | ||
) | ||
|
||
##_ Create survey design object with calibration weights ---- | ||
##_ NOTE: The survey package uses special behavior when subsetting such survey designs. | ||
##_ Rows are never removed, the weights are simply set effectively to zero (technically, Inf) | ||
|
||
### Add raking weights for school type | ||
pop.types <- data.frame(stype=c("E","H","M"), Freq=c(4421,755,1018)) | ||
pop.schwide <- data.frame(sch.wide=c("No","Yes"), Freq=c(1072,5122)) | ||
|
||
raked_design <- rake( | ||
cluster_design, | ||
sample.margins = list(~stype,~sch.wide), | ||
population.margins = list(pop.types, pop.schwide) | ||
) | ||
|
||
# semi_join ---- | ||
|
||
test_that( | ||
"semi_join works with `by = NULL`", { | ||
# Stratified design | ||
expect_equal( | ||
## Calculate statistic, after using a filtering join | ||
object = stratified_design %>% | ||
semi_join(y = filter(apistrat, stype == "E")) %>% | ||
summarize(stat = survey_mean(pcttest)) %>% | ||
pull("stat"), | ||
## Calculate statistic after manually filtering | ||
expected = stratified_design %>% | ||
filter(stype == "E") %>% | ||
summarize(stat = survey_mean(pcttest)) %>% | ||
pull("stat") | ||
) | ||
|
||
# Cluster design | ||
expect_equal( | ||
## Calculate statistic, after using a filtering join | ||
object = cluster_design %>% | ||
semi_join(y = filter(apiclus1, stype == "E")) %>% | ||
summarize(stat = survey_mean(pcttest)) %>% | ||
pull("stat"), | ||
## Calculate statistic after manually filtering | ||
expected = cluster_design %>% | ||
filter(stype == "E") %>% | ||
summarize(stat = survey_mean(pcttest)) %>% | ||
pull("stat") | ||
) | ||
|
||
# Calibration weighted design | ||
expect_equal( | ||
## Calculate statistic, after using a filtering join | ||
object = raked_design %>% | ||
semi_join(y = filter(apiclus1, stype == "E")) %>% | ||
summarize(stat = survey_mean(pcttest)) %>% | ||
pull("stat"), | ||
## Calculate statistic after manually filtering | ||
expected = raked_design %>% | ||
filter(stype == "E") %>% | ||
summarize(stat = survey_mean(pcttest)) %>% | ||
pull("stat") | ||
) | ||
}) | ||
|
||
test_that( | ||
"semi_join works with supplied `by` argument", { | ||
# Stratified design | ||
expect_equal( | ||
## Calculate statistic, after using a filtering join | ||
object = stratified_design %>% | ||
semi_join(y = filter(apistrat, stype == "E"), | ||
by = "stype") %>% | ||
summarize(stat = survey_mean(pcttest)) %>% | ||
pull("stat"), | ||
## Calculate statistic after manually filtering | ||
expected = stratified_design %>% | ||
filter(stype == "E") %>% | ||
summarize(stat = survey_mean(pcttest)) %>% | ||
pull("stat") | ||
) | ||
|
||
# Cluster design | ||
expect_equal( | ||
## Calculate statistic, after using a filtering join | ||
object = cluster_design %>% | ||
semi_join(y = filter(apiclus1, stype == "E"), | ||
by = "stype") %>% | ||
summarize(stat = survey_mean(pcttest)) %>% | ||
pull("stat"), | ||
## Calculate statistic after manually filtering | ||
expected = cluster_design %>% | ||
filter(stype == "E") %>% | ||
summarize(stat = survey_mean(pcttest)) %>% | ||
pull("stat") | ||
) | ||
|
||
# Calibration weighted design | ||
expect_equal( | ||
## Calculate statistic, after using a filtering join | ||
object = raked_design %>% | ||
semi_join(y = filter(apiclus1, stype == "E"), | ||
by = "stype") %>% | ||
summarize(stat = survey_mean(pcttest)) %>% | ||
pull("stat"), | ||
## Calculate statistic after manually filtering | ||
expected = raked_design %>% | ||
filter(stype == "E") %>% | ||
summarize(stat = survey_mean(pcttest)) %>% | ||
pull("stat") | ||
) | ||
}) | ||
|
||
# anti_join ---- | ||
|
||
test_that( | ||
"anti_join works with `by = NULL`", { | ||
# Stratified design | ||
expect_equal( | ||
## Calculate statistic, after using a filtering join | ||
object = stratified_design %>% | ||
anti_join(y = filter(apistrat, stype == "E")) %>% | ||
summarize(stat = survey_mean(pcttest)) %>% | ||
pull("stat"), | ||
## Calculate statistic after manually filtering | ||
expected = stratified_design %>% | ||
filter(stype != "E") %>% | ||
summarize(stat = survey_mean(pcttest)) %>% | ||
pull("stat") | ||
) | ||
|
||
# Cluster design | ||
expect_equal( | ||
## Calculate statistic, after using a filtering join | ||
object = cluster_design %>% | ||
anti_join(y = filter(apiclus1, stype == "E")) %>% | ||
summarize(stat = survey_mean(pcttest)) %>% | ||
pull("stat"), | ||
## Calculate statistic after manually filtering | ||
expected = cluster_design %>% | ||
filter(stype != "E") %>% | ||
summarize(stat = survey_mean(pcttest)) %>% | ||
pull("stat") | ||
) | ||
|
||
# Calibration weighted design | ||
expect_equal( | ||
## Calculate statistic, after using a filtering join | ||
object = raked_design %>% | ||
anti_join(y = filter(apiclus1, stype == "E")) %>% | ||
summarize(stat = survey_mean(pcttest)) %>% | ||
pull("stat"), | ||
## Calculate statistic after manually filtering | ||
expected = raked_design %>% | ||
filter(stype != "E") %>% | ||
summarize(stat = survey_mean(pcttest)) %>% | ||
pull("stat") | ||
) | ||
}) | ||
|
||
test_that( | ||
"anti_join works with supplied `by` argument", { | ||
# Stratified design | ||
expect_equal( | ||
## Calculate statistic, after using a filtering join | ||
object = stratified_design %>% | ||
anti_join(y = filter(apistrat, stype == "E"), | ||
by = "stype") %>% | ||
summarize(stat = survey_mean(pcttest)) %>% | ||
pull("stat"), | ||
## Calculate statistic after manually filtering | ||
expected = stratified_design %>% | ||
filter(stype != "E") %>% | ||
summarize(stat = survey_mean(pcttest)) %>% | ||
pull("stat") | ||
) | ||
|
||
# Cluster design | ||
expect_equal( | ||
## Calculate statistic, after using a filtering join | ||
object = cluster_design %>% | ||
anti_join(y = filter(apiclus1, stype == "E"), | ||
by = "stype") %>% | ||
summarize(stat = survey_mean(pcttest)) %>% | ||
pull("stat"), | ||
## Calculate statistic after manually filtering | ||
expected = cluster_design %>% | ||
filter(stype != "E") %>% | ||
summarize(stat = survey_mean(pcttest)) %>% | ||
pull("stat") | ||
) | ||
|
||
# Calibration weighted design | ||
expect_equal( | ||
## Calculate statistic, after using a filtering join | ||
object = raked_design %>% | ||
anti_join(y = filter(apiclus1, stype == "E"), | ||
by = "stype") %>% | ||
summarize(stat = survey_mean(pcttest)) %>% | ||
pull("stat"), | ||
## Calculate statistic after manually filtering | ||
expected = raked_design %>% | ||
filter(stype != "E") %>% | ||
summarize(stat = survey_mean(pcttest)) %>% | ||
pull("stat") | ||
) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you skip the
___retained
variable and just filter on the expression? (If not, I thnk you need to de-select the___row_number
variable, don't you?)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that's a good idea. I didn't de-select
___row_number
because it was actually removed internally in the call tofilter()
. But just to be careful, in f240676 added a conditional select to remove that column if it still exists after usingfilter()
.