-
Notifications
You must be signed in to change notification settings - Fork 6
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 some data for tests, and add_weights function #9
Merged
yannsay-impact
merged 3 commits into
impact-initiatives:main
from
AbrahamAz:add_weights
Jun 19, 2023
Merged
Changes from all 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,3 +28,4 @@ Suggests: | |
testthat (>= 3.0.0), | ||
tibble | ||
Config/testthat/edition: 3 | ||
LazyData: true |
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,80 @@ | ||
#' Add a weight variable using the sample frame | ||
#' | ||
#' @param .dataset the clean dataframe | ||
#' @param sample_data sample dataframe including poplution numbers and the strata | ||
#' @param strata_column_dataset name of strata column in the clean dataframe | ||
#' @param strata_column_sample name of strata column in the sample dataframe | ||
#' @param population_column name of population column in the sample dataframe | ||
#' @param weight_column name of the added weight column. By default "weights" | ||
#' | ||
#' @return The clean dataset with 1 new column: weight | ||
#' @export | ||
#' | ||
#' @examples | ||
#' clean_data <- data.frame(uuid = c(1,2,3,4,5,6,7,8), | ||
#' strata = c("strata1","strata2","strata1", | ||
#' "strata2","strata1","strata2", | ||
#' "strata1","strata1")) | ||
#' sample <- data.frame(strata = c("strata1","strata2"), | ||
#' population = c(30000,50000)) | ||
#' | ||
#' clean_data_weighted <- clean_data %>% | ||
#' add_weights(sample, | ||
#' strata_column_dataset = "strata", | ||
#' strata_column_sample = "strata", | ||
#' population_column = "population") | ||
add_weights<- function(.dataset, | ||
sample_data, | ||
strata_column_dataset = NULL, | ||
strata_column_sample = NULL, | ||
population_column = NULL, | ||
weight_column = "weights"){ | ||
|
||
# make dataset a dataframe | ||
.dataset <- as.data.frame(.dataset) | ||
|
||
# If strata_column do not exist in sample_data or dataset | ||
if(!strata_column_sample %in% names(sample_data)) | ||
stop("Cannot find the defined strata column in the provided sample frame.") | ||
if(!strata_column_dataset %in% names(.dataset)) | ||
stop("Cannot find the defined strata column in the provided dataset.") | ||
|
||
# IF all strata from dataset not in sample frame | ||
if(!all(.dataset[[strata_column_dataset]] %in% sample_data[[strata_column_sample]])) | ||
stop("Not all strata from dataset are in sample frame") | ||
|
||
if(!all(sample_data[[strata_column_sample]] %in% .dataset[[strata_column_dataset]])) | ||
stop("Not all strata from sample frame are in dataset") | ||
|
||
# If population_column do not exist in sample_data | ||
if(!population_column %in% names(sample_data)) | ||
stop("Cannot find the defined population_column column in the provided sample frame.") | ||
|
||
# if weight column already exist in dataset | ||
if(weight_column %in% names(.dataset)) | ||
stop("Weight column already exists in the dataset. Please input another weights column") | ||
|
||
# Count number of entries by strata | ||
count <- .dataset %>% | ||
dplyr::group_by(!!rlang::sym(strata_column_dataset)) %>% | ||
dplyr::summarise(count = dplyr::n()) | ||
|
||
# Create a weight table to left_join to the dataset | ||
weights <- sample_data %>% | ||
dplyr::rename(!!strata_column_dataset := !!rlang::sym(strata_column_sample)) %>% | ||
dplyr::group_by(!!rlang::sym(strata_column_dataset))%>% | ||
dplyr::summarise(population = sum(as.numeric(!!rlang::sym(population_column)))) %>% | ||
dplyr::left_join(count, by = strata_column_dataset) %>% | ||
dplyr::mutate( | ||
!!rlang::sym(weight_column) := (as.numeric(population)/sum(as.numeric(population)))/(as.numeric(count)/sum(as.numeric(count)))) %>% | ||
dplyr::select(dplyr::all_of(strata_column_dataset),dplyr::all_of(weight_column)) | ||
|
||
# join to dataset | ||
.dataset <- .dataset %>% | ||
dplyr::left_join(weights,by = strata_column_dataset) | ||
|
||
AbrahamAz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if(any(is.na(.dataset[[weight_column]]))) | ||
stop("There are NA values in the weights column") | ||
|
||
return(.dataset) | ||
} |
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,26 @@ | ||
############################### data documentation ####################### | ||
|
||
|
||
|
||
|
||
#' Clean data and Sample Frame | ||
#' | ||
#' | ||
#' | ||
#' @format NULL | ||
#' @examples | ||
#' analysistools_clean_data | ||
#' analysistools_sample_frame | ||
|
||
#' @title Clean data | ||
#' @name analysistools_clean_data | ||
#' @rdname analysistools_clean_data | ||
#' @format NULL | ||
"analysistools_clean_data" | ||
|
||
#' @name analysistools_sample_frame | ||
#' @title Sample frame | ||
#' @rdname analysistools_sample_frame | ||
#' @format NULL | ||
"analysistools_sample_frame" | ||
|
Binary file not shown.
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,71 @@ | ||
testthat::test_that("Error checks", { | ||
testthat::expect_error(add_weights(.dataset = analysistools::analysistools_clean_data, | ||
sample_data = analysistools::analysistools_sample_frame, | ||
strata_column_dataset = "sdsd", | ||
strata_column_sample = "Neighbourhood", | ||
population_column = "Total.no.of.HH")) | ||
testthat::expect_error(add_weights(.dataset = analysistools::analysistools_clean_data, | ||
sample_data = analysistools::analysistools_sample_frame, | ||
strata_column_dataset = "neighbourhood", | ||
strata_column_sample = "Neighbourd", | ||
population_column = "Total.no.of.HH")) | ||
testthat::expect_error(add_weights(.dataset = analysistools::analysistools_clean_data, | ||
sample_data = analysistools::analysistools_sample_frame, | ||
strata_column_dataset = "neighbourhood", | ||
strata_column_sample = "Neighbourhood", | ||
population_column = "Total")) | ||
|
||
test_data <- analysistools::analysistools_clean_data %>% | ||
dplyr::mutate(weights = 1) | ||
testthat::expect_error(add_weights(.dataset = test_data, | ||
sample_data = analysistools::analysistools_sample_frame, | ||
strata_column_dataset = "neighbourhood", | ||
strata_column_sample = "Neighbourhood", | ||
population_column = "Total.no.of.HH")) | ||
testthat::expect_no_error(add_weights(.dataset = analysistools::analysistools_clean_data, | ||
sample_data = analysistools::analysistools_sample_frame, | ||
strata_column_dataset = "neighbourhood", | ||
strata_column_sample = "Neighbourhood", | ||
population_column = "Total.no.of.HH")) | ||
test_data <- analysistools::analysistools_clean_data | ||
test_data$neighbourhood[2] <- "not_applicable" | ||
testthat::expect_error(add_weights(.dataset = test_data, | ||
sample_data = analysistools::analysistools_sample_frame, | ||
strata_column_dataset = "neighbourhood", | ||
strata_column_sample = "Neighbourhood", | ||
population_column = "Total.no.of.HH")) | ||
|
||
test_data <- analysistools::analysistools_clean_data %>% | ||
dplyr::filter(neighbourhood != "oyt") | ||
testthat::expect_error(add_weights(.dataset = test_data, | ||
sample_data = analysistools::analysistools_sample_frame, | ||
strata_column_dataset = "neighbourhood", | ||
strata_column_sample = "Neighbourhood", | ||
population_column = "Total.no.of.HH")) | ||
}) | ||
|
||
testthat::test_that("add_weights works", { | ||
AbrahamAz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
test_clean_data <- data.frame(uuid = c(1,2,3,4,5,6,7,8), | ||
strata = c("strata1","strata2","strata1", | ||
"strata2","strata1","strata2", | ||
"strata1","strata1")) | ||
test_sample <- data.frame(strata = c("strata1","strata2"), | ||
population = c("30000","50000")) | ||
actual_output <- test_clean_data %>% | ||
add_weights(test_sample, | ||
strata_column_dataset = "strata", | ||
strata_column_sample = "strata", | ||
population_column = "population") | ||
|
||
testthat::expect_equal(sum(actual_output$weight),nrow(test_clean_data)) | ||
expected_output <- data.frame(uuid = c(1,2,3,4,5,6,7,8), | ||
strata = c("strata1","strata2","strata1", | ||
"strata2","strata1","strata2", | ||
"strata1","strata1"), | ||
weights = c(0.60,1.67,0.60,1.67,0.60,1.67,0.60,0.60)) | ||
|
||
rounded_output <- actual_output %>% | ||
dplyr::mutate(weights = round(weights,2)) | ||
|
||
testthat::expect_equal(rounded_output, expected_output) | ||
}) |
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.
Perhaps we can add -
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.
done
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.
#missing strata in dataset : does not run
set.seed(2133)
my_data <- data.frame(aa = runif(100),
strata = sample(LETTERS[1:5],size = 100, T))
my_sf <- data.frame(strata = LETTERS[1:6],
pop = c(10000,10000,20000,30000,5000,5000))
add_weights(my_data,
my_sf,
strata_column_dataset = "strata",
strata_column_sample = "strata",
population_column = "pop") %>%
dplyr::summarise(sum(weight))
#missing strata in sampling frame : ok
set.seed(2133)
my_data <- data.frame(aa = runif(100),
strata = sample(LETTERS[1:6],size = 100, T))
my_sf <- data.frame(strata = LETTERS[1:5],
pop = c(10000,10000,20000,30000,5000))
add_weights(my_data,
my_sf,
strata_column_dataset = "strata",
strata_column_sample = "strata",
population_column = "pop") %>%
dplyr::summarise(sum(weight))
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.
For the third point, I think what Mehedi meant is that the sampling frame was done, then data collection happened in fewer locations than what was decided because of field reasons. I understand that this then requires either the change of the whole sample frame document to match the collected data, as filtering out the strata not collected in the sample frame will not resolve the issue.
I changed it for the time being to error as mentioned, but we can look again at it.