Skip to content

Commit

Permalink
Merge pull request #22 from yannsay-impact/fix_cleaningtools9002
Browse files Browse the repository at this point in the history
Fix cleaningtools9002
  • Loading branch information
yannsay-impact authored Oct 3, 2023
2 parents fa61cf8 + e4cb77b commit 5aa3246
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 58 deletions.
16 changes: 8 additions & 8 deletions R/create_analysis.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#' @param loa list of analysis: Default is NULL. If provided it will be used to create the analysis.
#' @param group_var Default is NULL. If provided, it will first create a list of analysis and then
#' will run the analysis. It should be a vector.
#' @param sm_seperator Separator for choice multiple questions. The default is "."
#' @param sm_separator Separator for choice multiple questions. The default is "."
#'
#' @return A list with 3 items:
#' - The results table in a long format with the analysis key
Expand Down Expand Up @@ -63,7 +63,7 @@
create_analysis <- function(.design,
loa = NULL,
group_var = NULL,
sm_seperator = ".") {
sm_separator = ".") {
if (!"tbl_svy" %in% attributes(.design)$class) {
stop("It seems object design is not a design, did you use srvyr::as_survey ?")
}
Expand All @@ -77,7 +77,7 @@ create_analysis <- function(.design,
}

if (is.null(loa)) {
loa <- create_loa(.design = .design, group_var = group_var, sm_seperator = sm_seperator)
loa <- create_loa(.design = .design, group_var = group_var, sm_separator = sm_separator)
}


Expand Down Expand Up @@ -110,7 +110,7 @@ create_analysis <- function(.design,
group_var = loa[["group_var"]],
analysis_var = loa[["analysis_var"]],
level = loa[["level"]],
sm_separator = sm_seperator
sm_separator = sm_separator
))
}
if (loa[["analysis_type"]] == "ratio") {
Expand Down Expand Up @@ -147,7 +147,7 @@ create_analysis <- function(.design,
#' and "admin1, population" are different:
#' - c("admin1", "population") : will perform the analysis grouping once by admin1, and once by population
#' - "admin1, population" : will perform the analysis grouping once by admin1 and admin 2
#' @param sm_seperator Separator for choice multiple questions. The default is "."
#' @param sm_separator Separator for choice multiple questions. The default is "."
#'
#' @return a list of analysis.
#' @export
Expand All @@ -165,16 +165,16 @@ create_analysis <- function(.design,
#' )
create_loa <- function(.design,
group_var = NULL,
sm_seperator = ".") {
sm_separator = ".") {
loa_dictionary <- data.frame(
type = c("character", "double", "double", "logical", "integer", "integer"),
analysis_type = c("prop_select_one", "mean", "median", "prop_select_multiple", "mean", "median")
)
cols_to_remove <- c("start", "end", "today", "uuid")

select_multiple_parents_columns <- .design %>% cleaningtools::auto_detect_sm_parents(sm_seperator = sm_seperator)
select_multiple_parents_columns <- .design %>% cleaningtools::auto_detect_sm_parents(sm_separator = sm_separator)
select_multiple_children_columns <- .design %>%
cleaningtools::auto_sm_parent_children(sm_seperator = sm_seperator) %>%
cleaningtools::auto_sm_parent_children(sm_separator = sm_separator) %>%
dplyr::pull(sm_child)

loa <- .design$variables %>%
Expand Down
2 changes: 1 addition & 1 deletion R/create_analysis_prop_select_multiple.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#' smvar.option4 = sample(c(TRUE, FALSE), size = 100, replace = TRUE, prob = c(.8, .2)),
#' uuid = 1:100 %>% as.character()
#' ) %>%
#' cleaningtools::recreate_parent_column(uuid = "uuid", sm_seperator = ".")
#' cleaningtools::recreate_parent_column(uuid = "uuid", sm_separator = ".")
#'
#' somedata <- somedata$data_with_fix_concat
#'
Expand Down
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ somedata <- data.frame(
smvar.option4 = sample(c(TRUE, FALSE), size = 100, replace = T, prob = c(.8, .2)),
uuid = 1:100 %>% as.character()
) %>%
cleaningtools::recreate_parent_column(uuid = "uuid", sm_seperator = ".")
cleaningtools::recreate_parent_column(uuid = "uuid", sm_separator = ".")
somedata <- somedata$data_with_fix_concat
create_analysis_prop_select_multiple(srvyr::as_survey(somedata),
Expand Down
64 changes: 34 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,15 @@ ex2_results[["loa"]]
``` r
ex3_results <- create_analysis(.design = srvyr::as_survey(shorter_df), group_var = c("admin1", "admin2"))
#> Joining with `by = join_by(type)`
#> ■■■■■■■■■■■■■■ 42% | ETA: 1s
#> ■■■■■■■■■■■■■■■ 47% | ETA: 1s
#> ■■■■■■■■■■■■■■■■■■■■■■ 68% | ETA: 1s
#> ■■■■■■■■■■■■■■■■■■■■■■■ 74% | ETA: 1s
#> ■■■■■■■■■■■■■■■■■■■■■■■■■ 79% | ETA: 1s
#> ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 95% | ETA: 0s
#> ■■■■■■■■■■■■ 37% | ETA: 3s
#> ■■■■■■■■■■■■■■ 42% | ETA: 3s
#> ■■■■■■■■■■■■■■■ 47% | ETA: 3s
#> ■■■■■■■■■■■■■■■■■ 53% | ETA: 2s
#> ■■■■■■■■■■■■■■■■■■ 58% | ETA: 2s
#> ■■■■■■■■■■■■■■■■■■■■■■ 68% | ETA: 2s
#> ■■■■■■■■■■■■■■■■■■■■■■■ 74% | ETA: 2s
#> ■■■■■■■■■■■■■■■■■■■■■■■■■■ 84% | ETA: 1s
#> ■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 89% | ETA: 1s
ex3_results[["loa"]]
#> analysis_type analysis_var group_var level
#> 1 prop_select_one admin1 <NA> 0.95
Expand Down Expand Up @@ -183,6 +186,7 @@ ex3_results[["loa"]]
``` r
ex4_results <- create_analysis(.design = srvyr::as_survey(shorter_df), group_var = "admin1, admin2")
#> Joining with `by = join_by(type)`
#> ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 92% | ETA: 0s
ex4_results[["loa"]]
#> analysis_type analysis_var group_var level
#> 1 prop_select_one admin1 <NA> 0.95
Expand Down Expand Up @@ -337,9 +341,9 @@ create_analysis_prop_select_one(srvyr::as_survey(somedata, strata = groups),
#> # A tibble: 3 × 13
#> analysis_type analysis_var analysis_var_value group_var group_var_value stat
#> <chr> <chr> <chr> <chr> <chr> <dbl>
#> 1 prop_select_o… value a <NA> <NA> 0.55
#> 2 prop_select_o… value b <NA> <NA> 0.35
#> 3 prop_select_o… value c <NA> <NA> 0.1
#> 1 prop_select_o… value a <NA> <NA> 0.51
#> 2 prop_select_o… value b <NA> <NA> 0.36
#> 3 prop_select_o… value c <NA> <NA> 0.13
#> # ℹ 7 more variables: stat_low <dbl>, stat_upp <dbl>, n <int>, n_total <int>,
#> # n_w <dbl>, n_w_total <dbl>, analysis_key <chr>
create_analysis_prop_select_one(srvyr::as_survey(somedata, strata = groups),
Expand All @@ -350,12 +354,12 @@ create_analysis_prop_select_one(srvyr::as_survey(somedata, strata = groups),
#> # A tibble: 6 × 13
#> analysis_type analysis_var analysis_var_value group_var group_var_value stat
#> <chr> <chr> <chr> <chr> <chr> <dbl>
#> 1 prop_select_o… value a groups group_a 0.6
#> 2 prop_select_o… value b groups group_a 0.28
#> 3 prop_select_o… value c groups group_a 0.12
#> 4 prop_select_o… value a groups group_b 0.5
#> 5 prop_select_o… value b groups group_b 0.42
#> 6 prop_select_o… value c groups group_b 0.08
#> 1 prop_select_o… value a groups group_a 0.516
#> 2 prop_select_o… value b groups group_a 0.359
#> 3 prop_select_o… value c groups group_a 0.125
#> 4 prop_select_o… value a groups group_b 0.5
#> 5 prop_select_o… value b groups group_b 0.361
#> 6 prop_select_o… value c groups group_b 0.139
#> # ℹ 7 more variables: stat_low <dbl>, stat_upp <dbl>, n <int>, n_total <int>,
#> # n_w <dbl>, n_w_total <dbl>, analysis_key <chr>
```
Expand All @@ -372,7 +376,7 @@ somedata <- data.frame(
smvar.option4 = sample(c(TRUE, FALSE), size = 100, replace = T, prob = c(.8, .2)),
uuid = 1:100 %>% as.character()
) %>%
cleaningtools::recreate_parent_column(uuid = "uuid", sm_seperator = ".")
cleaningtools::recreate_parent_column(uuid = "uuid", sm_separator = ".")
#> groups
#> smvar
#> smvar.option1
Expand All @@ -398,12 +402,12 @@ create_analysis_prop_select_multiple(srvyr::as_survey(somedata),
level = 0.95
)
#> # A tibble: 4 × 13
#> analysis_type analysis_var analysis_var_value group_var group_var_value stat
#> <chr> <chr> <chr> <chr> <chr> <dbl>
#> 1 prop_select_m… smvar option1 <NA> <NA> 0.7
#> 2 prop_select_m… smvar option2 <NA> <NA> 0.71
#> 3 prop_select_m… smvar option3 <NA> <NA> 0.15
#> 4 prop_select_m… smvar option4 <NA> <NA> 0.81
#> analysis_type analysis_var analysis_var_value group_var group_var_value stat
#> <chr> <chr> <chr> <chr> <chr> <dbl>
#> 1 prop_select_… smvar option1 <NA> <NA> 0.698
#> 2 prop_select_… smvar option2 <NA> <NA> 0.635
#> 3 prop_select_… smvar option3 <NA> <NA> 0.0312
#> 4 prop_select_… smvar option4 <NA> <NA> 0.854
#> # ℹ 7 more variables: stat_low <dbl>, stat_upp <dbl>, n <dbl>, n_total <dbl>,
#> # n_w <dbl>, n_w_total <dbl>, analysis_key <chr>

Expand All @@ -415,14 +419,14 @@ create_analysis_prop_select_multiple(srvyr::as_survey(somedata),
#> # A tibble: 8 × 13
#> analysis_type analysis_var analysis_var_value group_var group_var_value stat
#> <chr> <chr> <chr> <chr> <chr> <dbl>
#> 1 prop_select_m… smvar option1 groups group_a 0.72
#> 2 prop_select_m… smvar option2 groups group_a 0.66
#> 3 prop_select_m… smvar option3 groups group_a 0.14
#> 4 prop_select_m… smvar option4 groups group_a 0.82
#> 5 prop_select_m… smvar option1 groups group_b 0.68
#> 6 prop_select_m… smvar option2 groups group_b 0.76
#> 7 prop_select_m… smvar option3 groups group_b 0.16
#> 8 prop_select_m… smvar option4 groups group_b 0.8
#> 1 prop_select_m… smvar option1 groups group_a 0.66
#> 2 prop_select_m… smvar option2 groups group_a 0.66
#> 3 prop_select_m… smvar option3 groups group_a 0.06
#> 4 prop_select_m… smvar option4 groups group_a 0.84
#> 5 prop_select_m… smvar option1 groups group_b 0.739
#> 6 prop_select_m… smvar option2 groups group_b 0.609
#> 7 prop_select_m… smvar option3 groups group_b 0
#> 8 prop_select_m… smvar option4 groups group_b 0.870
#> # ℹ 7 more variables: stat_low <dbl>, stat_upp <dbl>, n <dbl>, n_total <dbl>,
#> # n_w <dbl>, n_w_total <dbl>, analysis_key <chr>
```
Expand Down
4 changes: 2 additions & 2 deletions man/create_analysis.Rd

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

2 changes: 1 addition & 1 deletion man/create_analysis_prop_select_multiple.Rd

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

4 changes: 2 additions & 2 deletions man/create_loa.Rd

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

8 changes: 4 additions & 4 deletions tests/testthat/test-create_analysis.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ test_that("Gives corrects results", {
no_loa_expected_output <- readRDS(testthat::test_path("fixtures", "results_create_analysis_no_loa_v2.RDS"))

no_loa_test_design <- srvyr::as_survey(no_loa_expected_output$dataset)
actual_output <- create_analysis(no_loa_test_design, group_var = "admin1", sm_seperator = "/")
actual_output <- create_analysis(no_loa_test_design, group_var = "admin1", sm_separator = "/")

expect_equal(actual_output, no_loa_expected_output, ignore_attr = T)

# with a loa #add ratios
with_loa_expected_output <- readRDS(testthat::test_path("fixtures", "results_create_analysis_with_loa_v2.RDS"))

with_loa_test_design <- srvyr::as_survey(with_loa_expected_output$dataset)
with_loa_actual_output <- create_analysis(with_loa_test_design, loa = with_loa_expected_output$loa, sm_seperator = "/")
with_loa_actual_output <- create_analysis(with_loa_test_design, loa = with_loa_expected_output$loa, sm_separator = "/")

expect_equal(with_loa_actual_output, with_loa_expected_output, ignore_attr = T)

# with loa and no ratio
no_ratio_loa <- with_loa_expected_output$loa %>%
dplyr::filter(analysis_type != "ratio")

no_ratio_loa_actual_output <- create_analysis(with_loa_test_design, no_ratio_loa, sm_seperator = "/")
no_ratio_loa_actual_output <- create_analysis(with_loa_test_design, no_ratio_loa, sm_separator = "/")

no_ratio_loa_expected_results_table <- with_loa_expected_output$results_table %>%
dplyr::filter(analysis_type != "ratio")
Expand Down Expand Up @@ -142,7 +142,7 @@ test_that("If loa and group variable are provided, group_var will be ignored", {
.design = srvyr::as_survey(expected_output$dataset),
loa = expected_output$loa,
group_var = "admin1",
sm_seperator = "/"
sm_separator = "/"
),
"You have provided a list of analysis and group variable, group variable will be ignored"
)
Expand Down
18 changes: 9 additions & 9 deletions tests/testthat/test-create_analysis_prop_select_multiple.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ test_that("create_analysis_prop_select_multiple returns correct output, no weigh
smvar.option4 = sample(c(TRUE, FALSE), size = 100, replace = T, prob = c(.8, .2)),
uuid = 1:100 %>% as.character()
) %>%
cleaningtools::recreate_parent_column(uuid = "uuid", sm_seperator = ".")
cleaningtools::recreate_parent_column(uuid = "uuid", sm_separator = ".")

somedata <- somedata$data_with_fix_concat

Expand Down Expand Up @@ -104,7 +104,7 @@ test_that("create_analysis_prop_select_multiple handles NA", {
smvar.option4 = rep(NA_character_, 100),
uuid = 1:100 %>% as.character()
) %>%
cleaningtools::recreate_parent_column(uuid = "uuid", sm_seperator = ".")
cleaningtools::recreate_parent_column(uuid = "uuid", sm_separator = ".")

somedata <- somedata$data_with_fix_concat

Expand Down Expand Up @@ -221,7 +221,7 @@ test_that("create_analysis_prop_select_multiple returns correct output, with wei
smvar.option4 = sample(c(TRUE, FALSE), size = 100, replace = T, prob = c(.8, .2)),
uuid = 1:100 %>% as.character()
) %>%
cleaningtools::recreate_parent_column(uuid = "uuid", sm_seperator = ".")
cleaningtools::recreate_parent_column(uuid = "uuid", sm_separator = ".")

somedata <- somedata$data_with_fix_concat

Expand Down Expand Up @@ -319,7 +319,7 @@ test_that("create_analysis_prop_select_multiple handles when only 1 value", {
smvar.option4 = c(rep(NA_integer_, 99), T),
uuid = 1:100 %>% as.character()
) %>%
cleaningtools::recreate_parent_column(uuid = "uuid", sm_seperator = ".")
cleaningtools::recreate_parent_column(uuid = "uuid", sm_separator = ".")
somedata <- somedata$data_with_fix_concat
one_value_expected_output <- data.frame(
analysis_type = rep("prop_select_multiple", 4),
Expand Down Expand Up @@ -415,7 +415,7 @@ test_that("create_analysis_prop_select_multiple handles lonely PSU", {
smvar.option4 = sample(c(TRUE, FALSE), size = 51, replace = T, prob = c(.8, .2)),
uuid = 1:51 %>% as.character()
) %>%
cleaningtools::recreate_parent_column(uuid = "uuid", sm_seperator = ".")
cleaningtools::recreate_parent_column(uuid = "uuid", sm_separator = ".")

somedata <- somedata$data_with_fix_concat

Expand Down Expand Up @@ -478,7 +478,7 @@ test_that("create_analysis_prop_select_multiple returns correct output with 3 gr
smvar.option4 = sample(c(TRUE, FALSE), size = 300, replace = T, prob = c(.8, .2)),
uuid = 1:300 %>% as.character()
) %>%
cleaningtools::recreate_parent_column(uuid = "uuid", sm_seperator = ".")
cleaningtools::recreate_parent_column(uuid = "uuid", sm_separator = ".")

somedata <- somedata$data_with_fix_concat

Expand Down Expand Up @@ -564,7 +564,7 @@ test_that("create_analysis_prop_select_multiple returns correct output with 2 gr
smvar.option4 = sample(c(TRUE, FALSE), size = 300, replace = T, prob = c(.8, .2)),
uuid = 1:300 %>% as.character()
) %>%
cleaningtools::recreate_parent_column(uuid = "uuid", sm_seperator = ".")
cleaningtools::recreate_parent_column(uuid = "uuid", sm_separator = ".")

somedata <- somedata$data_with_fix_concat

Expand Down Expand Up @@ -678,7 +678,7 @@ test_that("create_analysis_prop_select_multiple handles NA in the dummy variable
smvar.option4 = sample(c(TRUE, FALSE), size = 100, replace = T, prob = c(.8, .2)),
uuid = 1:100 %>% as.character()
) %>%
cleaningtools::recreate_parent_column(uuid = "uuid", sm_seperator = ".")
cleaningtools::recreate_parent_column(uuid = "uuid", sm_separator = ".")

somedata <- somedata$data_with_fix_concat

Expand Down Expand Up @@ -782,7 +782,7 @@ test_that("create_analysis_prop_select_multiple works with 0/1's instead of TRUE
smvar.option4 = sample(c(1, 0), size = 100, replace = T, prob = c(.8, .2)),
uuid = 1:100 %>% as.character()
) %>%
cleaningtools::recreate_parent_column(uuid = "uuid", sm_seperator = ".")
cleaningtools::recreate_parent_column(uuid = "uuid", sm_separator = ".")

somedata <- somedata$data_with_fix_concat

Expand Down

0 comments on commit 5aa3246

Please sign in to comment.