Skip to content
Snippets Groups Projects
Commit 7ed519f8 authored by Thomas  OBADIA's avatar Thomas OBADIA :speech_balloon:
Browse files

Remove unused functions

parent 2f0e6e01
No related branches found
No related tags found
1 merge request!1Implement sampling from population inventory
......@@ -61,27 +61,6 @@ get_target_list_for_observational_study <- function(x, n_target) {
#' Calculate the expected sample size for a given group
#'
#' @param n_obs The number of observations in a group nested within a cluster.
#' @param n_tot The total number of inhabitants in the corresponding cluster.
#' @param n_target The total target sample size in the cluster.
#' @return The number of participants to sample among the group of interest in the cluster population or, if there's too few, the total number of observations in that group.
get_group_sample_size <- function(n_obs, n_tot, n_target) {
if (n_tot <= n_target) {
return(n_obs)
} else {
n_group = round((n_obs/n_tot)*n_target, 0)
return(n_group)}
}
#' Randomly subset group to a given size
#'
#' @param x A data.frame containing the population inventory in a wide format. It should be the output of \code{INVENTORY_02_list_all_inventory_participants.R}.
......@@ -101,57 +80,3 @@ get_group_subset <- function(x, country_f, clusterid_f, sex_f, agey_cat_f, n_tar
return(as.data.frame(res))
}
#' Extract random subset from the population list
#'
#' @param x A data.frame containing the population inventory in a wide format. It should be the output of \code{INVENTORY_02_list_all_inventory_participants.R}.
#' @param clusterid The cluster ID to restrict the data to.
#' @param n_target The total target sample size in the cluster.
#' @return Writes the subset list into a dedicated CSV file.
get_population_subset_in_cluster <- function(clusterid, n_target) {
df <- x %>%
count(country, clusterid, sex, ageClass, name = "sexage_n") %>%
left_join(
x %>% group_by(clusterid) %>%
summarise(pop_cluster = n())) %>%
mutate(n_target = mapply(get_group_sample_size,
sexage_n,
pop_cluster,
n_target),
list = mapply(get_group_subset,
x,
clusterid,
sex,
ageClass,
n_target,
SIMPLIFY = FALSE)) %>%
filter(clusterid == clusterid)
sample_list <- bind_rows(df$list)
file_name <- paste0("outputs/sample_cluster_",
clusterid,"_",
strftime(Sys.time(),
format = "%Y%m%d_%H%M%S"),".csv")
return(write.csv2(sample_list, file_name))
}
#' Loop subset functions over multiple clusters
#'
#' @param clusterids The cluster IDs over which the subset routine should be iterated.
#' @param n_targets The total target sample sizes for each cluster in \code{clusterids}.
#' @return Writes the subset lists resulting from calls to \code{\link{get_population_subset_in_cluster}} into dedicated CSV file.
get_population_subset <- function(clusterids, n_targets) {
index <- 1
for (index in 1:length(clusterids)) {
get_population_subset_in_cluster(clusterids[index], n_targets[index])
index <- index + 1
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment