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

Add some data for tests, and add_weights function #9

Merged
merged 3 commits into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ Suggests:
testthat (>= 3.0.0),
tibble
Config/testthat/edition: 3
LazyData: true
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by roxygen2: do not edit by hand

export("%>%")
export(add_weights)
export(adding_analysis_key)
export(adding_analysis_key_ratio)
export(adding_group_var_value)
Expand Down
80 changes: 80 additions & 0 deletions R/add_weights.R
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.")

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we can add -

  1. Error message if all the strata from the dataset are not available in the sample frame
  2. Warning message -if all the strata from the sample frame are not available in the dataset [I am suggesting warning, because sometimes its normal to have population data across the country, including the areas where we are having access issue]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • if there is a missing strata in the dataset. the weights df will return only NA.
  • if you add a na.rm = T, the weights will including the population of the missing strata, hence not correct.
  • I am not really sure what Mehedi wanted to achieve here. We can change the warning to error until he comes back.

#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))

Copy link
Contributor Author

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.

# 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)
}
26 changes: 26 additions & 0 deletions R/data_set_documentation.R
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 added data/analysistools_clean_data.rda
Binary file not shown.
Binary file added data/analysistools_sample_frame.rda
Binary file not shown.
48 changes: 48 additions & 0 deletions man/add_weights.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions man/analysistools_clean_data.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions man/analysistools_sample_frame.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 71 additions & 0 deletions tests/testthat/test-add_weights.R
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)
})