diff --git a/DESCRIPTION b/DESCRIPTION index 87b6a0a..e7959d2 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -22,9 +22,11 @@ Imports: ggtext, glue, golem, + grid, janitor, kobocruncher, labelled, + openxlsx2, plyr, purrr, readxl, @@ -39,6 +41,7 @@ Imports: sjlabelled, sjmisc, stats, + stringdist, stringr, sysfonts, systemfonts, @@ -50,13 +53,10 @@ Imports: utils, withr Suggests: - here, - knitr, - openxlsx, - testthat + knitr VignetteBuilder: knitr -Remotes: +Remotes: edouard-legoupil/kobocruncher, edouard-legoupil/riddle, edouard-legoupil/unhcrshiny, diff --git a/NAMESPACE b/NAMESPACE index 5e0ac11..539d838 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,7 @@ # Generated by roxygen2: do not edit by hand +export(fct_build_map) +export(fct_build_requirement) export(fct_check_map) export(fct_compass_table) export(fct_get_all_variable_names) @@ -9,17 +11,39 @@ export(fct_plot_rbm_sdg) export(fct_re_map) export(fct_var_dummy) export(fct_var_mapping) +export(impact2_3) +export(impact3_2a) +export(impact3_2b) +export(impact3_3) export(impact_2_2) -export(inter_drinkingwater) -export(inter_electricity) -export(inter_healthcare) -export(inter_shelter) +export(outcome10_1) +export(outcome10_2) +export(outcome12_1) +export(outcome12_2) +export(outcome13_1) +export(outcome13_2) +export(outcome13_3) +export(outcome14_1) +export(outcome16_1) +export(outcome16_2) +export(outcome1_2) +export(outcome1_3) +export(outcome4_1) +export(outcome4_2) +export(outcome5_2) +export(outcome8_2) +export(outcome9_1) +export(outcome9_2) export(run_app) -export(var_dummy) +import(cli) +import(dplyr) +import(ggplot2) import(golem) +import(readxl) import(refugees) import(shiny) import(shinydashboard) +import(stringdist) import(tidyverse) importFrom(SDGsR,get_indicator) importFrom(cli,cli_alert_info) @@ -33,10 +57,6 @@ importFrom(dplyr,rename) importFrom(dplyr,row_number) importFrom(dplyr,tibble) importFrom(ggforce,geom_arc_bar) -importFrom(ggplot2,aes) -importFrom(ggplot2,expansion) -importFrom(ggplot2,ggplot) -importFrom(ggplot2,labs) importFrom(ggtext,geom_richtext) importFrom(golem,activate_js) importFrom(golem,add_resource_path) diff --git a/R/fct_build_map.R b/R/fct_build_map.R new file mode 100644 index 0000000..743d813 --- /dev/null +++ b/R/fct_build_map.R @@ -0,0 +1,145 @@ +# WARNING - Generated by {fusen} from dev/utilities.Rmd: do not edit by hand + +#' fct_build_map +#' +#' Quick helper to reformat the data from the mapping file into the mapping file +#' to use with each indicator calculation. T +#' +#' his needs to be done in two steps: +#' one for `ind` for individual level and +#' one for `main` for household level +#' +#' @param mappingfile full path to the xlsx file with the revise variable mapping generated with `fct_var_mapping()` +#' +#' @param IndicatorRequirementFile path to the file where the standard mapping is depending on form version +#' +#' @param thisMeasureLevel can be `ind` for individual level or `main` for household level +#' +#' @import readxl +#' @import dplyr +#' +#' @return a list +#' +#' @export +#' @examples +#' mappingfile <- system.file("RMS_CAPI_v2_mapping.xlsx", +#' package = "IndicatorCalc") +#' +#' IndicatorRequirementFile <- system.file("RMS_CAPI_v2_mapper.xlsx", +#' package = "IndicatorCalc") +#' +#' mappermain <- fct_build_map(mappingfile = mappingfile, +#' IndicatorRequirementFile = IndicatorRequirementFile, +#' thisMeasureLevel = "main") +#' +#' mapperind <- fct_build_map(mappingfile = mappingfile, +#' IndicatorRequirementFile = IndicatorRequirementFile, +#' thisMeasureLevel = "ind") +#' +fct_build_map <- function(mappingfile, + IndicatorRequirementFile, + thisMeasureLevel){ + + IndicMap <- readxl::read_excel( IndicatorRequirementFile) + ## Merge the map... + MeasureLevelQ <- IndicMap |> + dplyr::select(MeasureLevel, QuestionVar) |> + dplyr::filter(MeasureLevel == thisMeasureLevel)|> + dplyr::distinct() + + revisemapIndic <- readxl::read_excel( mappingfile, + sheet = "result_matchInd") + + revisemapIndic2 <- MeasureLevelQ |> + dplyr::left_join(revisemapIndic, by = c("QuestionVar") ) |> + dplyr::rename(#QuestionVar, + #label, + mappattern = "best_name") |> + dplyr::select(QuestionVar, label, mappattern)|> + dplyr::distinct() + + # names( IndicMap) + # names(revisemapIndic2) + # names(revisemapMod) + + + revisemapMod <- readxl::read_excel( mappingfile, + sheet = "result_matchMod") + revisemapMod2 <- MeasureLevelQ |> + dplyr::left_join(revisemapMod, by = c("QuestionVar") ) |> + dplyr::rename(#variable = "QuestionVar", + # label= "label_mod" , + # standard = "name_mod" , + map = "best_name") |> + dplyr::select(QuestionVar, label_mod, name_mod, map) + + + + filemap <- tempfile() + filemaplab <- tempfile() + #filemap <- "dev/test1.R" + if (file.exists(filemap)) file.remove(filemap) + + cat( paste0( "thismapper = list("), file = filemap , sep = "\n", append = TRUE) + cat( paste0( " hierarchy = \"", thisMeasureLevel,"\","), file = filemap , sep = "\n", append = TRUE) + + + ## Mapping variables + thisvar <- revisemapIndic2 |> + dplyr::select(QuestionVar, label, mappattern) |> + dplyr::distinct() + cat( paste0( " variablemap = data.frame("), file = filemap , sep = "\n", append = TRUE) + cat( paste0( " label = "), file = filemap , sep = "\n", append = TRUE) + ## Looping around labels + dput( thisvar |> dplyr::pull(label) , file = filemaplab ) + file_str <- paste(readLines(filemaplab), collapse="\n") + cat( paste0(file_str), file = filemap , sep = "\n", append = TRUE) + cat( paste0( " ,"), file = filemap , sep = "\n", append = TRUE) + cat( paste0( " mappattern = "), file = filemap , sep = "\n", append = TRUE) + ## Looping around mappattern + dput( thisvar |> dplyr::pull(mappattern) , file = filemaplab ) + file_str <- paste(readLines(filemaplab), collapse="\n") + cat( paste0(file_str), file = filemap , sep = "\n", append = TRUE) + cat( paste0( " ,"), file = filemap , sep = "\n", append = TRUE) + cat( paste0( " variable = "), file = filemap , sep = "\n", append = TRUE) + ## Looping around variable + dput( thisvar |> dplyr::pull(QuestionVar), file = filemaplab ) + file_str <- paste(readLines(filemaplab), collapse="\n") + cat( paste0(file_str), file = filemap , sep = "\n", append = TRUE) + cat( paste0( " ),"), file = filemap , sep = "\n", append = TRUE) + + ## Mapping modality + thismod <- revisemapMod2 |> + dplyr::select(QuestionVar, label_mod, name_mod, map) |> + dplyr::filter(! (is.null(name_mod)) ) |> + dplyr::distinct() + cat( paste0( " modalitymap = data.frame("), file = filemap , sep = "\n", append = TRUE) + cat( paste0( " variable = "), file = filemap , sep = "\n", append = TRUE) + ## Looping around variable for modalities + dput( thismod |> dplyr::pull(QuestionVar) , file = filemaplab ) + file_str <- paste(readLines(filemaplab), collapse="\n") + cat( paste0(file_str), file = filemap , sep = "\n", append = TRUE) + cat( paste0( " ,"), file = filemap , sep = "\n", append = TRUE) + cat( paste0( " label = "), file = filemap , sep = "\n", append = TRUE) + ## Looping around labels for modalities + dput( thismod |> dplyr::pull(label_mod) , file = filemaplab ) + file_str <- paste(readLines(filemaplab), collapse="\n") + cat( paste0(file_str), file = filemap , sep = "\n", append = TRUE) + cat( paste0( " ,"), file = filemap , sep = "\n", append = TRUE) + cat( paste0( " map = "), file = filemap , sep = "\n", append = TRUE) + ## Looping around labels for modalities + dput( thismod |> dplyr::pull(map) , file = filemaplab ) + file_str <- paste(readLines(filemaplab), collapse="\n") + cat( paste0(file_str), file = filemap , sep = "\n", append = TRUE) + cat( paste0( " ,"), file = filemap , sep = "\n", append = TRUE) + cat( paste0( " standard = "), file = filemap , sep = "\n", append = TRUE) + ## Looping around standard for modalities + dput( thismod |> dplyr::pull(name_mod) , file = filemaplab ) + file_str <- paste(readLines(filemaplab), collapse="\n") + cat( paste0(file_str), file = filemap , sep = "\n", append = TRUE) + cat( paste0( " ))"), file = filemap , sep = "\n", append = TRUE) + + source(filemap) + + return(thismapper) +} diff --git a/R/fct_build_requirement.R b/R/fct_build_requirement.R new file mode 100644 index 0000000..acae412 --- /dev/null +++ b/R/fct_build_requirement.R @@ -0,0 +1,153 @@ +# WARNING - Generated by {fusen} from dev/utilities.Rmd: do not edit by hand + +#' fct_build_requirement +#' +#' Write in a file the variable requirement for each single indicator functions +#' +#' The function is used to conveniently keep the mapping between the excel +#' tables used to show variable requirements for each indicator calculation +#' +#' It expects a specific table within the xlsform called `Indicator_to_question` + +#' @param xlsformpath path to the xlsform +#' @return print in console +#' +#' @export +#' @examples +#' ## Write in dev/mapper.R each of the function parameters - as recorded in system.file("IndicMap.xlsx", package = "IndicatorCalc") +#' xlsformpath <- system.file("RMS_CAPI_v2.xlsx", package = "IndicatorCalc") +#' RMS_CAPI_v2_mapper <- fct_build_requirement(xlsformpath ) +#' # openxlsx::write.xlsx(RMS_CAPI_v2_mapper, here::here("inst", "RMS_CAPI_v2_mapper.xlsx")) +#' +#' xlsformpath <- system.file("RMS_CAPI_v3.xlsx", package = "IndicatorCalc") +#' RMS_CAPI_v3_mapper <- fct_build_requirement(xlsformpath ) +#' # openxlsx::write.xlsx(RMS_CAPI_v3_mapper, here::here("inst", "RMS_CAPI_v3_mapper.xlsx")) +#' +#' ## CATI +#' xlsformpath <- system.file("RMS_CATI_v0.xlsx", package = "IndicatorCalc") +#' RMS_CATI_v0_mapper <- fct_build_requirement(xlsformpath ) +#' # openxlsx::write.xlsx(RMS_CATI_v0_mapper, here::here("inst", "RMS_CATI_v0_mapper.xlsx")) +#' +#' xlsformpath <- system.file("RMS_CATI_v3.xlsx", package = "IndicatorCalc") +#' RMS_CATI_v3_mapper <- fct_build_requirement(xlsformpath ) +#' # openxlsx::write.xlsx(RMS_CATI_v3_mapper, here::here("inst", "RMS_CATI_v3_mapper.xlsx")) +#' +fct_build_requirement <- function(xlsformpath){ + + mappers <- readxl::read_excel(xlsformpath, + sheet = "Indicator_to_question") |> + dplyr::select(IndName, MeasureLevel, QuestionVar) + + dico <- kobocruncher::kobo_dico( xlsformpath ) + + # View(dico[["variables"]]) + # names(dico[["variables"]]) + + mappers <- mappers |> + dplyr::left_join( dico[["variables"]] |> + dplyr::select(name, label, + type, list_name, name_or), + by= c("QuestionVar" = "name_or")) + + + IndicMap <- mappers |> + dplyr::left_join( dico[["modalities"]] |> + dplyr::select(name, label, list_name ) |> + dplyr::rename( name_mod = name, + label_mod = label), + by= c("list_name")) + + #writexl::write_xlsx(IndicMap, here::here( "inst", "IndicMap.xlsx")) + # IndicMap <- readxl::read_excel( system.file("IndicMap.xlsx", + # package = "IndicatorCalc")) + + ## Below is the format we are trying to replicate + + # mapper = list( + # hierarchy = "main", + # variablemap = data.frame( + # label = c("Does this household use anything for lighting?", + # "What source of electricity is used most of the time in this household?"), + # variable = c("LIGHT01", + # "LIGHT03"), + # mappattern = c("LIGHT01", + # "LIGHT03") ), + # modalitymap = data.frame( + # variable = c( "LIGHT01", + # "LIGHT03", "LIGHT03", "LIGHT03"), + # label = c( "yes", + # "No electricity in household", "Other, specify", "Don't know"), + # standard = c( "1", + # "1", "96", "98"), + # map = c("yes", + # "Noelec", "Other", "Dontknow") + # ) + # ) + + filemap <- tempfile() + filemaplab <- tempfile() + listind <- IndicMap |> dplyr::select(IndName) |> dplyr::distinct() |> dplyr::pull() + if (file.exists(filemap)) file.remove(filemap) + ## Roving around indicators + for ( thisInd in listind ) { + # thisInd <- listind[1] + cat( paste0("# --- ", thisInd, "\n"), file = filemap , sep = "\n", append = TRUE) + cat( paste0( "# -----------------------------\n"), file = filemap , sep = "\n", append = TRUE) + cat( paste0( "mapper = list("), file = filemap , sep = "\n", append = TRUE) + cat( paste0( " hierarchy = \"", + IndicMap |> + dplyr::filter(IndName == thisInd) |> + dplyr::select(MeasureLevel) |> + dplyr::distinct() |> + dplyr::pull(), + "\","), file = filemap , sep = "\n", append = TRUE) + ## Mapping variables + thisvar <- IndicMap |> + dplyr::filter(IndName == thisInd) |> + dplyr::select(QuestionVar, label) |> + dplyr::distinct() + cat( paste0( " variablemap = data.frame("), file = filemap , sep = "\n", append = TRUE) + cat( paste0( " label = "), file = filemap , sep = "\n", append = TRUE) + ## Looping around labels + dput( thisvar |> dplyr::pull(label) , file = filemaplab ) + file_str <- paste(readLines(filemaplab), collapse="\n") + cat( paste0(file_str), file = filemap , sep = "\n", append = TRUE) + cat( paste0( " ,"), file = filemap , sep = "\n", append = TRUE) + cat( paste0( " variable = "), file = filemap , sep = "\n", append = TRUE) + ## Looping around variable + dput( thisvar |> dplyr::pull(QuestionVar), file = filemaplab ) + file_str <- paste(readLines(filemaplab), collapse="\n") + cat( paste0(file_str), file = filemap , sep = "\n", append = TRUE) + cat( paste0( " ),"), file = filemap , sep = "\n", append = TRUE) + ## Mapping modality + thismod <- IndicMap |> + dplyr::filter(IndName == thisInd) |> + dplyr::select(QuestionVar, label_mod, name_mod) |> + dplyr::filter(! (is.null(name_mod)) ) |> + dplyr::distinct() + cat( paste0( " modalitymap = data.frame("), file = filemap , sep = "\n", append = TRUE) + cat( paste0( " variable = "), file = filemap , sep = "\n", append = TRUE) + ## Looping around variable for modalities + dput( thismod |> dplyr::pull(QuestionVar) , file = filemaplab ) + file_str <- paste(readLines(filemaplab), collapse="\n") + cat( paste0(file_str), file = filemap , sep = "\n", append = TRUE) + cat( paste0( " ,"), file = filemap , sep = "\n", append = TRUE) + cat( paste0( " label = "), file = filemap , sep = "\n", append = TRUE) + ## Looping around labels for modalities + dput( thismod |> dplyr::pull(label_mod) , file = filemaplab ) + file_str <- paste(readLines(filemaplab), collapse="\n") + cat( paste0(file_str), file = filemap , sep = "\n", append = TRUE) + cat( paste0( " ,"), file = filemap , sep = "\n", append = TRUE) + cat( paste0( " standard = "), file = filemap , sep = "\n", append = TRUE) + ## Looping around standard for modalities + dput( thismod |> dplyr::pull(name_mod) , file = filemaplab ) + file_str <- paste(readLines(filemaplab), collapse="\n") + cat( paste0(file_str), file = filemap , sep = "\n", append = TRUE) + cat( paste0( " ))"), file = filemap , sep = "\n", append = TRUE) + cat( paste0( "# -----------------------------\n"), file = filemap , sep = "\n", append = TRUE) + + } + cat(filemap) + return(IndicMap) + +} diff --git a/R/fct_check_map.R b/R/fct_check_map.R index a8fc249..e4a60c3 100644 --- a/R/fct_check_map.R +++ b/R/fct_check_map.R @@ -2,10 +2,12 @@ #' fct_check_map #' -#' This check if the standard variables and modalities included in the mapper are +#' This functions checks if the standard variables and modalities included in the mapper are #' actually present in the datalist with the same exact name - this is actually -#' usually not the case as the standard format to save kobodatset in xlsx includes -#' group in the variable name +#' usually not the case as the standard format to save kobo dataset in xlsx includes +#' group in the variable name. +#' This function is used internally in each indicator function as an eror catcher before +#' performing calculations... #' #' @param datalist A list with all hierarchical data frame for a survey data set. #' format is expected to match the Excel export synchronized from kobo to RILD @@ -16,29 +18,29 @@ #' between the the standard XlsForm and the contextualized dataset #' #' -#' @return TRUE or FALSE +#' @return boolean flag TRUE or FALSE indicating if we can go forward for the indicator calculation +#' +#' @import cli #' #' @export #' @examples -#' +#' ## below is the mapper to chck if we have the variables to calculate the +#' # electricity subindicators within impact 2.2 #' mapper = list( #' hierarchy = "main", #' variablemap = data.frame( -#' label = c("Does this household use anything for lighting?", -#' "What source of electricity is used most of the time in this household?"), +#' label = c( +#' "Does this household use anything for lighting?", +#' "What source of electricity is used most of the time in this household?"), #' variable = c("LIGHT01", -#' "LIGHT03"), -#' mappattern = c("LIGHT01", #' "LIGHT03") ), #' modalitymap = data.frame( #' variable = c( "LIGHT01", #' "LIGHT03", "LIGHT03", "LIGHT03"), #' label = c( "yes", -#' "No electricity in household", "Other, specify", "Don't know"), +#' "No electricity in household", "Other, specify", "Don't know"), #' standard = c( "1", -#' "1", "96", "98"), -#' map = c("1", -#' "1", "96", "98") +#' "1", "96", "98") #' ) #' ) #' @@ -47,43 +49,61 @@ #' LIGHT01 = c("1", "1", "0", "1", "1", "0", "1", "1", "1"), #' LIGHT03 = c("1", "96", "98", "1", "96", "98", "0", "96", "98")) #' ) -#' fct_check_map(datalist = data, mapper = mapper ) +#' check <- fct_check_map(datalist = data, mapper = mapper ) +#' check #' #' ## One variable is not correctly #' data <- list(main = data.frame( #' LIGHT01 = c("1", "1", "0", "1", "1", "0", "1", "1", "1"), #' LIGH03 = c("1", "96", "98", "1", "96", "98", "0", "96", "98")) #' ) -#' fct_check_map(datalist = data, mapper = mapper ) +#' check <- fct_check_map(datalist = data, mapper = mapper ) +#' check #' #' ## The first variable does not include a single 1... #' data <- list(main = data.frame( #' LIGHT01 = c("0", "0", "0", "0", "0", "0", "0", "0", "0"), #' LIGHT03 = c("1", "96", "98", "1", "96", "98", "0", "96", "98")) #' ) -#' fct_check_map(datalist = data, mapper = mapper ) +#' check <- fct_check_map(datalist = data, mapper = mapper ) +#' check fct_check_map <- function(datalist, mapper){ - + # rm(check ) + check <- data.frame( check = c("")) ## Loop around the variables within mapper for ( i in 1:nrow(mapper[["variablemap"]]) ) { - # i <- 1 + # i <- 3 thisvar <- mapper[["variablemap"]][["variable"]][[i]] if ( is.null( datalist[[mapper[["hierarchy"]] ]] [[ thisvar ]] ) ) - {cli::cli_alert_info(paste0( thisvar ," standard variable was not found in the dataset.\n")) + {cli::cli_alert_danger(paste0( thisvar ," not found in the dataset.\n")) + check <- rbind( check , check = c(FALSE)) } else { - ## If present check modalities - mod <- mapper[["modalitymap"]] |> + ## If present check modalities + mod <- mapper[["modalitymap"]] |> dplyr::filter( variable == thisvar ) |> dplyr::pull(standard) + ## If numeric - mod is NA + if( anyNA(mod) ) { + + cli::cli_alert_success(paste0( thisvar ," \n")) + check <- rbind( check , check = c(TRUE)) + } else { - if( any(levels(as.factor(datalist[[mapper[["hierarchy"]] ]] [[ thisvar ]])) %in% mod ) ) - { cat(paste0( thisvar ," is in the dataset and has at least one of the expected modality for calculation\n")) } else { - cli::cli_alert_info(paste0( thisvar , - " standard variable in the dataset misses at least one response among : ", - mod)) } + if( any(levels(as.factor(datalist[[mapper[["hierarchy"]] ]] [[ thisvar ]])) %in% mod ) ) + { + cli::cli_alert_success(paste0( thisvar ," \n")) + check <- rbind( check , check = c(TRUE)) + } else { + cli::cli_alert_info(paste0( thisvar , " misses responses options among : ", mod)) + check <- rbind( check , check = c(FALSE)) } } + } } + ## Check if have any fasel + finalcheck <- if( FALSE %in% check$check ) { paste0(FALSE)} else { paste0(TRUE)} + + return(finalcheck) } diff --git a/R/fct_compass_table.R b/R/fct_compass_table.R index 80e7103..b15072e 100644 --- a/R/fct_compass_table.R +++ b/R/fct_compass_table.R @@ -6,7 +6,9 @@ #' format for import into COMPASS. #' #' The function assumes you have already calculated the indicators and -#' have a datalist object with all the correct calculation inside +#' have a datalist object with all the correct calculation inside. +#' +#' The output can conveniently be upoaded as an indicator ressource in ridl #' #' @param country iso3 code for the country (easier to recall than the M49 used in the API) #' @param operation operation name diff --git a/R/fct_get_all_variable_names.R b/R/fct_get_all_variable_names.R index 345d09c..291263a 100644 --- a/R/fct_get_all_variable_names.R +++ b/R/fct_get_all_variable_names.R @@ -15,6 +15,8 @@ #' @importFrom tibble tibble as_tibble_col #' @importFrom tidyr unnest #' +#' @keywords internal +#' #' @export #' @examples #' datalist <- kobocruncher::kobo_data( system.file("demo_data.xlsx", #"test.xlsx", diff --git a/R/fct_kobo_dummy.R b/R/fct_kobo_dummy.R index cc97d7f..574453d 100644 --- a/R/fct_kobo_dummy.R +++ b/R/fct_kobo_dummy.R @@ -24,7 +24,7 @@ #' @param n number of main records to be generated #' @param nrepeat max random number of repeat records to be generated #' when repeat_count is not mentionned -#' @param file file as xlsx where to save to the +#' @param file file as xlsx where to save the resulting data #' #' @importFrom kobocruncher kobo_dico #' @@ -33,14 +33,33 @@ #' #' @export #' @examples -#' form <- system.file("RMSCAPI.xlsx", package = "IndicatorCalc") +#' ## generate dummy dataset for different form version +#' +#' ## CAPI +#' form <- system.file("RMS_CAPI_v2.xlsx", package = "IndicatorCalc") +#' datalist <- fct_kobo_dummy(form, +#' n = 384, +#' file = NULL) +#' # openxlsx::write.xlsx(datalist, here::here("inst", "dummy_RMS_CAPI_v2.xlsx")) +#' +#' form <- system.file("RMS_CAPI_v3.xlsx", package = "IndicatorCalc") #' datalist <- fct_kobo_dummy(form, #' n = 384, #' file = NULL) +#' # openxlsx::write.xlsx(datalist, here::here("inst", "dummy_RMS_CAPI_v3.xlsx")) #' -#' ## Save this to use it for testing the package... +#' ## CATI +#' form <- system.file("RMS_CATI_v0.xlsx", package = "IndicatorCalc") +#' datalist <- fct_kobo_dummy(form, +#' n = 384, +#' file = NULL) +#' # openxlsx::write.xlsx(datalist, here::here("inst", "dummy_RMS_CATI_v0.xlsx")) #' -#' openxlsx::write.xlsx(datalist, here::here("inst", "demo_data.xlsx")) +#' form <- system.file("RMS_CATI_v3.xlsx", package = "IndicatorCalc") +#' datalist <- fct_kobo_dummy(form, +#' n = 384, +#' file = NULL) +#' # openxlsx::write.xlsx(datalist, here::here("inst", "dummy_RMS_CATI_v3.xlsx")) fct_kobo_dummy <- function(form, n = 384, file){ @@ -53,6 +72,8 @@ fct_kobo_dummy <- function(form, conf <- data.frame( name <- dico[["variables"]] |> dplyr::filter(repeatvar == frame) |> + dplyr::filter(! is.na(name)) |> + dplyr::filter(!(type %in% c("note", "begin_group", "end_group","begin_repeat", "end_repeat") )) |> dplyr::select(name), # dico[["variables"]]|> @@ -60,11 +81,15 @@ fct_kobo_dummy <- function(form, # dplyr::distinct(type) type <- dico[["variables"]]|> dplyr::filter(repeatvar == frame) |> + dplyr::filter(! is.na(name)) |> + dplyr::filter(!(type %in% c("note", "begin_group", "end_group","begin_repeat", "end_repeat") )) |> dplyr::select(type), ## pulling list options... list_opt <- dico[["variables"]] |> dplyr::filter(repeatvar == frame) |> + dplyr::filter(! is.na(name)) |> + dplyr::filter(!(type %in% c("note", "begin_group", "end_group","begin_repeat", "end_repeat") )) |> dplyr::select(list_name) , #dplyr::left_join(modal, by = c("list_name")) |> # dplyr::select( list_opt) @@ -72,6 +97,8 @@ fct_kobo_dummy <- function(form, constraint <- dico[["variables"]]|> dplyr::filter(repeatvar == frame) |> + dplyr::filter(! is.na(name)) |> + dplyr::filter(!(type %in% c("note", "begin_group", "end_group","begin_repeat", "end_repeat") )) |> dplyr::select(constraint) ) @@ -89,7 +116,7 @@ fct_kobo_dummy <- function(form, ## then apply fct_var_dummy interactively... for(i in (1:nrow(conf)) ) { - # i <- 6 + # i <- 1 cat(paste0(i, "-", conf[i, c("type")], "-", conf[i, c("name")], "\n")) ## manage specific case when list name is not defined in ccoices but pulled from data.. @@ -99,7 +126,10 @@ fct_kobo_dummy <- function(form, this.listname <- conf[i , c("list_name")] this.constraint = conf[i, c("constraint")] - if( !(this.listname %in% c( dico[["modalities"]] |> dplyr::pull(list_name))) ) { + ## cas there's no list matching.. + if( this.type %in% c("select_one", "select_multiple") & + !(this.listname %in% c( dico[["modalities"]] |> + dplyr::pull(list_name))) ) { this.listname <- NULL this.type <- "text" } @@ -150,7 +180,10 @@ fct_kobo_dummy <- function(form, confrep <- data.frame( name <- dico[["variables"]] |> - dplyr::filter(repeatvar == rep) |> + dplyr::filter(repeatvar == rep) |> + dplyr::filter(! is.na(name)) |> + dplyr::filter(!(type %in% c("note", "begin_group", + "end_group","begin_repeat", "end_repeat") )) |> dplyr::select(name), # dico[["variables"]]|> @@ -158,11 +191,17 @@ fct_kobo_dummy <- function(form, # dplyr::distinct(type) type <- dico[["variables"]]|> dplyr::filter(repeatvar == rep) |> + dplyr::filter(! is.na(name)) |> + dplyr::filter(!(type %in% c("note", "begin_group", + "end_group","begin_repeat", "end_repeat") )) |> dplyr::select(type), ## pulling list options... list_opt <- dico[["variables"]] |> dplyr::filter(repeatvar == rep) |> + dplyr::filter(! is.na(name)) |> + dplyr::filter(!(type %in% c("note", "begin_group", + "end_group","begin_repeat", "end_repeat") )) |> dplyr::select(list_name) , #dplyr::left_join(modal, by = c("list_name")) |> # dplyr::select( list_opt) @@ -170,6 +209,9 @@ fct_kobo_dummy <- function(form, constraint <- dico[["variables"]]|> dplyr::filter(repeatvar == rep) |> + dplyr::filter(! is.na(name)) |> + dplyr::filter(!(type %in% c("note", "begin_group", + "end_group","begin_repeat", "end_repeat") )) |> dplyr::select(constraint) ) @@ -200,7 +242,9 @@ fct_kobo_dummy <- function(form, this.listname <- confrep[i , c("list_name")] this.constraint = confrep[i, c("constraint")] - if( !(this.listname %in% c( dico[["modalities"]] |> dplyr::pull(list_name))) ) { + if(this.type %in% c("select_one", "select_multiple") & + !(this.listname %in% c( dico[["modalities"]] |> + dplyr::pull(list_name))) ) { this.listname <- NULL this.type <- "text" } diff --git a/R/fct_plot_indic_donut.R b/R/fct_plot_indic_donut.R index 696ce39..5c32f7f 100644 --- a/R/fct_plot_indic_donut.R +++ b/R/fct_plot_indic_donut.R @@ -6,17 +6,17 @@ #' and interpretation threshold if available #' #' @param indicator vector -#' @param subtitle_chart -#' @param caption_chart -#' @param ordered_threhold vector with the different threshold +#' @param subtitle_chart subtitle for the chart +#' @param caption_chart caption for the chart +#' @param ordered_threhold vector with the different threshold #' (green, orange, red) -#' @param iconunicode unicode value for fontawesome --- +#' @param iconunicode unicode value for fontawesome --- #' see https://fontawesome.com/search?o=r&m=free #' #' #' @return ggplot2 object #' -#' @importFrom ggplot2 ggplot labs aes expansion +#' @import ggplot2 #' @importFrom unhcrthemes scale_fill_unhcr_d theme_unhcr #' @importFrom ggforce geom_arc_bar #' @importFrom ggtext geom_richtext @@ -27,14 +27,23 @@ #' shelter = rbinom(20, 1, 0.5)) |> #' dplyr::mutate( shelter = #' labelled::labelled( shelter, -#' labels = c( "Yes" = 1, "No" = 0), -#' label = "Access to adequate shelter")) +#' labels = c( "Yes" = 1, "No" = 0), +#' label = "Access to adequate shelter also testing a long title to see if it wraps well")) #' #' fct_plot_indic_donut(indicator = test$shelter, #' subtitle_chart = NULL, #' caption_chart = NULL, #' ordered_threhold = NULL, #' iconunicode = "e54f") +#' +#' ## test no value +#' test2 <- NULL +#' +#' fct_plot_indic_donut(indicator = test2, +#' subtitle_chart = NULL, +#' caption_chart = NULL, +#' ordered_threhold = NULL, +#' iconunicode = "e54f") fct_plot_indic_donut <- function(indicator, subtitle_chart = NULL, caption_chart = NULL, @@ -50,6 +59,10 @@ fct_plot_indic_donut <- function(indicator, # Loading data #indicator <- datalist[["main"]]$shelter + if (is.null(indicator)) { + cat("No value was supplied for plotting...") + } else { + df2 <-sjmisc::frq(indicator)[[1]] #df2$label <- factor(df2$label, levels = c( "Yes","No")) @@ -63,19 +76,26 @@ plot <- ggplot2::ggplot(df2) + fill = label ), stat = "pie", size = 1, color = "#FFFFFF" ) + - ggtext::geom_richtext( + ggtext::geom_textbox( x = 0, y = 0, label = paste0( "&#x", - iconunicode,";
", - sjlabelled::get_label(indicator), + iconunicode,";
", + sjlabelled::get_label(indicator) , "
", round(df2 |> dplyr::filter(val == 1) |> dplyr::pull(raw.prc), 2), "%"), size = 16 , - fill = NA, label.color = NA ) + + + # hjust = 0, vjust = 1, + halign = 0.5, # centered text + width = grid::unit(0.60, "npc"), # 73% of plot panel width + box.colour = NA, + fill = NA#, + # label.color = NA + ) + ggplot2::labs( #title = sjlabelled::get_label(x ), subtitle = subtitle_chart, @@ -91,5 +111,6 @@ plot <- ggplot2::ggplot(df2) + #plot return(plot) +} } diff --git a/R/fct_plot_rbm_sdg.R b/R/fct_plot_rbm_sdg.R index 038c563..f517b76 100644 --- a/R/fct_plot_rbm_sdg.R +++ b/R/fct_plot_rbm_sdg.R @@ -13,6 +13,7 @@ #' @param rbm the RBM variable name - that can match SDG #' @param years years to filter the chart - for instance c(2000,2022) #' +#' @import ggplot2 #' @importFrom SDGsR get_indicator #' @importFrom countrycode countrycode #' @importFrom janitor clean_names @@ -21,10 +22,10 @@ #' @return a ggplot2 object #' @export #' @examples -#' fct_plot_rbm_sdg( country = "BRA", -#' rbm = "impact2_2", -#' years = c(2000, 2022)) + -#' unhcrthemes::theme_unhcr(font_size = 10) +#' # fct_plot_rbm_sdg( country = "BRA", +#' # rbm = "impact2_2", +#' # years = c(2000, 2022)) + +#' # unhcrthemes::theme_unhcr(font_size = 10) fct_plot_rbm_sdg <- function( country = "BRA", rbm = "outcome16_2", years = c(2000, 2022)) { diff --git a/R/fct_re_map.R b/R/fct_re_map.R index 646c171..b3d9359 100644 --- a/R/fct_re_map.R +++ b/R/fct_re_map.R @@ -49,12 +49,42 @@ #' ) #' datalist <- fct_re_map(datalist = datalist, mapper = mapper ) #' -#' fct_check_map(datalist = datalist, mapper = mapper ) -#' +#' #fct_check_map(datalist = datalist, mapper = mapper ) +#' +#' ## Now testing on a full remap... +#' ## Dummy data created with fct_kobo_dummy +#' datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2.xlsx", +#' package = "IndicatorCalc")) +#' +#' ## Mapping file created from Dummy data +#' mappingfile <- system.file("RMS_CAPI_v2_mapping.xlsx", +#' package = "IndicatorCalc") +#' +#' IndicatorRequirementFile <- system.file("RMS_CAPI_v2_mapper.xlsx", +#' package = "IndicatorCalc") +#' +#' ## and now we remap both required variables for main and ind +#' mappermain <- fct_build_map(mappingfile = mappingfile, +#' IndicatorRequirementFile = IndicatorRequirementFile, +#' thisMeasureLevel = "main") +#' datalist <- fct_re_map(datalist = datalist, mapper = mappermain ) +#' +#' mapperind <- fct_build_map(mappingfile = mappingfile, +#' IndicatorRequirementFile = IndicatorRequirementFile, +#' thisMeasureLevel = "ind") +#' datalist <- fct_re_map(datalist = datalist, mapper = mapperind ) +#' +#' +#' +#' ## Writing this in the installation folder of the packages to run all examples +#' # openxlsx::write.xlsx( list ( main = as.data.frame(datalist[["main"]]), +#' # ind = as.data.frame(datalist[["ind"]]) ), +#' # here::here("inst","dummy_RMS_CAPI_v2_mapped.xlsx")) #' fct_re_map <- function(datalist, mapper){ varname <- fct_get_all_variable_names(datalist) + ## Loop around the variables within mapper for ( i in 1:nrow(mapper[["variablemap"]]) ) { # i <- 4 @@ -104,7 +134,6 @@ fct_re_map <- function(datalist, mapper){ rm(thisvar, newlev) } } - return(datalist) } diff --git a/R/fct_var_dummy.R b/R/fct_var_dummy.R index 817e3fd..20e1648 100644 --- a/R/fct_var_dummy.R +++ b/R/fct_var_dummy.R @@ -167,7 +167,7 @@ fct_var_dummy <- function( frame, } ## date ########### - if( type %in% c("date", "start ", "end")) { + if( type %in% c("date", "start", "end")) { res <- dplyr::tibble( thisvar = Sys.time() - abs(rnorm(n, 0, sd = 2) * 365 * 24 * 3600)) ## Now rename the variable diff --git a/R/fct_var_mapping.R b/R/fct_var_mapping.R index c628be1..db7dd59 100644 --- a/R/fct_var_mapping.R +++ b/R/fct_var_mapping.R @@ -3,26 +3,218 @@ #' fct_var_mapping #' #' Create a diff to identify the mapping between expected variables / modalities and -#' what is has been configured in a specific xlsform +#' what is has been configured in a specific xlsform. The function use a fuzzy matching +#' process to identify the best and second best match process... +#' It generates an excel file ready for manual review and that can be used as an input +#' for the next function in the pipeline fct_build_map() +#' +#' When you open the excel, filter for all match where the matching_index_best is different than 0 +#' +#' If you can confirm the match, manually set it to 0 so that the matching can be confirmed. +#' If the matching is not the expected one you may take the second best match, and if correct, +#' paste it and set the index to 0 #' #' @param xlsformpath path to the xlsform +#' @param IndicatorRequirementFile path to the file where the standard mapping is depending on form version #' @param mappingfile_out path to file to out the mapping #' +#' @import stringdist +#' @import dplyr +#' #' @return a list with the best mapping... #' #' @export #' @examples -#' -#' form <- system.file("RMSCAPI.xlsx", package = "IndicatorCalc") -#' #fct_var_mapping(xlsformpath = form) -fct_var_mapping <- function(xlsformpath){ +#' # Test the function +#' form <- system.file("RMS_CAPI_v2.xlsx", package = "IndicatorCalc") +#' IndicatorRequirementFile <- system.file("RMS_CAPI_v2_mapper.xlsx", package = "IndicatorCalc") +#' # Get the map to revise +#' mapper <- fct_var_mapping(xlsformpath = form, +#' IndicatorRequirementFile = IndicatorRequirementFile, +#' mappingfile_out = tempfile()) +#' # here::here("inst", "RMS_CAPI_v2_mapping.xlsx")) +fct_var_mapping <- function(xlsformpath, + IndicatorRequirementFile, + mappingfile_out){ + ## Pulling Comparison base ###### + IndicMap <- readxl::read_excel(IndicatorRequirementFile ) + dico <- kobocruncher::kobo_dico(xlsformpath) + + # Comparing variables ########## + # names(IndicMap) + varMapIndic <- IndicMap |> + dplyr::select(QuestionVar, label, type) |> + dplyr::distinct() |> + tibble::as_tibble() + ## Comparing based on variable name + vec1 <- varMapIndic$QuestionVar + + ## Now taking the form... + varMapForm <- dico[["variables"]] |> + # names(varMapForm) + # levels(as.factor(varMapForm$type)) + dplyr::select(name, name_or, type, label) |> + dplyr::filter(type %in% c("integer","multiple", "select_multiple", "select_one")) |> + dplyr::distinct() |> + tibble::as_tibble() + ## Comparing based on variable name + vec2 <- varMapForm$name_or - IndicMap <- readxl::read_excel( system.file("IndicMap.xlsx", package = "IndicatorCalc")) - dico <- kobocruncher::kobo_dico(xlsformpath) + # Initialize an empty dataframe to store the results + result_match <- data.frame( + Indic = character(), + best_match = character(), + matching_index_best = integer(), + second_best = character(), + matching_index_second_best = integer(), + stringsAsFactors = FALSE + ) + # Loop through each element in the first vector + for (i in seq_along(vec1)) { + #i <- 1 + # Calculate Levenshtein distances between the current element and all elements in the second vector + distances <- stringdist::stringdistmatrix(vec1[i], vec2) + + # Find the index of the minimum distance (best match) + best_match_index <- which.min(distances) + + # Sort distances to find the second best match + sorted_distances <- sort(distances) + second_best_match_index <- which(distances == sorted_distances[2]) + thismatch <- data.frame( + Indic = vec1[i], + best_match = vec2[best_match_index], + matching_index_best = distances[best_match_index], + second_best = vec2[second_best_match_index[1]], + matching_index_second_best = sorted_distances[2] + ) + # Append the results to the dataframe + result_match <- rbind( result_match, thismatch ) + } + ## merge this with type and label + result_matchInd <- varMapIndic |> + dplyr::left_join(result_match, by = c("QuestionVar"="Indic" )) |> + dplyr::left_join(varMapForm |> + dplyr::rename( best_name = "name", + best_type = "type", + best_label = "label"), + by = c("best_match"="name_or" )) |> + dplyr::left_join(varMapForm |> + dplyr::rename( second_name = "name", + second_type = "type", + second_label = "label"), + by = c("second_best"="name_or" )) |> + dplyr::select(QuestionVar, label, type, + best_match, matching_index_best, best_name, best_type, best_label, + second_best, matching_index_second_best, + second_name, second_type, second_label) - ## Map variable + # Comparing modalities ########## + # names(IndicMap) + varMapMod <- IndicMap |> + dplyr::select(QuestionVar, list_name, name_mod, label_mod) |> + dplyr::mutate( mod = paste0(QuestionVar, ".", name_mod)) |> + dplyr::distinct() |> + tibble::as_tibble() + ## Comparing based on variable name + vec1 <- varMapMod$mod + ## Now taking the form... + varMapFormMod1 <- + dico[["variables"]] |> + dplyr::filter(type %in% c( "select_multiple", "select_one")) |> + dplyr::select( name_or, type, list_name) |> + dplyr::rename(name_var = "name_or") |> + ## retain only modalities from the var matching + dplyr::filter( name_var %in% result_match$best_match | + name_var %in% result_match$second_best ) + + varMapFormMod2 <- dico[["modalities"]] |> + # names(varMapFormMod) + # levels(as.factor(varMapForm$type)) + dplyr::select(list_name, name, label)|> + ## retain only modalities from the var matching + dplyr::filter( list_name %in% varMapFormMod1$list_name ) + + varMapFormMod3 <- varMapFormMod2 |> + dplyr::left_join( varMapFormMod1 , + by = c( "list_name"), + relationship = "many-to-many") |> + dplyr::mutate( mod = paste0(name_var, ".", name)) |> + dplyr::distinct() |> + tibble::as_tibble() + ## Comparing based on variable name + vec2 <- varMapFormMod3$mod - ## Map modalities + # Initialize an empty dataframe to store the results + result_match <- data.frame( + Indic = character(), + best_match = character(), + matching_index_best = integer(), + second_best = character(), + matching_index_second_best = integer(), + stringsAsFactors = FALSE + ) + # Loop through each element in the first vector + for (i in seq_along(vec1)) { + #i <- 1 + # Calculate Levenshtein distances between the current element and all elements in the second vector + distances <- stringdist::stringdistmatrix(vec1[i], vec2) + + # Find the index of the minimum distance (best match) + best_match_index <- which.min(distances) + # Sort distances to find the second best match + sorted_distances <- sort(distances) + second_best_match_index <- which(distances == sorted_distances[2]) + thismatch <- data.frame( + Indic = vec1[i], + best_match = vec2[best_match_index], + matching_index_best = distances[best_match_index], + second_best = vec2[second_best_match_index[1]], + matching_index_second_best = sorted_distances[2] + ) + # Append the results to the dataframe + result_match <- rbind( result_match, thismatch ) + } + ## merge this with type and label + result_matchMod <- varMapMod |> + dplyr::left_join(result_match, by = c("mod"="Indic" )) |> + dplyr::left_join(varMapFormMod3 |> + dplyr::rename( best_name = "name", + best_type = "type", + best_list_name = "list_name", + best_label = "label"), + by = c("best_match"="mod" )) |> + dplyr::left_join(varMapFormMod3 |> + dplyr::rename( second_name = "name", + second_type = "type", + second_list_name = "list_name", + second_label = "label"), + by = c("second_best"="mod" )) |> + # dput( names(result_matchMod)) + dplyr::select(QuestionVar, list_name, name_mod, label_mod, mod, + best_match, matching_index_best, + best_list_name, best_name, best_label, best_type, + second_best, matching_index_second_best, + second_list_name, second_name, second_label, second_type) + results <- list( + result_matchInd = result_matchInd, + result_matchMod = result_matchMod + + ) + + ## Return the excel file + if(!(is.null(mappingfile_out))){ + openxlsx2::write_xlsx(results, + file = mappingfile_out, + overwrite = T, + na.strings = "") + } + + + # Return the results + return(results) } + + diff --git a/R/globals.R b/R/globals.R index ab8b4c3..da4d347 100644 --- a/R/globals.R +++ b/R/globals.R @@ -11,5 +11,17 @@ globalVariables(unique(c( # fct_plot_indic_donut: "label", "raw.prc", "val", # fct_re_map: - "df", "value", "variable" + "df", "value", "variable", + # fct_build_map: + "label_mod", "map", "mappattern", "MeasureLevel", "name_mod", "QuestionVar", "thismapper", + # fct_kobo_dummy: + "list_name", + # fct_plot_rbm_sdg: + "geo_area_name", "indicator", "RMS", "series_description", "time_period_start", + # fct_require: + "IndName", "label_mod", "MeasureLevel", "name_mod", "QuestionVar", + # fct_var_mapping: + "best_label", "best_list_name", "best_match", "best_name", "best_type", "label_mod", "list_name", "matching_index_best", "matching_index_second_best", "mod", "name", "name_mod", "name_or", "name_var", "QuestionVar", "second_best", "second_label", "second_list_name", "second_name", "second_type", "type", + # mod_variable_mapping_server: + "file_type", "geographies", "kobo_asset_id", "operational_purpose_of_data", "title", "type" ))) diff --git a/R/impact2_3.R b/R/impact2_3.R new file mode 100644 index 0000000..21c617e --- /dev/null +++ b/R/impact2_3.R @@ -0,0 +1,143 @@ +# WARNING - Generated by {fusen} from dev/indicators.Rmd: do not edit by hand + +#' impact2_3 +#' +#' **Proportion of PoC with access to health services** +#' +#' This indicator measures access to primary health care services for persons +#' of concern in need of health services. +#' +#' Health services refers to preventive, pro-motive, curative, rehabilitative +#' and palliative health services. +#' +#' It is linked to SDG indicator [3.8.1](https://www.who.int/data/gho/data/themes/topics/service-coverage) +#' on the coverage of essential health services. +#' +#' The standard questions for this indicator are taken from UNHCR's [HAUS](https://www.unhcr.org/strategic-health-information.html) monitors trends +#' in how refugees outside of camps access and utilize health services over time. +#' Healthcare can be delivered through a combination of community level, mobile +#' and fixed healthcare facilities. +#' The number, type and location of each will vary by context. +#' The standard questionnaire module will help us to understand these differences. +#' +#' | Standard Questions | +#' |:------------------:| +#' | HACC01 - HACC04 | +#' +#' **Numerator**: Population who have received the asked for health services in the previous 30 days +#' +#' **Denominator**: Total population who have asked for health services in the previous 30 days +#' +#' **Formula**: *HACC01*=1 + / ((*HACC03*=1 & *HACC04*!=7,8,96) + *HACC01*=1) +#' +#' This indicator comes from the individual dataset +#' Calculate those who were not able to access due to reasons +#' unrelated to asked services (when HACC04 is 7 or 8 or 96) +#' +#' @param datalist A list with all hierarchical data frame for a survey data set. +#' format is expected to match the Excel export synchronized from kobo to RILD +#' and loaded with kobocruncher::kobo_data() +#' +#' +#' @importFrom dplyr mutate case_when +#' @importFrom labelled labelled +#' +#' @return new calculated variable - appended or not... +#' +#' @export +#' @examples +#' ## data, cf example fct_re_map() +#' # datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", +#' # package = "IndicatorCalc")) +#' # ## Apply calculation +#' # datalist <- impact2_3(datalist ) +#' # +#' # ## Visualise value +#' # fct_plot_indic_donut(indicator = datalist[["ind"]]$impact2_3, +#' # iconunicode = "f140") +#' +impact2_3 <- function(datalist ){ + +# # Mapper , +# mapper = list( +# hierarchy = "ind", +# variablemap = data.frame( +# label = +# c("1. During the past 30 days, has ${name_individual} consulted a health practitioner, dentist, traditional healer, or pharmacist, or visited a health center?", +# "2. For what reason(s) did ${name_individual} seek consultation?", +# "3. During the past 30 days, has ${name_individual} needed health services that’s/he could not have access to?", +# "4. Why has ${name_individual} been unable to access a medical treatment in the past 30 days?" +# ) +# , +# variable = +# c("HACC01", "HACC02", "HACC03", "HACC04") +# ), +# modalitymap = data.frame( +# variable = +# c("HACC01", "HACC01", "HACC02", "HACC02", "HACC02", "HACC02", +# "HACC02", "HACC02", "HACC03", "HACC03", "HACC04", "HACC04", "HACC04", +# "HACC04", "HACC04", "HACC04", "HACC04", "HACC04", "HACC04", "HACC04", +# "HACC04") +# , +# label = +# c("Yes", "No", "Illness", "Injury", "General check-up (not for pregnancy)", +# "Pre/Postnatal check-up", "Giving birth", "Other (Specify)", +# "Yes", "No", "Lack of money", "No medical personnel available", +# "Turned away because facility was full", "Turned away because facility was closed", +# "Hospital/Clinic not having enough supplies or tests", "Health facility is too far", +# "Fear of contracting a communicable disease (e.g. COVID-19)", +# "Lockdown/Travel restrictions", "Too Far/transport issues", "Health facility is destroyed", +# "Other (Specify)") +# , +# standard = +# c("1", "0", "1", "2", "3", "4", "5", "96", "1", "0", "1", "2", +# "3", "4", "5", "6", "7", "8", "9", "10", "96") +# )) +# ## So first we check that we have what we need in the data set based on the mapper +# check_map <- fct_check_map(datalist = datalist, +# mapper = mapper) +# if ( check_map == FALSE) { +# cat( "There are missing data requirement to calculate Indicator Impact 2.3") +# } else { +# +# +# datalist[["ind"]]$health_NOacc <- dplyr::case_when( +# as.integer(datalist[["ind"]]$HACC03) == 1 & +# (as.integer(datalist[["ind"]]$HACC04_7) == 1 | +# as.integer(datalist[["ind"]]$HACC04_8) == 1 | +# as.integer(datalist[["ind"]]$HACC04_96) == 1 ) ~ 0, +# +# as.integer(datalist[["ind"]]$HACC03) == 1 & +# (as.integer(datalist[["ind"]]$HACC04_1) == 1 | +# as.integer(datalist[["ind"]]$HACC04_2) == 1 | +# as.integer(datalist[["ind"]]$HACC04_3) == 1 | +# as.integer(datalist[["ind"]]$HACC04_4) == 1 | +# as.integer(datalist[["ind"]]$HACC04_5) == 1 | +# as.integer(datalist[["ind"]]$HACC04_6) == 1 | +# as.integer(datalist[["ind"]]$HACC04_9) == 1 | +# as.integer(datalist[["ind"]]$HACC04_10) == 1) ~ 1, +# +# TRUE ~ 0) +# # table(datalist[["ind"]]$health_NOacc, useNA = "ifany") +# +# datalist[["ind"]]$health_NOacc <- as.numeric(datalist[["ind"]]$health_NOacc) +# datalist[["ind"]]$HACC01 <- as.numeric(datalist[["ind"]]$HACC01) +# +# +# ## Add up who needed services ( both who accessed and did not access) +# datalist[["ind"]]$HACC_need <- datalist[["ind"]]$HACC01 + datalist[["ind"]]$health_NOacc +# +# datalist[["ind"]]$impact2_3 <- datalist[["ind"]]$HACC01 / datalist[["ind"]]$HACC_need +# +# datalist[["ind"]]$impact2_3 <- dplyr::case_when( +# datalist[["ind"]]$impact2_3 == 1 ~ 1, +# datalist[["ind"]]$impact2_3 == 0.5 ~ 0, +# datalist[["ind"]]$impact2_3 == 0 ~ 0, +# TRUE ~ NA_real_ ) +# +# datalist[["ind"]]$impact2_3 <- labelled::labelled( datalist[["ind"]]$impact2_3, +# labels =c( "Yes"=1, "No"=0 ), +# label = "Proportion of PoC with access to health services") +# } +# return(datalist) +} diff --git a/R/impact3_2a.R b/R/impact3_2a.R new file mode 100644 index 0000000..13a2569 --- /dev/null +++ b/R/impact3_2a.R @@ -0,0 +1,128 @@ +# WARNING - Generated by {fusen} from dev/indicators.Rmd: do not edit by hand + +#' impact3_2a +#' +#' **Proportion of Persons of Concern enrolled in primary education** +#' +#' This indicator measures the number of students enrolled in primary education, +#' regardless of age, expressed as a percentage of the official school-age population +#' corresponding to the respective same level of education. +#' +#' This is also referred to as Gross enrollment rate (GER). +#' +#' It is linked to SGD indicator 4.1.1 on quality education. +#' +#' The standard questions for this indicator are taken from UNHCR's +#' [Standardized Education Module](https://www.unhcr.org/60804b214.pdf) which +#' are adapted primarily from the IHSN/EPDC and MICS indicator and questionnaire frameworks. +#' +#' *Definitions*: +#' +#' 1. "Enrollment" refers individuals officially registered in a primary/secondary school education programme. +#' +#' 2. "Primary education" is designed to provide students with fundamental skills in reading, writing and mathematics. +#' +#' Duration typically varies from 4 to 7 years. +#' The most common duration is 6 years. +#' It corresponds to ISCED (International Standard Classification of Education) level 1. +#' +#' 3. "Primary school age" depends on the education system and it varies from country to country. +#' Children typically enter in primary education between age 5 and 7 and leave at the age of 10 and 12. +#' +#' | Standard Questions | +#' |:------------------:| +#' | EDU01-EDU04 | +#' +#' **Numerator**: Population enrolled in primary education (regardless of age) +#' **Denominator**: Total primary school age population (to be adjusted by the country for enumeration) +#' +#' **Formula**: (*EDU01*=1 & *EDU02*=1 & *EDU03*=2) / Number of children aged from 6 to 10 +#' +#' This indicator comes from the individual dataset +#' +#' @param datalist A list with all hierarchical data frame for a survey data set. +#' format is expected to match the Excel export synchronized from kobo to RILD +#' and loaded with kobocruncher::kobo_data() +#' +#' @importFrom dplyr mutate case_when +#' @importFrom labelled labelled +#' +#' @return new calculated variable - appended or not... +#' +#' @export +#' @examples +#' ## data, cf example fct_re_map() +#' datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", +#' package = "IndicatorCalc")) +#' ## Apply calculation +#' datalist <- impact3_2a(datalist ) +#' +#' ## Visualise value +#' fct_plot_indic_donut(indicator = datalist[["ind"]]$impact3_2a, +#' iconunicode = "f140") +impact3_2a <- function(datalist ){ + + ## Mapper +mapper = list( + hierarchy = "ind", + variablemap = data.frame( + label = +c("1. Has ${child_edu_name} ever attended school?", "2. Did ${child_edu_name} attend school or pre-school at any time during current school year?", +"3. During this/that school year , what level is (was) ${child_edu_name} attending?", +"4. What type of school?", "6. Can you estimate how old is ${HH02}?" +) + , + variable = +c("EDU01", "EDU02", "EDU03", "EDU04", "HH07") + ), + modalitymap = data.frame( + variable = +c("EDU01", "EDU01", "EDU02", "EDU02", "EDU03", "EDU03", "EDU03", +"EDU03", "EDU03", "EDU03", "EDU03", "EDU04", "EDU04", "EDU04", +"EDU04", "EDU04", "EDU04", "EDU04", "HH07") + , + label = +c("Yes", "No", "Yes", "No", "Early Childhood Education or Pre-primary", +"Primary", "Secondary", "Secondary - Technical and Vocational Education and Training (TVET)", +"Post-secondary - Technical and Vocational Education and Training (TVET)", +"Tertiary", "Don’t know", "Government or public", "UN or NGO (non-governmental organization)", +"Religious or faith-based organization", "Community", "Private", +"Other (specify)", "Don’t know", NA) + , + standard = +c("1", "0", "1", "0", "1", "2", "3", "4", "5", "6", "98", "1", +"2", "3", "4", "5", "96", "98", NA) + )) + ## So first we check that we have what we need in the data set based on the mapper + check_map <- fct_check_map(datalist = datalist, + mapper = mapper) + if ( check_map == FALSE) { + cat( "There are missing data requirement to calculate Indicator Impact 3.2.a") + } else { + +datalist[["ind"]]$edu_primary<- dplyr::case_when( + as.integer(datalist[["ind"]]$EDU01) == 1 & + as.integer(datalist[["ind"]]$EDU02) == 1 & + as.integer(datalist[["ind"]]$EDU03) == 2 ~ 1, + + as.integer(datalist[["ind"]]$EDU01) == 0 | + as.integer(datalist[["ind"]]$EDU02) == 0 ~ 0, + + TRUE ~ 0) + +#Adjust age group for primary school enrollment --> this to go in function var... +datalist[["ind"]]$age_primary<- dplyr::case_when( + as.integer(datalist[["ind"]]$HH07) >= 6 & + as.integer(datalist[["ind"]]$HH07) <= 10 ~ 1, + TRUE ~ NA_real_) + +datalist[["ind"]]$impact3_2a <- sum(datalist[["ind"]]$edu_primary, na.rm = TRUE) / + sum(datalist[["ind"]]$age_primary, na.rm = TRUE) + +datalist[["ind"]]$impact3_2a <- labelled::labelled(datalist[["ind"]]$impact3_2a, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Proportion of persons of concern enrolled in primary education") + + } + return(datalist) +} diff --git a/R/impact3_2b.R b/R/impact3_2b.R new file mode 100644 index 0000000..69d05d6 --- /dev/null +++ b/R/impact3_2b.R @@ -0,0 +1,132 @@ +# WARNING - Generated by {fusen} from dev/indicators.Rmd: do not edit by hand + +#' impact3_2b +#' +#' **Proportion of Persons of Concern enrolled in secondary education** +#' +#' This indicator measures the number of students enrolled in secondary education, +#' regardless of age, expressed as a percentage of the official school-age population +#' corresponding to the respective same level of education. +#' +#' This is also referred to as Gross enrollment rate (GER). +#' It is linked to SGD indicator 4.1.1 on quality education. +#' +#' The standard questions for this indicator are taken from UNHCR's [Standardized Education Module](https://www.unhcr.org/60804b214.pdf) which are adapted primarily from +#' the IHSN/EPDC and MICS indicator and questionnaire frameworks. +#' +#' Definitions: +#' +#' 1. "Enrollment" refers individuals officially registered in a primary/secondary school education programme. +#' +#' 2. "Secondary education" provides learning and educational activities building on primary education and preparing for both first labour market entry as well as further study. +#' +#' The common duration is 6 years and is often divided between lower and upper secondary education (corresponding respectively to ISCED 2 and 3). +#' +#' 3. "Secondary school age" depends on the education system and differ from +#' country to country. +#' Children typically enter secondary education between age 11 and 13 and leave between age 17-19. +#' +#' 4. Whenever possible, operations are encouraged also to disaggregate data between lower and upper secondary +#' +#' | Standard Questions | +#' |:------------------:| +#' | EDU01-EDU04 | +#' +#' **Numerator**: Population enrolled in secondary education (regardless of age) +#' **Denominator**: Total secondary school age population (to be adjusted by the country for enumeration) +#' +#' **Formula**: (*EDU01*=1 & *EDU02*=1 & (*EDU03*=3,4) / Number of children aged from 11 to 18 +#' +#' This indicator comes from the individual dataset +#' +#' Include if they are attending secondary or secondary -technical and vocational +#' +#' @param datalist A list with all hierarchical data frame for a survey data set. +#' format is expected to match the Excel export synchronized from kobo to RILD +#' and loaded with kobocruncher::kobo_data() +#' +#' @importFrom dplyr mutate case_when +#' @importFrom labelled labelled +#' +#' @return new calculated variable - appended or not... +#' +#' @export +#' @examples +#' ## data, cf example fct_re_map() +#' datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", +#' package = "IndicatorCalc")) +#' ## Apply calculation +#' datalist <- impact3_2b(datalist ) +#' +#' ## Visualise value +#' fct_plot_indic_donut(indicator = datalist[["ind"]]$impact3_2b, +#' iconunicode = "f140") +#' +impact3_2b <- function(datalist ){ + # mapper , +mapper = list( + hierarchy = "ind", + variablemap = data.frame( + label = +c("1. Has ${child_edu_name} ever attended school?", "2. Did ${child_edu_name} attend school or pre-school at any time during current school year?", +"3. During this/that school year , what level is (was) ${child_edu_name} attending?", +"4. What type of school?", "6. Can you estimate how old is ${HH02}?" +) + , + variable = +c("EDU01", "EDU02", "EDU03", "EDU04", "HH07") + ), + modalitymap = data.frame( + variable = +c("EDU01", "EDU01", "EDU02", "EDU02", "EDU03", "EDU03", "EDU03", +"EDU03", "EDU03", "EDU03", "EDU03", "EDU04", "EDU04", "EDU04", +"EDU04", "EDU04", "EDU04", "EDU04", "HH07") + , + label = +c("Yes", "No", "Yes", "No", "Early Childhood Education or Pre-primary", +"Primary", "Secondary", "Secondary - Technical and Vocational Education and Training (TVET)", +"Post-secondary - Technical and Vocational Education and Training (TVET)", +"Tertiary", "Don’t know", "Government or public", "UN or NGO (non-governmental organization)", +"Religious or faith-based organization", "Community", "Private", +"Other (specify)", "Don’t know", NA) + , + standard = +c("1", "0", "1", "0", "1", "2", "3", "4", "5", "6", "98", "1", +"2", "3", "4", "5", "96", "98", NA) + )) + ## So first we check that we have what we need in the data set based on the mapper + check_map <- fct_check_map(datalist = datalist, + mapper = mapper) + if ( check_map == FALSE) { + cat( "There are missing data requirement to calculate Indicator Impact 3.2.b") + } else { + +datalist[["ind"]]$edu_secondary<- dplyr::case_when( + as.integer(datalist[["ind"]]$EDU01) == 1 & + as.integer(datalist[["ind"]]$EDU02) == 1 & + ( as.integer(datalist[["ind"]]$EDU03) == 3 | + as.integer(datalist[["ind"]]$EDU03) == 4) ~ 1, + + as.integer(datalist[["ind"]]$EDU01) == 0 | + as.integer(datalist[["ind"]]$EDU02) == 0 ~ 0, + TRUE ~ 0) + + +#Adjust age group for secondary school enrollment +##calculated as 11 to 18 above --> put as function variable... + +datalist[["ind"]]$age_secondary<- dplyr::case_when( + as.integer(datalist[["ind"]]$HH07) >= 11 & + as.integer(datalist[["ind"]]$HH07) <= 18 ~ 1, + TRUE ~ NA_real_) + +datalist[["ind"]]$impact3_2b <- sum(datalist[["ind"]]$edu_secondary, na.rm= TRUE) / + sum(datalist[["ind"]]$age_secondary, na.rm = TRUE) + +datalist[["ind"]]$impact3_2b <- labelled::labelled(datalist[["ind"]]$impact3_2b, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Proportion of persons of concern enrolled in secondary education") + + } + return(datalist) +} diff --git a/R/impact3_3.R b/R/impact3_3.R new file mode 100644 index 0000000..10ea8cc --- /dev/null +++ b/R/impact3_3.R @@ -0,0 +1,96 @@ +# WARNING - Generated by {fusen} from dev/indicators.Rmd: do not edit by hand + +#' impact3_3 +#' +#' **Proportion of population that feel safe walking alone in their neighbourhood** +#' +#' This indicator measures the proportion of persons of concern who self-report +#' feeling safe while walking alone in her/his neighborhood after dark. +#' It is linked to SDG indicator [16.1.4](https://unstats.un.org/sdgs/metadata/files/Metadata-16-01-04.pdf). +#' +#' The standard module for this indicator is taken from SGD indicator owner UNODC. +#' This indicator only pertains to self-reported feeling of 'safety' and not +#' 'security' since security is associated with additional external factors. +#' +#' | Standard Questions | +#' |:------------------:| +#' | SAF01 | +#' +#' **Numerator**: Population who self-report feeling safe walking alone in +#' their neighborhood after dark +#' +#' **Denominator**: Total population +#' **Formula**: *SAF01*=1,2 / *SAF01*=1,2,3,4 +#' +#' This indicator comes from main dataset based on the respondent randomly +#' selected for individual level +#' +#' if unsafe or very unsafe 0, 98 and 99 go into blank +#' I never walk alone will also go into blank if any +#' +#' @param datalist A list with all hierarchical data frame for a survey data set. +#' format is expected to match the Excel export synchronized from kobo to RILD +#' and loaded with kobocruncher::kobo_data() +#' +#' @importFrom dplyr mutate case_when +#' @importFrom labelled labelled +#' +#' @return new calculated variable - appended or not... +#' +#' @export +#' @examples +#' ## data, cf example fct_re_map() +#' datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", +#' package = "IndicatorCalc")) +#' ## Apply calculation +#' datalist <- impact3_3(datalist) +#' +#' ## Visualise value +#' fct_plot_indic_donut(indicator = datalist[["main"]]$impact3_3, +#' iconunicode = "f140") +impact3_3 <- function(datalist ){ + + # Mapper +mapper = list( + hierarchy = "main", + variablemap = data.frame( + label = +"1. How safe does ${name_selectedadult18} feel walking alone in your area/neighbourhood after dark?" + , + variable = +"SAF01" + ), + modalitymap = data.frame( + variable = +c("SAF01", "SAF01", "SAF01", "SAF01", "SAF01", "SAF01") + , + label = +c("Very safe", "Fairly safe", "Bit unsafe", "Very unsafe", "Don’t know", +"Prefer not to respond") + , + standard = +c("1", "2", "3", "4", "98", "99") + )) + + ## So first we check that we have what we need in the data set based on the mapper + check_map <- fct_check_map(datalist = datalist, + mapper = mapper) + if ( check_map == FALSE) { + cat( "There are missing data requirement to calculate Indicator Impact 3.3") + } else { + +datalist[["main"]]$impact3_3 <- dplyr::case_when( + as.integer(datalist[["main"]]$SAF01) == 1 | + as.integer(datalist[["main"]]$SAF01) == 2 ~ 1, + as.integer(datalist[["main"]]$SAF01) == 3 | + as.integer(datalist[["main"]]$SAF01) == 4 ~ 0 , + as.integer(datalist[["main"]]$SAF01) == 98 | + as.integer(datalist[["main"]]$SAF01) == 99 ~ NA_real_) + +datalist[["main"]]$impact3_3 <- labelled::labelled(datalist[["main"]]$impact3_3, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Proportion of population that feel safe walking alone in their neighbourhood") + + } + return(datalist) +} diff --git a/R/impact_2_2.R b/R/impact_2_2.R index 9399bfa..1d2eb1f 100644 --- a/R/impact_2_2.R +++ b/R/impact_2_2.R @@ -9,6 +9,99 @@ #' calculated, check the values for each variable before, #' calculating 2.2 impact indicator. #' +#' +#' --------- +#' # inter_electricity +#' +#' Households lighting provides a sense of safety and security within and outside +#' the households after sunset. +#' +#' If households lack access to electricity, especially for lighting and +#' connectivity, this affects the occupants' security and limits their +#' opportunities for socialization, learning and self-reliance. +#' +#' **Numerator**: Population with access to electricity, especially for lighting +#' and connectivity +#' +#' **Denominator**: Total population +#' +#' **Formula**: *LIGHT01* = 1 & *LIGHT03* != 1, 96, 98 +#' +#' --------- +#' # inter_healthcare +#' +#' Access to healthcare depends on availability of healthcare, including physical +#' reach, acceptability and affordability for all. +#' For this indicator, the focus is on the availability of healthcare system. +#' According to [The Sphere Handbook](https://spherestandards.org/wp-content/uploads/Sphere-Handbook-2018-EN.pdf), +#' primary healthcare facility should be accessible within one hour's walk from dwellings. +#' **Numerator**: Population that can reach a primary healthcare facility within one hour from dwellings +#' **Denominator**: Total population +#' **Formula**: *HEA01* != 96, 98 & *HEA03* <= 60 +#' (reachable within one hour/60 minutes) +#' +#' +#' --------- +#' # inter_drinkingwater +#' +#' Access to clean drinking water is essential for a person's survival and well +#' being and a precursor for achieving protection outcomes related to health, +#' education and economic developed. +#' The calculation for access drinking water is linked to SGD Indicator +#' [6.1.1](https://unstats.un.org/sdgs/metadata/files/Metadata-06-01-01.pdf). +#' The questionnaire module and the analysis guidance is taken from [UNICEF MICS6](https://mics.unicef.org/tools#analysis). +#' +#' **Numerator**: Population using improved sources of drinking water either in their dwelling/yard/plot or within 30 minutes round trip collection time +#' +#' **Denominator**: Total population +#' +#' **Formula**: +#' *DWA03* < 30 (under 30 minutes), & +#' *DWA01* != 7,9,13,96,98 & +#' *DWA02* != 3 +#' +#' This basic service is calculated from the main dataset +#' There are three conditions as below improved source, +#' in dwelling/yard/plot and reachable under 30 minutes +#' +#' +#' --------- +#' # inter_shelter +#' +#' The right to access adequate housing is protected by international law. +#' The concept of "adequacy" means that housing is more than four walls and a +#' roof as indicated in [The Sphere Handbook](https://spherestandards.org/wp-content/uploads/Sphere-Handbook-2018-EN.pdf). +#' Habitable housing primarily refers to the fact that the housing should provide +#' protection from cold, damp, heat, rain, wind, and other threats to health, +#' structural hazards, and disease vectors and it should not be overcrowded. +#' As shelter/housing is primarily a contextual element, there may be +#' discrepancies from country to country on how this data is measured. +#' +#' Adequate shelter is measured based on having improved material for the +#' dwelling as indicated in [DHS](https://dhsprogram.com/pubs/pdf/AS61/AS61.pdf) +#' publication on housing conditions which is also used by [MICS6](https://mics.unicef.org/tools). +#' +#' Overcrowding is also used which occurs if there are more than three people +#' per habitable room as defined by [UN-Habitat](https://www.ncbi.nlm.nih.gov/books/NBK535289/table/ch3.tab2/). +#' +#' **Numerator**: Population that have access to adequate housing +#' **Denominator**: Total population +#' +#' **Formula**: +#' +#' *DWE01* = 1,2 & +#' *DWE02* = 3,4,5,6,7,8,9 & +#' *DWE03* = 8,9,10,11,12,13 & +#' *DWE04* = 10,11,12,13,14,15 & +#' crowding (*HH01*/*DWE05*) \<= 3 +#' +#' Adequate shelter is calculated from the main dataset +#' classify as habitable when improved/adequate shelter +#' +#' +#' +#' --------- +#' # Compile alll #' Once all variables are correctly calculated, we can compute the final #' variable for impact 2.2 indicator. #' **Numerator**: Population residing in physically safe and secure settlements with access to basic facilities @@ -20,11 +113,7 @@ #' @param datalist A list with all hierarchical data frame for a survey data set. #' format is expected to match the Excel export synchronized from kobo to RILD #' and loaded with kobocruncher::kobo_data() -#' -#' @param mapper a list providing the mapping of the variables used for the -#' calculation - this mapper is potentially to be adjusted in relation with deviation -#' between the the standard XlsForm and the contextualized dataset -#' +#' #' #' @importFrom dplyr mutate case_when #' @importFrom labelled labelled @@ -33,243 +122,306 @@ #' #' @export #' @examples -#' datalist <- kobocruncher::kobo_data( system.file("test.xlsx", +#' ## data, cf example fct_re_map() +#' datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", #' package = "IndicatorCalc")) +#' ## Apply calculation +#' datalist <- impact_2_2(datalist) #' -#' #Healthcare -#' mapper <- list( -#' hierarchy = "main", -#' variablemap = data.frame( -#' label = c( -#' "In general, when anyone in your household is sick, -#' where do they go to seek care?", -#' "How long does it take to go there when you use the mode of transport -#' that you mentioned above?"), -#' variable = c("HEA01", -#' "HEA03"), -#' mappattern = c("HEA01", -#' "HEA03") ), -#' modalitymap = data.frame( -#' variable = c( "HEA01", "HEA01" ), -#' label = c( "Other, specify", "Don't know"), -#' standard = c("96", "98" ), -#' map = c("96", "98" ))) +#' table(datalist[["main"]]$impact2_2, useNA = "ifany") #' -#' datalist <- inter_healthcare(datalist, mapper ) -#' -#' ## Electricity -#' mapper <- list( -#' hierarchy = "main", -#' variablemap = data.frame( -#' label = c("Does this household use anything for lighting?", -#' "What source of electricity is used most of the time in this household?"), -#' variable = c("LIGHT01", -#' "LIGHT03"), -#' mappattern = c("LIGHT01", -#' "LIGHT03") ), -#' modalitymap = data.frame( -#' variable = c( "LIGHT01", -#' "LIGHT03", "LIGHT03", "LIGHT03"), -#' label = c( "yes", -#' "No electricity in household", "Other, specify", "Don't know"), -#' standard = c( "1", -#' "1", "96", "98"), -#' map = c("yes", -#' "1", "96", "98"))) +#' fct_plot_indic_donut(indicator = datalist[["main"]]$impact2_2, +#' iconunicode = "f140") #' -#' datalist <- inter_electricity( datalist =datalist, mapper = mapper ) #' -#' ## Drinking Water -#' ## in the contextualised form - DWA03a has been skipped and all results are in min... -#' ## only manual transformation can adjust this before we use the mapper.. +#' ## Can get the details as well #' -#' datalist[["main"]]$DWA03a <- "1" +#' table(datalist[["main"]]$electricity, useNA = "ifany") +#' fct_plot_indic_donut(indicator = datalist[["main"]]$electricity, +#' iconunicode = "f0e7") #' -#' datalist[["main"]]$DWA03b <- -#' datalist[["main"]]$VulnerabilityScoring.BasicNeeds.DWA03 +#' table(datalist[["main"]]$healthcare, useNA = "ifany") +#' fct_plot_indic_donut(indicator = datalist[["main"]]$healthcare, +#' iconunicode = "f479") #' -#' # now the mapper -#' mapper <- list( -#' hierarchy = "main", -#' variablemap = data.frame( -#' label = c( -#' "What is the main source of drinking water for this household?", -#' "Where is this source located?", -#' "Unit used to measure time to access", -#' "How long does it take to go there, get water, and come back, -#' including waiting time?"), -#' variable = c("DWA01", -#' "DWA02", -#' "DWA03a", -#' "DWA03b" ), -#' mappattern = c("DWA01", -#' "DWA02", -#' "DWA03a", -#' "DWA03b" ) ), -#' modalitymap = data.frame( -#' variable = c("DWA01", "DWA01", "DWA01","DWA01", "DWA01", -#' "DWA02","DWA02", "DWA02", -#' "DWA03a","DWA03a"), -#' label = c( -#' ##DWA01 -#' "Unprotected Dug Well", -#' "Unprotected Spring", -#' "Surface Water (River, Stream, Pond, Dam, Canal)", -#' "Other, specify", -#' "Don't know", -#' ##DWA02 -#' "In Own Dwelling", -#' "In Own Yard/Plot", -#' "Elsewhere", -#' ## DWA03a -#' "Minutes", -#' "Hours" ), -#' standard = c( "7", "9", "13", "96", "98", -#' "1", "2", "3", -#' "1", "2"), -#' map = c( "7", "9", "13", "96", "98", -#' "1", "2", "3", -#' "1", "2") ) -#' ) -#' -#' datalist <- inter_drinkingwater(datalist, mapper ) -#' -#' ##Shelter -#' mapper <- list( -#' hierarchy = "main", -#' variablemap = data.frame( -#' label = c( -#' "What type of dwelling does the household live in?", -#' "Main material of the dwelling floor", -#' "Main material of the roof", -#' "Main material of the exterior walls", -#' "How many separate rooms do the members of your household occupy?", -#' "What is the total number of persons in this household?"), -#' variable = c("DWE01","DWE02","DWE03","DWE04","DWE05", -#' "HH01"), -#' mappattern = c("DWE01","DWE02","DWE03","DWE04","DWE05", -#' "progres_groupsize") ), -#' modalitymap = data.frame( -#' variable = c( "DWE01","DWE01", -#' "DWE02","DWE02","DWE02", -#' "DWE03","DWE03","DWE03","DWE03","DWE03","DWE03", -#' "DWE04","DWE04","DWE04","DWE04","DWE04","DWE04"), -#' label = c( "Apartment", "House", # DWE01 -#' "Earth/sand", "Dung", "Other (Specify)", #DWE02 -#' -#' "Metal/tin", "Wood", "Calamine/Cement fibre", -#' "Ceramic tiles", "Cement", "Roofing shingles",#DWE03 -#' -#' "Cement", "Stone with lime/ cement", "Bricks", -#' "Cement blocks", "Covered adobe", "Wood planks/shingles" # DWE04 -#' ), -#' standard = c( "1","2", -#' "1", "2","96", -#' "8","9","10","11","12","13", -#' "10","11","12","13","14","15"), -#' map = c("1","2", -#' "1", "2","96", -#' "8","9","10","11","12","13", -#' "10","11","12","13","14","15"))) -#' ## Calculate -#' datalist <- inter_shelter(datalist, mapper) +#' +#' table(datalist[["main"]]$drinkingwater, useNA = "ifany") +#' fct_plot_indic_donut(indicator = datalist[["main"]]$drinkingwater, +#' iconunicode = "e006") #' -#' ## and now impact +#' ## Check intermediary variables +#' table(datalist[["main"]]$dwa_cond1, useNA = "ifany") +#' table(datalist[["main"]]$reachableU30, useNA = "ifany") +#' table(datalist[["main"]]$DWA02, useNA = "ifany") +#' table(datalist[["main"]]$dwa_cond2, useNA = "ifany") #' -#' mapper <- list( -#' hierarchy = "main", -#' variablemap = data.frame( -#' label = c("Access to shelter", -#' "Access to electricity", -#' "Access to drinking water", -#' "Access to healthcare"), -#' variable = c("shelter", -#' "electricity", -#' "drinkingwater", -#' "healthcare"), -#' mappattern = c("shelter", -#' "electricity", -#' "drinkingwater", -#' "healthcare") ), -#' modalitymap = data.frame( -#' variable = c( "shelter", "shelter", -#' "electricity", "electricity", -#' "drinkingwater", "drinkingwater", -#' "healthcare","healthcare"), -#' label = c( "Yes","No", -#' "Yes","No", -#' "Yes","No", -#' "Yes","No"), -#' standard = c( "1","0", -#' "1","0", -#' "1","0", -#' "1","0"), -#' map = c("1","0", -#' "1","0", -#' "1","0", -#' "1","0"))) -#' -#' datalist <- impact_2_2(datalist, mapper) +#' # Tabulate +#' table(datalist[["main"]]$dwe01_cat, useNA = "ifany") +#' table(datalist[["main"]]$dwe02_cat, useNA = "ifany") +#' table(datalist[["main"]]$dwe03_cat, useNA = "ifany") +#' table(datalist[["main"]]$dwe04_cat, useNA = "ifany") +#' table(datalist[["main"]]$dwe05_cat, useNA = "ifany") +#' table(datalist[["main"]]$shelter, useNA = "ifany") +#' #plot +#' fct_plot_indic_donut(datalist[["main"]]$shelter, +#' iconunicode = "e54f") #' -#' fct_plot_indic_donut(indicator = datalist[["main"]]$impact2_2, -#' iconunicode = "f140") -impact_2_2 <- function(datalist, - mapper = list( - hierarchy = "main", - variablemap = data.frame( - label = c("Access to shelter", - "Access to electricity", - "Access to drinking water", - "Access to healthcare"), - variable = c("shelter", - "electricity", - "drinkingwater", - "healthcare"), - mappattern = c("shelter", - "electricity", - "drinkingwater", - "healthcare") ), - modalitymap = data.frame( - variable = c( "shelter", "shelter", - "electricity", "electricity", - "drinkingwater", "drinkingwater", - "healthcare","healthcare"), - label = c( "Yes","No", - "Yes","No", - "Yes","No", - "Yes","No"), - standard = c( "1","0", - "1","0", - "1","0", - "1","0"), - map = c("1","0", - "1","0", - "1","0", - "1","0"))) ){ +impact_2_2 <- function(datalist ){ + ## Mapper +mapper = list( + hierarchy = "main", + variablemap = data.frame( + label = +c("1. When anyone in your household is sick, where do they go to seek care?", +"2. How do you reach this facility if you need to seek care?", +"3. How long does it take to go there when you use the mode of transport that you mentioned above?", +"4. Does this household use anything for lighting?", "5. What does this household use most of the time as energy for lighting, or as a light source?", +"6. What source of electricity is used most of the time in this household?", +"1. What is the main source of drinking water for this household?", +"2. Where is this ${source} - ${source2} located?", "Unit", "Time", +"4. In the last 30 days, has there been any time when your household did not have sufficient quantities of drinking water when needed?", +"1.What type of dwelling does the household live in?", "2. Main material of the dwelling floor", +"3. Main material of the roof.", "4. Main material of the exterior walls", +"5. How many separate rooms do the members of your household occupy?", +"8. Does your household pay any rent?", "9. Can your household generally afford to pay the rent without any major financial distress?", +"What is the total number of persons in this household ? (including the respondent)" +) + , + variable = +c("HEA01", "HEA02", "HEA03", "LIGHT01", "LIGHT02", "LIGHT03", +"DWA01", "DWA02", "DWA03a", "DWA03b", "DWA04", "DWE01", "DWE02", +"DWE03", "DWE04", "DWE05", "DWE08", "DWE09", "HH01") + ), + modalitymap = data.frame( + variable = +c("HEA01", "HEA01", "HEA01", "HEA01", "HEA01", "HEA01", "HEA01", +"HEA02", "HEA02", "HEA02", "HEA02", "HEA03", "LIGHT01", "LIGHT01", +"LIGHT02", "LIGHT02", "LIGHT02", "LIGHT02", "LIGHT02", "LIGHT02", +"LIGHT02", "LIGHT02", "LIGHT02", "LIGHT02", "LIGHT02", "LIGHT02", +"LIGHT02", "LIGHT02", "LIGHT03", "LIGHT03", "LIGHT03", "LIGHT03", +"LIGHT03", "LIGHT03", "LIGHT03", "LIGHT03", "LIGHT03", "LIGHT03", +"DWA01", "DWA01", "DWA01", "DWA01", "DWA01", "DWA01", "DWA01", +"DWA01", "DWA01", "DWA01", "DWA01", "DWA01", "DWA01", "DWA01", +"DWA01", "DWA01", "DWA01", "DWA01", "DWA02", "DWA02", "DWA02", +"DWA03a", "DWA03a", "DWA03b", "DWA04", "DWA04", "DWE01", "DWE01", +"DWE01", "DWE01", "DWE01", "DWE01", "DWE01", "DWE01", "DWE01", +"DWE01", "DWE02", "DWE02", "DWE02", "DWE02", "DWE02", "DWE02", +"DWE02", "DWE02", "DWE02", "DWE02", "DWE03", "DWE03", "DWE03", +"DWE03", "DWE03", "DWE03", "DWE03", "DWE03", "DWE03", "DWE03", +"DWE03", "DWE03", "DWE03", "DWE03", "DWE04", "DWE04", "DWE04", +"DWE04", "DWE04", "DWE04", "DWE04", "DWE04", "DWE04", "DWE04", +"DWE04", "DWE04", "DWE04", "DWE04", "DWE04", "DWE04", "DWE05", +"DWE08", "DWE08", "DWE09", "DWE09", "DWE09", "DWE09", "HH01") + , + label = +c("NGO facility (charity, faith-based organization)", "UNHCR Health Partner (Caritas, Save the Children)", +"Public Clinics / Hospitals", "Private Clinics / Hospitals", +"Pharmacy", "Other, specify", "Don't know", "By walk", "Private car", +"Public transport (bus, boat)", "Other, specify", NA, "Yes", +"No", "Electricity (including solar mini-grids, hybrid mini-grids and national grid)", +"Electricity (from diesel generator)", "Solar home system", "Solar-powered lantern or flashlight", +"Rechargeable flashlight, mobile, torch or lantern", "Battery powered flashlight, torch or lantern", +"Biogas lamp", "LPG lamp", "Gasoline lamp", "Kerosene or paraffin lamp", +"Oil lamp", "Candle", "Open fire", "Other, specify", "No electricity in household", +"National grid connection from [COMPANY]", "Local mini grid", +"Solar home system", "Solar lantern", "Electric generator", "Rechargeable battery", +"Dry cell battery / torch", "Other, specify", "Don't know", "Piped Into Dwelling", +"Piped Into Yard/Plot", "Piped To Neighbor", "Public Tap/Standpipe", +"Tube Well/Borehole", "Protected Dug Well", "Unprotected Dug Well", +"Protected Spring", "Unprotected Spring", "Rain Water Collection", +"Tanker Truck/Water Vendor", "Cart With Small Tank/Drum", "Surface Water (River, Stream, Pond, Dam, Canal)", +"Bottled Water", "Sachet Water", "Water Kiosk", "Other (Specify)", +"Don't Know", "In Own Dwelling", "In Own Yard/Plot", "Elsewhere", +"Minutes", "Hours", NA, "Yes", "No", "Apartment", "House", "Tent", +"Caravan", "Collective Center", "Worksite/Unfinished Home/ Abandoned Building", +"Farm Building", "School, mosque, church or other religious building", +"Garage, shop, workshop, or other structure not meant as residential space", +"Other (Specify)", "Earth/sand", "Dung", "Wood planks", "Palm/bamboo", +"Parquet or polished wood", "Vinyl or asphalt strips", "Ceramic tiles", +"Cement", "Carpet", "Other (Specify)", "No roof", "Thatch/Palm leaf", +"Sod", "Rustic mat", "Palm/bamboo", "Wood planks", "Cardboard", +"Metal/tin", "Wood", "Calamine/Cement fibre", "Ceramic tiles", +"Cement", "Roofing shingles", "Other (Specify)", "No walls", +"Cane/Palm/ Trunks", "Dirt", "Bamboo with mud", "Stone with mud", +"Uncovered adobe", "Plywood", "Cardboard", "Reused wood", "Cement", +"Stone with lime/ cement", "Bricks", "Cement blocks", "Covered adobe", +"Wood planks/shingles", "Other (Specify)", NA, "Yes", "No", "Always", +"Often", "Sometimes", "Never", NA) + , + standard = +c("1", "2", "3", "4", "5", "96", "98", "1", "2", "3", "96", NA, +"1", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", +"11", "12", "13", "96", "1", "2", "3", "4", "5", "6", "7", "8", +"96", "98", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", +"11", "12", "13", "14", "15", "16", "96", "98", "1", "2", "3", +"1", "2", NA, "1", "0", "1", "2", "3", "4", "5", "6", "7", "8", +"9", "96", "1", "2", "3", "4", "5", "6", "7", "8", "9", "96", +"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", +"13", "96", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", +"11", "12", "13", "14", "15", "96", NA, "1", "0", "1", "2", "3", +"4", NA) + )) ## So first we check that we have what we need in the data set based on the mapper check_map <- fct_check_map(datalist = datalist, mapper = mapper) + if ( check_map == FALSE) { + cat( "There are missing data requirement to calculate Indicator Impact 2.2") + } else { + + ## now pull the calculation... + + ## Electricity ######## + datalist[["main"]]$electricity <- dplyr::case_when( + datalist[["main"]]$LIGHT01 == 1 & + (as.integer(datalist[["main"]]$LIGHT03) != 1 | + as.integer(datalist[["main"]]$LIGHT03)!= 96 | + as.integer(datalist[["main"]]$LIGHT03) != 98) ~ 1, + TRUE ~ 0 ) + + datalist[["main"]]$electricity <- labelled::labelled( + datalist[["main"]]$electricity, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Access to electricity") + ## healthcare ######## + datalist[["main"]]$healthcare <- + dplyr::case_when(( + as.integer(datalist[["main"]]$HEA01) != 96 | + as.integer(datalist[["main"]]$HEA01) != 98) & + as.integer(datalist[["main"]]$HEA03) <= 60 ~ 1, + TRUE ~ 0) + + datalist[["main"]]$healthcare <- labelled::labelled(datalist[["main"]]$healthcare, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Access to healthcare facility") + ## Drinking Water ################# + datalist[["main"]]$time_DWA <- dplyr::case_when( + as.integer(datalist[["main"]]$DWA03a) == 1 ~ 1, + as.integer(datalist[["main"]]$DWA03a) == 2 ~ 60) + #convert hour into minutes + # datalist[["main"]]$time_DWA <- as.numeric(datalist[["main"]]$time_DWA) + datalist[["main"]]$time_tot <- datalist[["main"]]$time_DWA * + as.numeric(datalist[["main"]]$DWA03b) + + datalist[["main"]]$reachableU30 <- dplyr::case_when( + datalist[["main"]]$time_tot > 30 ~ 0, + TRUE ~ 1) + # reachable under 30 minutes + datalist[["main"]]$dwa_cond1 <- dplyr::case_when( + as.integer(datalist[["main"]]$DWA01) != 7 | + as.integer(datalist[["main"]]$DWA01) != 9 | + as.integer(datalist[["main"]]$DWA01) != 13 | + as.integer(datalist[["main"]]$DWA01) != 96 | + as.integer(datalist[["main"]]$DWA01) != 98 ~ 1, + TRUE ~ 0) + # improved source + datalist[["main"]]$dwa_cond2 <- dplyr::case_when( + + #UNDER30 MIN + as.integer(datalist[["main"]]$reachableU30) == 1 & + as.integer(datalist[["main"]]$DWA02) == 3 ~ 1, + + # NOT UNDER30 MIN + as.integer(datalist[["main"]]$reachableU30) == 0 & + as.integer(datalist[["main"]]$DWA02) == 3 ~ 0, + + # in the dwelling/yard/plot + as.integer(datalist[["main"]]$DWA02) == 1 | + as.integer(datalist[["main"]]$DWA02) == 2 ~ 1, + TRUE ~ NA ) + + datalist[["main"]]$drinkingwater <- dplyr::case_when( + (as.integer(datalist[["main"]]$dwa_cond1) == 1 & + as.integer(datalist[["main"]]$dwa_cond2) == 1 ) ~ 1, + TRUE ~ 0) + + datalist[["main"]]$drinkingwater <- labelled::labelled(datalist[["main"]]$drinkingwater, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Access to drinking water") + + + ## shelter ######### + #Only apartment and house + datalist[["main"]]$dwe01_cat <- + dplyr::case_when( + ( as.integer(datalist[["main"]]$DWE01) == 1 | + as.integer(datalist[["main"]]$DWE01) == 2) ~ 1, + TRUE ~ 0 ) + ## unimproved floor when earth,sand,clay,mud, dung or other + datalist[["main"]]$dwe02_cat<- dplyr::case_when( + (as.integer(datalist[["main"]]$DWE02) == 1 | + as.integer(datalist[["main"]]$DWE02) == 2 | + as.integer(datalist[["main"]]$DWE02) == 96) ~ 0, + TRUE ~ 1 ) + ## unimproved roof all options except metal,wood,ceramic tiles, cement, roofing shingles/sheets + datalist[["main"]]$dwe03_cat<- dplyr::case_when( + (as.integer(datalist[["main"]]$DWE03) == 8 | + as.integer(datalist[["main"]]$DWE03) == 9 | + as.integer(datalist[["main"]]$DWE03) == 10 | + as.integer(datalist[["main"]]$DWE03) == 11 | + as.integer(datalist[["main"]]$DWE03) == 12 | + as.integer(datalist[["main"]]$DWE03) == 13 ) ~ 1 , + TRUE ~ 0) + ## improved wall: cement,stone,bricks,cement blocks, covered adobe, wood planks + datalist[["main"]]$dwe04_cat<- dplyr::case_when( + (as.integer(datalist[["main"]]$DWE04) == 10| + as.integer(datalist[["main"]]$DWE04) == 11 | + as.integer(datalist[["main"]]$DWE04) == 12 | + as.integer(datalist[["main"]]$DWE04) == 13 | + as.integer(datalist[["main"]]$DWE04) == 14 | + as.integer(datalist[["main"]]$DWE04) == 15 ) ~ 1, + TRUE ~ 0) + + ## Calculate crowding index - overcrowded when more than 3 persons + datalist[["main"]]$crowding <- as.numeric(datalist[["main"]]$HH01) / + as.numeric(datalist[["main"]]$DWE05) + + datalist[["main"]]$dwe05_cat<- dplyr::case_when( ##if crowding <= 3, not overcrowded + datalist[["main"]]$crowding <= 3 ~ 1, + TRUE ~ 0) + + ####Calculate if all 5 conditions are met for adequate shelter + ##dwe01_cat / dwe02_cat / dwe03_cat / dwe04_cat / dwe05_cat + + datalist[["main"]]$shelter<- dplyr::case_when( + as.integer(datalist[["main"]]$dwe01_cat) == 0 | + as.integer(datalist[["main"]]$dwe02_cat) == 0 | + as.integer(datalist[["main"]]$dwe03_cat) == 0 | + as.integer(datalist[["main"]]$dwe04_cat) == 0 | + as.integer(datalist[["main"]]$dwe05_cat) == 0 ~ 0, + + as.integer(datalist[["main"]]$dwe01_cat) == 1 & + as.integer(datalist[["main"]]$dwe02_cat) == 1 & + as.integer(datalist[["main"]]$dwe03_cat) == 1 & + as.integer(datalist[["main"]]$dwe04_cat) == 1 & + as.integer(datalist[["main"]]$dwe05_cat) == 1 ~ 1) + + datalist[["main"]]$shelter <- labelled::labelled( datalist[["main"]]$shelter, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Access to adequate shelter") + + ## Compile alll #### datalist[["main"]]$impact2_2 <- dplyr::case_when( - datalist[["main"]]$shelter=="0" | - datalist[["main"]]$electricity=="0" | - datalist[["main"]]$drinkingwater=="0" | - datalist[["main"]]$healthcare=="0" ~ "0", + as.integer(datalist[["main"]]$shelter) == 0 | + as.integer(datalist[["main"]]$electricity) == 0 | + as.integer(datalist[["main"]]$drinkingwater) == 0 | + as.integer(datalist[["main"]]$healthcare) == 0 ~ 0, - datalist[["main"]]$shelter=="1" & - datalist[["main"]]$electricity=="1" & - datalist[["main"]]$drinkingwater=="1" & - datalist[["main"]]$healthcare=="1" ~ "1") + as.integer(datalist[["main"]]$shelter) == 1 & + as.integer(datalist[["main"]]$electricity) == 1 & + as.integer(datalist[["main"]]$drinkingwater) == 1 & + as.integer(datalist[["main"]]$healthcare) == 1 ~ 1) datalist[["main"]]$impact2_2 <- labelled::labelled( datalist[["main"]]$impact2_2, labels =c( - "Yes"="1", - "No"="0" + "Yes" = 1, + "No" = 0 ), - label="Proportion of PoCs residing in physically safe and secure settlements with access to basic facilities") + label = "Proportion of PoCs residing in physically safe and secure settlements with access to basic facilities") -return(datalist) + } + return(datalist) } diff --git a/R/inter_drinkingwater.R b/R/inter_drinkingwater.R deleted file mode 100644 index 6c3f7f7..0000000 --- a/R/inter_drinkingwater.R +++ /dev/null @@ -1,213 +0,0 @@ -# WARNING - Generated by {fusen} from dev/indicators.Rmd: do not edit by hand - -#' inter_drinkingwater -#' -#' Access to clean drinking water is essential for a person's survival and well -#' being and a precursor for achieving protection outcomes related to health, -#' education and economic developed. -#' The calculation for access drinking water is linked to SGD Indicator -#' [6.1.1](https://unstats.un.org/sdgs/metadata/files/Metadata-06-01-01.pdf). -#' The questionnaire module and the analysis guidance is taken from [UNICEF MICS6](https://mics.unicef.org/tools#analysis). -#' -#' **Numerator**: Population using improved sources of drinking water either in their dwelling/yard/plot or within 30 minutes round trip collection time -#' -#' **Denominator**: Total population -#' -#' **Formula**: -#' *DWA03* < 30 (under 30 minutes), & -#' *DWA01* != 7,9,13,96,98 & -#' *DWA02* != 3 -#' -#' This basic service is calculated from the main dataset -#' There are three conditions as below improved source, -#' in dwelling/yard/plot and reachable under 30 minutes -#' -#' @param datalist A list with all hierarchical data frame for a survey data set. -#' format is expected to match the Excel export synchronized from kobo to RILD -#' and loaded with kobocruncher::kobo_data() -#' -#' @param mapper a list providing the mapping of the variables used for the -#' calculation - this mapper is potentially to be adjusted in relation with deviation -#' between the the standard XlsForm and the contextualized dataset -#' -#' -#' @importFrom dplyr mutate case_when -#' @importFrom labelled labelled -#' -#' @return new calculated variable - appended or not... -#' -#' @export -#' @examples -#' datalist <- kobocruncher::kobo_data( system.file("test.xlsx", -#' package = "IndicatorCalc")) -#' -#' ## in the contextualised form - DWA03a has been skipped and all results are in min... -#' ## only manual transformation can adjust this before we use the mapper.. -#' -#' datalist[["main"]]$DWA03a <- "1" -#' -#' datalist[["main"]]$DWA03b <- -#' datalist[["main"]]$VulnerabilityScoring.BasicNeeds.DWA03 -#' -#' # now the mapper -#' mapper <- list( -#' hierarchy = "main", -#' variablemap = data.frame( -#' label = c( -#' "What is the main source of drinking water for this household?", -#' "Where is this source located?", -#' "Unit used to measure time to access", -#' "How long does it take to go there, wait get water, and come back?"), -#' variable = c("DWA01", -#' "DWA02", -#' "DWA03a", -#' "DWA03b" ), -#' mappattern = c("DWA01", -#' "DWA02", -#' "DWA03a", -#' "DWA03b" ) ), -#' modalitymap = data.frame( -#' variable = c("DWA01", "DWA01", "DWA01","DWA01", "DWA01", -#' "DWA02","DWA02", "DWA02", -#' "DWA03a","DWA03a"), -#' label = c( -#' ##DWA01 -#' "Unprotected Dug Well", -#' "Unprotected Spring", -#' "Surface Water (River, Stream, Pond, Dam, Canal)", -#' "Other, specify", -#' "Don't know", -#' ##DWA02 -#' "In Own Dwelling", -#' "In Own Yard/Plot", -#' "Elsewhere", -#' ## DWA03a -#' "Minutes", -#' "Hours" ), -#' standard = c( "7", "9", "13", "96", "98", -#' "1", "2", "3", -#' "1", "2"), -#' map = c( "7", "9", "13", "96", "98", -#' "1", "2", "3", -#' "1", "2") ) -#' ) -#' -#' datalist <- inter_drinkingwater(datalist, mapper ) -#' -#' ## Indicator summary -#' table(datalist[["main"]]$drinkingwater, useNA = "ifany") -#' fct_plot_indic_donut(indicator = datalist[["main"]]$drinkingwater, -#' iconunicode = "e006") -#' -#' ## Check auxilliary -#' table(datalist[["main"]]$dwa_cond1, useNA = "ifany") -#' table(datalist[["main"]]$reachableU30, useNA = "ifany") -#' table(datalist[["main"]]$DWA02, useNA = "ifany") -#' table(datalist[["main"]]$dwa_cond2, useNA = "ifany") -inter_drinkingwater <- function( - datalist, - mapper = list( - hierarchy = "main", - variablemap = data.frame( - label = c( - "What is the main source of drinking water for this household?", - "Where is this source located?", - "Unit used to measure time to access", - "How long does it take to go there, get water, and come back, - including waiting time?"), - variable = c("DWA01", - "DWA02", - "DWA03a", - "DWA03b" ), - mappattern = c("DWA01", - "DWA02", - "DWA03a", - "DWA03b" ) ), - modalitymap = data.frame( - variable = c("DWA01", - "DWA01", - "DWA01", - "DWA01", - "DWA01", - "DWA02", - "DWA02", - "DWA02", - "DWA03a", - "DWA03a"), - label = c( - ##DWA01 - "Unprotected Dug Well", - "Unprotected Spring", - "Surface Water (River, Stream, Pond, Dam, Canal)", - "Other, specify", - "Don't know", - ##DWA02 - "In Own Dwelling", - "In Own Yard/Plot", - "Elsewhere", - ## DWA03 - "Minutes", "Hours" ), - standard = c( "7", "9", "13", "96", "98", - "1", "2", "3", - "1", "2"), - map = c( "7", "9", "13", "96", "98", - "1", "2", "3", - "1", "2"))) - ){ - - ## So first we check that we have what we need in the data set based on the mapper - check_map <- fct_check_map(datalist = datalist, - mapper = mapper) - # if (check_map == FALSE) { - datalist <- fct_re_map(datalist = datalist, - mapper = mapper) - # } - -datalist[["main"]]$time_DWA <- dplyr::case_when( - datalist[["main"]]$DWA03a=="1"~ 1, - datalist[["main"]]$DWA03a=="2"~ 60) #convert hour into minutes -# datalist[["main"]]$time_DWA <- as.numeric(datalist[["main"]]$time_DWA) - -datalist[["main"]]$time_tot <- datalist[["main"]]$time_DWA * - as.numeric(datalist[["main"]]$DWA03b) - -datalist[["main"]]$reachableU30 <- dplyr::case_when( - datalist[["main"]]$time_tot > 30 ~ "0", - TRUE ~ "1") # reachable under 30 minutes - -datalist[["main"]]$dwa_cond1 <- dplyr::case_when( - datalist[["main"]]$DWA01!="7" | - datalist[["main"]]$DWA01 !="9" | - datalist[["main"]]$DWA01 != "13" | - datalist[["main"]]$DWA01 != "96" | - datalist[["main"]]$DWA01 !="98" ~ "1", - TRUE ~ "0") # improved source - -datalist[["main"]]$dwa_cond2= dplyr::case_when( - - #UNDER30 MIN - datalist[["main"]]$reachableU30 == "1" & - datalist[["main"]]$DWA02 == "3" ~ "1", - - # NOT UNDER30 MIN - datalist[["main"]]$reachableU30 == "0" & - datalist[["main"]]$DWA02 == "3" ~ "0", - - # in the dwelling/yard/plot - datalist[["main"]]$DWA02 == "1" | - datalist[["main"]]$DWA02 == "2" ~ "1", - TRUE ~ NA_character_ ) - -datalist[["main"]]$drinkingwater <- dplyr::case_when( - (datalist[["main"]]$dwa_cond1 == "1" & - datalist[["main"]]$dwa_cond2 == "1" ) ~ "1", - TRUE ~ "0") - -datalist[["main"]]$drinkingwater = labelled::labelled(datalist[["main"]]$drinkingwater, - labels = c( - "Yes" = "1", - "No" = "0" - ), - label = "Access to drinking water") - return(datalist) -} diff --git a/R/inter_electricity.R b/R/inter_electricity.R deleted file mode 100644 index cbb8ce6..0000000 --- a/R/inter_electricity.R +++ /dev/null @@ -1,107 +0,0 @@ -# WARNING - Generated by {fusen} from dev/indicators.Rmd: do not edit by hand - -#' inter_electricity -#' -#' Households lighting provides a sense of safety and security within and outside -#' the households after sunset. -#' -#' If households lack access to electricity, especially for lighting and -#' connectivity, this affects the occupants' security and limits their -#' opportunities for socialization, learning and self-reliance. -#' -#' **Numerator**: Population with access to electricity, especially for lighting -#' and connectivity -#' -#' **Denominator**: Total population -#' -#' **Formula**: *LIGHT01* = 1 & *LIGHT03* != 1, 96, 98 -#' -#' -#' @param datalist A list with all hierarchical data frame for a survey data set. -#' format is expected to match the Excel export synchronized from kobo to RILD -#' and loaded with kobocruncher::kobo_data() -#' -#' @param mapper a list providing the mapping of the variables used for the -#' calculation - this mapper is potentially to be adjusted in relation with deviation -#' between the the standard XlsForm and the contextualized dataset -#' -#' -#' @importFrom dplyr mutate case_when -#' @importFrom labelled labelled -#' -#' @return new calculated variable - appended or not... -#' -#' @export -#' @examples -#' datalist <- kobocruncher::kobo_data( system.file("test.xlsx", -#' package = "IndicatorCalc")) -#' mapper <- list( -#' hierarchy = "main", -#' variablemap = data.frame( -#' label = c("Does this household use anything for lighting?", -#' "What source of electricity is used most of the time in this household?"), -#' variable = c("LIGHT01", -#' "LIGHT03"), -#' mappattern = c("LIGHT01", -#' "LIGHT03") ), -#' modalitymap = data.frame( -#' variable = c( "LIGHT01", -#' "LIGHT03", "LIGHT03", "LIGHT03"), -#' label = c( "yes", -#' "No electricity in household", "Other, specify", "Don't know"), -#' standard = c( "1", -#' "1", "96", "98"), -#' map = c("yes", -#' "1", "96", "98"))) -#' -#' datalist <- inter_electricity( datalist =datalist, mapper = mapper ) -#' -#' -#' table(datalist[["main"]]$electricity) -#' fct_plot_indic_donut(indicator = datalist[["main"]]$electricity, -#' iconunicode = "f0e7") -#' -inter_electricity <- function(datalist, - mapper = list( - hierarchy = "main", - variablemap = data.frame( - label = c("Does this household use anything for lighting?", - "What source of electricity is used most of the time in this household?"), - variable = c("LIGHT01", - "LIGHT03"), - mappattern = c("LIGHT01", - "LIGHT03") ), - modalitymap = data.frame( - variable = c( "LIGHT01", - "LIGHT03", "LIGHT03", "LIGHT03"), - label = c( "yes", - "No electricity in household", "Other, specify", "Don't know"), - standard = c( "1", - "1", "96", "98"), - map = c("1", - "1", "96", "98"))) ) { - - ## So first we check that we have what we need in the data set based on the mapper - fct_check_map(datalist = datalist, - mapper = mapper) - #if (check_map == FALSE) { - datalist <- fct_re_map(datalist = datalist, - mapper = mapper) - # } - - ## now pull the calculation... - datalist[["main"]]$electricity <- dplyr::case_when(datalist[["main"]]$LIGHT01=="1" & - (datalist[["main"]]$LIGHT03!="1" | - datalist[["main"]]$LIGHT03!="96" | - datalist[["main"]]$LIGHT03!="98") ~ "1", - TRUE ~ "0" ) - ## Apply Label - datalist[["main"]]$electricity <- labelled::labelled( - datalist[["main"]]$electricity, - labels = c( - "Yes" = "1", - "No" = "0" - ), - label = "Access to electricity") - return(datalist) -} diff --git a/R/inter_healthcare.R b/R/inter_healthcare.R deleted file mode 100644 index 7cdf4bf..0000000 --- a/R/inter_healthcare.R +++ /dev/null @@ -1,97 +0,0 @@ -# WARNING - Generated by {fusen} from dev/indicators.Rmd: do not edit by hand - -#' inter_healthcare -#' -#' Access to healthcare depends on availability of healthcare, including physical -#' reach, acceptability and affordability for all. -#' For this indicator, the focus is on the availability of healthcare system. -#' According to [The Sphere Handbook](https://spherestandards.org/wp-content/uploads/Sphere-Handbook-2018-EN.pdf), -#' primary healthcare facility should be accessible within one hour's walk from dwellings. -#' **Numerator**: Population that can reach a primary healthcare facility within one hour from dwellings -#' **Denominator**: Total population -#' **Formula**: *HEA01* != 96, 98 & *HEA03* <= 60 -#' (reachable within one hour/60 minutes) -#' -#' -#' @param datalist A list with all hierarchical data frame for a survey data set. -#' format is expected to match the Excel export synchronized from kobo to RILD -#' and loaded with kobocruncher::kobo_data() -#' -#' @param mapper a list providing the mapping of the variables used for the -#' calculation - this mapper is potentially to be adjusted in relation with deviation -#' between the the standard XlsForm and the contextualized dataset -#' -#' @importFrom dplyr mutate case_when -#' @importFrom labelled labelled -#' @importFrom cli cli_alert_info -#' -#' @return new calculated variable - appended or not... -#' -#' @export -#' -#' @examples -#' datalist <- kobocruncher::kobo_data( system.file("test.xlsx", -#' package = "IndicatorCalc")) -#' -#' mapper <- list( -#' hierarchy = "main", -#' variablemap = data.frame( -#' label = c( -#' "In general, when anyone in your household is sick, where do they go to seek care?", -#' "How long it takes to go there when you use the mode of transport mentioned above?"), -#' variable = c("HEA01", -#' "HEA03"), -#' mappattern = c("HEA01", -#' "HEA03") ), -#' modalitymap = data.frame( -#' variable = c( "HEA01", "HEA01" ), -#' label = c( "Other, specify", "Don't know"), -#' standard = c("96", "98" ), -#' map = c("96", "98" ))) -#' -#' datalist <- inter_healthcare(datalist, mapper ) -#' -#' table(datalist[["main"]]$healthcare) -#' fct_plot_indic_donut(indicator = datalist[["main"]]$healthcare, -#' iconunicode = "f479") -#' -inter_healthcare <- function(datalist, - mapper = list( - hierarchy = "main", - variablemap = data.frame( - label = c( - "In general, when anyone in your household is sick, where do they go to seek care?", - "How long does it take to go there when you use the mode of transport that you mentioned above?"), - variable = c("HEA01", - "HEA03"), - mappattern = c("HEA01", - "HEA03") ), - modalitymap = data.frame( - variable = c( "HEA01", "HEA01" ), - label = c( "Other, specify", "Don't know"), - standard = c("96", "98" ), - map = c("96", "98" ))) ){ - - ## So first we check that we have what we need in the data set based on the mapper - check_map <- fct_check_map(datalist = datalist, - mapper = mapper) - #if (check_map == FALSE) { - datalist <- fct_re_map(datalist = datalist, - mapper = mapper) - #} - - -datalist[["main"]]$healthcare <- - dplyr::case_when((datalist[["main"]]$HEA01!="96" | - datalist[["main"]]$HEA01!="98") & - datalist[["main"]]$HEA03 <= 60 ~ "1", - TRUE ~ "0") - -datalist[["main"]]$healthcare = labelled::labelled(datalist[["main"]]$healthcare, - labels = c( - "Yes" = "1", - "No" = "0"), - label = "Access to healthcare facility") - -return(datalist) -} diff --git a/R/inter_shelter.R b/R/inter_shelter.R deleted file mode 100644 index 8d92127..0000000 --- a/R/inter_shelter.R +++ /dev/null @@ -1,211 +0,0 @@ -# WARNING - Generated by {fusen} from dev/indicators.Rmd: do not edit by hand - -#' inter_shelter -#' -#' The right to access adequate housing is protected by international law. -#' The concept of "adequacy" means that housing is more than four walls and a -#' roof as indicated in [The Sphere Handbook](https://spherestandards.org/wp-content/uploads/Sphere-Handbook-2018-EN.pdf). -#' Habitable housing primarily refers to the fact that the housing should provide -#' protection from cold, damp, heat, rain, wind, and other threats to health, -#' structural hazards, and disease vectors and it should not be overcrowded. -#' As shelter/housing is primarily a contextual element, there may be -#' discrepancies from country to country on how this data is measured. -#' -#' Adequate shelter is measured based on having improved material for the -#' dwelling as indicated in [DHS](https://dhsprogram.com/pubs/pdf/AS61/AS61.pdf) -#' publication on housing conditions which is also used by [MICS6](https://mics.unicef.org/tools). -#' -#' Overcrowding is also used which occurs if there are more than three people -#' per habitable room as defined by [UN-Habitat](https://www.ncbi.nlm.nih.gov/books/NBK535289/table/ch3.tab2/). -#' -#' **Numerator**: Population that have access to adequate housing -#' **Denominator**: Total population -#' -#' **Formula**: -#' -#' *DWE01* = 1,2 & -#' *DWE02* = 3,4,5,6,7,8,9 & -#' *DWE03* = 8,9,10,11,12,13 & -#' *DWE04* = 10,11,12,13,14,15 & -#' crowding (*HH01*/*DWE05*) \<= 3 -#' -#' Adequate shelter is calculated from the main dataset -#' classify as habitable when improved/adequate shelter -#' -#' @param datalist A list with all hierarchical data frame for a survey data set. -#' format is expected to match the Excel export synchronized from kobo to RILD -#' and loaded with kobocruncher::kobo_data() -#' -#' @param mapper a list providing the mapping of the variables used for the -#' calculation - this mapper is potentially to be adjusted in relation with deviation -#' between the the standard XlsForm and the contextualized dataset -#' -#' @importFrom dplyr mutate case_when -#' @importFrom labelled labelled -#' -#' @return new calculated variable - appended or not... -#' -#' @export -#' @examples -#' datalist <- kobocruncher::kobo_data( system.file("test.xlsx", -#' package = "IndicatorCalc")) -#' -#' mapper <- list( -#' hierarchy = "main", -#' variablemap = data.frame( -#' label = c( -#' "What type of dwelling does the household live in?", -#' "Main material of the dwelling floor", -#' "Main material of the roof", -#' "Main material of the exterior walls", -#' "How many separate rooms do the members of your household occupy?", -#' "What is the total number of persons in this household?"), -#' variable = c("DWE01","DWE02","DWE03","DWE04","DWE05", -#' "HH01"), -#' mappattern = c("DWE01","DWE02","DWE03","DWE04","DWE05", -#' "progres_groupsize") ), -#' modalitymap = data.frame( -#' variable = c( "DWE01","DWE01", -#' "DWE02","DWE02","DWE02", -#' "DWE03","DWE03","DWE03","DWE03","DWE03","DWE03", -#' "DWE04","DWE04","DWE04","DWE04","DWE04","DWE04"), -#' label = c( "Apartment", "House", # DWE01 -#' "Earth/sand", "Dung", "Other (Specify)", #DWE02 -#' -#' "Metal/tin", "Wood", "Calamine/Cement fibre", -#' "Ceramic tiles", "Cement", "Roofing shingles",#DWE03 -#' -#' "Cement", "Stone with lime/ cement", "Bricks", -#' "Cement blocks", "Covered adobe", "Wood planks/shingles" # DWE04 -#' ), -#' standard = c( "1","2", -#' "1", "2","96", -#' "8","9","10","11","12","13", -#' "10","11","12","13","14","15"), -#' map = c("1","2", -#' "1", "2","96", -#' "8","9","10","11","12","13", -#' "10","11","12","13","14","15"))) -#' ## Calculate -#' datalist <- inter_shelter(datalist, mapper) -#' # Tabulate -#' table(datalist[["main"]]$dwe01_cat) -#' table(datalist[["main"]]$dwe02_cat) -#' table(datalist[["main"]]$dwe03_cat) -#' table(datalist[["main"]]$dwe04_cat) -#' table(datalist[["main"]]$dwe05_cat) -#' table(datalist[["main"]]$shelter) -#' #plot -#' fct_plot_indic_donut(datalist[["main"]]$shelter, -#' iconunicode = "e54f") -inter_shelter <- function(datalist, - mapper = list( - hierarchy = "main", - variablemap = data.frame( - label = c( - "What type of dwelling does the household live in?", -"Main material of the dwelling floor", -"Main material of the roof", -"Main material of the exterior walls", -"How many separate rooms do the members of your household occupy?", -"What is the total number of persons in this household?"), - variable = c("DWE01","DWE02","DWE03","DWE04","DWE05", - "HH01"), - mappattern = c("DWE01","DWE02","DWE03","DWE04","DWE05", - "HH01") ), - modalitymap = data.frame( - variable = c( "DWE01","DWE01", - "DWE02","DWE02","DWE02", - "DWE03","DWE03","DWE03","DWE03","DWE03","DWE03", - "DWE04","DWE04","DWE04","DWE04","DWE04","DWE04"), - label = c( "Apartment", "House", # DWE01 - "Earth/sand", "Dung", "Other (Specify)", #DWE02 - - "Metal/tin", "Wood", "Calamine/Cement fibre", - "Ceramic tiles", "Cement", "Roofing shingles",#DWE03 - - "Cement", "Stone with lime/ cement", "Bricks", - "Cement blocks", "Covered adobe", "Wood planks/shingles" # DWE04 - ), - standard = c( "1","2", - "1", "2","96", - "8","9","10","11","12","13", - "10","11","12","13","14","15"), - map = c("1","2", - "1", "2","96", - "8","9","10","11","12","13", - "10","11","12","13","14","15"))) ){ - - ## So first we check that we have what we need in the data set based on the mapper - check_map <- fct_check_map(datalist = datalist, - mapper = mapper) - # if (check_map == FALSE) { - datalist <- fct_re_map(datalist = datalist, - mapper = mapper) - # } - - #Only apartment and house -datalist[["main"]]$dwe01_cat <- - dplyr::case_when( - (datalist[["main"]]$DWE01=="1" | - datalist[["main"]]$DWE01=="2") ~ "1", - TRUE ~ "0" ) -## unimproved floor when earth,sand,clay,mud, dung or other -datalist[["main"]]$dwe02_cat= dplyr::case_when( - (datalist[["main"]]$DWE02=="1" | - datalist[["main"]]$DWE02=="2" | - datalist[["main"]]$DWE02=="96") ~ "0", - TRUE ~ "1" ) -## unimproved roof all options except metal,wood,ceramic tiles, cement, roofing shingles/sheets -datalist[["main"]]$dwe03_cat= dplyr::case_when( - (datalist[["main"]]$DWE03=="8" | - datalist[["main"]]$DWE03=="9" | - datalist[["main"]]$DWE03=="10" | - datalist[["main"]]$DWE03=="11" | - datalist[["main"]]$DWE03=="12" | - datalist[["main"]]$DWE03=="13" ) ~ "1" , - TRUE ~ "0") - -## improved wall: cement,stone,bricks,cement blocks, covered adobe, wood planks -datalist[["main"]]$dwe04_cat= dplyr::case_when( - (datalist[["main"]]$DWE04=="10"| - datalist[["main"]]$DWE04=="11"| - datalist[["main"]]$DWE04=="12"| - datalist[["main"]]$DWE04=="13"| - datalist[["main"]]$DWE04=="14"| - datalist[["main"]]$DWE04=="15") ~ "1", - TRUE ~ "0") - -## Calculate crowding index - overcrowded when more than 3 persons -datalist[["main"]]$crowding <- as.numeric(datalist[["main"]]$HH01) / - as.numeric(datalist[["main"]]$DWE05) - -datalist[["main"]]$dwe05_cat= dplyr::case_when( ##if crowding <= 3, not overcrowded - datalist[["main"]]$crowding <= 3 ~ "1", - TRUE ~ "0") - -####Calculate if all 5 conditions are met for adequate shelter -##dwe01_cat / dwe02_cat / dwe03_cat / dwe04_cat / dwe05_cat - -datalist[["main"]]$shelter= dplyr::case_when( - datalist[["main"]]$dwe01_cat=="0" | - datalist[["main"]]$dwe02_cat=="0" | - datalist[["main"]]$dwe03_cat=="0" | - datalist[["main"]]$dwe04_cat=="0" | - datalist[["main"]]$dwe05_cat=="0" ~ "0", - - datalist[["main"]]$dwe01_cat=="1" & - datalist[["main"]]$dwe02_cat=="1" & - datalist[["main"]]$dwe03_cat=="1" & - datalist[["main"]]$dwe04_cat=="1" & - datalist[["main"]]$dwe05_cat=="1" ~ "1") - -datalist[["main"]]$shelter = labelled::labelled( - datalist[["main"]]$shelter, - labels = c( - "Yes" = "1", - "No" = "0" - ), - label = "Access to adequate shelter") -return(datalist) -} diff --git a/R/outcome10_1.R b/R/outcome10_1.R new file mode 100644 index 0000000..8e87218 --- /dev/null +++ b/R/outcome10_1.R @@ -0,0 +1,91 @@ +# WARNING - Generated by {fusen} from dev/indicators.Rmd: do not edit by hand + +#' outcome10_1 +#' +#' +#' **Proportion of children aged 9 months to five years who have received measles vaccination** +#' +#' Coverage of measles vaccination in children in the target age group of 9 months to 5 years of age. +#' Measles vaccination is an essential preventive primary care intervention to protect children from measles infection. +#' +#' Standard module is from [UNICEF MICS6](https://mics.unicef.org/tools#survey-design) Children under 5 module. +#' The calculation of this module is also aligned with UNICEF MICS6. +#' +#' | Standard Questions | +#' |:------------------:| +#' | MMR01-MMR04 | +#' +#' **Numerator**: Total number of children 9 months to 5 years who have received a measles containing vaccine (measles or MMR- Measles Mumps and Rubella) +#' +#' **Denominator**: Total number of children 9 months to 5 years +#' +#' **Formula**: *MMR03=*1 / Total number of children 9 months to 5 years +#' +#' This comes from the main dataset +#' +#' MICS TC.1.1 UNICEF calculates on the first dose received## +#' Children who had et least one dose of measles vaccination +#' +#' @param datalist A list with all hierarchical data frame for a survey data set. +#' format is expected to match the Excel export synchronized from kobo to RILD +#' and loaded with kobocruncher::kobo_data() +#' +#' @importFrom dplyr mutate case_when +#' @importFrom labelled labelled +#' +#' @return new calculated variable - appended or not... +#' +#' @export +#' @examples +#' ## data, cf example fct_re_map() +#' datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", +#' package = "IndicatorCalc")) +#' +#' ## Apply indicator function on datalist +#' datalist <- outcome10_1(datalist) +#' +#' ## Visualise value +#' fct_plot_indic_donut(indicator = datalist[["ind"]]$outcome10_1, +#' iconunicode = "f140") +outcome10_1 <- function(datalist){ + # Mapper , +mapper = list( + hierarchy = "ind", + variablemap = data.frame( + label = +"3. Has ${indiv_05Less_name} ever received a measles containing vaccine(i.e. measles, MR or MMR)? That is a shot at the age of 9 months or older - to prevent (him/her) from getting measles" + , + variable = +"MMR03" + ), + modalitymap = data.frame( + variable = +c("MMR03", "MMR03", "MMR03") + , + label = +c("Yes", "No", "Don't know") + , + standard = +c("1", "0", "98") + )) + + ## So first we check that we have what we need in the data set based on the mapper + check_map <- fct_check_map(datalist = datalist, + mapper = mapper) + if ( check_map == FALSE) { + cat( "There are missing data requirement to calculate Indicator Outcome 10.1") + } else { + + +datalist[["ind"]]$outcome10_1 <- dplyr::case_when( + as.integer(datalist[["ind"]]$MMR03) == 1 ~ 1, + + as.integer(datalist[["ind"]]$MMR03) == 0 | + as.integer(datalist[["ind"]]$MMR03) == 98 ~ 0) + +datalist[["ind"]]$outcome10_1 <- labelled::labelled(datalist[["ind"]]$outcome10_1, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Proportion of children aged 9 months to five years who have received measles vaccination") +} + return(datalist) +} diff --git a/R/outcome10_2.R b/R/outcome10_2.R new file mode 100644 index 0000000..5d8e6cf --- /dev/null +++ b/R/outcome10_2.R @@ -0,0 +1,113 @@ +# WARNING - Generated by {fusen} from dev/indicators.Rmd: do not edit by hand + +#' outcome10_2 +#' +#' **Proportion of births attended by skilled health personnel** +#' +#' This indicator measures the percentage of births attended by personnel trained to give the necessary supervision, care, and advice to women during pregnancy, labor, and the postpartum period; to conduct deliveries on their own; and to care for newborns. +#' This indicator is linked to SGD [3.1.2](https://unstats.un.org/sdgs/metadata/files/Metadata-03-01-02.pdf). +#' +#' National-level household surveys are the main data sources used to collect data for skilled health personnel providing childbirth care. +#' These surveys include Demographic and Health Surveys (DHS), Multiple Indicator Cluster Surveys (MICS), Reproductive Health Surveys (RHS) and other national surveys based on similar methodologies. +#' Standard module is from [UNICEF MICS6](https://mics.unicef.org/tools#survey-design) individual questionnaire for woman. +#' The calculation of this module is also aligned with UNICEF MICS6. +#' +#' The national categories of skilled health personnel are verified, and the estimates for some countries may include additional categories of trained personnel beyond doctor, nurse, and midwife. +#' +#' | Standard Questions | +#' |:------------------:| +#' | BIR01-BIR04 | +#' +#' **Numerator**: Total number of births attended by skilled health personnel) +#' +#' **Denominator**: Total number of live births +#' +#' **Formula**: (BIR01=1 | BIR02=1) & (BIR03=1,2,3) / Total number of live births +#' This comes from the main dataset +#' MICS TM.5.a UNICEF MICS calculation if there was a trained health personnel ## +#' If there are live births in the last 2 years +#' Traditional birth attendant and community health worker can be included if they are trained +#' +#' @param datalist A list with all hierarchical data frame for a survey data set. +#' format is expected to match the Excel export synchronized from kobo to RILD +#' and loaded with kobocruncher::kobo_data() +#' +#' @importFrom dplyr mutate case_when +#' @importFrom labelled labelled +#' +#' @return new calculated variable - appended or not... +#' +#' @export +#' @examples +#' ## data, cf example fct_re_map() +#' datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", +#' package = "IndicatorCalc")) +#' ## Apply indicator function on datalist +#' datalist <- outcome10_2(datalist ) +#' +#' ## Visualise value +#' fct_plot_indic_donut(indicator = datalist[["main"]]$outcome10_2, +#' iconunicode = "f140") +outcome10_2 <- function(datalist){ + ## Mapper , +mapper = list( + hierarchy = "main", + variablemap = data.frame( + label = +c("1. In addition to ${namechild2less} was there any live birth in this household in the past 2 years?", +"2. Was there any live birth in this household in the past 2 years?", +"3. Who assisted with the delivery of the latest child born alive ?", +"4. Where did the birth take place for the latest child born alive ?" +) + , + variable = +c("BIR01", "BIR02", "BIR03", "BIR04") + ), + modalitymap = data.frame( + variable = +c("BIR01", "BIR01", "BIR02", "BIR02", "BIR03", "BIR03", "BIR03", +"BIR03", "BIR03", "BIR03", "BIR03", "BIR03", "BIR04", "BIR04", +"BIR04", "BIR04") + , + label = +c("Yes", "No", "Yes", "No", "Health professional", "Doctor", +"Nurse/midwife", "Traditional birth attendant", "Community health worker", +"Relative/friend", "Other (specify)", "Don't know", "Respondent’s home", +"Public sector", "Private sector", "Other, specify") + , + standard = +c("1", "0", "1", "0", "1", "2", "3", "4", "5", "6", "96", "98", +"1", "2", "3", "96") + )) + + ## So first we check that we have what we need in the data set based on the mapper + check_map <- fct_check_map(datalist = datalist, + mapper = mapper) + if ( check_map == FALSE) { + cat( "There are missing data requirement to calculate Indicator Outcome 10.2") + } else { + +datalist[["main"]]$outcome10_2 <- dplyr::case_when( + ( as.integer(datalist[["main"]]$BIR01) == 1 | + as.integer(datalist[["main"]]$BIR02) == 1) & + (as.integer(datalist[["main"]]$BIR03) == 1 | + as.integer(datalist[["main"]]$BIR03) == 2 | + as.integer(datalist[["main"]]$BIR03) == 3 ) ~ 1, + + (as.integer(datalist[["main"]]$BIR01) == 1 | + as.integer(datalist[["main"]]$BIR02) == 1) & ( + as.integer(datalist[["main"]]$BIR03) == 4 | + as.integer(datalist[["main"]]$BIR03) == 5 | + as.integer(datalist[["main"]]$BIR03) == 6 | + as.integer(datalist[["main"]]$BIR03) == 96| + as.integer(datalist[["main"]]$BIR03) == 98) ~ 0, + + TRUE ~ NA_real_) + +datalist[["main"]]$outcome10_2 <- labelled::labelled(datalist[["main"]]$outcome10_2, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Proportion of births attended + by skilled health personnel") +} + return(datalist) +} diff --git a/R/outcome12_1.R b/R/outcome12_1.R new file mode 100644 index 0000000..86d3270 --- /dev/null +++ b/R/outcome12_1.R @@ -0,0 +1,156 @@ +# WARNING - Generated by {fusen} from dev/indicators.Rmd: do not edit by hand + +#' outcome12_1 +#' +#' **Proportion of Persons of Concern using at least basic drinking water services** +#' +#' This indicator is defined as the percentage of PoCs using at least basic water services. +#' Access to clean drinking water is essential for a person's survival and well being and a precursor for achieving protection outcomes related to health, education and economic developed. +#' The calculation for access drinking water is linked to SGD Indicator [6.1.1](https://unstats.un.org/sdgs/metadata/files/Metadata-06-01-01.pdf). +#' The questionnaire module and the analysis guidance is taken from [UNICEF MICS6](https://mics.unicef.org/tools#analysis). +#' +#' +#' +#' | Standard Questions | +#' |:------------------:| +#' | DWA01-DWA04 | +#' +#' **Numerator**: Population using improved sources of drinking water either in their dwelling/yard/plot or within 30 minutes round trip collection time +#' +#' **Denominator**: Total population +#' +#' **Formula**: +#' *DWA03* \< 30 (under 30 minutes), & +#' *DWA01* !=7,9,13,96,98 & +#' *DWA02* !=3 +#' +#' This comes from the main dataset +#' There are two conditions as below improved source, in dwelling/yard/plot or reachable under 30 minutes +#' +#' +#' @param datalist A list with all hierarchical data frame for a survey data set. +#' format is expected to match the Excel export synchronized from kobo to RILD +#' and loaded with kobocruncher::kobo_data() +#' +#' @importFrom dplyr mutate case_when +#' @importFrom labelled labelled +#' +#' @return new calculated variable - appended or not... +#' +#' @export +#' @examples +#' ## data, cf example fct_re_map() +#' datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", +#' package = "IndicatorCalc")) +#' ## Apply indicator function on datalist +#' datalist <- outcome12_1(datalist ) +#' +#' ## Visualise value +#' fct_plot_indic_donut(indicator = datalist[["main"]]$outcome12_1, +#' iconunicode = "f140") +outcome12_1 <- function(datalist ){ + # Mapper , +mapper = list( + hierarchy = "main", + variablemap = data.frame( + label = +c("1. What is the main source of drinking water for this household?", +"2. Where is this ${source} - ${source2} located?", "Unit", "Time", +"4. In the last 30 days, has there been any time when your household did not have sufficient quantities of drinking water when needed?" +) + , + variable = +c("DWA01", "DWA02", "DWA03a", "DWA03b", "DWA04") + ), + modalitymap = data.frame( + variable = +c("DWA01", "DWA01", "DWA01", "DWA01", "DWA01", "DWA01", "DWA01", +"DWA01", "DWA01", "DWA01", "DWA01", "DWA01", "DWA01", "DWA01", +"DWA01", "DWA01", "DWA01", "DWA01", "DWA02", "DWA02", "DWA02", +"DWA03a", "DWA03a", "DWA03b", "DWA04", "DWA04") + , + label = +c("Piped Into Dwelling", "Piped Into Yard/Plot", "Piped To Neighbor", +"Public Tap/Standpipe", "Tube Well/Borehole", "Protected Dug Well", +"Unprotected Dug Well", "Protected Spring", "Unprotected Spring", +"Rain Water Collection", "Tanker Truck/Water Vendor", "Cart With Small Tank/Drum", +"Surface Water (River, Stream, Pond, Dam, Canal)", "Bottled Water", +"Sachet Water", "Water Kiosk", "Other (Specify)", "Don't Know", +"In Own Dwelling", "In Own Yard/Plot", "Elsewhere", "Minutes", +"Hours", NA, "Yes", "No") + , + standard = +c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", +"13", "14", "15", "16", "96", "98", "1", "2", "3", "1", "2", +NA, "1", "0") + )) + ## So first we check that we have what we need in the data set based on the mapper + check_map <- fct_check_map(datalist = datalist, + mapper = mapper) + if ( check_map == FALSE) { + cat( "There are missing data requirement to calculate Indicator Outcome 12.1") + } else { + + #convert hour into minutes +datalist[["main"]]$time_DWA <- dplyr::case_when( + as.integer(datalist[["main"]]$DWA03a) == 1 ~ 1, + as.integer(datalist[["main"]]$DWA03a) == 2 ~ 60) + +datalist[["main"]]$time_tot <- datalist[["main"]]$time_DWA * + as.integer(datalist[["main"]]$DWA03b) + +# reachable under 30 minutes +datalist[["main"]]$reachableU30 <- dplyr::case_when( + datalist[["main"]]$time_tot > 30 ~ 0, + TRUE ~ 1) + +# improved source +datalist[["main"]]$dwa_cond1 <- dplyr::case_when( + as.integer(datalist[["main"]]$DWA01) != 7 | + as.integer(datalist[["main"]]$DWA01) != 9 | + as.integer(datalist[["main"]]$DWA01) != 13 | + as.integer(datalist[["main"]]$DWA01) != 96 | + as.integer(datalist[["main"]]$DWA01) != 98 ~ 1, + + TRUE ~ 0) + +datalist[["main"]]$dwa_cond1 <- labelled::labelled(datalist[["main"]]$dwa_cond1 , + labels = c('Yes' = 1, 'No' = 0 ), + label = "Water: improved source") + +## Second condition + +datalist[["main"]]$dwa_cond2 <- dplyr::case_when( + + as.integer(datalist[["main"]]$DWA02) == 3 ~ 1, + + #UNDER30 MIN + as.integer(datalist[["main"]]$reachableU30) == 1 & + # NOT UNDER30 MIN + as.integer(datalist[["main"]]$reachableU30) == 0 & + as.integer(datalist[["main"]]$DWA02) == 3 ~ 0, + + # in the dwelling/yard/plot + as.integer(datalist[["main"]]$DWA02) == 1 | + as.integer(datalist[["main"]]$DWA02) == 2 ~ 1, + + TRUE ~ NA ) + +datalist[["main"]]$dwa_cond2 <- labelled::labelled(datalist[["main"]]$dwa_cond2 , + labels = c('Yes' = 1, 'No' = 0 ), + label = "Water accessible") + + +## Composite +datalist[["main"]]$outcome12_1 <- dplyr::case_when( + (datalist[["main"]]$dwa_cond1 == 1 & + datalist[["main"]]$dwa_cond2 == 1 ) ~ 1, + TRUE ~ 0) + +datalist[["main"]]$outcome12_1 <- labelled::labelled(datalist[["main"]]$outcome12_1, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Proportion of PoC using at least basic drinking water services") + + } + return(datalist) +} diff --git a/R/outcome12_2.R b/R/outcome12_2.R new file mode 100644 index 0000000..36941cf --- /dev/null +++ b/R/outcome12_2.R @@ -0,0 +1,155 @@ +# WARNING - Generated by {fusen} from dev/indicators.Rmd: do not edit by hand + +#' outcome12_2 +#' +#' **Proportion of Persons of Concern with access to a safe household toilet** +#' +#' This indicator measures the proportion of persons of concerns with access to at least basic sanitation services -- toilets -- that are not shared with other households. +#' A toilet is defined as a basic sanitation facility. +#' This indicator is linked to SGD indicator [6.2.1](https://sdg-tracker.org/water-and-sanitation#:~:text=Definition%3A%20Indicator%206.2.,at%20least%20basic%20handwashing%20facilities.). +#' The standard module is taken from [UNICEF MICS6](https://mics.unicef.org/tools#analysis) main household questionnaire. +#' Calculation of the indicator is based on [MICS6 analysis tools](https://mics.unicef.org/tools#analysis). +#' +#' | Standard Questions | +#' |:------------------:| +#' | TOI01-TOI05 | +#' +#' **Numerator**: Total population with access to sanitation facility at their household +#' +#' **Denominator**: Total population +#' +#' **Formula**: *TOI01*=1,2,3,4,5,6,7,9 & (*TOI02* =1 & *TOI03*=1,2,3,4) & *TOI05*=1 +#' This comes from the main dataset +#' MICS calculation WS3.1/WS3.4 +#' +#' @param datalist A list with all hierarchical data frame for a survey data set. +#' format is expected to match the Excel export synchronized from kobo to RILD +#' and loaded with kobocruncher::kobo_data() +#' +#' @importFrom dplyr mutate case_when +#' @importFrom labelled labelled +#' +#' @return new calculated variable - appended or not... +#' +#' @export +#' @examples +#' ## data, cf example fct_re_map() +#' datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", +#' package = "IndicatorCalc")) +#' ## Apply indicator function on datalist +#' datalist <- outcome12_2(datalist) +#' +#' ## Visualise value +#' fct_plot_indic_donut(indicator = datalist[["main"]]$outcome12_2, +#' iconunicode = "f140") +outcome12_2 <- function(datalist ){ + ## mapper, + mapper = list( + hierarchy = "main", + hierarchy = "ind", + variablemap = data.frame( + label = +c("1. What is the MAIN type of toilet facility used by your household?", +"2. Has your toilet ever been emptied?", "3. The last time it was emptied, where were the contents emptied to?", +"4. Where is this toilet facility located?", "5. Does your household share this facility with others households?" +) + , + variable = +c("TOI01", "TOI02", "TOI03", "TOI04", "TOI05") + ), + modalitymap = data.frame( + variable = +c("TOI01", "TOI01", "TOI01", "TOI01", "TOI01", "TOI01", "TOI01", +"TOI01", "TOI01", "TOI01", "TOI01", "TOI01", "TOI01", "TOI02", +"TOI02", "TOI02", "TOI03", "TOI03", "TOI03", "TOI03", "TOI03", +"TOI03", "TOI03", "TOI04", "TOI04", "TOI04", "TOI05", "TOI05" +) + , + label = +c("Flush/pour flush to the piped sewer system", "Flush/pour-flush to septic tank", +"Flush/pour-flush to pit latrine", "Flush/pour-flush to open drain", +"Flush/pour-flush to don't know where", "Ventilated improved pit latrine", +"Pit latrine with slab", "Pit latrine without slab/open pit", +"Composting toilet", "Bucket", "Hanging toilet/hanging latrine", +"No facility/bush/field", "Other (Specify)", "Yes, emptied", +"No, never emptied", "Don't know", "Removed by a service provider to a treatment plant", +"Removed by a service provider and buried in a covered pit", +"Removed by a service provider to don’t know where", "Emptied by household and buried in a covered pit", +"Emptied by household to uncovered pit, open ground, water body, or elsewhere", +"Other (specify)", "Don’t know", "In Own Dwelling", "In Own Yard/Plot", +"Elsewhere", "Yes", "No") + , + standard = +c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", +"96", "1", "2", "98", "1", "2", "3", "4", "5", "96", "98", "1", +"2", "3", "1", "0") + )) + + ## So first we check that we have what we need in the data set based on the mapper + check_map <- fct_check_map(datalist = datalist, + mapper = mapper) + if ( check_map == FALSE) { + cat( "There are missing data requirement to calculate Indicator Outcome 12.2") + } else { + + +datalist[["main"]]$toi_cond1 <- dplyr::case_when( + as.integer(datalist[["main"]]$TOI01) == 1 | + as.integer(datalist[["main"]]$TOI01) == 2 | + as.integer(datalist[["main"]]$TOI01) == 3 | + as.integer(datalist[["main"]]$TOI01) == 4 | + as.integer(datalist[["main"]]$TOI01) == 5 | + as.integer(datalist[["main"]]$TOI01) == 6 | + as.integer(datalist[["main"]]$TOI01) == 7 | + as.integer(datalist[["main"]]$TOI01) == 9 ~ 1, + + as.integer(datalist[["main"]]$TOI01) == 8 | + as.integer(datalist[["main"]]$TOI01) == 10 | + as.integer(datalist[["main"]]$TOI01) == 11 | + as.integer(datalist[["main"]]$TOI01) == 12 | + as.integer(datalist[["main"]]$TOI01) == 96 ~ 0, + + TRUE ~ NA_real_) + +#Unsafe disposal +datalist[["main"]]$toi_cond2 <- dplyr::case_when( + + as.integer(datalist[["main"]]$TOI02) == 1 & + ( as.integer(datalist[["main"]]$TOI03) == 1 | + as.integer(datalist[["main"]]$TOI03) == 2 | + as.integer(datalist[["main"]]$TOI03) == 3 | + as.integer(datalist[["main"]]$TOI03) == 4 ) ~ 1, + + as.integer(datalist[["main"]]$TOI02) == 1 & + (as.integer(datalist[["main"]]$TOI03) ==5 | + as.integer(datalist[["main"]]$TOI03) == 96 | + as.integer(datalist[["main"]]$TOI03) == 98) ~ 0, + + as.integer(datalist[["main"]]$TOI02) == 2 ~ 0, + as.integer(datalist[["main"]]$TOI02) == 98 ~ 0, + TRUE ~ NA_real_) + + # toilet not shared with other households +datalist[["main"]]$toi_cond3<- dplyr::case_when( + as.integer(datalist[["main"]]$TOI05) == 1 ~ 0, + as.integer(datalist[["main"]]$TOI05) == 0 ~ 1) + + +###Combine all three conditions below +### improved sanitation facility / +# Safe disposal in situ of excreta from on-site sanitation facilities / +# not shared with other HHs + +datalist[["main"]]$outcome12_2<- dplyr::case_when( + datalist[["main"]]$toi_cond1 == 1 | + datalist[["main"]]$toi_cond2 == 1 | + datalist[["main"]]$toi_cond3 == 1 ~ 1, + TRUE ~ 0) + +datalist[["main"]]$outcome12_2 <- labelled::labelled(datalist[["main"]]$outcome12_2, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Proportion of Persons of Concern + with acess to a safe household toilet") +} + return(datalist) +} diff --git a/R/outcome13_1.R b/R/outcome13_1.R new file mode 100644 index 0000000..0525501 --- /dev/null +++ b/R/outcome13_1.R @@ -0,0 +1,101 @@ +# WARNING - Generated by {fusen} from dev/indicators.Rmd: do not edit by hand + +#' outcome13_1 +#' +#' **Proportion of Persons of Concern with an account at a bank or other financial institution or with a mobile-money-service provider** +#' +#' This indicator measures the percentage of persons of concern (ages 15+) who report having an account (by themselves or together with someone else) at a bank or another type of financial institution or personally using a mobile money service in the past 12 months in the country of asylum or habitual residence (for returnees, countries of origin are included). +#' +#' The methodology is taken from [The Global Findex Database](https://www.worldbank.org/en/publication/globalfindex) which is developed by the World Bank. +#' This indicator is also linked to SGD Indicator [8.10.2](https://unstats.un.org/sdgs/metadata/files/Metadata-08-10-02.pdf). +#' +#' | Standard Questions | +#' |:------------------:| +#' | BANK01-BANK05 | +#' +#' **Numerator**: Total population having a personal mobile or bank account +#' +#' **Denominator**: Total population +#' +#' **Formula**: *BANK01*=1 \| *BANK02*=1 \| *BANK03*=1 \|*BANK05*=1 +#' This indicator comes from main dataset based on the respondent randomly selected for individual level +#' include if the respondent has an account on their own/with someone else, ATM card, personal bank card +#' or personally used phone to send money (proxy) +#' +#' +#' @param datalist A list with all hierarchical data frame for a survey data set. +#' format is expected to match the Excel export synchronized from kobo to RILD +#' and loaded with kobocruncher::kobo_data() +#' +#' @importFrom dplyr mutate case_when +#' @importFrom labelled labelled +#' +#' @return new calculated variable - appended or not... +#' +#' @export +#' @examples +#' ## data, cf example fct_re_map() +#' datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", +#' package = "IndicatorCalc")) +#' ## Apply indicator function on datalist +#' datalist <- outcome13_1(datalist ) +#' +#' ## Visualise value +#' fct_plot_indic_donut(indicator = datalist[["main"]]$outcome13_1, +#' iconunicode = "f140") +outcome13_1 <- function(datalist){ + + ## Mapper , +mapper = list( + hierarchy = "main", + variablemap = data.frame( + label = +c("1. Do you, either by yourself or together with someone else, currently have an account at a bank or another type of formal financial institution in ${countryname} ?", +"2. Do you, personally, have a/an [local terminology for ATM/debit card] in ${countryname} ?", +"3. Is this [local terminology for ATM/debit card] connected to an account with your name on it in ${countryname} ?", +"4. In the past 12 months, have you used a mobile phone to make payments, to buy things, or to send or receive money?", +"5. In the past 12 months, have you, personally, used a mobile phone to make payments, to buy things, or to send or receive money using a service such as [local example of mobile money from GSMA database, like M-PESA]?" +) + , + variable = +c("BANK01", "BANK02", "BANK03", "BANK04", "BANK05") + ), + modalitymap = data.frame( + variable = +c("BANK01", "BANK01", "BANK02", "BANK02", "BANK03", "BANK03", +"BANK04", "BANK04", "BANK05", "BANK05") + , + label = +c("Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", +"No") + , + standard = +c("1", "0", "1", "0", "1", "0", "1", "0", "1", "0") + )) + ## So first we check that we have what we need in the data set based on the mapper + check_map <- fct_check_map(datalist = datalist, + mapper = mapper) + if ( check_map == FALSE) { + cat( "There are missing data requirement to calculate Indicator Outcome 13.1") + } else { + +datalist[["main"]]$outcome13_1 <- dplyr::case_when( + as.integer(datalist[["main"]]$BANK01) == 1 | + as.integer(datalist[["main"]]$BANK02) == 1 | + as.integer(datalist[["main"]]$BANK03) == 1 | + as.integer(datalist[["main"]]$BANK05) == 1 ~ 1, + + as.integer(datalist[["main"]]$BANK01) == 0 & + as.integer(datalist[["main"]]$BANK02) == 0 & + as.integer(datalist[["main"]]$BANK03) == 0 & + as.integer(datalist[["main"]]$BANK05) == 0 ~ 0, + TRUE ~ 0) + +datalist[["main"]]$outcome13_1 <- labelled::labelled(datalist[["main"]]$outcome13_1, + labels = c('Yes' = 1, 'No' = 0 ), + label = "PoC with an account at a bank or other financial + institution or with a mobile-money service provider") + +} + return(datalist) +} diff --git a/R/outcome13_2.R b/R/outcome13_2.R new file mode 100644 index 0000000..267e472 --- /dev/null +++ b/R/outcome13_2.R @@ -0,0 +1,84 @@ +# WARNING - Generated by {fusen} from dev/indicators.Rmd: do not edit by hand + +#' outcome13_2 +#' +#' **Proportion of Persons of Concern who self-report positive changes in their income compared to previous year** +#' +#' This indicator measures the proportion of PoC who self-report positive changes in their income compared to previous year. +#' +#' | Standard Questions | +#' |:------------------:| +#' | INC01 | +#' +#' **Numerator**: Population 18 and above who self-report increased income compared to last year +#' +#' **Denominator**: Total population 18 and above +#' +#' **Formula**: *INC01*=1 +#' +#' This indicator comes from main dataset based on the respondent randomly selected for individual level +#' Only calculate as positive if they responded 'more' +#' +#' @param datalist A list with all hierarchical data frame for a survey data set. +#' format is expected to match the Excel export synchronized from kobo to RILD +#' and loaded with kobocruncher::kobo_data() +#' +#' @importFrom dplyr mutate case_when +#' @importFrom labelled labelled +#' +#' @return new calculated variable - appended or not... +#' +#' @export +#' @examples +#' ## data, cf example fct_re_map() +#' datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", +#' package = "IndicatorCalc")) +#' ## Apply indicator function on datalist +#' datalist <- outcome13_2(datalist) +#' +#' ## Visualise value +#' fct_plot_indic_donut(indicator = datalist[["main"]]$outcome13_2, +#' iconunicode = "f140") +outcome13_2 <- function(datalist){ + ## Mapper , +mapper = list( + hierarchy = "main", + variablemap = data.frame( + label = +"11. Compared to this time last year, do you think you can now afford more goods and services, the same, or fewer goods and services?" + , + variable = +"INC01" + ), + modalitymap = data.frame( + variable = +c("INC01", "INC01", "INC01", "INC01") + , + label = +c("More", "The same", "Fewer", "Don't know") + , + standard = +c("1", "2", "3", "98") + )) + ## So first we check that we have what we need in the data set based on the mapper + check_map <- fct_check_map(datalist = datalist, + mapper = mapper) + if ( check_map == FALSE) { + cat( "There are missing data requirement to calculate Indicator Outcome 13.2") + } else { + +datalist[["main"]]$outcome13_2<- dplyr::case_when( + + as.integer(datalist[["main"]]$INC01) == 1 ~ 1, + + as.integer(datalist[["main"]]$INC01) == 2 | + as.integer(datalist[["main"]]$INC01) == 3 | + as.integer(datalist[["main"]]$INC01) == 98 ~ 0 ) + +datalist[["main"]]$outcome13_2 <- labelled::labelled(datalist[["main"]]$outcome13_2, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Proportion of PoC who self-report positive changes in their income compared to previous year") + +} + return(datalist) +} diff --git a/R/outcome13_3.R b/R/outcome13_3.R new file mode 100644 index 0000000..1a3827d --- /dev/null +++ b/R/outcome13_3.R @@ -0,0 +1,161 @@ +# WARNING - Generated by {fusen} from dev/indicators.Rmd: do not edit by hand + +#' outcome13_3#' +#' +#' **Proportion of Persons of Concern (working age) who are unemployed** +#' The unemployment rate is a standard indicator to measure the efficiency and effectiveness of an economy. +#' For Persons of Concern, access to employment is central for self-reliance, socio-economic inclusion and dignity and often restricted through laws, policies or practical barriers. +#' This indicator is also linked to SGD [8.5.2](https://unstats.un.org/sdgs/metadata/files/Metadata-08-05-02.pdf). +#' The standard questionnaire module is from [Standardized Employment Module](https://www.unhcr.org/5ea81b954) of UNHCR. +#' The calculations are also done based on the same guidance. +#' | Standard Questions | +#' |:------------------:| +#' | UNEM01-UNEM10 | +#' +#' **Numerator**: Unemployed working age population within a country +#' +#' **Denominator**: Total working age population who are employed and unemployed within a country (often referred to as labour force) +#' +#' **Formula**: *UNEM01*=1 \| (*UNEM02*=1 & *UNEM07*=3) \| *UNEM04*=1 \| (*UNEM02*=1 & *UNEM07*=1 &( *UNEM08*=1,2)) \|(*UNEM05*=1 & *UNEM06*=3) \| ( *UNEM05*=1 & ( *UNEM06*=1,2 \| *UNEM08*=1,2) +#' This indicator comes from main dataset based on the respondent randomly selected for individual level +#' +#' +#' #Numerator: Those of working age who were not in employment, looked for employment in the past 30 days and were available to take up employment +####Denominator: Those of working age in labour force +#' +#' @param datalist A list with all hierarchical data frame for a survey data set. +#' format is expected to match the Excel export synchronized from kobo to RILD +#' and loaded with kobocruncher::kobo_data() +#' +#' @importFrom dplyr mutate case_when +#' @importFrom labelled labelled +#' +#' @return new calculated variable - appended or not... +#' +#' @export +#' @examples +#' ## data, cf example fct_re_map() +#' datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", +#' package = "IndicatorCalc")) +#' ## Apply indicator function on datalist +#' datalist <- outcome13_3(datalist ) +#' +#' table( datalist[["main"]]$outcome13_3, useNA = "ifany") +#' ## Visualise value +#' fct_plot_indic_donut(indicator = datalist[["main"]]$outcome13_3, +#' iconunicode = "f140") +outcome13_3 <- function(datalist ){ + # mapper , + +mapper = list( + hierarchy = "main", + variablemap = data.frame( + label = +c("1. During the past 7 days, did ${name_selectedadult18} work for someone else for pay, for one or more hours?", +"2. During the past 7 days, did ${name_selectedadult18} run or do any kind of business, farming, or other activity to generate income?", +"3. During the past 7 days, did ${name_selectedadult18} help in a family business or farm?", +"4. Even though ${name_selectedadult18} did not work, during the past 7 days, did he/she have a paid job from which he/she was temporarily absent?", +"5. Even though ${name_selectedadult18} did not work, during the past 7 days, did he/she have a business or a helper job in a family business/farm from which he/she was temporarily absent?", +"6. Last week, did ${name_selectedadult18} do any work in…?", +"7. Was this work that ${name_selectedadult18} mentioned in...?", +"8. Thinking about this work, are the products intended...?", +"9. During the last 30 days, did ${name_selectedadult18} do anything to find a paid job or try to start a business?", +"10. Could ${name_selectedadult18} start working within the next two weeks?" +) + , + variable = +c("UNEM01", "UNEM02", "UNEM03", "UNEM04", "UNEM05", "UNEM06", +"UNEM07", "UNEM08", "UNEM09", "UNEM10") + ), + modalitymap = data.frame( + variable = +c("UNEM01", "UNEM01", "UNEM02", "UNEM02", "UNEM03", "UNEM03", +"UNEM04", "UNEM04", "UNEM05", "UNEM05", "UNEM06", "UNEM06", "UNEM06", +"UNEM07", "UNEM07", "UNEM07", "UNEM08", "UNEM08", "UNEM08", "UNEM08", +"UNEM09", "UNEM09", "UNEM10", "UNEM10") + , + label = +c("Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", +"No", "Farming or rearing farm animals", "Fishing or fish farming", +"None of the above", "Farming or rearing farm animals", "Fishing or fish farming", +"None of the above", "Only for sale", "Mainly for sale", "Only for family use", +"Mainly for family use", "Yes", "No", "Yes", "No") + , + standard = +c("1", "0", "1", "0", "1", "0", "1", "0", "1", "0", "1", "2", +"3", "1", "2", "3", "1", "2", "3", "4", "1", "0", "1", "0") + )) + + ## So first we check that we have what we need in the data set based on the mapper + check_map <- fct_check_map(datalist = datalist, + mapper = mapper) + if ( check_map == FALSE) { + cat( "There are missing data requirement to calculate Indicator Outcome 13.23") + } else { + + +datalist[["main"]]$employed <- dplyr::case_when( + + as.integer(datalist[["main"]]$UNEM01) == 1 ~ 1, + + as.integer(datalist[["main"]]$UNEM02) == 1 & + as.integer(datalist[["main"]]$UNEM07) == 3 ~ 1, + + as.integer(datalist[["main"]]$UNEM04) == 1 ~ 1, + + as.integer(datalist[["main"]]$UNEM02) == 1 & + as.integer(datalist[["main"]]$UNEM07) == 1 & + (as.integer(datalist[["main"]]$UNEM08) == 1 | + as.integer(datalist[["main"]]$UNEM08) == 2) ~ 1, + + as.integer(datalist[["main"]]$UNEM05) == 1 & + as.integer(datalist[["main"]]$UNEM06) == 3 ~ 1, + as.integer(datalist[["main"]]$UNEM05) == 1 & + ( as.integer(datalist[["main"]]$UNEM06) == 1 | + as.integer(datalist[["main"]]$UNEM06) == 2) & + ( as.integer(datalist[["main"]]$UNEM08) == 1 | + as.integer(datalist[["main"]]$UNEM08) == 2) ~ 1, + TRUE ~ 0) + + +datalist[["main"]]$employed <- labelled::labelled(datalist[["main"]]$employed, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Proportion employed") + + +datalist[["main"]]$unemployed<- dplyr::case_when( + as.integer(datalist[["main"]]$employed) == 0 & + as.integer(datalist[["main"]]$UNEM09) == 1 & + as.integer(datalist[["main"]]$UNEM10) == 1 ~ 1, + TRUE ~ 0) + + +datalist[["main"]]$unemployed <- labelled::labelled(datalist[["main"]]$unemployed, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Proportion unemployed") + +datalist[["main"]]$labour_force <- dplyr::case_when( + datalist[["main"]]$employed == 1 | + datalist[["main"]]$unemployed == 1 ~ 1) + + +datalist[["main"]]$labour_force <- labelled::labelled(datalist[["main"]]$labour_force, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Proportion forming labour force") + +datalist[["main"]]$outcome13_3 <- dplyr::case_when( + (datalist[["main"]]$unemployed == 1 & + datalist[["main"]]$labour_force == 1) ~ 1, + (datalist[["main"]]$employed == 1 & + datalist[["main"]]$labour_force == 1) ~ 0, + TRUE ~ NA_real_ ) + + +datalist[["main"]]$outcome13_3 <- labelled::labelled(datalist[["main"]]$outcome13_3, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Proportion of unemployed while being part of labour force") + + } + return(datalist) + +} diff --git a/R/outcome14_1.R b/R/outcome14_1.R new file mode 100644 index 0000000..6b0730c --- /dev/null +++ b/R/outcome14_1.R @@ -0,0 +1,175 @@ +# WARNING - Generated by {fusen} from dev/indicators.Rmd: do not edit by hand + +#' outcome14_1 +#' +#' **Proportion of returnees with legally recognized identity documents or credentials** +#' +#' This indicator measures the proportion of returned refugees who possess legally recognized and valid identity documents or credentials to support their return . +#' +#' Commonly in the context of return, returnees require civil documentation or credentials and inclusion in or updating of civil registries , as well as access to services. +#' +#' ***Definitions*** +#' +#' • Identity document or credential is any document or credential which may be used as proof of identity, which may also include reference to the individuals' legal status and associated rights vis-à-vis the host State and/or UNHCR. +#' +#' | Standard Questions | +#' |:-------------------------------------:| +#' | REG01 - REG02 - REG03 / REG05 - REG06 | +#' +#' **Numerator**: Total number of returnees with with legally recognized identity documents or credentials +#' +#' **Denominator**: Total number of returnees +#' +#' **Formula**: *REG01*=1 \| *REG02*=1 \| *REG03*=1 \| *REG05*=1 \| *REG06*=1 +#' +#'This indicator comes from the individual dataset +#' Calculate valid identity documents for under 5 with REG05 and REG06 variables +#' +#' @param datalist A list with all hierarchical data frame for a survey data set. +#' format is expected to match the Excel export synchronized from kobo to RILD +#' and loaded with kobocruncher::kobo_data() +#' +#' @importFrom dplyr mutate case_when +#' @importFrom labelled labelled +#' +#' @return new calculated variable - appended or not... +#' +#' @export +#' @examples +#' ## data, cf example fct_re_map() +#' datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", +#' package = "IndicatorCalc")) +#' +#' ## Apply indicator function on datalist +#' datalist <- outcome14_1(datalist ) +#' +#' ## Visualise value +#' fct_plot_indic_donut(indicator = datalist[["ind"]]$outcome14_1, +#' iconunicode = "f140") +outcome14_1 <- function(datalist ){ + ## Mapper , + +mapper = list( + hierarchy = "ind", + variablemap = data.frame( + label = +c("Passport?", "Birth certificate?", "Civil/government-issued ID card ?", +"Residency permit (both temporary and permanent)?", "Statelessness documentation?", +"Household card of address / family book/ marriage certificate?", +"Social security card?", "2. Does ${name_individual} have any other document that establishes his/her legal identity?", +"3. Does ${name_individual} have a birth certificate?", "4. Has ${name_individual}'s birth been registered with civil authorities?", +"Passport?", "Civil/government-issued ID card ?", "Residency permit (both temporary and permanent)?", +"Statelessness documentation?", "Household card of address / family book/ marriage certificate?", +"Social security card?", "6. Does ${name_individual} have any other document that establishes your legal identity?" +) + , + variable = +c("REG01a", "REG01b", "REG01c", "REG01d", "REG01e", "REG01f", +"REG01g", "REG02", "REG03", "REG04", "REG05a", "REG05b", "REG05c", +"REG05d", "REG05e", "REG05f", "REG06") + ), + modalitymap = data.frame( + variable = +c("REG01a", "REG01a", "REG01a", "REG01b", "REG01b", "REG01b", +"REG01c", "REG01c", "REG01c", "REG01d", "REG01d", "REG01d", "REG01e", +"REG01e", "REG01e", "REG01f", "REG01f", "REG01f", "REG01g", "REG01g", +"REG01g", "REG02", "REG02", "REG02", "REG03", "REG03", "REG03", +"REG04", "REG04", "REG04", "REG04", "REG05a", "REG05a", "REG05a", +"REG05b", "REG05b", "REG05b", "REG05c", "REG05c", "REG05c", "REG05d", +"REG05d", "REG05d", "REG05e", "REG05e", "REG05e", "REG05f", "REG05f", +"REG05f", "REG06", "REG06", "REG06") + , + label = +c("Yes", "No", "Prefer not to respond", "Yes", "No", "Prefer not to respond", +"Yes", "No", "Prefer not to respond", "Yes", "No", "Prefer not to respond", +"Yes", "No", "Prefer not to respond", "Yes", "No", "Prefer not to respond", +"Yes", "No", "Prefer not to respond", "Yes", "No", "Prefer not to respond", +"Yes", "No", "Don't know", "Yes", "No", "Don't know", "Prefer not to say", +"Yes", "No", "Prefer not to respond", "Yes", "No", "Prefer not to respond", +"Yes", "No", "Prefer not to respond", "Yes", "No", "Prefer not to respond", +"Yes", "No", "Prefer not to respond", "Yes", "No", "Prefer not to respond", +"Yes", "No", "Prefer not to respond") + , + standard = +c("1", "0", "99", "1", "0", "99", "1", "0", "99", "1", "0", "99", +"1", "0", "99", "1", "0", "99", "1", "0", "99", "1", "0", "99", +"1", "0", "98", "1", "0", "98", "99", "1", "0", "99", "1", "0", +"99", "1", "0", "99", "1", "0", "99", "1", "0", "99", "1", "0", +"99", "1", "0", "99") + )) + + ## So first we check that we have what we need in the data set based on the mapper + check_map <- fct_check_map(datalist = datalist, + mapper = mapper) + if ( check_map == FALSE) { + cat( "There are missing data requirement to calculate Indicator Outcome 14.1") + } else { + +datalist[["ind"]]$document_under5 <- dplyr::case_when( + as.integer(datalist[["ind"]]$REG05a) == 1 | #ind$REG05a - passport + as.integer(datalist[["ind"]]$REG05b) == 1 | #ind$REG05b - civil/government issued ID + as.integer(datalist[["ind"]]$REG05c) == 1 | #ind$REG05c - residency permit + as.integer(datalist[["ind"]]$REG05d) == 1 | #ind$REG05d - statelessness documentation + as.integer(datalist[["ind"]]$REG05e) == 1 | #ind$REG05e - household card of address/family book + as.integer(datalist[["ind"]]$REG05f) == 1 | #ind$REG05f - social security card + as.integer(datalist[["ind"]]$REG06) == 1 | #ind$REG06 - any other document establishes identity + as.integer(datalist[["ind"]]$REG03) == 1 #add birth certificate as additional document from REG03 + ~ 1, + as.integer(datalist[["ind"]]$REG05a) == 0 & + as.integer(datalist[["ind"]]$REG05b) == 0 & + as.integer(datalist[["ind"]]$REG05c) == 0 & + as.integer(datalist[["ind"]]$REG05d) == 0 & + as.integer(datalist[["ind"]]$REG05e) == 0 & + as.integer(datalist[["ind"]]$REG05f) == 0 & + as.integer(datalist[["ind"]]$REG06) == 0 & + as.integer(datalist[["ind"]]$REG03) == 0 ~ 0, + TRUE ~ NA_real_ ) + + ###Calculate valid identity documents for above 5 with REG01 and REG02 variables + + + #ind$REG01a # passport + #ind$REG01b # birth certificate + #ind$REG01c # civil/ government issued ID + #ind$REG01d # residency permit + #ind$REG01e # statelessness documentation + #ind$REG01f # household card of address/family book + #ind$REG01g # social security card + #ind$REG02 # any other document establishes identity + +datalist[["ind"]]$document_above5 <- dplyr::case_when( + as.integer(datalist[["ind"]]$REG01a) == 1 | + as.integer(datalist[["ind"]]$REG01b) == 1 | + as.integer(datalist[["ind"]]$REG01c) == 1 | + as.integer(datalist[["ind"]]$REG01d) == 1 | + as.integer(datalist[["ind"]]$REG01e) == 1 | + as.integer(datalist[["ind"]]$REG01f) == 1 | + as.integer(datalist[["ind"]]$REG01g) == 1 | + as.integer(datalist[["ind"]]$REG02) == 1 ~ 1, + + as.integer(datalist[["ind"]]$REG01a) == 0 & + as.integer(datalist[["ind"]]$REG01b) == 0 & + as.integer(datalist[["ind"]]$REG01c) == 0 & + as.integer(datalist[["ind"]]$REG01d) == 0 & + as.integer(datalist[["ind"]]$REG01e) == 0 & + as.integer(datalist[["ind"]]$REG01f) == 0 & + as.integer(datalist[["ind"]]$REG01g) == 0 & + as.integer(datalist[["ind"]]$REG02) == 0 ~ 0, + + TRUE ~ NA_real_) + + ##Combine both age groups +datalist[["ind"]]$outcome14_1<- dplyr::case_when( + (datalist[["ind"]]$document_above5 == 1 | + datalist[["ind"]]$document_under5 == 1) ~ 1, + + (datalist[["ind"]]$document_above5 == 0 | + datalist[["ind"]]$document_under5 == 0) ~ 0) + +datalist[["ind"]]$outcome14_1 <- labelled::labelled(datalist[["ind"]]$outcome14_1, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Proportion of returnees with legally recognized identity + documents or credentials") + } + return(datalist) +} diff --git a/R/outcome16_1.R b/R/outcome16_1.R new file mode 100644 index 0000000..17fb2e8 --- /dev/null +++ b/R/outcome16_1.R @@ -0,0 +1,144 @@ +# WARNING - Generated by {fusen} from dev/indicators.Rmd: do not edit by hand + +#' outcome16_1 +#' +#' **Proportion of Persons of Concern with secure tenure rights and/or property rights to housing and/or land** +#' +#' This indicator measures the proportion of persons of concern that have secure tenure rights to housing and/or land. +#' Security of tenure means that persons of concern can live in their homes without fear of forced eviction, whether in communal settlement situations, informal settlements, host communities or after return. +#' +#' This indicator is linked to SGD Indicator [1.4.2](https://unstats.un.org/sdgs/metadata/files/Metadata-01-04-02.pdf). The further guidance can be found [here](https://gltn.net/download/measuring-individuals-rights-to-land-an-integrated-approach-to-data-collection-for-sdg-indicators-1-4-2-and-5-a-1-english/?wpdmdl=16316&refresh=5efb342458df61593521188) +#' +#' *Concept* +#' +#' - Secure tenure rights: comprised of two sub-components: (i) legally recognized documentation and (ii) perception of the security of tenure, which are both necessary to provide a full measurement of tenure security. +#' +#' - Legally recognized documentation: Legal documentation of rights refers to the recording and publication of information on the nature and location of land, rights and right holders in a form that is recognized by government, and is therefore official. +#' +#' - Perceived security of tenure: Perception of tenure security refers to an individual's perception of the likelihood of involuntary loss of land, such as disagreement of the ownership rights over land or ability to use it, regardless of the formal status and can be more optimistic or pessimistic. +#' +#' | Standard Questions | +#' |:-------------------------:| +#' | DWE06-DWE07 & DWE10-DWE11 | +#' +#' **Numerator**: Total population with secure tenure rights to housing and/or land +#' +#' **Denominator**: Total population +#' **Formula**: *DWE011* = 1,2 & *DWE10* = 1,2,3,4,5,6 & *DWE06* != 9, 96,98,99 & *DWE07* = 9, 96,98,99 +#' This indicator is calculated from the main dataset +#' +#' @param datalist A list with all hierarchical data frame for a survey data set. +#' format is expected to match the Excel export synchronized from kobo to RILD +#' and loaded with kobocruncher::kobo_data() +#' +#' @importFrom dplyr mutate case_when +#' @importFrom labelled labelled +#' +#' @return new calculated variable - appended or not... +#' +#' @export +#' @examples +#' ## data, cf example fct_re_map() +#' datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", +#' package = "IndicatorCalc")) +#' +#' ## Apply indicator function on datalist +#' datalist <- outcome16_1(datalist ) +#' +#' ## Visualise value +#' fct_plot_indic_donut(indicator = datalist[["main"]]$outcome16_1, +#' iconunicode = "f140") +outcome16_1 <- function(datalist ){ + # Mapper , + +mapper = list( + hierarchy = "main", + variablemap = data.frame( + label = +c("6. Who owns the dwelling that you currently live in?", "7. Who owns the land that your current dwelling is built on?", +"10. What type of documents does your household have for the housing you live in?", +"11. In the next 12 months, how likely or unlikely is it that you could lose your right to this housing and/or land, against your will?" +) + , + variable = +c("DWE06", "DWE07", "DWE10", "DWE11") + ), + modalitymap = data.frame( + variable = +c("DWE06", "DWE06", "DWE06", "DWE06", "DWE06", "DWE06", "DWE06", +"DWE06", "DWE06", "DWE06", "DWE06", "DWE06", "DWE07", "DWE07", +"DWE07", "DWE07", "DWE07", "DWE07", "DWE07", "DWE07", "DWE07", +"DWE07", "DWE07", "DWE07", "DWE07", "DWE07", "DWE10", "DWE10", +"DWE10", "DWE10", "DWE10", "DWE10", "DWE10", "DWE11", "DWE11", +"DWE11", "DWE11", "DWE11") + , + label = +c("Respondent or another HH member", "Unrelated Person", "Friend Or Relative", +"Ngo/Non-Religious Charity", "Government Agency Or Municipality", +"UNHCR", "Religious Organization/ Charity", "Foreign Government", +"Unowned/Squatting", "Other (Specify)", "Don't know", "Prefer not to respond", +"Respondent or another HH member", "Unrelated Person", "Friend Or Relative", +"Ngo/Non-Religious Charity", "Government Agency Or Municipality", +"UNHCR", "Religious Organization/ Charity", "Foreign Government", +"Unowned/Squatting", "Apartment Building", "Community", "Other(Specify)", +"Don't know", "Prefer not to respond", "Title deed", "Certificate of customary ownership", +"Certificate of occupancy", "Certificate of hereditary acquisition listed in registry", +"Rental contract", "Lease registered", "Other (Specify)", "Very unlikely", +"Somewhat unlikely", "Somewhat likely", "Very likely", "Don't Know" +) + , + standard = +c("1", "2", "3", "4", "5", "6", "7", "8", "9", "96", "98", "99", +"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "96", +"98", "99", "1", "2", "3", "4", "5", "6", "96", "1", "2", "3", +"4", "99") + )) + ## So first we check that we have what we need in the data set based on the mapper + check_map <- fct_check_map(datalist = datalist, + mapper = mapper) + if ( check_map == FALSE) { + cat( "There are missing data requirement to calculate Indicator Outcome 16.1") + } else { + + # likelihood of losing right for housing is unlikely + datalist[["main"]]$housing_cond1<- dplyr::case_when( + (as.integer(datalist[["main"]]$DWE11) == 1 | + as.integer(datalist[["main"]]$DWE11) == 2 ) ~ 1, + TRUE ~ 0) +# have official documents to proof residency + datalist[["main"]]$housing_cond2 <- dplyr::case_when( + as.integer(datalist[["main"]]$DWE10) == 96 ~ 0, + as.integer(datalist[["main"]]$DWE10) == 1 | + as.integer(datalist[["main"]]$DWE10) == 2 | + as.integer(datalist[["main"]]$DWE10) == 3 | + as.integer(datalist[["main"]]$DWE10) == 4 | + as.integer(datalist[["main"]]$DWE10) == 5 | + as.integer(datalist[["main"]]$DWE10) == 6 ~ 1) + +# not un-owned or not squatting + datalist[["main"]]$housing_cond3 <- dplyr::case_when( + as.integer(datalist[["main"]]$DWE06) == 9 | + as.integer(datalist[["main"]]$DWE06) == 96 | + as.integer(datalist[["main"]]$DWE06) == 98 | + as.integer(datalist[["main"]]$DWE06) == 99 | + as.integer(datalist[["main"]]$DWE07) == 9 | + as.integer(datalist[["main"]]$DWE07) == 96 | + as.integer(datalist[["main"]]$DWE07) == 98 | + as.integer(datalist[["main"]]$DWE07) == 99 ~ 0, + TRUE ~ 1) + +datalist[["main"]]$outcome16_1<- dplyr::case_when( + as.integer(datalist[["main"]]$housing_cond1) == 1 & + as.integer(datalist[["main"]]$housing_cond2) == 1 & + as.integer(datalist[["main"]]$housing_cond3) == 1 ~ 1, + + as.integer(datalist[["main"]]$housing_cond1) == 0 | + as.integer(datalist[["main"]]$housing_cond2) == 0 | + as.integer(datalist[["main"]]$housing_cond3) == 0 ~ 0) + +datalist[["main"]]$outcome16_1 <- labelled::labelled(datalist[["main"]]$outcome16_1, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Proportion of PoC with secure tenure rights and/or property rights to housing and/or land") + } + return(datalist) +} diff --git a/R/outcome16_2.R b/R/outcome16_2.R new file mode 100644 index 0000000..bb3610c --- /dev/null +++ b/R/outcome16_2.R @@ -0,0 +1,107 @@ +# WARNING - Generated by {fusen} from dev/indicators.Rmd: do not edit by hand + +#' outcome16_2 +#' +#' **Proportion of Persons of Concern covered by social protection floors/systems** +#' This indicator measures the proportion of persons of concern who receive government social protection benefits. +#' Social protection is a set of policies and programmes aimed at preventing or +#' protecting all people against poverty, vulnerability and social exclusion +#' throughout their life-course, with particular emphasis on vulnerable groups. +#' For persons of concern, being part of a social protection system is a measure +#' of integration and stability. This indicator is linked to SGD indicator +#' [1.3.1](https://unstats.un.org/sdgs/metadata/files/Metadata-01-03-01a.pdf). +#' +#' This indicator focuses on the social benefits received from the government in which UNHCR might be supporting UNHCR. +#' +#' | Standard Questions | +#' |:-------------------------:| +#' | SPF01 | +#' +#' **Numerator**: Total population covered by social protection floors/systems +#' **Denominator**: Total population +#' **Formula**: *SPF01*=1 +#' +#' This indicator is calculated from the main dataset +#' If PoC has covered by at least one of the social protection floors/systems +#' +#' @param datalist A list with all hierarchical data frame for a survey data set. +#' format is expected to match the Excel export synchronized from kobo to RILD +#' and loaded with kobocruncher::kobo_data() +#' +#' @importFrom dplyr mutate case_when +#' @importFrom labelled labelled +#' +#' @return new calculated variable - appended or not... +#' +#' @export +#' @examples +#' ## data, cf example fct_re_map() +#' datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", +#' package = "IndicatorCalc")) +#' ## Apply indicator function on datalist +#' datalist <- outcome16_2(datalist) +#' +#' table( datalist[["main"]]$outcome16_2, useNA = "ifany") +#' ## Visualise value +#' fct_plot_indic_donut(indicator = datalist[["main"]]$outcome16_2, +#' iconunicode = "f140") +outcome16_2 <- function(datalist ){ + # Mapper , + +mapper = list( + hierarchy = "main", + variablemap = data.frame( + label = +c("Social protection cash benefit", "Parental benefit", "Disability benefit", +"Unemployment benefit", "Employment injury benefit", "Old-age pension", +"Other vulnerability benefit", "Social assistance cash benefit" +) + , + variable = +c("SPF01a", "SPF01b", "SPF01c", "SPF01d", "SPF01e", "SPF01f", +"SPF01g", "SPF01h") + ), + modalitymap = data.frame( + variable = +c("SPF01a", "SPF01a", "SPF01a", "SPF01b", "SPF01b", "SPF01b", +"SPF01c", "SPF01c", "SPF01c", "SPF01d", "SPF01d", "SPF01d", "SPF01e", +"SPF01e", "SPF01e", "SPF01f", "SPF01f", "SPF01f", "SPF01g", "SPF01g", +"SPF01g", "SPF01h", "SPF01h", "SPF01h") + , + label = +c("Yes", "No", "Don't know", "Yes", "No", "Don't know", "Yes", +"No", "Don't know", "Yes", "No", "Don't know", "Yes", "No", "Don't know", +"Yes", "No", "Don't know", "Yes", "No", "Don't know", "Yes", +"No", "Don't know") + , + standard = +c("1", "0", "98", "1", "0", "98", "1", "0", "98", "1", "0", "98", +"1", "0", "98", "1", "0", "98", "1", "0", "98", "1", "0", "98" +) + )) + ## So first we check that we have what we need in the data set based on the mapper + check_map <- fct_check_map(datalist = datalist, + mapper = mapper) + if ( check_map == FALSE) { + cat( "There are missing data requirement to calculate Indicator Outcome 16.2") + } else { + +datalist[["main"]]$outcome16_2 <- dplyr::case_when( + datalist[["main"]]$SPF01a == 1 | + datalist[["main"]]$SPF01b == 1 | + datalist[["main"]]$SPF01c == 1 | + datalist[["main"]]$SPF01d == 1 | + datalist[["main"]]$SPF01e == 1 | + datalist[["main"]]$SPF01f == 1 | + datalist[["main"]]$SPF01g == 1 | + datalist[["main"]]$SPF01h == 1 ~ 1, + TRUE ~ 0) + + datalist[["main"]]$outcome16_2 <- labelled::labelled(datalist[["main"]]$outcome16_2, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Proportion of Persons of Concern covered by social protection + floors/systems") + + } + return(datalist) +} diff --git a/R/outcome1_2.R b/R/outcome1_2.R new file mode 100644 index 0000000..0226193 --- /dev/null +++ b/R/outcome1_2.R @@ -0,0 +1,156 @@ +# WARNING - Generated by {fusen} from dev/indicators.Rmd: do not edit by hand + +#' outcome1_2 +#' +#' **Proportion of children under 5 years of age whose births have been registered with a civil authority** +#' +#' This indicator measures the proportion of children under 5 years old whose births +#' have been registered with a civil authority and civil documentation has been issued. +#' This is directly linked to SGD indicator [16.9.1](https://unstats.un.org/sdgs/metadata/files/Metadata-16-09-01.pdf). +#' +#' The standard module for this indicator is taken from MICS and adjusted to UNHCR context. +#' +#' **Definitions** +#' +#' •**Birth registration** refers to the registration of new births by civil +#' authorities and completion of the process through the issuance of a birth certificate. +#' This also includes documents issued by UNHCR or other relevant organizations +#' when **given the authority by the State and these documents are recognized by national authorities**. +#' +#' **Clarifications** +#' +#' •Birth notifications or other hospital records and records from midwives or +#' traditional birth attendants, **issued solely by UNHCR or its partners shall +#' not be considered** as birth certificates although they are important sources +#' for establishing the total number of births. +#' Operations are encouraged to track both the number of birth notifications and +#' birth registrations but for the purpose of this indicator should report +#' the number of births registered. +#' +#' • The standard indicator used in DHS, MICS and RMS to report on birth +#' registration refers to the percentage of children under age 5 (0-59 months) +#' with a birth certificate, regardless of whether or not it was seen by the +#' interviewer, or whose birth was reported as registered with civil +#' authorities at the time of survey. +#' +#' | Standard Questions | +#' |:------------------:| +#' | REG03 - REG04 | +#' +#' **Numerator**: Number of children under 5 years old who are registered with civil authorities +#' **Denominator**: Total number of children under 5 years old +#' **Formula**: (*REG03=1 \| REG04=1) / Number of children under 5* +#' +#' This indicator comes from the individual dataset +#' ind$REG03 - birth certificate +#' ind$REG04 - birth has been registered +#' +#' @param datalist A list with all hierarchical data frame for a survey data set. +#' format is expected to match the Excel export synchronized from kobo to RILD +#' and loaded with kobocruncher::kobo_data() +#' +#' @importFrom dplyr mutate case_when +#' @importFrom labelled labelled +#' +#' @return new calculated variable - appended or not... +#' +#' @export +#' @examples +#' ## data, cf example fct_re_map() +#' datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", +#' package = "IndicatorCalc")) +#' +#' ## Apply indicator function on datalist +#' datalist <- outcome1_2(datalist) +#' table(datalist[["ind"]]$outcome1_2, useNA = "ifany") +#' table(datalist[["ind"]]$less_than_5, useNA = "ifany") +#' table(datalist[["ind"]]$HH07, useNA = "ifany") +#' barplot(as.integer(datalist[["ind"]]$HH07)) +#' table(datalist[["ind"]]$birthCertificate, useNA = "ifany") +#' table(datalist[["ind"]]$birthRegistered, useNA = "ifany") +#' ## Visualise value +#' fct_plot_indic_donut(indicator = datalist[["ind"]]$outcome1_2, +#' iconunicode = "f140") +#' fct_plot_indic_donut(indicator = datalist[["ind"]]$birthCertificate, +#' iconunicode = "f140") +#' fct_plot_indic_donut(indicator = datalist[["ind"]]$birthRegistered, +#' iconunicode = "f140") +outcome1_2 <- function(datalist){ + ## mapper +mapper = list( + hierarchy = "ind", + variablemap = data.frame( + label = + c("3. Does ${name_individual} have a birth certificate?", "4. Has ${name_individual}'s birth been registered with civil authorities?", + "6. Can you estimate how old is ${HH02}?") + , + variable = + c("REG03", "REG04", "HH07") + ), + modalitymap = data.frame( + variable = + c("REG03", "REG03", "REG03", "REG04", "REG04", "REG04", "REG04", + "HH07") + , + label = + c("Yes", "No", "Don't know", "Yes", "No", "Don't know", "Prefer not to say", + NA) + , + standard = + c("1", "0", "98", "1", "0", "98", "99", NA) + )) + ## So first we check that we have what we need in the data set based on the mapper + check_map <- fct_check_map(datalist = datalist, + mapper = mapper) + if ( check_map == FALSE) { + cat( "There are missing data requirement to calculate Indicator Outcome 1.2") + } else { + + ##Calculate children who has a birth certificate + datalist[["ind"]]$birthCertificate <- dplyr::case_when( + as.integer(datalist[["ind"]]$REG03) == 0 | + as.integer(datalist[["ind"]]$REG03) == 98 ~ 0, + as.integer(datalist[["ind"]]$REG03) == 1 ~ 1) + + datalist[["ind"]]$birthCertificate <- labelled::labelled(datalist[["ind"]]$birthCertificate, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Children with a birth certificate") + + ##Calculate children who has been registered with civil authorities + datalist[["ind"]]$birthRegistered <- dplyr::case_when( + as.integer(datalist[["ind"]]$REG04) == 0 | + as.integer(datalist[["ind"]]$REG04) == 98 ~ 0, + as.integer(datalist[["ind"]]$REG04) == 1 ~ 1, + as.integer(datalist[["ind"]]$REG04) == 99 ~ NA_real_) + + datalist[["ind"]]$birthRegistered <- labelled::labelled(datalist[["ind"]]$birthRegistered, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Children with birth registered with civil authorities") + + ## less than 5 + datalist[["ind"]]$less_than_5 <- dplyr::case_when( + as.numeric(datalist[["ind"]]$HH07) < 5 ~ 1, + TRUE ~ 0) + datalist[["ind"]]$less_than_5 <- labelled::labelled(datalist[["ind"]]$less_than_5, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Is a Children") + + ##if the birth is registered or child has a birth certificate + datalist[["ind"]]$outcome1_2 <- dplyr::case_when( + (datalist[["ind"]]$birthRegistered == 1 | + datalist[["ind"]]$birthCertificate == 1) & + as.integer(datalist[["ind"]]$HH07) < 5 ~ 1, + + (datalist[["ind"]]$birthRegistered == 0 & + datalist[["ind"]]$birthCertificate == 0) & + as.integer(datalist[["ind"]]$HH07) < 5 ~ 0, + + TRUE ~ NA_real_ ) + + datalist[["ind"]]$outcome1_2 <- labelled::labelled(datalist[["ind"]]$outcome1_2, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Proportion of children under 5 years of age whose births have been registered with a civil authority") + + } + return(datalist) +} diff --git a/R/outcome1_3.R b/R/outcome1_3.R new file mode 100644 index 0000000..3d26598 --- /dev/null +++ b/R/outcome1_3.R @@ -0,0 +1,207 @@ +# WARNING - Generated by {fusen} from dev/indicators.Rmd: do not edit by hand + +#' outcome1_3 +#' +#' **Proportion of Persons of Concern with legally recognized identity documents or credentials** +#' +#' This indicator measures the proportion of persons of concern who possess legally +#' recognized and valid identity documents or credentials. +#' +#' Establishing one's identity and possessing legally recognized and valid identity +#' documents or credentials ensures the legal protection of persons of concern, +#' protection from refoulement, registration of life events to prevent statelessness, +#' as well as access to services. +#' +#' **Definitions** +#' +#' Identity document or credential is any document or credential which may be +#' used as proof of identity, which may also include reference to the +#' individuals' legal status and associated rights vis-à-vis the host +#' State and/or UNHCR. +#' +#' | Standard Questions | +#' |:-------------------------------------:| +#' | REG01 - REG02 - REG03 / REG05 - REG06 | +#' +#' **Numerator**: Total population with valid identity documents or credentials +#' +#' **Denominator**: Total population +#' +#' **Formula**: ( *REG01=1 \| REG02=1 \| REG03=1 \| REG05=1 \| REG06=1) / Total population* +#' +#' This indicator comes from the individual dataset - +#' Calculate valid identity documents for under 5 with REG05 and REG06 variables +#' ind$REG05a - passport +#' ind$REG05b - civil/government issued ID +#' ind$REG05c - residency permit +#' ind$REG05d - statelessness documentation +#' ind$REG05e - household card of address/family book +#' ind$REG05f - social security card +#' ind$REG06 - any other document establishes identity +#' add birth certificate as additional document from REG03 +#' +#' @param datalist A list with all hierarchical data frame for a survey data set. +#' format is expected to match the Excel export synchronized from kobo to RILD +#' and loaded with kobocruncher::kobo_data() +#' +#' @importFrom dplyr mutate case_when +#' @importFrom labelled labelled +#' +#' @return new calculated variable - appended or not... +#' +#' @export +#' @examples +#' ## data, cf example fct_re_map() +#' datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", +#' package = "IndicatorCalc")) +#' ## Apply indicator function on datalist +#' datalist <- outcome1_3(datalist) +#' +#' ## Visualise value +#' fct_plot_indic_donut(indicator = datalist[["ind"]]$outcome1_3, +#' iconunicode = "f140") +#' +#' fct_plot_indic_donut(indicator = datalist[["ind"]]$document_above5, +#' iconunicode = "f140") +#' +#' fct_plot_indic_donut(indicator = datalist[["ind"]]$document_under5, +#' iconunicode = "f140") +outcome1_3 <- function(datalist){ + ## Mapper +mapper = list( + hierarchy = "ind", + variablemap = data.frame( + label = +c("Passport?", "Birth certificate?", "Civil/government-issued ID card ?", +"Residency permit (both temporary and permanent)?", "Statelessness documentation?", +"Household card of address / family book/ marriage certificate?", +"Social security card?", "2. Does ${name_individual} have any other document that establishes his/her legal identity?", +"3. Does ${name_individual} have a birth certificate?", "Passport?", +"Civil/government-issued ID card ?", "Residency permit (both temporary and permanent)?", +"Statelessness documentation?", "Household card of address / family book/ marriage certificate?", +"Social security card?", "6. Does ${name_individual} have any other document that establishes your legal identity?" +) + , + variable = +c("REG01a", "REG01b", "REG01c", "REG01d", "REG01e", "REG01f", +"REG01g", "REG02", "REG03", "REG05a", "REG05b", "REG05c", "REG05d", +"REG05e", "REG05f", "REG06") + ), + modalitymap = data.frame( + variable = +c("REG01a", "REG01a", "REG01a", "REG01b", "REG01b", "REG01b", +"REG01c", "REG01c", "REG01c", "REG01d", "REG01d", "REG01d", "REG01e", +"REG01e", "REG01e", "REG01f", "REG01f", "REG01f", "REG01g", "REG01g", +"REG01g", "REG02", "REG02", "REG02", "REG03", "REG03", "REG03", +"REG05a", "REG05a", "REG05a", "REG05b", "REG05b", "REG05b", "REG05c", +"REG05c", "REG05c", "REG05d", "REG05d", "REG05d", "REG05e", "REG05e", +"REG05e", "REG05f", "REG05f", "REG05f", "REG06", "REG06", "REG06" +) + , + label = +c("Yes", "No", "Prefer not to respond", "Yes", "No", "Prefer not to respond", +"Yes", "No", "Prefer not to respond", "Yes", "No", "Prefer not to respond", +"Yes", "No", "Prefer not to respond", "Yes", "No", "Prefer not to respond", +"Yes", "No", "Prefer not to respond", "Yes", "No", "Prefer not to respond", +"Yes", "No", "Don't know", "Yes", "No", "Prefer not to respond", +"Yes", "No", "Prefer not to respond", "Yes", "No", "Prefer not to respond", +"Yes", "No", "Prefer not to respond", "Yes", "No", "Prefer not to respond", +"Yes", "No", "Prefer not to respond", "Yes", "No", "Prefer not to respond" +) + , + standard = +c("1", "0", "99", "1", "0", "99", "1", "0", "99", "1", "0", "99", +"1", "0", "99", "1", "0", "99", "1", "0", "99", "1", "0", "99", +"1", "0", "98", "1", "0", "99", "1", "0", "99", "1", "0", "99", +"1", "0", "99", "1", "0", "99", "1", "0", "99", "1", "0", "99" +) + )) + + ## So first we check that we have what we need in the data set based on the mapper + check_map <- fct_check_map(datalist = datalist, + mapper = mapper) + if ( check_map == FALSE) { + cat( "There are missing data requirement to calculate Indicator Outcome 1.3") + } else { + + +datalist[["ind"]]$document_under5 <- dplyr::case_when( + as.integer(datalist[["ind"]]$REG05a) == 1 | + as.integer(datalist[["ind"]]$REG05b) == 1 | + as.integer(datalist[["ind"]]$REG05c) == 1 | + as.integer(datalist[["ind"]]$REG05d) == 1 | + as.integer(datalist[["ind"]]$REG05e) == 1 | + as.integer(datalist[["ind"]]$REG05f) == 1 | + as.integer(datalist[["ind"]]$REG06) == 1 | + as.integer(datalist[["ind"]]$REG03) == 1 ~ 1, + + as.integer(datalist[["ind"]]$REG05a) != 1 & + as.integer(datalist[["ind"]]$REG05b) != 1 & + as.integer(datalist[["ind"]]$REG05c) != 1 & + as.integer(datalist[["ind"]]$REG05d) != 1 & + as.integer(datalist[["ind"]]$REG05e) != 1 & + as.integer(datalist[["ind"]]$REG05f) != 1 & + as.integer(datalist[["ind"]]$REG06) != 1 & + as.integer(datalist[["ind"]]$REG03) != 1 ~ 0, + + TRUE ~ NA_real_ ) + +datalist[["ind"]]$document_under5 <- labelled::labelled(datalist[["ind"]]$document_under5, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Proportion of Persons above 5 with legally + recognized identity documents or credentials") + + ###Calculate valid identity documents for above 5 with REG01 and REG02 variables + #ind$REG01a # passport + #ind$REG01b # birth certificate + #ind$REG01c # civil/ government issued ID + #ind$REG01d # residency permit + #ind$REG01e # statelessness documentation + #ind$REG01f # household card of address/family book + #ind$REG01g # social security card + #ind$REG02 # any other document establishes identity + +#Make sure to delete REG01e below from the script if you don't have any stateless + +datalist[["ind"]]$document_above5 <- dplyr::case_when( + as.integer(datalist[["ind"]]$REG01a) == 1 | + as.integer(datalist[["ind"]]$REG01b) == 1 | + as.integer(datalist[["ind"]]$REG01c) == 1 | + as.integer(datalist[["ind"]]$REG01d) == 1 | + as.integer(datalist[["ind"]]$REG01e) == 1 | + as.integer(datalist[["ind"]]$REG01f) == 1 | + as.integer(datalist[["ind"]]$REG01g) == 1 | + as.integer(datalist[["ind"]]$REG02) == 1 ~ 1, + + as.integer(datalist[["ind"]]$REG01a) != 1 & + as.integer(datalist[["ind"]]$REG01b) != 1 & + as.integer(datalist[["ind"]]$REG01c) != 1 & + as.integer(datalist[["ind"]]$REG01d) != 1 & + as.integer(datalist[["ind"]]$REG01e) != 1 & + as.integer(datalist[["ind"]]$REG01f) != 1 & + as.integer(datalist[["ind"]]$REG01g) != 1 & + as.integer(datalist[["ind"]]$REG02) != 1 ~ 0, + + TRUE ~ NA_real_) + +datalist[["ind"]]$document_above5 <- labelled::labelled(datalist[["ind"]]$document_above5, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Proportion of Persons above 5 with legally + recognized identity documents or credentials") + + ##Combine both age groups +datalist[["ind"]]$outcome1_3<- dplyr::case_when( + (datalist[["ind"]]$document_above5 == 1 | + datalist[["ind"]]$document_under5 == 1 ) ~ 1, + + (datalist[["ind"]]$document_above5 == 0 | + datalist[["ind"]]$document_under5 == 0 ) ~ 0) + +datalist[["ind"]]$outcome1_3 <- labelled::labelled(datalist[["ind"]]$outcome1_3, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Proportion of Persons with legally + recognized identity documents or credentials") + + } + return(datalist) +} diff --git a/R/outcome4_1.R b/R/outcome4_1.R new file mode 100644 index 0000000..063e372 --- /dev/null +++ b/R/outcome4_1.R @@ -0,0 +1,103 @@ +# WARNING - Generated by {fusen} from dev/indicators.Rmd: do not edit by hand + +#' outcome4_1 +#' +#' +#' **Proportion of Persons of Concern who know where to access available GBV services** +#' +#' The indicator is defined as the proportion of persons of concern who know where to access at least one of the four available gender-based violence services in the aftermath of a GBV incident. +#' +#' This is linked to the Inter-Agency Minimum Standards for Gender-Based Violence in Emergencies Programming. +#' +#' The response options will appear as below when the standardized module is used. +#' +#' *Definitions:* +#' +#' 1. **GBV01a (**Health): "Medical treatment and health care to address the immediate and long-term physical and mental health effects of GBV. This can include initial examination and treatment, follow-up medical care, mental health care, and health-related legal services, such as preparation of documentation and provision of evidence during judicial and related processes." +#' +#' 2. **GBV01b** (Psycho-social/ case management): "Psychosocial care and support to assist with healing and recovery from emotional, psychological and social effects. This includes crisis care as well as longer-term emotional and practical support for the survivor and her/his family, information and advocacy, case management, and educating family members so that they can support the survivor's healing and recovery. +#' +#' 3. **GBV01c** (Safety/security): "Options for safety and protection for survivors and their families who are at risk of further violence and who wish to be protected. This can include safe shelters, police or community security, relocation, or in the case of children, alternative care arrangements". +#' +#' 4. **GBV01d** (Legal assistance): "Legal actors will clearly and honestly inform the victim/survivor of the #' procedures, limitations, pros, and cons of all existing legal options". +#' +#' | Standard Questions | +#' |:------------------:| +#' | GBV01 | +#' +#' **Numerator**: Total population who indicates knowing where to access available GBV services +#' +#' **Denominator**: Total population +#' +#' **Formula**: ( *GBV01a=1 \| GBV01b=1 \| GBV01c=1 \| GBV01d=1) / Total population* +#' +#' This indicator comes from main dataset based on the respondent randomly selected for individual level +#' +#' +#' @param datalist A list with all hierarchical data frame for a survey data set. +#' format is expected to match the Excel export synchronized from kobo to RILD +#' and loaded with kobocruncher::kobo_data() +#' +#' @importFrom dplyr mutate case_when +#' @importFrom labelled labelled +#' +#' @return new calculated variable - appended or not... +#' +#' @export +#' @examples +#' ## data, cf example fct_re_map() +#' datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", +#' package = "IndicatorCalc")) +#' ## Apply indicator function on datalist +#' datalist <- outcome4_1(datalist ) +#' +#' ## Visualise value +#' fct_plot_indic_donut(indicator = datalist[["main"]]$outcome1_4, +#' iconunicode = "f140") +outcome4_1 <- function(datalist){ + + # mapper , +mapper = list( + hierarchy = "main", + variablemap = data.frame( + label = +c("Health services", "Psycho-social services", "Safety and security services? (police, safe shelters)", +"Legal assistance") + , + variable = +c("GBV01a", "GBV01b", "GBV01c", "GBV01d") + ), + modalitymap = data.frame( + variable = +c("GBV01a", "GBV01a", "GBV01a", "GBV01b", "GBV01b", "GBV01b", +"GBV01c", "GBV01c", "GBV01c", "GBV01d", "GBV01d", "GBV01d") + , + label = +c("Yes", "No", "Don't know", "Yes", "No", "Don't know", "Yes", +"No", "Don't know", "Yes", "No", "Don't know") + , + standard = +c("1", "0", "98", "1", "0", "98", "1", "0", "98", "1", "0", "98" +) + )) + ## So first we check that we have what we need in the data set based on the mapper + check_map <- fct_check_map(datalist = datalist, + mapper = mapper) + if ( check_map == FALSE) { + cat( "There are missing data requirement to calculate Indicator Outcome 4.1") + } else { + +datalist[["main"]]$outcome4_1 <- dplyr::case_when( + as.integer(datalist[["main"]]$GBV01a) == 1 | + as.integer(datalist[["main"]]$GBV01b) == 1 | + as.integer(datalist[["main"]]$GBV01c) == 1 | + as.integer(datalist[["main"]]$GBV01d) == 1 ~ 1, + TRUE ~ 0) + +datalist[["main"]]$outcome4_1 <- labelled::labelled(datalist[["main"]]$outcome4_1, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Proportion of PoC who know where to + access available GBV services") + } + return(datalist) +} diff --git a/R/outcome4_2.R b/R/outcome4_2.R new file mode 100644 index 0000000..ec52f83 --- /dev/null +++ b/R/outcome4_2.R @@ -0,0 +1,126 @@ +# WARNING - Generated by {fusen} from dev/indicators.Rmd: do not edit by hand + +#' outcome4_1 +#' +#' +#' **Proportion of Persons of Concern who do not accept violence against women** +#' +#' This indicator measures the proportion of persons of concern who disagree that a husband is justified in hitting or beating his wife for a list of specific reasons. +#' This serves as a proxy indicator for those who do not accept violence against women. +#' +#' This module is taken from [Domestic Violence Module](https://dhsprogram.com/pubs/pdf/DHSQMP/DHS6_Module_Domestic_Violence_6Aug2014_DHSQMP.pdf) from DHS and similar module used by MICS. +#' Due to its sensitivity, this module is only recommended for face-to-face surveys. +#' +#' *Data Collection Safeguards* +#' +#' Enumerators (and data collection teams) must be trained on GBV safe disclosures and referrals prior to undertaking household surveys (or at least get a briefing on the GBV referral pathway in their area). +#' To ensure quality feedback and limit risks of harm, it is advisable to administer the survey to members of the household separately in confidential spaces (consider using interview rooms at community centres in settings where layout of communities' homes do not allow for confidential discussions). +#' +#' It is also recommended to ensure same-sex enumerators to minimize risks of SEA particularly for female respondents. +#' +#' | Standard Questions | +#' |:------------------:| +#' | VAW01 | +#' +#' **Numerator**: Population 18 and above who disagree that a husband is justified in hitting or beating his wife for all the following reasons: +#' +#' 1. Going out without telling him. +#' +#' 2. Neglecting the children. +#' +#' 3. Arguing with him. +#' +#' 4. Refusing to have sexual intercourse with him. +#' +#' 5. Burning food. +#' +#' **Denominator**: Total population 18 and above +#' +#' **Formula**: ( *GBV01a=1 \| GBV01b=1 \| GBV01c=1 \| GBV01d=1) / Total population* +#' +#' +#' This indicator comes from main dataset based on the respondent randomly selected for individual level +#' +#' If randomly selected adult who believes that a husband is justified in beating his wife in various circumstances +#' +#' If yes selected for any of the circumstances +#' Prefer not to respond will be put into missing +#' +#' @param datalist A list with all hierarchical data frame for a survey data set. +#' format is expected to match the Excel export synchronized from kobo to RILD +#' and loaded with kobocruncher::kobo_data() +#' +#' @importFrom dplyr mutate case_when +#' @importFrom labelled labelled +#' +#' @return new calculated variable - appended or not... +#' +#' @export +#' @examples +#' ## data, cf example fct_re_map() +#' datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", +#' package = "IndicatorCalc")) +#' ## Apply indicator function on datalist +#' datalist <- outcome4_2(datalist) +#' +#' ## Visualise value +#' fct_plot_indic_donut(indicator = datalist[["main"]]$outcome4_2, +#' iconunicode = "f140") +outcome4_2 <- function(datalist ){ + # Mapper , +mapper = list( + hierarchy = "main", + variablemap = data.frame( + label = +c("If she goes out without telling him?", "If she neglects the children?", +"If she argues with him?", "If she refuses to have sex with him?", +"If she burns the food?") + , + variable = +c("VAW01a", "VAW01b", "VAW01c", "VAW01d", "VAW01e") + ), + modalitymap = data.frame( + variable = +c("VAW01a", "VAW01a", "VAW01a", "VAW01b", "VAW01b", "VAW01b", +"VAW01c", "VAW01c", "VAW01c", "VAW01d", "VAW01d", "VAW01d", "VAW01e", +"VAW01e", "VAW01e") + , + label = +c("Yes", "No", "Prefer not to respond", "Yes", "No", "Prefer not to respond", +"Yes", "No", "Prefer not to respond", "Yes", "No", "Prefer not to respond", +"Yes", "No", "Prefer not to respond") + , + standard = +c("1", "0", "99", "1", "0", "99", "1", "0", "99", "1", "0", "99", +"1", "0", "99") + )) + + ## So first we check that we have what we need in the data set based on the mapper + check_map <- fct_check_map(datalist = datalist, + mapper = mapper) + if ( check_map == FALSE) { + cat( "There are missing data requirement to calculate Indicator Outcome 4.2") + } else { + +datalist[["main"]]$outcome4_2<- dplyr::case_when( + as.integer(datalist[["main"]]$VAW01a) == 1 | + as.integer(datalist[["main"]]$VAW01b) == 1 | + as.integer(datalist[["main"]]$VAW01c) == 1 | + as.integer(datalist[["main"]]$VAW01d) == 1 | + as.integer(datalist[["main"]]$VAW01e) == 1 ~ 0, + + as.integer(datalist[["main"]]$VAW01a) == 0 & + as.integer(datalist[["main"]]$VAW01b) == 0 & + as.integer(datalist[["main"]]$VAW01c) == 0 & + as.integer(datalist[["main"]]$VAW01d) == 0 & + as.integer(datalist[["main"]]$VAW01e) == 0 ~ 1, + + TRUE ~ NA_real_) + +datalist[["main"]]$outcome4_2 <- labelled::labelled(datalist[["main"]]$outcome4_2, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Proportion of PoC who do not accept + violence against women" ) + } + return(datalist) +} diff --git a/R/outcome5_2.R b/R/outcome5_2.R new file mode 100644 index 0000000..4eeebdf --- /dev/null +++ b/R/outcome5_2.R @@ -0,0 +1,102 @@ +# WARNING - Generated by {fusen} from dev/indicators.Rmd: do not edit by hand + +#' outcome5_2 +#' +#' **Proportion of children who participate in community-based child protection programmes** +#' +#' The indicator is defined as the the proportion of children who participate in community-based child protection programmes. +#' The module for this question comes from UNHCR Core indicator metadata. +#' +#' Children' participation in community-based recreational and child protection activities is a key mechanism for the protection of children by providing children with a protected environment in which they can participate in organized activities to play, socialize, learn, and express themselves, promoting their psycho-social well-being and reducing the risk of abuse, violence or exploitation. +#' +#' | Standard Questions | +#' |:------------------:| +#' | COMM01-COMM04 | +#' +#' **Numerator**: Total number of children between 4-17 years who participate in community-based recreational and child protection programmes +#' +#' **Denominator**: Total [number of children between 4-17 years +#' +#' **Formula**: ( *COMM01=1 & COMM02 \>=2 \| & COMM03=1 & COMM04=1) / Total number of children 4-17 years* +#' This indicator comes from the individual level dataset +#' +#' +#' Children who participate in community-based programmes at least 2 times, under adult supervision in a physically safe area +#' +#' @param datalist A list with all hierarchical data frame for a survey data set. +#' format is expected to match the Excel export synchronized from kobo to RILD +#' and loaded with kobocruncher::kobo_data() +#' +#' @importFrom dplyr mutate case_when +#' @importFrom labelled labelled +#' +#' @return new calculated variable - appended or not... +#' +#' @export +#' @examples +#' ## data, cf example fct_re_map() +#' datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", +#' package = "IndicatorCalc")) +#' ## Apply indicator function on datalist +#' datalist <- outcome5_2(datalist ) +#' +#' ## Visualise value +#' fct_plot_indic_donut(indicator = datalist[["ind"]]$outcome5_2, +#' iconunicode = "f140") +outcome5_2 <- function(datalist){ + ## Mapper , +mapper = list( + hierarchy = "ind", + variablemap = data.frame( + label = +c("1. Has ${child_edu_name} participated in sports, arts, cultural activities or other after-school programmes for children outside the home in the last month?", +"2. In the past month, how many times did ${child_edu_name} participate in these activities?", +"3. Were they in a physically safe area while participating in the activity?", +"4. Were there adults supervising the activities?") + , + variable = +c("COMM01", "COMM02", "COMM03", "COMM04") + ), + modalitymap = data.frame( + variable = +c("COMM01", "COMM01", "COMM02", "COMM03", "COMM03", "COMM03", +"COMM04", "COMM04", "COMM04") + , + label = +c("Yes", "No", NA, "Yes", "No", "Don't know", "Yes", "No", "Don't know" +) + , + standard = +c("1", "0", NA, "1", "0", "98", "1", "0", "98") + )) + + ## So first we check that we have what we need in the data set based on the mapper + check_map <- fct_check_map(datalist = datalist, + mapper = mapper) + if ( check_map == FALSE) { + cat( "There are missing data requirement to calculate Indicator Outcome 5.2") + } else { + +datalist[["ind"]]$outcome5_2<- dplyr::case_when( + ( as.integer(datalist[["ind"]]$COMM01) == 1 & + as.integer(datalist[["ind"]]$COMM02) >= 2 & + as.integer(datalist[["ind"]]$COMM03) == 1 & + as.integer(datalist[["ind"]]$COMM04) == 1) ~ 1, + + ( as.integer(datalist[["ind"]]$COMM01) == 0 | + ( as.integer(datalist[["ind"]]$COMM02) < 2 | + as.integer(datalist[["ind"]]$COMM02) == 98 ) | + ( as.integer(datalist[["ind"]]$COMM03) == 0 | + as.integer(datalist[["ind"]]$COMM03) == 98) | + ( as.integer(datalist[["ind"]]$COMM04) == 0 | + as.integer(datalist[["ind"]]$COMM04) == 98)) ~ 0, + + TRUE ~ NA_real_) + +datalist[["ind"]]$outcome5_2 <- labelled::labelled(datalist[["ind"]]$outcome5_2, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Proportion of children who participate + in community-based child protection programmes") + } + return(datalist) +} diff --git a/R/outcome8_2.R b/R/outcome8_2.R new file mode 100644 index 0000000..71df41b --- /dev/null +++ b/R/outcome8_2.R @@ -0,0 +1,118 @@ +# WARNING - Generated by {fusen} from dev/indicators.Rmd: do not edit by hand + +#' outcome8_2 +#' +#' +#' **Proportion of Persons of Concern with primary reliance on clean (cooking) fuels and technology** +#' +#' This indicator measures the proportion of population who use clean fuel and technology as primary source of cooking. +#' Any type of cooking fuel and technology meeting the WHO set standards on indoor air emissions, as their main cooking solution. +#' This is also linked to SGD indicator [7.1.2](https://sdg-tracker.org/energy) . +#' +#' The standard questions are similar to MICS and the calculation of indicators are based on [UNICEF MICS](https://mics.unicef.org/files?job=W1siZiIsIjIwMTcvMDIvMDMvMTYvMjcvMjUvNTk5L1BpY3RvcmlhbHNfV0hPX0hvdXNlaG9sZF9FbmVyZ3lfVXNlX0NhdGFsb2d1ZV9TZXB0ZW1iZXJfMjAxNl8ucGRmIl1d&sha=57b4a452fcc0ac88) and [WHO](https://www.who.int/data/gho/indicator-metadata-registry/imr-details/5662) guidance on the calculation of the same SGD indicator. +#' +#' | Standard Questions | +#' |:------------------:| +#' | COOK01- COOK03 | +#' +#' **Numerator**: Percentage of population with primary reliance on clean fuels and technologies for cooking +#' +#' **Denominator**: Total population +#' +#' **Formula**: COOK01=1 (there is a cooking device) & (COOK02=1,2,3,4,5) \| (COOK02=6 & COOK03=1,2) / Total Population +#' +#' This indicator comes from household level dataset +#' Based on MICS calculation : TC4.1 +#' +#' @param datalist A list with all hierarchical data frame for a survey data set. +#' format is expected to match the Excel export synchronized from kobo to RILD +#' and loaded with kobocruncher::kobo_data() +#' +#' @importFrom dplyr mutate case_when +#' @importFrom labelled labelled +#' +#' @return new calculated variable - appended or not... +#' +#' @export +#' @examples +#' ## data, cf example fct_re_map() +#' datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", +#' package = "IndicatorCalc")) +#' ## Apply indicator function on datalist +#' datalist <- outcome8_2(datalist ) +#' +#' ## Visualise value +#' fct_plot_indic_donut(indicator = datalist[["main"]]$outcome8_2, +#' iconunicode = "f140") +outcome8_2 <- function(datalist){ +mapper = list( + hierarchy = "ind", + hierarchy = "main", + variablemap = data.frame( + label = +c("1. Is any food or drink consumed by household members cooked or prepared at the household dwelling using a cookstove, fire or other cooking device?", +"2. What does this household use for cooking most of the time, including cooking food, making tea/coffee, boiling drinking water?", +"3. What type of fuel or energy source does this household use most of the time in this cookstove or device for cooking food, making tea/coffee and boiling drinking water?" +) + , + variable = +c("COOK01", "COOK02", "COOK03") + ), + modalitymap = data.frame( + variable = +c("COOK01", "COOK01", "COOK02", "COOK02", "COOK02", "COOK02", +"COOK02", "COOK02", "COOK02", "COOK02", "COOK02", "COOK02", "COOK02", +"COOK03", "COOK03", "COOK03", "COOK03", "COOK03", "COOK03", "COOK03", +"COOK03", "COOK03", "COOK03", "COOK03", "COOK03", "COOK03", "COOK03", +"COOK03", "COOK03", "COOK03", "COOK03", "COOK03", "COOK03", "COOK03", +"COOK03") + , + label = +c("Yes", "No", "Solar cooker (thermal energy from the sun, not solar panels)", +"Electric stove", "Piped natural gas stove", "Biogas stove", +"Liquefied petroleum gas (LPG)/ cooking gas stove", "Liquid fuel stove", +"Manufactured solid fuel stove", "Traditional solid fuel stove (non-manufactured)", +"Moveable firepan", "Three stone stove/open fire", "Other, specify", +"Alcohol/ethanol", "Liquefied petroleum gas", "Biogas", "Electricity from the national grid", +"Electricity from solar mini-grid", "Electricity from diesel generator", +"Electricity from hybrid mini-grid (i.e., solar+ diesel, solar+ national grid, etc..)", +"Gasoline/diesel (not in generator)", "Kerosene/paraffin", "Coal/lignite unprocessed", +"Coal/lignite briquettes/pellets", "Charcoal unprocessed", "Charcoal briquettes/pellets", +"Wood (collected)", "Wood (distributed/purchased)", "Agricultural or crop residue/grass/ straw/ shrubs/ corn cobs", +"Animal waste/dung", "Processed biomass pellets/briquettes", +"Woodchips", "Garbage/plastic", "Sawdust", "Other, specify") + , + standard = +c("1", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", +"96", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", +"12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "96" +) + )) + + ## So first we check that we have what we need in the data set based on the mapper + check_map <- fct_check_map(datalist = datalist, + mapper = mapper) + if ( check_map == FALSE) { + cat( "There are missing data requirement to calculate Indicator Outcome 8.2") + } else { + +datalist[["main"]]$outcome8_2 <- dplyr::case_when( + ( as.integer(datalist[["main"]]$COOK01) == 1 & + (datalist[["main"]]$COOK02 %in% c("1", "2", "3", "4", "5")) | + (datalist[["main"]]$COOK02 %in% c("6") & + datalist[["main"]]$COOK03 %in% c("1", "3")) ) ~ 1, + + ( as.integer(datalist[["main"]]$COOK01) == 1 & + (datalist[["main"]]$COOK02 %in% c("7", "8", "9", "10", "96")) | + ((datalist[["main"]]$COOK02 %in% c("6") & + !(datalist[["main"]]$COOK03 %in% c("1", "3") )))) ~ 0 , + TRUE ~ NA_real_ ) + +datalist[["main"]]$outcome8_2 <- labelled::labelled(datalist[["main"]]$outcome8_2, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Proportion of Persons of Concern with + primary reliance on clean (cooking) fuels and technology") + } + + return(datalist) +} diff --git a/R/outcome9_1.R b/R/outcome9_1.R new file mode 100644 index 0000000..9413edc --- /dev/null +++ b/R/outcome9_1.R @@ -0,0 +1,222 @@ +# WARNING - Generated by {fusen} from dev/indicators.Rmd: do not edit by hand + +#' outcome9_1 +#' +#' **Proportion of Persons of Concern living in habitable and affordable housing** +#' +#' This indicator measures the proportion of persons of concern living in habitable and affordable housing. +#' This indicator focuses only on habitability and affordability. +#' It is limited in its reflection on other key aspects of adequate housing including security of tenure, availability of other basic services and infrastructure, accessibility, location of housing, and cultural appropriateness. +#' It is also linked to SGD indicator [11.1.1](https://unstats.un.org/sdgs/metadata/files/Metadata-11-01-01.pdf). +#' +#' The right to access adequate housing is protected by international law. +#' The concept of "adequacy" means that housing is more than four walls and a roof as indicated in [The Sphere Handbook](https://spherestandards.org/wp-content/uploads/Sphere-Handbook-2018-EN.pdf). +#' Habitable housing primarily refers to the fact that the housing should provide protection from cold, damp, heat, rain, wind, and other threats to health, structural hazards, and disease vectors and it should not be overcrowded. +#' As shelter/housing is primarily a contextual element, there may be discrepancies from country to country on how this data is measured.Habitable shelter is measured based on having improved material for the dwelling as indicated in [DHS](https://dhsprogram.com/pubs/pdf/AS61/AS61.pdf) publication on housing conditions which is also used by [MICS6](https://mics.unicef.org/tools). +#' Overcrowding is also used which occurs if there are more than three people per habitable room as defined by [UN-Habitat](https://www.ncbi.nlm.nih.gov/books/NBK535289/table/ch3.tab2/). +#' +#' Affordable housing refers to the fact that the cost of housing and its related expenditures on maintenance and household items should be at such a level that it should not compromise the attainment and satisfaction of other basic needs +#' +#' | Standard Questions | +#' |:-------------------------:| +#' | DWE01-DWE05 & DWE08-DWE09 | +#' +#' **Numerator**: Total population living in habitable and affordable housing +#' +#' **Denominator**: Total population +#' +#' **Formula**: *DWE01* = 1,2 & *DWE02* = 3,4,5,6,7,8,9 & *DWE03* = 8,9,10,11,12,13 & *DWE04* = 10,11,12,13,14,15 & crowding (*HH01*/*DWE05*) \<= 3 & (DWE08=1 & DWE09=1,2) \| DWE08=0 +#' +#' +#' This indicator is calculated from the main dataset +#' +#' classify as habitable when improved/adequate shelter +#' +#' @param datalist A list with all hierarchical data frame for a survey data set. +#' format is expected to match the Excel export synchronized from kobo to RILD +#' and loaded with kobocruncher::kobo_data() +#' +#' @importFrom dplyr mutate case_when +#' @importFrom labelled labelled +#' +#' @return new calculated variable - appended or not... +#' +#' @export +#' @examples +#' ## data, cf example fct_re_map() +#' datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", +#' package = "IndicatorCalc")) +#' ## Apply indicator function on datalist +#' datalist <- outcome9_1(datalist) +#' +#' ## Visualise value +#' fct_plot_indic_donut(indicator = datalist[["main"]]$outcome9_1, +#' iconunicode = "f140") +outcome9_1 <- function(datalist){ + # Mapper , +mapper = list( + hierarchy = "main", + variablemap = data.frame( + label = +c("1.What type of dwelling does the household live in?", "2. Main material of the dwelling floor", +"3. Main material of the roof.", "4. Main material of the exterior walls", +"5. How many separate rooms do the members of your household occupy?", +"8. Does your household pay any rent?", "9. Can your household generally afford to pay the rent without any major financial distress?", +"What is the total number of persons in this household ? (including the respondent)" +) + , + variable = +c("DWE01", "DWE02", "DWE03", "DWE04", "DWE05", "DWE08", "DWE09", +"HH01") + ), + modalitymap = data.frame( + variable = +c("DWE01", "DWE01", "DWE01", "DWE01", "DWE01", "DWE01", "DWE01", +"DWE01", "DWE01", "DWE01", "DWE02", "DWE02", "DWE02", "DWE02", +"DWE02", "DWE02", "DWE02", "DWE02", "DWE02", "DWE02", "DWE03", +"DWE03", "DWE03", "DWE03", "DWE03", "DWE03", "DWE03", "DWE03", +"DWE03", "DWE03", "DWE03", "DWE03", "DWE03", "DWE03", "DWE04", +"DWE04", "DWE04", "DWE04", "DWE04", "DWE04", "DWE04", "DWE04", +"DWE04", "DWE04", "DWE04", "DWE04", "DWE04", "DWE04", "DWE04", +"DWE04", "DWE05", "DWE08", "DWE08", "DWE09", "DWE09", "DWE09", +"DWE09", "HH01") + , + label = +c("Apartment", "House", "Tent", "Caravan", "Collective Center", +"Worksite/Unfinished Home/ Abandoned Building", "Farm Building", +"School, mosque, church or other religious building", "Garage, shop, workshop, or other structure not meant as residential space", +"Other (Specify)", "Earth/sand", "Dung", "Wood planks", "Palm/bamboo", +"Parquet or polished wood", "Vinyl or asphalt strips", "Ceramic tiles", +"Cement", "Carpet", "Other (Specify)", "No roof", "Thatch/Palm leaf", +"Sod", "Rustic mat", "Palm/bamboo", "Wood planks", "Cardboard", +"Metal/tin", "Wood", "Calamine/Cement fibre", "Ceramic tiles", +"Cement", "Roofing shingles", "Other (Specify)", "No walls", +"Cane/Palm/ Trunks", "Dirt", "Bamboo with mud", "Stone with mud", +"Uncovered adobe", "Plywood", "Cardboard", "Reused wood", "Cement", +"Stone with lime/ cement", "Bricks", "Cement blocks", "Covered adobe", +"Wood planks/shingles", "Other (Specify)", NA, "Yes", "No", "Always", +"Often", "Sometimes", "Never", NA) + , + standard = +c("1", "2", "3", "4", "5", "6", "7", "8", "9", "96", "1", "2", +"3", "4", "5", "6", "7", "8", "9", "96", "1", "2", "3", "4", +"5", "6", "7", "8", "9", "10", "11", "12", "13", "96", "1", "2", +"3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", +"15", "96", NA, "1", "0", "1", "2", "3", "4", NA) + )) + + ## So first we check that we have what we need in the data set based on the mapper + check_map <- fct_check_map(datalist = datalist, + mapper = mapper) + if ( check_map == FALSE) { + cat( "There are missing data requirement to calculate Indicator Outcome 9.1") + } else { + + #Only apartment and house +datalist[["main"]]$dwe01_cat<- dplyr::case_when( + (as.integer(datalist[["main"]]$DWE01) == 1 | + as.integer(datalist[["main"]]$DWE01) == 2) ~ 1, + TRUE ~ 0 ) + +datalist[["main"]]$dwe01_cat <- labelled::labelled(datalist[["main"]]$dwe01_cat, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Proportion of apartment and house") + +#unimproved floor when not earth,sand,clay,mud, dung or other +datalist[["main"]]$dwe02_cat<- dplyr::case_when( + (as.integer(datalist[["main"]]$DWE02) == 1 | + as.integer(datalist[["main"]]$DWE02) == 2 | + as.integer(datalist[["main"]]$DWE02) == 96) ~ 0, + TRUE ~ 1 ) +datalist[["main"]]$dwe02_cat <- labelled::labelled(datalist[["main"]]$dwe02_cat, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Proportion of improved floor, ie. no earth,sand,clay,mud, + dung or other") + +#unimproved roof all options except metal,wood,ceramic tiles, cement, roofing shingles/sheets +datalist[["main"]]$dwe03_cat<- dplyr::case_when( + (as.integer(datalist[["main"]]$DWE03) == 8 | + as.integer(datalist[["main"]]$DWE03) == 9 | + as.integer(datalist[["main"]]$DWE03) == 10 | + as.integer(datalist[["main"]]$DWE03) == 11 | + as.integer(datalist[["main"]]$DWE03) == 12 | + as.integer(datalist[["main"]]$DWE03) == 13 ) ~ 1 , + TRUE ~ 0) +datalist[["main"]]$dwe03_cat <- labelled::labelled(datalist[["main"]]$dwe03_cat, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Proportion of improved roof all options except metal, + wood,ceramic tiles, cement, roofing shingles/sheets") + +#improved wall: cement,stone,bricks,cement blocks, covered adobe, wood planks +datalist[["main"]]$dwe04_cat<- dplyr::case_when( + ( as.integer(datalist[["main"]]$DWE04) == 10 | + as.integer(datalist[["main"]]$DWE04) == 11 | + as.integer(datalist[["main"]]$DWE04) == 12 | + as.integer(datalist[["main"]]$DWE04) == 13 | + as.integer(datalist[["main"]]$DWE04) == 14 | + as.integer(datalist[["main"]]$DWE04) == 15) ~ 1, + TRUE ~ 0) +datalist[["main"]]$dwe04_cat <- labelled::labelled(datalist[["main"]]$dwe04_cat, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Proportion of improved wall: cement,stone,bricks, + cement blocks, covered adobe, wood planks") + +####Calculate crowding index - overcrowded when more than 3 persons + +datalist[["main"]]$crowding <- as.integer(datalist[["main"]]$HH01) / + as.integer(datalist[["main"]]$DWE05) +##if crowding <= 3, not overcrowded +datalist[["main"]]$dwe05_cat <- dplyr::case_when( + datalist[["main"]]$crowding <= 3 ~ 1, + TRUE ~ 0) +datalist[["main"]]$dwe05_cat <- labelled::labelled(datalist[["main"]]$dwe05_cat, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Proportion of overcrowded sheters") + + +## Add DWE08 and DWE09 to calculations - +# if household is paying rent, they should be able to afford to pay rent without any financial distress + +#affordable if HH pays rent and often and always without financial distress +datalist[["main"]]$dwe09_cat <- dplyr::case_when( + (as.integer(datalist[["main"]]$DWE08) == 1 & + (as.integer(datalist[["main"]]$DWE09) == 1 | + as.integer(datalist[["main"]]$DWE09) == 2 ) ) ~ 1, + + (as.integer(datalist[["main"]]$DWE08) == 1 & + (as.integer(datalist[["main"]]$DWE09) == 3 | + as.integer(datalist[["main"]]$DWE09) == 4 ) ) ~ 0, + + datalist[["main"]]$DWE08== 0 ~ 1) + +datalist[["main"]]$dwe09_cat <- labelled::labelled(datalist[["main"]]$dwe09_cat, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Proportion of affordable sheters - HH pays rent and often and always without financial distress") + +####Combine all shelter indicators + +##dwe01_cat / dwe02_cat / dwe03_cat / dwe04_cat / dwe05_cat / dwe09_cat + + +datalist[["main"]]$outcome9_1 <- dplyr::case_when( + datalist[["main"]]$dwe01_cat == 0 | + datalist[["main"]]$dwe02_cat == 0 | + datalist[["main"]]$dwe03_cat == 0 | + datalist[["main"]]$dwe04_cat == 0 | + datalist[["main"]]$dwe05_cat == 0 | + datalist[["main"]]$dwe09_cat == 0 ~ 0, + + datalist[["main"]]$dwe01_cat == 1 & + datalist[["main"]]$dwe02_cat == 1 & + datalist[["main"]]$dwe03_cat == 1 & + datalist[["main"]]$dwe04_cat == 1 & + datalist[["main"]]$dwe05_cat == 1 & + datalist[["main"]]$dwe09_cat == 1 ~ 1) + +datalist[["main"]]$outcome9_1 <- labelled::labelled(datalist[["main"]]$outcome9_1, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Proportion of PoCs living in habitable and affordable housing") + + } + return(datalist) +} diff --git a/R/outcome9_2.R b/R/outcome9_2.R new file mode 100644 index 0000000..75ba7be --- /dev/null +++ b/R/outcome9_2.R @@ -0,0 +1,112 @@ +# WARNING - Generated by {fusen} from dev/indicators.Rmd: do not edit by hand + +#' outcome9_2 +#' +#' +#' **Proportion of Persons of Concern that have energy to ensure lighting** +#' +#' This indicator measures the percentage of Persons of Concern who have access to clean and sustainable source of lighting at household level. +#' Safe and sustainable access to clean energy is increasingly recognized as human right and is an integral part of the Sustainable Development Goals. +#' +#' The indicator for lighting is constructed from the essential questions *LIGHT01* and *LIGHT02* which captures the most used light source. +#' [LSMS Guidebook](https://documents1.worldbank.org/curated/en/557341633679857128/pdf/Measuring-Energy-Access-A-Guide-to-Collecting-Data-Using-the-Core-Questions-on-Household-Energy-Use.pdf) is the main source for measuring energy access as defined for SGD 7.1.12. +#' +#' | Standard Questions | +#' |:------------------:| +#' | LIGHT01 - LIGHT03 | +#' +#' : $\frac{Population with reliance on clean fuels and technologies for lighting}{Total population}$ +#' +#' **Numerator**: Population living in households with primary reliance on clean fuels and technologies for lighting +#' +#' **Denominator**: Total population +#' +#' **Formula**: *LIGHT01* = 1 & *LIGHT02* = 1, 3, 4, 5, 6, 7, 8 / Total Population +#' +#' This comes from the main dataset +#' +#' The below Calculates percentage of PoC having access to clean fuel for lighting and / or basic connectivity (9.1 Outcome Indicator) +#' +#' +#' @param datalist A list with all hierarchical data frame for a survey data set. +#' format is expected to match the Excel export synchronized from kobo to RILD +#' and loaded with kobocruncher::kobo_data() +#' +#' @importFrom dplyr mutate case_when +#' @importFrom labelled labelled +#' +#' @return new calculated variable - appended or not... +#' +#' @export +#' @examples +#' ## data, cf example fct_re_map() +#' datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", +#' package = "IndicatorCalc")) +#' ## Apply indicator function on datalist +#' datalist <- outcome9_2(datalist ) +#' +#' ## Visualise value +#' fct_plot_indic_donut(indicator = datalist[["main"]]$outcome9_2, +#' iconunicode = "f140") +outcome9_2 <- function(datalist ){ + # Mapper , +mapper = list( + hierarchy = "main", + variablemap = data.frame( + label = +c("4. Does this household use anything for lighting?", "5. What does this household use most of the time as energy for lighting, or as a light source?", +"6. What source of electricity is used most of the time in this household?" +) + , + variable = +c("LIGHT01", "LIGHT02", "LIGHT03") + ), + modalitymap = data.frame( + variable = +c("LIGHT01", "LIGHT01", "LIGHT02", "LIGHT02", "LIGHT02", "LIGHT02", +"LIGHT02", "LIGHT02", "LIGHT02", "LIGHT02", "LIGHT02", "LIGHT02", +"LIGHT02", "LIGHT02", "LIGHT02", "LIGHT02", "LIGHT03", "LIGHT03", +"LIGHT03", "LIGHT03", "LIGHT03", "LIGHT03", "LIGHT03", "LIGHT03", +"LIGHT03", "LIGHT03") + , + label = +c("Yes", "No", "Electricity (including solar mini-grids, hybrid mini-grids and national grid)", +"Electricity (from diesel generator)", "Solar home system", "Solar-powered lantern or flashlight", +"Rechargeable flashlight, mobile, torch or lantern", "Battery powered flashlight, torch or lantern", +"Biogas lamp", "LPG lamp", "Gasoline lamp", "Kerosene or paraffin lamp", +"Oil lamp", "Candle", "Open fire", "Other, specify", "No electricity in household", +"National grid connection from [COMPANY]", "Local mini grid", +"Solar home system", "Solar lantern", "Electric generator", "Rechargeable battery", +"Dry cell battery / torch", "Other, specify", "Don't know") + , + standard = +c("1", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", +"11", "12", "13", "96", "1", "2", "3", "4", "5", "6", "7", "8", +"96", "98") + )) + + ## So first we check that we have what we need in the data set based on the mapper + check_map <- fct_check_map(datalist = datalist, + mapper = mapper) + if ( check_map == FALSE) { + cat( "There are missing data requirement to calculate Indicator Outcome 9.2") + } else { + +datalist[["main"]]$outcome9_2 <- dplyr::case_when( + as.integer(datalist[["main"]]$LIGHT01) == 1 & + (as.integer(datalist[["main"]]$LIGHT02) == 1 | + as.integer(datalist[["main"]]$LIGHT02) == 3 | + as.integer(datalist[["main"]]$LIGHT02) == 4 | + as.integer(datalist[["main"]]$LIGHT02) == 5 | + as.integer(datalist[["main"]]$LIGHT02) == 6 | + as.integer(datalist[["main"]]$LIGHT02) == 7 | + as.integer(datalist[["main"]]$LIGHT02) == 8 ) ~ 1, + TRUE ~ 0) + +datalist[["main"]]$outcome9_2 <- labelled::labelled(datalist[["main"]]$outcome9_2, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Proportion of PoC that have energy to ensure lighting") + +} + return(datalist) +} diff --git a/R/var_dummy.R b/R/var_dummy.R deleted file mode 100644 index 6a8df3d..0000000 --- a/R/var_dummy.R +++ /dev/null @@ -1,182 +0,0 @@ -# WARNING - Generated by {fusen} from dev/utilities.Rmd: do not edit by hand - -#' var_dummy -#' -#' Append to an existing dataframe new Generate the variable based on 1. type, 2. constraint and 3.relevance -#' -#' @param frame dataframe object where new dummy varaible will be created... -#' @param name name of the variable to be created -#' @param type type of the variable to be created in line with xlsform - -#' one of the following 6 options: -#' "select_one", "select_multiple", "text", "integer", "numeric", "date" -#' -#' "start ", "end", "calculate", "geopoint","acknowledge", -#' "begin_group", "end_group", "begin_repeat", "end_repeat" , "note" -#' -#' @param list_opt a vector with all possible values for select_one and select_multiple -#' - can be null -#' @param constraint specific constraint - used for for numeric or integer -## @param seed random seed -#' -#' @return dataframe -#' -#' @importFrom withr with_seed -#' @importFrom dplyr mutate n tibble rename -#' @importFrom stats rlnorm -#' @importFrom tidyr unnest -#' -#' @noRd -#' -#' @export -#' @examples -#' -#' ## let's initiate a dataframe with an index of n r records -#' n <- 384 -#' frame <- dplyr::tibble( -#' index = paste0( "ID-", -#' purrr::as_vector( -#' purrr::map(n, sample(LETTERS, 4)) |> -#' purrr::map(paste0, collapse = "")), -#' "-", -#' formatC(1:n, width = nchar(n) + 1, flag = "0") -#' )) -#' -#' ## test inject select_one -#' frame <- var_dummy( -#' frame = frame, -#' name = "testselect_one", -#' type = "select_one", -#' list_opt = c("alpha", "beta", "delta"), -#' constraint = NULL -#' ) -#' ## test inject select_multiple in main -#' frame <- var_dummy( -#' frame = frame, -#' name = "testselect_multiple", -#' type = "select_multiple", -#' list_opt = c("alpha", "beta", "delta"), -#' constraint = NULL -#' ) -#' -#' ## test inject text -#' frame <- var_dummy( -#' frame = frame, -#' name = "testtext", -#' type = "text", -#' list_opt = NULL, -#' constraint = NULL -#' ) -#' -#' -#' ## test inject numeric -#' frame <- var_dummy( -#' frame = frame, -#' name = "testnumeric", -#' type = "numeric", -#' list_opt = NULL, -#' constraint = NULL -#' ) -#' -#' ## test inject date -#' frame <- var_dummy( -#' frame = frame, -#' name = "testdate", -#' type = "date", -#' list_opt = NULL, -#' constraint = NULL -#' ) -#' -#' ## Preview out out -#' knitr::kable(head(frame, 5)) -var_dummy <- function( frame, - name, - type, - list_opt, - constraint #, - # seed = 1976 - ) { - ## number of records to be generated depends on the initial frame.. - n <- nrow(frame) - #withr::with_seed(seed = seed) - # withr::with_seed( - # seed = seed, - # suppressWarnings( - - # no content ########### - if( type %in% c("begin_group", "end_group", "begin_repeat", "end_repeat" , "note" )) { - res <- dplyr::tibble( - thisvar = NA) - names(res)[1] <- name - frame <- cbind(frame,res) - } - - - ## acknowledge ########### - if( type %in% c("acknowledge")) { - res <- dplyr::tibble( - thisvar = sample(c(0,1), n, replace = TRUE)) - names(res)[1] <- name - frame <- cbind(frame,res) - } - - - ## select one ########### - if( type %in% c("select_one") ) { - res <- dplyr::tibble( thisvar = sample(c(list_opt), n, replace = TRUE) ) - ## Now rename the variable - names(res)[1] <- name - frame <- cbind(frame,res) - } - - ## select multiple ########### - if( type %in% c("select_multiple")) { - for (modality in list_opt) { - res <- dplyr::tibble( - thisvar = sample(c(0,1), n, replace = TRUE)) - names(res)[1] <- paste0(name, "_",modality ) - frame <- cbind(frame,res) - } - } - - ## stext ########### - if( type %in% c("text")) { - res <- dplyr::tibble( - thisvar = paste0( "FreeText_", - purrr::as_vector( - purrr::map(n, sample(LETTERS, 6)) |> - purrr::map(paste0, collapse = "")), - "-", - formatC(1:n, width = nchar(n) + 1, flag = "0") - )) - ## Now rename the variable - names(res)[1] <- name - frame <- cbind(frame,res) - } - - ## numeric ########### - if( type %in% c("numeric", "integer", "calculate", "geopoint")) { - ## TODO extract elements from constraint to define num1 an num2 - num1 <- 18 - num2 <- 95 - - res <- dplyr::tibble( - thisvar = sample(num1:num2, n, replace = TRUE)) - ## Now rename the variable - names(res)[1] <- name - frame <- cbind(frame,res) - } - - ## date ########### - if( type %in% c("date", "start ", "end")) { - res <- dplyr::tibble( - thisvar = Sys.time() - abs(rnorm(n, 0, sd = 2) * 365 * 24 * 3600)) - ## Now rename the variable - names(res)[1] <- name - frame <- cbind(frame,res) - } - - # ) - # ) - - return(frame) -} diff --git a/_pkgdown.yml b/_pkgdown.yml index 71c4226..849c755 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -6,11 +6,11 @@ reference: - title: Impact desc: Calculation for Impact Indicators - contents: - - starts_with("impact_") + - starts_with("impact") - title: Outcome desc: Calculation for Outcome Indicators contents: - - starts_with("outcome_") + - starts_with("outcome") - title: Intermediate desc: Calculation for Intermediate Variables contents: diff --git a/dev/config_fusen.yaml b/dev/config_fusen.yaml index a7621f4..877f107 100644 --- a/dev/config_fusen.yaml +++ b/dev/config_fusen.yaml @@ -2,29 +2,57 @@ indicators.Rmd: path: dev/indicators.Rmd state: active R: - - R/fct_check_map.R - - R/fct_get_all_variable_names.R - - R/fct_plot_indic_donut.R - - R/fct_re_map.R - R/impact_2_2.R - - R/inter_drinkingwater.R - - R/inter_electricity.R - - R/inter_healthcare.R - - R/inter_shelter.R + - R/impact2_3.R + - R/impact3_2a.R + - R/impact3_2b.R + - R/impact3_3.R + - R/outcome1_2.R + - R/outcome1_3.R + - R/outcome10_1.R + - R/outcome10_2.R + - R/outcome12_1.R + - R/outcome12_2.R + - R/outcome13_1.R + - R/outcome13_2.R + - R/outcome13_3.R + - R/outcome14_1.R + - R/outcome16_1.R + - R/outcome16_2.R + - R/outcome4_1.R + - R/outcome4_2.R + - R/outcome5_2.R + - R/outcome8_2.R + - R/outcome9_1.R + - R/outcome9_2.R tests: - - tests/testthat/test-fct_check_map.R - - tests/testthat/test-fct_get_all_variable_names.R - - tests/testthat/test-fct_re_map.R - - tests/testthat/test-fct_plot_indic_donut.R - - tests/testthat/test-inter_electricity.R - - tests/testthat/test-inter_healthcare.R - - tests/testthat/test-inter_drinkingwater.R - - tests/testthat/test-inter_shelter.R - tests/testthat/test-impact_2_2.R - vignettes: vignettes/development.Rmd + - tests/testthat/test-impact2_3.R + - tests/testthat/test-impact3_2a.R + - tests/testthat/test-impact3_2b.R + - tests/testthat/test-impact3_3.R + - tests/testthat/test-outcome1_2.R + - tests/testthat/test-outcome1_3.R + - tests/testthat/test-outcome4_1.R + - tests/testthat/test-outcome4_2.R + - tests/testthat/test-outcome5_2.R + - tests/testthat/test-outcome8_2.R + - tests/testthat/test-outcome9_1.R + - tests/testthat/test-outcome9_2.R + - tests/testthat/test-outcome10_1.R + - tests/testthat/test-outcome10_2.R + - tests/testthat/test-outcome12_1.R + - tests/testthat/test-outcome12_2.R + - tests/testthat/test-outcome13_1.R + - tests/testthat/test-outcome13_2.R + - tests/testthat/test-outcome13_3.R + - tests/testthat/test-outcome14_1.R + - tests/testthat/test-outcome16_1.R + - tests/testthat/test-outcome16_2.R + vignettes: vignettes/indicators-calculation-functions.Rmd inflate: flat_file: dev/indicators.Rmd - vignette_name: Development + vignette_name: Indicators calculation functions open_vignette: true check: true document: true @@ -33,6 +61,8 @@ utilities.Rmd: path: dev/utilities.Rmd state: active R: + - R/fct_build_map.R + - R/fct_build_requirement.R - R/fct_check_map.R - R/fct_compass_table.R - R/fct_get_all_variable_names.R @@ -43,15 +73,17 @@ utilities.Rmd: - R/fct_var_dummy.R - R/fct_var_mapping.R tests: + - tests/testthat/test-fct_var_dummy.R + - tests/testthat/test-fct_kobo_dummy.R - tests/testthat/test-fct_var_mapping.R - - tests/testthat/test-fct_check_map.R + - tests/testthat/test-fct_build_map.R - tests/testthat/test-fct_get_all_variable_names.R - tests/testthat/test-fct_re_map.R + - tests/testthat/test-fct_check_map.R - tests/testthat/test-fct_plot_indic_donut.R - - tests/testthat/test-fct_var_dummy.R - - tests/testthat/test-fct_kobo_dummy.R - tests/testthat/test-fct_plot_rbm_sdg.R - tests/testthat/test-fct_compass_table.R + - tests/testthat/test-fct_build_requirement.R vignettes: vignettes/utilities.Rmd inflate: flat_file: dev/utilities.Rmd diff --git a/dev/indicators.Rmd b/dev/indicators.Rmd index 04c1c2f..2632b5e 100644 --- a/dev/indicators.Rmd +++ b/dev/indicators.Rmd @@ -137,11 +137,7 @@ pkgload::load_all(export_all = FALSE) #' @param datalist A list with all hierarchical data frame for a survey data set. #' format is expected to match the Excel export synchronized from kobo to RILD #' and loaded with kobocruncher::kobo_data() -#' -#' @param mapper a list providing the mapping of the variables used for the -#' calculation - this mapper is potentially to be adjusted in relation with deviation -#' between the the standard XlsForm and the contextualized dataset -#' +#' #' #' @importFrom dplyr mutate case_when #' @importFrom labelled labelled @@ -152,32 +148,29 @@ pkgload::load_all(export_all = FALSE) impact_2_2 <- function(datalist ){ ## Mapper - mapper = list( +mapper = list( hierarchy = "main", variablemap = data.frame( label = -c("1. In general, when anyone in your household is sick, where do they go to seek care?", +c("1. When anyone in your household is sick, where do they go to seek care?", "2. How do you reach this facility if you need to seek care?", "3. How long does it take to go there when you use the mode of transport that you mentioned above?", "4. Does this household use anything for lighting?", "5. What does this household use most of the time as energy for lighting, or as a light source?", "6. What source of electricity is used most of the time in this household?", "1. What is the main source of drinking water for this household?", -"2. Where is this ${source} - ${source2} located?", "3. How long does it take to go there, get water, and come back, including waiting time?", +"2. Where is this ${source} - ${source2} located?", "Unit", "Time", "4. In the last 30 days, has there been any time when your household did not have sufficient quantities of drinking water when needed?", -"1.What type of dwelling does the household live in?", "2. Please observe if the door is lockable ?", -"3. Please observe if windows are lockable ?", "4. Please observe if shelter collapsed or partially collapsed ?", -"5. Please observe if the roof is damaged?", "6. Please observe if windows/doors are sealed to natural elements ?", -"7. Please observe if there is a leakage /rottenness in the walls / floors?", -"8. In the last 12 months, has your neighbourhood experienced flooding for more than 7 consecutive days? ", -"9. Are there any known landmines or unexploded ordnance reported in your neighbourhood's vicinity? ", -"10. How many separate rooms do the members of your household occupy?" +"1.What type of dwelling does the household live in?", "2. Main material of the dwelling floor", +"3. Main material of the roof.", "4. Main material of the exterior walls", +"5. How many separate rooms do the members of your household occupy?", +"8. Does your household pay any rent?", "9. Can your household generally afford to pay the rent without any major financial distress?", +"What is the total number of persons in this household ? (including the respondent)" ) , variable = c("HEA01", "HEA02", "HEA03", "LIGHT01", "LIGHT02", "LIGHT03", -"DWA01", "DWA02", "DWA03", "DWA04", "DWE01", "SHEL01", "SHEL02", -"SHEL03", "SHEL04", "SHEL05", "SHEL06", "RISK01", "RISK02", "DWE05" -) +"DWA01", "DWA02", "DWA03a", "DWA03b", "DWA04", "DWE01", "DWE02", +"DWE03", "DWE04", "DWE05", "DWE08", "DWE09", "HH01") ), modalitymap = data.frame( variable = @@ -185,17 +178,20 @@ c("HEA01", "HEA01", "HEA01", "HEA01", "HEA01", "HEA01", "HEA01", "HEA02", "HEA02", "HEA02", "HEA02", "HEA03", "LIGHT01", "LIGHT01", "LIGHT02", "LIGHT02", "LIGHT02", "LIGHT02", "LIGHT02", "LIGHT02", "LIGHT02", "LIGHT02", "LIGHT02", "LIGHT02", "LIGHT02", "LIGHT02", -"LIGHT02", "LIGHT03", "LIGHT03", "LIGHT03", "LIGHT03", "LIGHT03", -"LIGHT03", "LIGHT03", "LIGHT03", "LIGHT03", "LIGHT03", "DWA01", +"LIGHT02", "LIGHT02", "LIGHT03", "LIGHT03", "LIGHT03", "LIGHT03", +"LIGHT03", "LIGHT03", "LIGHT03", "LIGHT03", "LIGHT03", "LIGHT03", "DWA01", "DWA01", "DWA01", "DWA01", "DWA01", "DWA01", "DWA01", "DWA01", "DWA01", "DWA01", "DWA01", "DWA01", "DWA01", "DWA01", -"DWA01", "DWA01", "DWA01", "DWA02", "DWA02", "DWA02", "DWA03", -"DWA04", "DWA04", "DWE01", "DWE01", "DWE01", "DWE01", "DWE01", -"DWE01", "DWE01", "DWE01", "DWE01", "SHEL01", "SHEL01", "SHEL01", -"SHEL02", "SHEL02", "SHEL02", "SHEL03", "SHEL03", "SHEL03", "SHEL04", -"SHEL04", "SHEL04", "SHEL05", "SHEL05", "SHEL05", "SHEL06", "SHEL06", -"SHEL06", "RISK01", "RISK01", "RISK02", "RISK02", "RISK02", "DWE05" -) +"DWA01", "DWA01", "DWA01", "DWA01", "DWA02", "DWA02", "DWA02", +"DWA03a", "DWA03a", "DWA03b", "DWA04", "DWA04", "DWE01", "DWE01", +"DWE01", "DWE01", "DWE01", "DWE01", "DWE01", "DWE01", "DWE01", +"DWE01", "DWE02", "DWE02", "DWE02", "DWE02", "DWE02", "DWE02", +"DWE02", "DWE02", "DWE02", "DWE02", "DWE03", "DWE03", "DWE03", +"DWE03", "DWE03", "DWE03", "DWE03", "DWE03", "DWE03", "DWE03", +"DWE03", "DWE03", "DWE03", "DWE03", "DWE04", "DWE04", "DWE04", +"DWE04", "DWE04", "DWE04", "DWE04", "DWE04", "DWE04", "DWE04", +"DWE04", "DWE04", "DWE04", "DWE04", "DWE04", "DWE04", "DWE05", +"DWE08", "DWE08", "DWE09", "DWE09", "DWE09", "DWE09", "HH01") , label = c("NGO facility (charity, faith-based organization)", "UNHCR Health Partner (Caritas, Save the Children)", @@ -203,10 +199,10 @@ c("NGO facility (charity, faith-based organization)", "UNHCR Health Partner (Car "Pharmacy", "Other, specify", "Don't know", "By walk", "Private car", "Public transport (bus, boat)", "Other, specify", NA, "Yes", "No", "Electricity (including solar mini-grids, hybrid mini-grids and national grid)", -"Solar home system", "Solar-powered lantern or flashlight", "Rechargeable flashlight, mobile, torch or lantern", -"Battery powered flashlight, torch or lantern", "Biogas lamp", -"LPG lamp", "Gasoline lamp", "Kerosene or paraffin lamp", "Oil lamp", -"Candle", "Open fire", "Other, specify", "No electricity in household", +"Electricity (from diesel generator)", "Solar home system", "Solar-powered lantern or flashlight", +"Rechargeable flashlight, mobile, torch or lantern", "Battery powered flashlight, torch or lantern", +"Biogas lamp", "LPG lamp", "Gasoline lamp", "Kerosene or paraffin lamp", +"Oil lamp", "Candle", "Open fire", "Other, specify", "No electricity in household", "National grid connection from [COMPANY]", "Local mini grid", "Solar home system", "Solar lantern", "Electric generator", "Rechargeable battery", "Dry cell battery / torch", "Other, specify", "Don't know", "Piped Into Dwelling", @@ -216,135 +212,192 @@ c("NGO facility (charity, faith-based organization)", "UNHCR Health Partner (Car "Tanker Truck/Water Vendor", "Cart With Small Tank/Drum", "Surface Water (River, Stream, Pond, Dam, Canal)", "Bottled Water", "Sachet Water", "Water Kiosk", "Other (Specify)", "Don't Know", "In Own Dwelling", "In Own Yard/Plot", "Elsewhere", -NA, "Yes", "No", "House", "Tent", "Caravan", "Collective Center", -"Worksite/Unfinished Home/ Abandoned Building", "Farm Building", -"School, mosque, church or other religious building", "Garage, shop, workshop, or other structure not meant as residential space", -"Other (Specify)", "Yes", "No", "Not possible to observe", "Yes", -"No", "Not possible to observe", "Yes", "No", "Not possible to observe", -"Yes", "No", "Not possible to observe", "Yes", "No", "Not possible to observe", -"Yes", "No", "Not possible to observe", "Yes", "No", "Yes", "No", -"Don't know", NA) +"Minutes", "Hours", NA, "Yes", "No", "Apartment", "House", "Tent", +"Caravan", "Collective Center", "Worksite/Unfinished Home/ Abandoned Building", +"Farm Building", "School, mosque, church or other religious building", +"Garage, shop, workshop, or other structure not meant as residential space", +"Other (Specify)", "Earth/sand", "Dung", "Wood planks", "Palm/bamboo", +"Parquet or polished wood", "Vinyl or asphalt strips", "Ceramic tiles", +"Cement", "Carpet", "Other (Specify)", "No roof", "Thatch/Palm leaf", +"Sod", "Rustic mat", "Palm/bamboo", "Wood planks", "Cardboard", +"Metal/tin", "Wood", "Calamine/Cement fibre", "Ceramic tiles", +"Cement", "Roofing shingles", "Other (Specify)", "No walls", +"Cane/Palm/ Trunks", "Dirt", "Bamboo with mud", "Stone with mud", +"Uncovered adobe", "Plywood", "Cardboard", "Reused wood", "Cement", +"Stone with lime/ cement", "Bricks", "Cement blocks", "Covered adobe", +"Wood planks/shingles", "Other (Specify)", NA, "Yes", "No", "Always", +"Often", "Sometimes", "Never", NA) , standard = c("1", "2", "3", "4", "5", "96", "98", "1", "2", "3", "96", NA, "1", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", -"11", "12", "96", "0", "1", "2", "3", "4", "5", "6", "7", "96", -"98", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", -"12", "13", "14", "15", "16", "96", "98", "1", "2", "3", NA, -"1", "0", "2", "3", "4", "5", "6", "7", "8", "9", "96", "1", -"0", "98", "1", "0", "98", "1", "0", "98", "1", "0", "98", "1", -"0", "98", "1", "0", "98", "1", "0", "1", "0", "98", NA) +"11", "12", "13", "96", "1", "2", "3", "4", "5", "6", "7", "8", +"96", "98", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", +"11", "12", "13", "14", "15", "16", "96", "98", "1", "2", "3", +"1", "2", NA, "1", "0", "1", "2", "3", "4", "5", "6", "7", "8", +"9", "96", "1", "2", "3", "4", "5", "6", "7", "8", "9", "96", +"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", +"13", "96", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", +"11", "12", "13", "14", "15", "96", NA, "1", "0", "1", "2", "3", +"4", NA) )) - ## So first we check that we have what we need in the data set based on the mapper check_map <- fct_check_map(datalist = datalist, mapper = mapper) - if (! isTRUE(check_map )) { + if ( check_map == FALSE) { cat( "There are missing data requirement to calculate Indicator Impact 2.2") } else { ## now pull the calculation... ## Electricity ######## - datalist[["main"]]$electricity <- dplyr::case_when(datalist[["main"]]$LIGHT01=="1" & - (datalist[["main"]]$LIGHT03!="1" | - datalist[["main"]]$LIGHT03!="96" | - datalist[["main"]]$LIGHT03!="98") ~ "1", - TRUE ~ "0" ) + datalist[["main"]]$electricity <- dplyr::case_when( + datalist[["main"]]$LIGHT01 == 1 & + (as.integer(datalist[["main"]]$LIGHT03) != 1 | + as.integer(datalist[["main"]]$LIGHT03)!= 96 | + as.integer(datalist[["main"]]$LIGHT03) != 98) ~ 1, + TRUE ~ 0 ) datalist[["main"]]$electricity <- labelled::labelled( datalist[["main"]]$electricity, - labels = c('Yes' = "1", 'No' = "0" ), + labels = c('Yes' = 1, 'No' = 0 ), label = "Access to electricity") ## healthcare ######## datalist[["main"]]$healthcare <- - dplyr::case_when((datalist[["main"]]$HEA01!="96" | - datalist[["main"]]$HEA01!="98") & - datalist[["main"]]$HEA03 <= 60 ~ "1", - TRUE ~ "0") + dplyr::case_when(( + as.integer(datalist[["main"]]$HEA01) != 96 | + as.integer(datalist[["main"]]$HEA01) != 98) & + as.integer(datalist[["main"]]$HEA03) <= 60 ~ 1, + TRUE ~ 0) datalist[["main"]]$healthcare <- labelled::labelled(datalist[["main"]]$healthcare, - labels = c('Yes' = "1", 'No' = "0" ), + labels = c('Yes' = 1, 'No' = 0 ), label = "Access to healthcare facility") + ## Drinking Water ################# + datalist[["main"]]$time_DWA <- dplyr::case_when( + as.integer(datalist[["main"]]$DWA03a) == 1 ~ 1, + as.integer(datalist[["main"]]$DWA03a) == 2 ~ 60) + #convert hour into minutes + # datalist[["main"]]$time_DWA <- as.numeric(datalist[["main"]]$time_DWA) + datalist[["main"]]$time_tot <- datalist[["main"]]$time_DWA * + as.numeric(datalist[["main"]]$DWA03b) + + datalist[["main"]]$reachableU30 <- dplyr::case_when( + datalist[["main"]]$time_tot > 30 ~ 0, + TRUE ~ 1) + # reachable under 30 minutes + datalist[["main"]]$dwa_cond1 <- dplyr::case_when( + as.integer(datalist[["main"]]$DWA01) != 7 | + as.integer(datalist[["main"]]$DWA01) != 9 | + as.integer(datalist[["main"]]$DWA01) != 13 | + as.integer(datalist[["main"]]$DWA01) != 96 | + as.integer(datalist[["main"]]$DWA01) != 98 ~ 1, + TRUE ~ 0) + # improved source + datalist[["main"]]$dwa_cond2 <- dplyr::case_when( + + #UNDER30 MIN + as.integer(datalist[["main"]]$reachableU30) == 1 & + as.integer(datalist[["main"]]$DWA02) == 3 ~ 1, + + # NOT UNDER30 MIN + as.integer(datalist[["main"]]$reachableU30) == 0 & + as.integer(datalist[["main"]]$DWA02) == 3 ~ 0, + + # in the dwelling/yard/plot + as.integer(datalist[["main"]]$DWA02) == 1 | + as.integer(datalist[["main"]]$DWA02) == 2 ~ 1, + TRUE ~ NA ) + + datalist[["main"]]$drinkingwater <- dplyr::case_when( + (as.integer(datalist[["main"]]$dwa_cond1) == 1 & + as.integer(datalist[["main"]]$dwa_cond2) == 1 ) ~ 1, + TRUE ~ 0) + + datalist[["main"]]$drinkingwater <- labelled::labelled(datalist[["main"]]$drinkingwater, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Access to drinking water") + + ## shelter ######### #Only apartment and house -datalist[["main"]]$dwe01_cat <- - dplyr::case_when( - (datalist[["main"]]$DWE01=="1" | - datalist[["main"]]$DWE01=="2") ~ "1", - TRUE ~ "0" ) -## unimproved floor when earth,sand,clay,mud, dung or other -datalist[["main"]]$dwe02_cat<- dplyr::case_when( - (datalist[["main"]]$DWE02=="1" | - datalist[["main"]]$DWE02=="2" | - datalist[["main"]]$DWE02=="96") ~ "0", - TRUE ~ "1" ) -## unimproved roof all options except metal,wood,ceramic tiles, cement, roofing shingles/sheets -datalist[["main"]]$dwe03_cat<- dplyr::case_when( - (datalist[["main"]]$DWE03=="8" | - datalist[["main"]]$DWE03=="9" | - datalist[["main"]]$DWE03=="10" | - datalist[["main"]]$DWE03=="11" | - datalist[["main"]]$DWE03=="12" | - datalist[["main"]]$DWE03=="13" ) ~ "1" , - TRUE ~ "0") - -## improved wall: cement,stone,bricks,cement blocks, covered adobe, wood planks -datalist[["main"]]$dwe04_cat<- dplyr::case_when( - (datalist[["main"]]$DWE04=="10"| - datalist[["main"]]$DWE04=="11"| - datalist[["main"]]$DWE04=="12"| - datalist[["main"]]$DWE04=="13"| - datalist[["main"]]$DWE04=="14"| - datalist[["main"]]$DWE04=="15") ~ "1", - TRUE ~ "0") - -## Calculate crowding index - overcrowded when more than 3 persons -datalist[["main"]]$crowding <- as.numeric(datalist[["main"]]$HH01) / - as.numeric(datalist[["main"]]$DWE05) - -datalist[["main"]]$dwe05_cat<- dplyr::case_when( ##if crowding <= 3, not overcrowded - datalist[["main"]]$crowding <= 3 ~ "1", - TRUE ~ "0") - -####Calculate if all 5 conditions are met for adequate shelter -##dwe01_cat / dwe02_cat / dwe03_cat / dwe04_cat / dwe05_cat - -datalist[["main"]]$shelter<- dplyr::case_when( - datalist[["main"]]$dwe01_cat=="0" | - datalist[["main"]]$dwe02_cat=="0" | - datalist[["main"]]$dwe03_cat=="0" | - datalist[["main"]]$dwe04_cat=="0" | - datalist[["main"]]$dwe05_cat=="0" ~ "0", - - datalist[["main"]]$dwe01_cat=="1" & - datalist[["main"]]$dwe02_cat=="1" & - datalist[["main"]]$dwe03_cat=="1" & - datalist[["main"]]$dwe04_cat=="1" & - datalist[["main"]]$dwe05_cat=="1" ~ "1") - -datalist[["main"]]$shelter <- labelled::labelled( datalist[["main"]]$shelter, - labels = c('Yes' = "1", 'No' = "0" ), - label = "Access to adequate shelter") + datalist[["main"]]$dwe01_cat <- + dplyr::case_when( + ( as.integer(datalist[["main"]]$DWE01) == 1 | + as.integer(datalist[["main"]]$DWE01) == 2) ~ 1, + TRUE ~ 0 ) + ## unimproved floor when earth,sand,clay,mud, dung or other + datalist[["main"]]$dwe02_cat<- dplyr::case_when( + (as.integer(datalist[["main"]]$DWE02) == 1 | + as.integer(datalist[["main"]]$DWE02) == 2 | + as.integer(datalist[["main"]]$DWE02) == 96) ~ 0, + TRUE ~ 1 ) + ## unimproved roof all options except metal,wood,ceramic tiles, cement, roofing shingles/sheets + datalist[["main"]]$dwe03_cat<- dplyr::case_when( + (as.integer(datalist[["main"]]$DWE03) == 8 | + as.integer(datalist[["main"]]$DWE03) == 9 | + as.integer(datalist[["main"]]$DWE03) == 10 | + as.integer(datalist[["main"]]$DWE03) == 11 | + as.integer(datalist[["main"]]$DWE03) == 12 | + as.integer(datalist[["main"]]$DWE03) == 13 ) ~ 1 , + TRUE ~ 0) + + ## improved wall: cement,stone,bricks,cement blocks, covered adobe, wood planks + datalist[["main"]]$dwe04_cat<- dplyr::case_when( + (as.integer(datalist[["main"]]$DWE04) == 10| + as.integer(datalist[["main"]]$DWE04) == 11 | + as.integer(datalist[["main"]]$DWE04) == 12 | + as.integer(datalist[["main"]]$DWE04) == 13 | + as.integer(datalist[["main"]]$DWE04) == 14 | + as.integer(datalist[["main"]]$DWE04) == 15 ) ~ 1, + TRUE ~ 0) + + ## Calculate crowding index - overcrowded when more than 3 persons + datalist[["main"]]$crowding <- as.numeric(datalist[["main"]]$HH01) / + as.numeric(datalist[["main"]]$DWE05) + + datalist[["main"]]$dwe05_cat<- dplyr::case_when( ##if crowding <= 3, not overcrowded + datalist[["main"]]$crowding <= 3 ~ 1, + TRUE ~ 0) + + ####Calculate if all 5 conditions are met for adequate shelter + ##dwe01_cat / dwe02_cat / dwe03_cat / dwe04_cat / dwe05_cat + + datalist[["main"]]$shelter<- dplyr::case_when( + as.integer(datalist[["main"]]$dwe01_cat) == 0 | + as.integer(datalist[["main"]]$dwe02_cat) == 0 | + as.integer(datalist[["main"]]$dwe03_cat) == 0 | + as.integer(datalist[["main"]]$dwe04_cat) == 0 | + as.integer(datalist[["main"]]$dwe05_cat) == 0 ~ 0, + + as.integer(datalist[["main"]]$dwe01_cat) == 1 & + as.integer(datalist[["main"]]$dwe02_cat) == 1 & + as.integer(datalist[["main"]]$dwe03_cat) == 1 & + as.integer(datalist[["main"]]$dwe04_cat) == 1 & + as.integer(datalist[["main"]]$dwe05_cat) == 1 ~ 1) + + datalist[["main"]]$shelter <- labelled::labelled( datalist[["main"]]$shelter, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Access to adequate shelter") ## Compile alll #### datalist[["main"]]$impact2_2 <- dplyr::case_when( - datalist[["main"]]$shelter=="0" | - datalist[["main"]]$electricity=="0" | - datalist[["main"]]$drinkingwater=="0" | - datalist[["main"]]$healthcare=="0" ~ "0", + as.integer(datalist[["main"]]$shelter) == 0 | + as.integer(datalist[["main"]]$electricity) == 0 | + as.integer(datalist[["main"]]$drinkingwater) == 0 | + as.integer(datalist[["main"]]$healthcare) == 0 ~ 0, - datalist[["main"]]$shelter=="1" & - datalist[["main"]]$electricity=="1" & - datalist[["main"]]$drinkingwater=="1" & - datalist[["main"]]$healthcare=="1" ~ "1") + as.integer(datalist[["main"]]$shelter) == 1 & + as.integer(datalist[["main"]]$electricity) == 1 & + as.integer(datalist[["main"]]$drinkingwater) == 1 & + as.integer(datalist[["main"]]$healthcare) == 1 ~ 1) datalist[["main"]]$impact2_2 <- labelled::labelled( datalist[["main"]]$impact2_2, labels =c( - "Yes"="1", - "No"="0" + "Yes" = 1, + "No" = 0 ), label = "Proportion of PoCs residing in physically safe and secure settlements with access to basic facilities") @@ -353,11 +406,14 @@ datalist[["main"]]$impact2_2 <- labelled::labelled( } ``` -```{r example-impact_2_2} -datalist <- kobocruncher::kobo_data( system.file(#"demo_data.xlsx", - "test.xlsx", +```{r example-impact_2_2, message=TRUE, warning=FALSE} +## data, cf example fct_re_map() +datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", package = "IndicatorCalc")) -datalist <- impact_2_2(datalist, mapper) +## Apply calculation +datalist <- impact_2_2(datalist) + +table(datalist[["main"]]$impact2_2, useNA = "ifany") fct_plot_indic_donut(indicator = datalist[["main"]]$impact2_2, iconunicode = "f140") @@ -444,11 +500,7 @@ test_that("impact2_2 works", { #' @param datalist A list with all hierarchical data frame for a survey data set. #' format is expected to match the Excel export synchronized from kobo to RILD #' and loaded with kobocruncher::kobo_data() -#' -#' @param mapper a list providing the mapping of the variables used for the -#' calculation - this mapper is potentially to be adjusted in relation with deviation -#' between the the standard XlsForm and the contextualized dataset -#' +#' #' #' @importFrom dplyr mutate case_when #' @importFrom labelled labelled @@ -458,118 +510,101 @@ test_that("impact2_2 works", { #' @export impact2_3 <- function(datalist ){ - # Mapper , - mapper = list( - hierarchy = "ind", - variablemap = data.frame( - label = c( - "In the past 3 months, did ${name_individual} need to see a health professional for any reason?", - "In the past 3 months, did you receive medical care when needed for the reason above?", - "Why have you been unable to access a medical care in the past 3 months? - 1 Health facility too far", - "Why have you been unable to access a medical care in the past 3 months? - 2 Medicine or health facility too expensive", - "Why have you been unable to access a medical care in the past 3 months? - 3 No treatment exists/ Not necessary", - "Why have you been unable to access a medical care in the past 3 months? - 4 Don't know where to go", - "Why have you been unable to access a medical care in the past 3 months? - 5 No time -6 Prefer other options", - "Why have you been unable to access a medical care in the past 3 months? - 7 Health facility does not accept new patients", - "Why have you been unable to access a medical care in the past 3 months? - 8 Don't trust modern medicine", - "Why have you been unable to access a medical care in the past 3 months? - 9 Don't trust doctors", - "Why have you been unable to access a medical care in the past 3 months? - 10 Administrative/documentation issues (certificates, service cards etc.)", - "Why have you been unable to access a medical care in the past 3 months? - 96 Other (Specify) " - - ), - variable = c("HACC01", - "HACC03", - "HACC04_1", - "HACC04_2", - "HACC04_3", - "HACC04_4", - "HACC04_5", - "HACC04_6", - "HACC04_7", - "HACC04_8", - "HACC04_9", - "HACC04_10", - "HACC04_96") ), - modalitymap = data.frame( - variable = c( "HACC01", "HACC03" , - "HACC04_1", - "HACC04_2", - "HACC04_3", - "HACC04_4", - "HACC04_5", - "HACC04_6", - "HACC04_7", - "HACC04_8", - "HACC04_9", - "HACC04_10", - "HACC04_96"), - label = c( "yes", "yes", - "yes", "yes", "yes", "yes", "yes", "yes", "yes", - "yes", "yes", "yes", "yes"), - standard = c( "1","1" , - "1","1" ,"1" ,"1" ,"1" ,"1" ,"1" ,"1" ,"1" ,"1","1" ))) - - ## So first we check that we have what we need in the data set based on the mapper - check_map <- fct_check_map(datalist = datalist, - mapper = mapper) - if (! isTRUE(check_map )) { - cat( "There are missing data requirement to calculate Indicator Impact 2.3") - } else { - - -datalist[["ind"]]$health_NOacc <- dplyr::case_when( - datalist[["ind"]]$HACC03 == "1" & - (datalist[["ind"]]$HACC04_7 == "1" | - datalist[["ind"]]$HACC04_8 == "1" | - datalist[["ind"]]$HACC04_96 == "1" ) ~ "0", - - datalist[["ind"]]$HACC03 == "1" & - (datalist[["ind"]]$HACC04_1 == "1" | - datalist[["ind"]]$HACC04_2 == "1" | - datalist[["ind"]]$HACC04_3 == "1" | - datalist[["ind"]]$HACC04_4 == "1" | - datalist[["ind"]]$HACC04_5 == "1" | - datalist[["ind"]]$HACC04_6 == "1" | - datalist[["ind"]]$HACC04_9 == "1" | - datalist[["ind"]]$HACC04_10 == "1") ~ "1", - - TRUE ~ "0") -# table(datalist[["ind"]]$health_NOacc, useNA = "ifany") - -datalist[["ind"]]$health_NOacc <- as.numeric(datalist[["ind"]]$health_NOacc) -datalist[["ind"]]$HACC01 <- as.numeric(datalist[["ind"]]$HACC01) - - - ## Add up who needed services ( both who accessed and did not access) -datalist[["ind"]]$HACC_need <- datalist[["ind"]]$HACC01 + datalist[["ind"]]$health_NOacc - -datalist[["ind"]]$impact2_3 <- datalist[["ind"]]$HACC01 / datalist[["ind"]]$HACC_need - -datalist[["ind"]]$impact2_3 <- dplyr::case_when( - datalist[["ind"]]$impact2_3 == 1 ~ 1, - datalist[["ind"]]$impact2_3 == 0.5 ~ 0, - datalist[["ind"]]$impact2_3 == 0 ~ 0, - TRUE ~ NA_real_ ) - -datalist[["ind"]]$impact2_3 <- labelled::labelled( datalist[["ind"]]$impact2_3, - labels =c( "Yes"=1, "No"=0 ), - label = "Proportion of PoC with access to health services") - } - return(datalist) +# # Mapper , +# mapper = list( +# hierarchy = "ind", +# variablemap = data.frame( +# label = +# c("1. During the past 30 days, has ${name_individual} consulted a health practitioner, dentist, traditional healer, or pharmacist, or visited a health center?", +# "2. For what reason(s) did ${name_individual} seek consultation?", +# "3. During the past 30 days, has ${name_individual} needed health services that’s/he could not have access to?", +# "4. Why has ${name_individual} been unable to access a medical treatment in the past 30 days?" +# ) +# , +# variable = +# c("HACC01", "HACC02", "HACC03", "HACC04") +# ), +# modalitymap = data.frame( +# variable = +# c("HACC01", "HACC01", "HACC02", "HACC02", "HACC02", "HACC02", +# "HACC02", "HACC02", "HACC03", "HACC03", "HACC04", "HACC04", "HACC04", +# "HACC04", "HACC04", "HACC04", "HACC04", "HACC04", "HACC04", "HACC04", +# "HACC04") +# , +# label = +# c("Yes", "No", "Illness", "Injury", "General check-up (not for pregnancy)", +# "Pre/Postnatal check-up", "Giving birth", "Other (Specify)", +# "Yes", "No", "Lack of money", "No medical personnel available", +# "Turned away because facility was full", "Turned away because facility was closed", +# "Hospital/Clinic not having enough supplies or tests", "Health facility is too far", +# "Fear of contracting a communicable disease (e.g. COVID-19)", +# "Lockdown/Travel restrictions", "Too Far/transport issues", "Health facility is destroyed", +# "Other (Specify)") +# , +# standard = +# c("1", "0", "1", "2", "3", "4", "5", "96", "1", "0", "1", "2", +# "3", "4", "5", "6", "7", "8", "9", "10", "96") +# )) +# ## So first we check that we have what we need in the data set based on the mapper +# check_map <- fct_check_map(datalist = datalist, +# mapper = mapper) +# if ( check_map == FALSE) { +# cat( "There are missing data requirement to calculate Indicator Impact 2.3") +# } else { +# +# +# datalist[["ind"]]$health_NOacc <- dplyr::case_when( +# as.integer(datalist[["ind"]]$HACC03) == 1 & +# (as.integer(datalist[["ind"]]$HACC04_7) == 1 | +# as.integer(datalist[["ind"]]$HACC04_8) == 1 | +# as.integer(datalist[["ind"]]$HACC04_96) == 1 ) ~ 0, +# +# as.integer(datalist[["ind"]]$HACC03) == 1 & +# (as.integer(datalist[["ind"]]$HACC04_1) == 1 | +# as.integer(datalist[["ind"]]$HACC04_2) == 1 | +# as.integer(datalist[["ind"]]$HACC04_3) == 1 | +# as.integer(datalist[["ind"]]$HACC04_4) == 1 | +# as.integer(datalist[["ind"]]$HACC04_5) == 1 | +# as.integer(datalist[["ind"]]$HACC04_6) == 1 | +# as.integer(datalist[["ind"]]$HACC04_9) == 1 | +# as.integer(datalist[["ind"]]$HACC04_10) == 1) ~ 1, +# +# TRUE ~ 0) +# # table(datalist[["ind"]]$health_NOacc, useNA = "ifany") +# +# datalist[["ind"]]$health_NOacc <- as.numeric(datalist[["ind"]]$health_NOacc) +# datalist[["ind"]]$HACC01 <- as.numeric(datalist[["ind"]]$HACC01) +# +# +# ## Add up who needed services ( both who accessed and did not access) +# datalist[["ind"]]$HACC_need <- datalist[["ind"]]$HACC01 + datalist[["ind"]]$health_NOacc +# +# datalist[["ind"]]$impact2_3 <- datalist[["ind"]]$HACC01 / datalist[["ind"]]$HACC_need +# +# datalist[["ind"]]$impact2_3 <- dplyr::case_when( +# datalist[["ind"]]$impact2_3 == 1 ~ 1, +# datalist[["ind"]]$impact2_3 == 0.5 ~ 0, +# datalist[["ind"]]$impact2_3 == 0 ~ 0, +# TRUE ~ NA_real_ ) +# +# datalist[["ind"]]$impact2_3 <- labelled::labelled( datalist[["ind"]]$impact2_3, +# labels =c( "Yes"=1, "No"=0 ), +# label = "Proportion of PoC with access to health services") +# } +# return(datalist) } ``` -```{r example-impact2_3} -## data -datalist <- kobocruncher::kobo_data( system.file("demo_data.xlsx", #"test.xlsx", - package = "IndicatorCalc")) -## Apply indicator function on datalist -datalist <- impact2_3(datalist ) - -## Visualise value -fct_plot_indic_donut(indicator = datalist[["ind"]]$impact2_3, - iconunicode = "f140") +```{r example-impact2_3, message=TRUE, warning=FALSE} +## data, cf example fct_re_map() +# datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", +# package = "IndicatorCalc")) +# ## Apply calculation +# datalist <- impact2_3(datalist ) +# +# ## Visualise value +# fct_plot_indic_donut(indicator = datalist[["ind"]]$impact2_3, +# iconunicode = "f140") ``` @@ -626,13 +661,6 @@ test_that("impact2_3 works", { #' @param datalist A list with all hierarchical data frame for a survey data set. #' format is expected to match the Excel export synchronized from kobo to RILD #' and loaded with kobocruncher::kobo_data() -#' -#' @param mapper a list providing the mapping of the variables used for the -#' calculation - this mapper is potentially to be adjusted in relation with deviation -#' between the the standard XlsForm and the contextualized dataset -#' -#' @param chain Boolean TRUE/FALSE to tell whether the function should return -#' the dataset expanded with the variable or a single factor #' #' @importFrom dplyr mutate case_when #' @importFrom labelled labelled @@ -643,23 +671,23 @@ test_that("impact2_3 works", { impact3_2a <- function(datalist ){ ## Mapper - mapper = list( hierarchy = "ind", variablemap = data.frame( label = -c("1. Has ${name_individual} ever attended school?", "2. Did ${name_individual} attend school or pre-school at any time during current school year?", -"3. During this/that school year , what level is (was) ${name_individual} attending?", -"4. What type of school?") +c("1. Has ${child_edu_name} ever attended school?", "2. Did ${child_edu_name} attend school or pre-school at any time during current school year?", +"3. During this/that school year , what level is (was) ${child_edu_name} attending?", +"4. What type of school?", "6. Can you estimate how old is ${HH02}?" +) , variable = -c("EDU01", "EDU02", "EDU03", "EDU04") +c("EDU01", "EDU02", "EDU03", "EDU04", "HH07") ), modalitymap = data.frame( variable = c("EDU01", "EDU01", "EDU02", "EDU02", "EDU03", "EDU03", "EDU03", "EDU03", "EDU03", "EDU03", "EDU03", "EDU04", "EDU04", "EDU04", -"EDU04", "EDU04", "EDU04", "EDU04") +"EDU04", "EDU04", "EDU04", "EDU04", "HH07") , label = c("Yes", "No", "Yes", "No", "Early Childhood Education or Pre-primary", @@ -667,54 +695,52 @@ c("Yes", "No", "Yes", "No", "Early Childhood Education or Pre-primary", "Post-secondary - Technical and Vocational Education and Training (TVET)", "Tertiary", "Don’t know", "Government or public", "UN or NGO (non-governmental organization)", "Religious or faith-based organization", "Community", "Private", -"Other (specify)", "Don’t know") +"Other (specify)", "Don’t know", NA) , standard = c("1", "0", "1", "0", "1", "2", "3", "4", "5", "6", "98", "1", -"2", "3", "4", "5", "96", "98") +"2", "3", "4", "5", "96", "98", NA) )) ## So first we check that we have what we need in the data set based on the mapper check_map <- fct_check_map(datalist = datalist, mapper = mapper) - if (! isTRUE(check_map )) { + if ( check_map == FALSE) { cat( "There are missing data requirement to calculate Indicator Impact 3.2.a") } else { datalist[["ind"]]$edu_primary<- dplyr::case_when( - datalist[["ind"]]$EDU01 == 1 & - datalist[["ind"]]$EDU02 == 1 & - datalist[["ind"]]$EDU03==2 ~ 1, + as.integer(datalist[["ind"]]$EDU01) == 1 & + as.integer(datalist[["ind"]]$EDU02) == 1 & + as.integer(datalist[["ind"]]$EDU03) == 2 ~ 1, - datalist[["ind"]]$EDU01 == 0 | - datalist[["ind"]]$EDU02== 0 ~ 0, + as.integer(datalist[["ind"]]$EDU01) == 0 | + as.integer(datalist[["ind"]]$EDU02) == 0 ~ 0, TRUE ~ 0) #Adjust age group for primary school enrollment --> this to go in function var... datalist[["ind"]]$age_primary<- dplyr::case_when( - datalist[["ind"]]$HH07 >= 6 & - datalist[["ind"]]$HH07 <= 10 ~ 1, + as.integer(datalist[["ind"]]$HH07) >= 6 & + as.integer(datalist[["ind"]]$HH07) <= 10 ~ 1, TRUE ~ NA_real_) datalist[["ind"]]$impact3_2a <- sum(datalist[["ind"]]$edu_primary, na.rm = TRUE) / sum(datalist[["ind"]]$age_primary, na.rm = TRUE) datalist[["ind"]]$impact3_2a <- labelled::labelled(datalist[["ind"]]$impact3_2a, - labels = c('Yes' = "1", 'No' = "0" ), - label = "Proportion of persons of concern enrolled - in primary education") + labels = c('Yes' = 1, 'No' = 0 ), + label = "Proportion of persons of concern enrolled in primary education") } return(datalist) } ``` -```{r example-impact3_2a} -## data -datalist <- kobocruncher::kobo_data( system.file("demo_data.xlsx", #"test.xlsx", +```{r example-impact3_2a, message=TRUE, warning=FALSE} +## data, cf example fct_re_map() +datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", package = "IndicatorCalc")) - -## Apply indicator function on datalist +## Apply calculation datalist <- impact3_2a(datalist ) ## Visualise value @@ -776,13 +802,6 @@ test_that("impact3_2a works", { #' @param datalist A list with all hierarchical data frame for a survey data set. #' format is expected to match the Excel export synchronized from kobo to RILD #' and loaded with kobocruncher::kobo_data() -#' -#' @param mapper a list providing the mapping of the variables used for the -#' calculation - this mapper is potentially to be adjusted in relation with deviation -#' between the the standard XlsForm and the contextualized dataset -#' -#' @param chain Boolean TRUE/FALSE to tell whether the function should return -#' the dataset expanded with the variable or a single factor #' #' @importFrom dplyr mutate case_when #' @importFrom labelled labelled @@ -796,18 +815,19 @@ mapper = list( hierarchy = "ind", variablemap = data.frame( label = -c("1. Has ${name_individual} ever attended school?", "2. Did ${name_individual} attend school or pre-school at any time during current school year?", -"3. During this/that school year , what level is (was) ${name_individual} attending?", -"4. What type of school?") +c("1. Has ${child_edu_name} ever attended school?", "2. Did ${child_edu_name} attend school or pre-school at any time during current school year?", +"3. During this/that school year , what level is (was) ${child_edu_name} attending?", +"4. What type of school?", "6. Can you estimate how old is ${HH02}?" +) , variable = -c("EDU01", "EDU02", "EDU03", "EDU04") +c("EDU01", "EDU02", "EDU03", "EDU04", "HH07") ), modalitymap = data.frame( variable = c("EDU01", "EDU01", "EDU02", "EDU02", "EDU03", "EDU03", "EDU03", "EDU03", "EDU03", "EDU03", "EDU03", "EDU04", "EDU04", "EDU04", -"EDU04", "EDU04", "EDU04", "EDU04") +"EDU04", "EDU04", "EDU04", "EDU04", "HH07") , label = c("Yes", "No", "Yes", "No", "Early Childhood Education or Pre-primary", @@ -815,28 +835,27 @@ c("Yes", "No", "Yes", "No", "Early Childhood Education or Pre-primary", "Post-secondary - Technical and Vocational Education and Training (TVET)", "Tertiary", "Don’t know", "Government or public", "UN or NGO (non-governmental organization)", "Religious or faith-based organization", "Community", "Private", -"Other (specify)", "Don’t know") +"Other (specify)", "Don’t know", NA) , standard = c("1", "0", "1", "0", "1", "2", "3", "4", "5", "6", "98", "1", -"2", "3", "4", "5", "96", "98") +"2", "3", "4", "5", "96", "98", NA) )) - ## So first we check that we have what we need in the data set based on the mapper check_map <- fct_check_map(datalist = datalist, mapper = mapper) - if (! isTRUE(check_map )) { + if ( check_map == FALSE) { cat( "There are missing data requirement to calculate Indicator Impact 3.2.b") } else { datalist[["ind"]]$edu_secondary<- dplyr::case_when( - datalist[["ind"]]$EDU01 == 1 & - datalist[["ind"]]$EDU02 == 1 & - ( datalist[["ind"]]$EDU03 == 3 | - datalist[["ind"]]$EDU03 == 4) ~ 1, + as.integer(datalist[["ind"]]$EDU01) == 1 & + as.integer(datalist[["ind"]]$EDU02) == 1 & + ( as.integer(datalist[["ind"]]$EDU03) == 3 | + as.integer(datalist[["ind"]]$EDU03) == 4) ~ 1, - datalist[["ind"]]$EDU01 == 0 | - datalist[["ind"]]$EDU02== 0 ~ 0, + as.integer(datalist[["ind"]]$EDU01) == 0 | + as.integer(datalist[["ind"]]$EDU02) == 0 ~ 0, TRUE ~ 0) @@ -844,8 +863,8 @@ datalist[["ind"]]$edu_secondary<- dplyr::case_when( ##calculated as 11 to 18 above --> put as function variable... datalist[["ind"]]$age_secondary<- dplyr::case_when( - datalist[["ind"]]$HH07 >= 11 & - datalist[["ind"]]$HH07 <= 18 ~ 1, + as.integer(datalist[["ind"]]$HH07) >= 11 & + as.integer(datalist[["ind"]]$HH07) <= 18 ~ 1, TRUE ~ NA_real_) datalist[["ind"]]$impact3_2b <- sum(datalist[["ind"]]$edu_secondary, na.rm= TRUE) / @@ -860,12 +879,11 @@ datalist[["ind"]]$impact3_2b <- labelled::labelled(datalist[["ind"]]$impact3_2b } ``` -```{r example-impact3_2b} -## data -datalist <- kobocruncher::kobo_data( system.file("demo_data.xlsx", #"test.xlsx", +```{r example-impact3_2b, message=TRUE, warning=FALSE} +## data, cf example fct_re_map() +datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", package = "IndicatorCalc")) - -## Apply indicator function on datalist +## Apply calculation datalist <- impact3_2b(datalist ) ## Visualise value @@ -915,13 +933,6 @@ test_that("impact3_2b works", { #' @param datalist A list with all hierarchical data frame for a survey data set. #' format is expected to match the Excel export synchronized from kobo to RILD #' and loaded with kobocruncher::kobo_data() -#' -#' @param mapper a list providing the mapping of the variables used for the -#' calculation - this mapper is potentially to be adjusted in relation with deviation -#' between the the standard XlsForm and the contextualized dataset -#' -#' @param chain Boolean TRUE/FALSE to tell whether the function should return -#' the dataset expanded with the variable or a single factor #' #' @importFrom dplyr mutate case_when #' @importFrom labelled labelled @@ -932,11 +943,11 @@ test_that("impact3_2b works", { impact3_3 <- function(datalist ){ # Mapper - mapper = list( +mapper = list( hierarchy = "main", variablemap = data.frame( label = -"1. How safe do you feel walking alone in your neighbourhood after dark?" +"1. How safe does ${name_selectedadult18} feel walking alone in your area/neighbourhood after dark?" , variable = "SAF01" @@ -946,7 +957,7 @@ impact3_3 <- function(datalist ){ c("SAF01", "SAF01", "SAF01", "SAF01", "SAF01", "SAF01") , label = -c("Very safe", "Safe", "Unsafe", "Very unsafe", "Don’t know", +c("Very safe", "Fairly safe", "Bit unsafe", "Very unsafe", "Don’t know", "Prefer not to respond") , standard = @@ -956,35 +967,36 @@ c("1", "2", "3", "4", "98", "99") ## So first we check that we have what we need in the data set based on the mapper check_map <- fct_check_map(datalist = datalist, mapper = mapper) - if (! isTRUE(check_map )) { + if ( check_map == FALSE) { cat( "There are missing data requirement to calculate Indicator Impact 3.3") } else { datalist[["main"]]$impact3_3 <- dplyr::case_when( - datalist[["main"]]$SAF01 == 1 | datalist[["main"]]$SAF01 == 2 ~ 1, - datalist[["main"]]$SAF01 == 3 | datalist[["main"]]$SAF01 == 4 ~ 0 , - datalist[["main"]]$SAF01 == 98 | datalist[["main"]]$SAF01 == 99 ~ NA_real_) + as.integer(datalist[["main"]]$SAF01) == 1 | + as.integer(datalist[["main"]]$SAF01) == 2 ~ 1, + as.integer(datalist[["main"]]$SAF01) == 3 | + as.integer(datalist[["main"]]$SAF01) == 4 ~ 0 , + as.integer(datalist[["main"]]$SAF01) == 98 | + as.integer(datalist[["main"]]$SAF01) == 99 ~ NA_real_) datalist[["main"]]$impact3_3 <- labelled::labelled(datalist[["main"]]$impact3_3, - labels = c('Yes' = 1, 'No' = 0 ), - label = "Proportion of population that feel safe walking - alone in their neighbourhood") + labels = c('Yes' = 1, 'No' = 0 ), + label = "Proportion of population that feel safe walking alone in their neighbourhood") } return(datalist) } ``` -```{r example-impact3_3} -## data -datalist <- kobocruncher::kobo_data( system.file("demo_data.xlsx", #"test.xlsx", - package = "IndicatorCalc")) - -## Apply indicator function on datalist +```{r example-impact3_3, message=TRUE, warning=FALSE} +## data, cf example fct_re_map() +datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", + package = "IndicatorCalc")) +## Apply calculation datalist <- impact3_3(datalist) ## Visualise value -fct_plot_indic_donut(indicator = datalist[["ind"]]$impact3_3, +fct_plot_indic_donut(indicator = datalist[["main"]]$impact3_3, iconunicode = "f140") ``` @@ -1048,13 +1060,6 @@ test_that("impact3_3 works", { #' @param datalist A list with all hierarchical data frame for a survey data set. #' format is expected to match the Excel export synchronized from kobo to RILD #' and loaded with kobocruncher::kobo_data() -#' -#' @param mapper a list providing the mapping of the variables used for the -#' calculation - this mapper is potentially to be adjusted in relation with deviation -#' between the the standard XlsForm and the contextualized dataset -#' -#' @param chain Boolean TRUE/FALSE to tell whether the function should return -#' the dataset expanded with the variable or a single factor #' #' @importFrom dplyr mutate case_when #' @importFrom labelled labelled @@ -1064,121 +1069,104 @@ test_that("impact3_3 works", { #' @export outcome1_2 <- function(datalist){ ## mapper - mapper = list( - hierarchy = "ind", - variablemap = data.frame( - label = - c( - "3. Does ${name_individual} have a birth certificate?", - "4. Has ${name_individual}'s birth been registered with civil authorities?", - "5. Does ${name_individual} have the documents below?", - "6. Does ${name_individual} have any other document that establishes your legal identity?" - ) - , - variable = - c("REG03", "REG04", "REG05", "REG06") - ), - modalitymap = data.frame( - variable = - c( - "REG03", - "REG03", - "REG03", - "REG04", - "REG04", - "REG04", - "REG04", - "REG05", - "REG05", - "REG05", - "REG06", - "REG06", - "REG06" - ) - , - label = - c( - "Yes", - "No", - "Don't know", - "Yes", - "No", - "Don't know", - "Prefer not to say", - "Yes", - "No", - "Prefer not to respond", - "Yes", - "No", - "Prefer not to respond" - ) - , - standard = - c("1", "0", "98", "1", "0", "98", "99", "1", "0", "99", "1", - "0", "99") - ) - ) +mapper = list( + hierarchy = "ind", + variablemap = data.frame( + label = + c("3. Does ${name_individual} have a birth certificate?", "4. Has ${name_individual}'s birth been registered with civil authorities?", + "6. Can you estimate how old is ${HH02}?") + , + variable = + c("REG03", "REG04", "HH07") + ), + modalitymap = data.frame( + variable = + c("REG03", "REG03", "REG03", "REG04", "REG04", "REG04", "REG04", + "HH07") + , + label = + c("Yes", "No", "Don't know", "Yes", "No", "Don't know", "Prefer not to say", + NA) + , + standard = + c("1", "0", "98", "1", "0", "98", "99", NA) + )) ## So first we check that we have what we need in the data set based on the mapper check_map <- fct_check_map(datalist = datalist, mapper = mapper) - if (! isTRUE(check_map )) { + if ( check_map == FALSE) { cat( "There are missing data requirement to calculate Indicator Outcome 1.2") } else { ##Calculate children who has a birth certificate - datalist[["ind"]]$birthCertificate<- dplyr::case_when( - datalist[["ind"]]$REG03== 0 | - datalist[["ind"]]$REG03 == 98 ~ 0, - - datalist[["ind"]]$REG03 == 1 ~ 1) + datalist[["ind"]]$birthCertificate <- dplyr::case_when( + as.integer(datalist[["ind"]]$REG03) == 0 | + as.integer(datalist[["ind"]]$REG03) == 98 ~ 0, + as.integer(datalist[["ind"]]$REG03) == 1 ~ 1) datalist[["ind"]]$birthCertificate <- labelled::labelled(datalist[["ind"]]$birthCertificate, - labels = c('Yes' = 1, 'No' = 0 ), - label = "Children under 5 with a birth - certificate") + labels = c('Yes' = 1, 'No' = 0 ), + label = "Children with a birth certificate") ##Calculate children who has been registered with civil authorities -datalist[["ind"]]$birthRegistered<- dplyr::case_when( - datalist[["ind"]]$REG04 == 0 | - datalist[["ind"]]$REG04 == 98 ~ 0, - datalist[["ind"]]$REG04 == 1 ~ 1, - datalist[["ind"]]$REG04 == 99 ~ NA_real_) - -datalist[["ind"]]$birthRegistered <- labelled::labelled(datalist[["ind"]]$birthRegistered, - labels = c('Yes' = 1, 'No' = 0 ), - label = "Children under 5 birth registered - with civil authorities") - + datalist[["ind"]]$birthRegistered <- dplyr::case_when( + as.integer(datalist[["ind"]]$REG04) == 0 | + as.integer(datalist[["ind"]]$REG04) == 98 ~ 0, + as.integer(datalist[["ind"]]$REG04) == 1 ~ 1, + as.integer(datalist[["ind"]]$REG04) == 99 ~ NA_real_) + + datalist[["ind"]]$birthRegistered <- labelled::labelled(datalist[["ind"]]$birthRegistered, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Children with birth registered with civil authorities") + + ## less than 5 + datalist[["ind"]]$less_than_5 <- dplyr::case_when( + as.numeric(datalist[["ind"]]$HH07) < 5 ~ 1, + TRUE ~ 0) + datalist[["ind"]]$less_than_5 <- labelled::labelled(datalist[["ind"]]$less_than_5, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Is a Children") ##if the birth is registered or child has a birth certificate -datalist[["ind"]]$outcome1_2<- dplyr::case_when( + datalist[["ind"]]$outcome1_2 <- dplyr::case_when( (datalist[["ind"]]$birthRegistered == 1 | datalist[["ind"]]$birthCertificate == 1) & - datalist[["ind"]]$HH07 <5 ~ 1, + as.integer(datalist[["ind"]]$HH07) < 5 ~ 1, - (datalist[["ind"]]$birthRegistered== 0 & - datalist[["ind"]]$birthCertificate== 0) & - datalist[["ind"]]$HH07 <5 ~ 0) + (datalist[["ind"]]$birthRegistered == 0 & + datalist[["ind"]]$birthCertificate == 0) & + as.integer(datalist[["ind"]]$HH07) < 5 ~ 0, + + TRUE ~ NA_real_ ) -datalist[["ind"]]$outcome1_2 <- labelled::labelled(datalist[["ind"]]$outcome1_2, - labels = c('Yes' = 1, 'No' = 0 ), - label = "Proportion of children under 5 years of age whose - births have been registered with a civil authority") + datalist[["ind"]]$outcome1_2 <- labelled::labelled(datalist[["ind"]]$outcome1_2, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Proportion of children under 5 years of age whose births have been registered with a civil authority") } return(datalist) } ``` -```{r example-outcome1_2} -## data -datalist <- kobocruncher::kobo_data( system.file("demo_data.xlsx", #"test.xlsx", +```{r example-outcome1_2, message=TRUE, warning=FALSE} +## data, cf example fct_re_map() +datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", package = "IndicatorCalc")) + ## Apply indicator function on datalist datalist <- outcome1_2(datalist) - +table(datalist[["ind"]]$outcome1_2, useNA = "ifany") +table(datalist[["ind"]]$less_than_5, useNA = "ifany") +table(datalist[["ind"]]$HH07, useNA = "ifany") +barplot(as.integer(datalist[["ind"]]$HH07)) +table(datalist[["ind"]]$birthCertificate, useNA = "ifany") +table(datalist[["ind"]]$birthRegistered, useNA = "ifany") ## Visualise value -fct_plot_indic_donut(indicator = datalist[["main"]]$outcome1_2, +fct_plot_indic_donut(indicator = datalist[["ind"]]$outcome1_2, + iconunicode = "f140") +fct_plot_indic_donut(indicator = datalist[["ind"]]$birthCertificate, + iconunicode = "f140") +fct_plot_indic_donut(indicator = datalist[["ind"]]$birthRegistered, iconunicode = "f140") ``` @@ -1234,13 +1222,6 @@ test_that("outcome1_2 works", { #' @param datalist A list with all hierarchical data frame for a survey data set. #' format is expected to match the Excel export synchronized from kobo to RILD #' and loaded with kobocruncher::kobo_data() -#' -#' @param mapper a list providing the mapping of the variables used for the -#' calculation - this mapper is potentially to be adjusted in relation with deviation -#' between the the standard XlsForm and the contextualized dataset -#' -#' @param chain Boolean TRUE/FALSE to tell whether the function should return -#' the dataset expanded with the variable or a single factor #' #' @importFrom dplyr mutate case_when #' @importFrom labelled labelled @@ -1250,65 +1231,88 @@ test_that("outcome1_2 works", { #' @export outcome1_3 <- function(datalist){ ## Mapper - mapper = list( - hierarchy = "main", - variablemap = data.frame( - label = - c( - "1. Does ${name_individual} have the documents below?", - "2. Does ${name_individual} have any other document that establishes his/her legal identity?" - ) - , - variable = - c("REG01", "REG02") - ), - modalitymap = data.frame( - variable = - c("REG01", "REG01", "REG01", "REG02", "REG02", "REG02") - , - label = - c( - "Yes", - "No", - "Prefer not to respond", - "Yes", - "No", - "Prefer not to respond" - ) - , - standard = - c("1", "0", "99", "1", "0", "99") - ) -) +mapper = list( + hierarchy = "ind", + variablemap = data.frame( + label = +c("Passport?", "Birth certificate?", "Civil/government-issued ID card ?", +"Residency permit (both temporary and permanent)?", "Statelessness documentation?", +"Household card of address / family book/ marriage certificate?", +"Social security card?", "2. Does ${name_individual} have any other document that establishes his/her legal identity?", +"3. Does ${name_individual} have a birth certificate?", "Passport?", +"Civil/government-issued ID card ?", "Residency permit (both temporary and permanent)?", +"Statelessness documentation?", "Household card of address / family book/ marriage certificate?", +"Social security card?", "6. Does ${name_individual} have any other document that establishes your legal identity?" +) + , + variable = +c("REG01a", "REG01b", "REG01c", "REG01d", "REG01e", "REG01f", +"REG01g", "REG02", "REG03", "REG05a", "REG05b", "REG05c", "REG05d", +"REG05e", "REG05f", "REG06") + ), + modalitymap = data.frame( + variable = +c("REG01a", "REG01a", "REG01a", "REG01b", "REG01b", "REG01b", +"REG01c", "REG01c", "REG01c", "REG01d", "REG01d", "REG01d", "REG01e", +"REG01e", "REG01e", "REG01f", "REG01f", "REG01f", "REG01g", "REG01g", +"REG01g", "REG02", "REG02", "REG02", "REG03", "REG03", "REG03", +"REG05a", "REG05a", "REG05a", "REG05b", "REG05b", "REG05b", "REG05c", +"REG05c", "REG05c", "REG05d", "REG05d", "REG05d", "REG05e", "REG05e", +"REG05e", "REG05f", "REG05f", "REG05f", "REG06", "REG06", "REG06" +) + , + label = +c("Yes", "No", "Prefer not to respond", "Yes", "No", "Prefer not to respond", +"Yes", "No", "Prefer not to respond", "Yes", "No", "Prefer not to respond", +"Yes", "No", "Prefer not to respond", "Yes", "No", "Prefer not to respond", +"Yes", "No", "Prefer not to respond", "Yes", "No", "Prefer not to respond", +"Yes", "No", "Don't know", "Yes", "No", "Prefer not to respond", +"Yes", "No", "Prefer not to respond", "Yes", "No", "Prefer not to respond", +"Yes", "No", "Prefer not to respond", "Yes", "No", "Prefer not to respond", +"Yes", "No", "Prefer not to respond", "Yes", "No", "Prefer not to respond" +) + , + standard = +c("1", "0", "99", "1", "0", "99", "1", "0", "99", "1", "0", "99", +"1", "0", "99", "1", "0", "99", "1", "0", "99", "1", "0", "99", +"1", "0", "98", "1", "0", "99", "1", "0", "99", "1", "0", "99", +"1", "0", "99", "1", "0", "99", "1", "0", "99", "1", "0", "99" +) + )) ## So first we check that we have what we need in the data set based on the mapper check_map <- fct_check_map(datalist = datalist, mapper = mapper) - if (! isTRUE(check_map )) { + if ( check_map == FALSE) { cat( "There are missing data requirement to calculate Indicator Outcome 1.3") } else { -datalist[["ind"]]$document_under5<- dplyr::case_when( - datalist[["ind"]]$REG05a == 1 | - datalist[["ind"]]$REG05b == 1 | - datalist[["ind"]]$REG05c == 1 | - datalist[["ind"]]$REG05d == 1 | - datalist[["ind"]]$REG05e == 1 | - datalist[["ind"]]$REG05f == 1 | - datalist[["ind"]]$REG06 == 1 | - datalist[["ind"]]$REG03 == 1 ~ 1, +datalist[["ind"]]$document_under5 <- dplyr::case_when( + as.integer(datalist[["ind"]]$REG05a) == 1 | + as.integer(datalist[["ind"]]$REG05b) == 1 | + as.integer(datalist[["ind"]]$REG05c) == 1 | + as.integer(datalist[["ind"]]$REG05d) == 1 | + as.integer(datalist[["ind"]]$REG05e) == 1 | + as.integer(datalist[["ind"]]$REG05f) == 1 | + as.integer(datalist[["ind"]]$REG06) == 1 | + as.integer(datalist[["ind"]]$REG03) == 1 ~ 1, - datalist[["ind"]]$REG05a != 1 & - datalist[["ind"]]$REG05b != 1 & - datalist[["ind"]]$REG05c != 1 & - datalist[["ind"]]$REG05d != 1 & - datalist[["ind"]]$REG05e != 1 & - datalist[["ind"]]$REG05f != 1 & - datalist[["ind"]]$REG06 != 1 & - datalist[["ind"]]$REG03 != 1 ~ 0, + as.integer(datalist[["ind"]]$REG05a) != 1 & + as.integer(datalist[["ind"]]$REG05b) != 1 & + as.integer(datalist[["ind"]]$REG05c) != 1 & + as.integer(datalist[["ind"]]$REG05d) != 1 & + as.integer(datalist[["ind"]]$REG05e) != 1 & + as.integer(datalist[["ind"]]$REG05f) != 1 & + as.integer(datalist[["ind"]]$REG06) != 1 & + as.integer(datalist[["ind"]]$REG03) != 1 ~ 0, TRUE ~ NA_real_ ) + +datalist[["ind"]]$document_under5 <- labelled::labelled(datalist[["ind"]]$document_under5, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Proportion of Persons above 5 with legally + recognized identity documents or credentials") ###Calculate valid identity documents for above 5 with REG01 and REG02 variables #ind$REG01a # passport @@ -1322,26 +1326,31 @@ datalist[["ind"]]$document_under5<- dplyr::case_when( #Make sure to delete REG01e below from the script if you don't have any stateless -datalist[["ind"]]$document_above5<- dplyr::case_when( - datalist[["ind"]]$REG01a == 1 | - datalist[["ind"]]$REG01b == 1 | - datalist[["ind"]]$REG01c == 1 | - datalist[["ind"]]$REG01d == 1 | - datalist[["ind"]]$REG01e == 1 | - datalist[["ind"]]$REG01f == 1 | - datalist[["ind"]]$REG01g == 1 | - datalist[["ind"]]$REG02 == 1 ~ 1, +datalist[["ind"]]$document_above5 <- dplyr::case_when( + as.integer(datalist[["ind"]]$REG01a) == 1 | + as.integer(datalist[["ind"]]$REG01b) == 1 | + as.integer(datalist[["ind"]]$REG01c) == 1 | + as.integer(datalist[["ind"]]$REG01d) == 1 | + as.integer(datalist[["ind"]]$REG01e) == 1 | + as.integer(datalist[["ind"]]$REG01f) == 1 | + as.integer(datalist[["ind"]]$REG01g) == 1 | + as.integer(datalist[["ind"]]$REG02) == 1 ~ 1, - datalist[["ind"]]$REG01a != 1 & - datalist[["ind"]]$REG01b != 1 & - datalist[["ind"]]$REG01c != 1 & - datalist[["ind"]]$REG01d != 1 & - datalist[["ind"]]$REG01e != 1 & - datalist[["ind"]]$REG01f != 1 & - datalist[["ind"]]$REG01g != 1 & - datalist[["ind"]]$REG02 != 1 ~ 0, + as.integer(datalist[["ind"]]$REG01a) != 1 & + as.integer(datalist[["ind"]]$REG01b) != 1 & + as.integer(datalist[["ind"]]$REG01c) != 1 & + as.integer(datalist[["ind"]]$REG01d) != 1 & + as.integer(datalist[["ind"]]$REG01e) != 1 & + as.integer(datalist[["ind"]]$REG01f) != 1 & + as.integer(datalist[["ind"]]$REG01g) != 1 & + as.integer(datalist[["ind"]]$REG02) != 1 ~ 0, TRUE ~ NA_real_) + +datalist[["ind"]]$document_above5 <- labelled::labelled(datalist[["ind"]]$document_above5, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Proportion of Persons above 5 with legally + recognized identity documents or credentials") ##Combine both age groups datalist[["ind"]]$outcome1_3<- dplyr::case_when( @@ -1353,7 +1362,7 @@ datalist[["ind"]]$outcome1_3<- dplyr::case_when( datalist[["ind"]]$outcome1_3 <- labelled::labelled(datalist[["ind"]]$outcome1_3, labels = c('Yes' = 1, 'No' = 0 ), - label = "Proportion of Persons of Concern with legally + label = "Proportion of Persons with legally recognized identity documents or credentials") } @@ -1361,19 +1370,21 @@ datalist[["ind"]]$outcome1_3 <- labelled::labelled(datalist[["ind"]]$outcome1_3 } ``` -```{r example-outcome1_3} -## data -datalist <- kobocruncher::kobo_data( system.file("demo_data.xlsx", #"test.xlsx", +```{r example-outcome1_3, message=TRUE, warning=FALSE} +## data, cf example fct_re_map() +datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", package = "IndicatorCalc")) - -## mapper -mapper <- list("") - ## Apply indicator function on datalist -datalist <- outcome1_3(datalist, mapper ) +datalist <- outcome1_3(datalist) ## Visualise value -fct_plot_indic_donut(indicator = datalist[["main"]]$outcome1_3, +fct_plot_indic_donut(indicator = datalist[["ind"]]$outcome1_3, + iconunicode = "f140") + +fct_plot_indic_donut(indicator = datalist[["ind"]]$document_above5, + iconunicode = "f140") + +fct_plot_indic_donut(indicator = datalist[["ind"]]$document_under5, iconunicode = "f140") ``` @@ -1424,13 +1435,6 @@ test_that("outcome1_3 works", { #' @param datalist A list with all hierarchical data frame for a survey data set. #' format is expected to match the Excel export synchronized from kobo to RILD #' and loaded with kobocruncher::kobo_data() -#' -#' @param mapper a list providing the mapping of the variables used for the -#' calculation - this mapper is potentially to be adjusted in relation with deviation -#' between the the standard XlsForm and the contextualized dataset -#' -#' @param chain Boolean TRUE/FALSE to tell whether the function should return -#' the dataset expanded with the variable or a single factor #' #' @importFrom dplyr mutate case_when #' @importFrom labelled labelled @@ -1441,39 +1445,41 @@ test_that("outcome1_3 works", { outcome4_1 <- function(datalist){ # mapper , - mapper = list( +mapper = list( hierarchy = "main", variablemap = data.frame( label = -c("2. If someone in your community is subject to gender-based violence and asks for your help, would you be able to tell this person about the following services in this area?", -"Health services", "Psycho-social services and Case management services", -"Safety and security services? (police, safe shelters)", "Legal assistance") , +c("Health services", "Psycho-social services", "Safety and security services? (police, safe shelters)", +"Legal assistance") + , variable = -c("GBV01_note", "GBV01a", "GBV01b", "GBV01c", "GBV01d") ), +c("GBV01a", "GBV01b", "GBV01c", "GBV01d") + ), modalitymap = data.frame( variable = -c("GBV01_note", "GBV01a", "GBV01a", "GBV01a", "GBV01b", "GBV01b", -"GBV01b", "GBV01c", "GBV01c", "GBV01c", "GBV01d", "GBV01d", "GBV01d") , +c("GBV01a", "GBV01a", "GBV01a", "GBV01b", "GBV01b", "GBV01b", +"GBV01c", "GBV01c", "GBV01c", "GBV01d", "GBV01d", "GBV01d") + , label = -c(NA, "Yes", "No", "Don't know", "Yes", "No", "Don't know", "Yes", -"No", "Don't know", "Yes", "No", "Don't know") , +c("Yes", "No", "Don't know", "Yes", "No", "Don't know", "Yes", +"No", "Don't know", "Yes", "No", "Don't know") + , standard = -c(NA, "1", "0", "98", "1", "0", "98", "1", "0", "98", "1", "0", -"98") - )) - +c("1", "0", "98", "1", "0", "98", "1", "0", "98", "1", "0", "98" +) + )) ## So first we check that we have what we need in the data set based on the mapper check_map <- fct_check_map(datalist = datalist, mapper = mapper) - if (! isTRUE(check_map )) { + if ( check_map == FALSE) { cat( "There are missing data requirement to calculate Indicator Outcome 4.1") } else { datalist[["main"]]$outcome4_1 <- dplyr::case_when( - datalist[["main"]]$GBV01a== 1 | - datalist[["main"]]$GBV01b== 1 | - datalist[["main"]]$GBV01c== 1 | - datalist[["main"]]$GBV01d== 1 ~ 1, + as.integer(datalist[["main"]]$GBV01a) == 1 | + as.integer(datalist[["main"]]$GBV01b) == 1 | + as.integer(datalist[["main"]]$GBV01c) == 1 | + as.integer(datalist[["main"]]$GBV01d) == 1 ~ 1, TRUE ~ 0) datalist[["main"]]$outcome4_1 <- labelled::labelled(datalist[["main"]]$outcome4_1, @@ -1485,13 +1491,12 @@ datalist[["main"]]$outcome4_1 <- labelled::labelled(datalist[["main"]]$outcome4 } ``` -```{r example-outcome4_1} -## data -datalist <- kobocruncher::kobo_data( system.file("demo_data.xlsx", #"test.xlsx", +```{r example-outcome4_1, message=TRUE, warning=FALSE} +## data, cf example fct_re_map() +datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", package = "IndicatorCalc")) - ## Apply indicator function on datalist -datalist <- outcome1_4(datalist ) +datalist <- outcome4_1(datalist ) ## Visualise value fct_plot_indic_donut(indicator = datalist[["main"]]$outcome1_4, @@ -1557,13 +1562,6 @@ test_that("outcome4_1 works", { #' @param datalist A list with all hierarchical data frame for a survey data set. #' format is expected to match the Excel export synchronized from kobo to RILD #' and loaded with kobocruncher::kobo_data() -#' -#' @param mapper a list providing the mapping of the variables used for the -#' calculation - this mapper is potentially to be adjusted in relation with deviation -#' between the the standard XlsForm and the contextualized dataset -#' -#' @param chain Boolean TRUE/FALSE to tell whether the function should return -#' the dataset expanded with the variable or a single factor #' #' @importFrom dplyr mutate case_when #' @importFrom labelled labelled @@ -1573,54 +1571,52 @@ test_that("outcome4_1 works", { #' @export outcome4_2 <- function(datalist ){ # Mapper , - mapper = list( +mapper = list( hierarchy = "main", variablemap = data.frame( label = -c("In your opinion, is a husband justified in hitting or beating his wife in the following situations:", -"If she goes out without telling him?", "If she neglects the children?", +c("If she goes out without telling him?", "If she neglects the children?", "If she argues with him?", "If she refuses to have sex with him?", "If she burns the food?") , variable = -c("VAW01_note", "VAW01a", "VAW01b", "VAW01c", "VAW01d", "VAW01e" -) +c("VAW01a", "VAW01b", "VAW01c", "VAW01d", "VAW01e") ), modalitymap = data.frame( variable = -c("VAW01_note", "VAW01a", "VAW01a", "VAW01a", "VAW01b", "VAW01b", -"VAW01b", "VAW01c", "VAW01c", "VAW01c", "VAW01d", "VAW01d", "VAW01d", -"VAW01e", "VAW01e", "VAW01e") +c("VAW01a", "VAW01a", "VAW01a", "VAW01b", "VAW01b", "VAW01b", +"VAW01c", "VAW01c", "VAW01c", "VAW01d", "VAW01d", "VAW01d", "VAW01e", +"VAW01e", "VAW01e") , label = -c(NA, "Yes", "No", "Prefer not to respond", "Yes", "No", "Prefer not to respond", +c("Yes", "No", "Prefer not to respond", "Yes", "No", "Prefer not to respond", "Yes", "No", "Prefer not to respond", "Yes", "No", "Prefer not to respond", "Yes", "No", "Prefer not to respond") , standard = -c(NA, "1", "0", "99", "1", "0", "99", "1", "0", "99", "1", "0", -"99", "1", "0", "99") - )) +c("1", "0", "99", "1", "0", "99", "1", "0", "99", "1", "0", "99", +"1", "0", "99") + )) ## So first we check that we have what we need in the data set based on the mapper check_map <- fct_check_map(datalist = datalist, mapper = mapper) - if (! isTRUE(check_map )) { + if ( check_map == FALSE) { cat( "There are missing data requirement to calculate Indicator Outcome 4.2") } else { datalist[["main"]]$outcome4_2<- dplyr::case_when( - datalist[["main"]]$VAW01a== 1 | - datalist[["main"]]$VAW01b== 1 | - datalist[["main"]]$VAW01c== 1 | - datalist[["main"]]$VAW01d== 1 | - datalist[["main"]]$VAW01e== 1 ~ 0, + as.integer(datalist[["main"]]$VAW01a) == 1 | + as.integer(datalist[["main"]]$VAW01b) == 1 | + as.integer(datalist[["main"]]$VAW01c) == 1 | + as.integer(datalist[["main"]]$VAW01d) == 1 | + as.integer(datalist[["main"]]$VAW01e) == 1 ~ 0, - datalist[["main"]]$VAW01a== 0 & - datalist[["main"]]$VAW01b== 0 & - datalist[["main"]]$VAW01c== 0 & - datalist[["main"]]$VAW01d== 0 & - datalist[["main"]]$VAW01e== 0 ~ 1, + as.integer(datalist[["main"]]$VAW01a) == 0 & + as.integer(datalist[["main"]]$VAW01b) == 0 & + as.integer(datalist[["main"]]$VAW01c) == 0 & + as.integer(datalist[["main"]]$VAW01d) == 0 & + as.integer(datalist[["main"]]$VAW01e) == 0 ~ 1, TRUE ~ NA_real_) @@ -1633,11 +1629,10 @@ datalist[["main"]]$outcome4_2 <- labelled::labelled(datalist[["main"]]$outcome4 } ``` -```{r example-outcome4_2} -## data -datalist <- kobocruncher::kobo_data( system.file("demo_data.xlsx", #"test.xlsx", +```{r example-outcome4_2, message=TRUE, warning=FALSE} +## data, cf example fct_re_map() +datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", package = "IndicatorCalc")) - ## Apply indicator function on datalist datalist <- outcome4_2(datalist) @@ -1681,13 +1676,6 @@ test_that("outcome4_2 works", { #' @param datalist A list with all hierarchical data frame for a survey data set. #' format is expected to match the Excel export synchronized from kobo to RILD #' and loaded with kobocruncher::kobo_data() -#' -#' @param mapper a list providing the mapping of the variables used for the -#' calculation - this mapper is potentially to be adjusted in relation with deviation -#' between the the standard XlsForm and the contextualized dataset -#' -#' @param chain Boolean TRUE/FALSE to tell whether the function should return -#' the dataset expanded with the variable or a single factor #' #' @importFrom dplyr mutate case_when #' @importFrom labelled labelled @@ -1697,12 +1685,12 @@ test_that("outcome4_2 works", { #' @export outcome5_2 <- function(datalist){ ## Mapper , - mapper = list( +mapper = list( hierarchy = "ind", variablemap = data.frame( label = -c("1. Has ${name_individual} participated in sports, arts, cultural activities or other after-school programmes for children outside the home in the last month?", -"2. In the past month, how many times did ${name_individual} participate in these activities?", +c("1. Has ${child_edu_name} participated in sports, arts, cultural activities or other after-school programmes for children outside the home in the last month?", +"2. In the past month, how many times did ${child_edu_name} participate in these activities?", "3. Were they in a physically safe area while participating in the activity?", "4. Were there adults supervising the activities?") , @@ -1720,28 +1708,28 @@ c("Yes", "No", NA, "Yes", "No", "Don't know", "Yes", "No", "Don't know" , standard = c("1", "0", NA, "1", "0", "98", "1", "0", "98") - )) + )) ## So first we check that we have what we need in the data set based on the mapper check_map <- fct_check_map(datalist = datalist, mapper = mapper) - if (! isTRUE(check_map )) { + if ( check_map == FALSE) { cat( "There are missing data requirement to calculate Indicator Outcome 5.2") } else { datalist[["ind"]]$outcome5_2<- dplyr::case_when( - ( datalist[["ind"]]$COMM01 == 1 & - datalist[["ind"]]$COMM02 >= 2 & - datalist[["ind"]]$COMM03 == 1 & - datalist[["ind"]]$COMM04 == 1) ~ 1, + ( as.integer(datalist[["ind"]]$COMM01) == 1 & + as.integer(datalist[["ind"]]$COMM02) >= 2 & + as.integer(datalist[["ind"]]$COMM03) == 1 & + as.integer(datalist[["ind"]]$COMM04) == 1) ~ 1, - ( datalist[["ind"]]$COMM01 == 0 | - ( datalist[["ind"]]$COMM02 < 2 | - datalist[["ind"]]$COMM02 == 98 ) | - ( datalist[["ind"]]$COMM03== 0 | - datalist[["ind"]]$COMM03 == 98) | - ( datalist[["ind"]]$COMM04== 0 | - datalist[["ind"]]$COMM04==98)) ~ 0, + ( as.integer(datalist[["ind"]]$COMM01) == 0 | + ( as.integer(datalist[["ind"]]$COMM02) < 2 | + as.integer(datalist[["ind"]]$COMM02) == 98 ) | + ( as.integer(datalist[["ind"]]$COMM03) == 0 | + as.integer(datalist[["ind"]]$COMM03) == 98) | + ( as.integer(datalist[["ind"]]$COMM04) == 0 | + as.integer(datalist[["ind"]]$COMM04) == 98)) ~ 0, TRUE ~ NA_real_) @@ -1754,15 +1742,15 @@ datalist[["ind"]]$outcome5_2 <- labelled::labelled(datalist[["ind"]]$outcome5_2 } ``` -```{r example-outcome5_2} -## data -datalist <- kobocruncher::kobo_data( system.file("demo_data.xlsx", #"test.xlsx", +```{r example-outcome5_2, message=TRUE, warning=FALSE} +## data, cf example fct_re_map() +datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", package = "IndicatorCalc")) ## Apply indicator function on datalist datalist <- outcome5_2(datalist ) ## Visualise value -fct_plot_indic_donut(indicator = datalist[["main"]]$outcome5_2, +fct_plot_indic_donut(indicator = datalist[["ind"]]$outcome5_2, iconunicode = "f140") ``` @@ -1804,13 +1792,6 @@ test_that("outcome5_2 works", { #' @param datalist A list with all hierarchical data frame for a survey data set. #' format is expected to match the Excel export synchronized from kobo to RILD #' and loaded with kobocruncher::kobo_data() -#' -#' @param mapper a list providing the mapping of the variables used for the -#' calculation - this mapper is potentially to be adjusted in relation with deviation -#' between the the standard XlsForm and the contextualized dataset -#' -#' @param chain Boolean TRUE/FALSE to tell whether the function should return -#' the dataset expanded with the variable or a single factor #' #' @importFrom dplyr mutate case_when #' @importFrom labelled labelled @@ -1819,8 +1800,8 @@ test_that("outcome5_2 works", { #' #' @export outcome8_2 <- function(datalist){ - # Mapper , - mapper = list( +mapper = list( + hierarchy = "ind", hierarchy = "main", variablemap = data.frame( label = @@ -1838,48 +1819,52 @@ c("COOK01", "COOK01", "COOK02", "COOK02", "COOK02", "COOK02", "COOK02", "COOK02", "COOK02", "COOK02", "COOK02", "COOK02", "COOK02", "COOK03", "COOK03", "COOK03", "COOK03", "COOK03", "COOK03", "COOK03", "COOK03", "COOK03", "COOK03", "COOK03", "COOK03", "COOK03", "COOK03", +"COOK03", "COOK03", "COOK03", "COOK03", "COOK03", "COOK03", "COOK03", "COOK03") , label = c("Yes", "No", "Solar cooker (thermal energy from the sun, not solar panels)", "Electric stove", "Piped natural gas stove", "Biogas stove", -"Liquefied petroleum gas (LPG)/ cooking gas stove", "Manufactured solid fuel stove", -"Traditional solid fuel stove (non-manufactured)", "Moveable firepan", -"Three stone stove/open fire", "Liquid fuel stove", "Other, specify", -"Alcohol/ethanol", "Gasoline/diesel (not in generator)", "Kerosene/paraffin", -"Coal/lignite unprocessed", "Coal/lignite briquettes/pellets", -"Charcoal unprocessed", "Charcoal briquettes/pellets", "Wood", -"Agricultural or crop residue/grass/ straw/ shrubs/ corn cobs", +"Liquefied petroleum gas (LPG)/ cooking gas stove", "Liquid fuel stove", +"Manufactured solid fuel stove", "Traditional solid fuel stove (non-manufactured)", +"Moveable firepan", "Three stone stove/open fire", "Other, specify", +"Alcohol/ethanol", "Liquefied petroleum gas", "Biogas", "Electricity from the national grid", +"Electricity from solar mini-grid", "Electricity from diesel generator", +"Electricity from hybrid mini-grid (i.e., solar+ diesel, solar+ national grid, etc..)", +"Gasoline/diesel (not in generator)", "Kerosene/paraffin", "Coal/lignite unprocessed", +"Coal/lignite briquettes/pellets", "Charcoal unprocessed", "Charcoal briquettes/pellets", +"Wood (collected)", "Wood (distributed/purchased)", "Agricultural or crop residue/grass/ straw/ shrubs/ corn cobs", "Animal waste/dung", "Processed biomass pellets/briquettes", "Woodchips", "Garbage/plastic", "Sawdust", "Other, specify") , standard = c("1", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "96", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", -"12", "13", "14", "96") - )) +"12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "96" +) + )) ## So first we check that we have what we need in the data set based on the mapper check_map <- fct_check_map(datalist = datalist, mapper = mapper) - if (! isTRUE(check_map )) { + if ( check_map == FALSE) { cat( "There are missing data requirement to calculate Indicator Outcome 8.2") } else { datalist[["main"]]$outcome8_2 <- dplyr::case_when( - ( datalist[["main"]]$COOK01 == 1 & + ( as.integer(datalist[["main"]]$COOK01) == 1 & (datalist[["main"]]$COOK02 %in% c("1", "2", "3", "4", "5")) | (datalist[["main"]]$COOK02 %in% c("6") & - datalist[["main"]]$COOK03 %in% c("1", "3")) ) ~ "1", + datalist[["main"]]$COOK03 %in% c("1", "3")) ) ~ 1, - (datalist[["main"]]$COOK01 == 1 & + ( as.integer(datalist[["main"]]$COOK01) == 1 & (datalist[["main"]]$COOK02 %in% c("7", "8", "9", "10", "96")) | ((datalist[["main"]]$COOK02 %in% c("6") & - !(datalist[["main"]]$COOK03 %in% c("1", "3") )))) ~ "0" , + !(datalist[["main"]]$COOK03 %in% c("1", "3") )))) ~ 0 , TRUE ~ NA_real_ ) datalist[["main"]]$outcome8_2 <- labelled::labelled(datalist[["main"]]$outcome8_2, - labels = c('Yes' = "1", 'No' = "0" ), + labels = c('Yes' = 1, 'No' = 0 ), label = "Proportion of Persons of Concern with primary reliance on clean (cooking) fuels and technology") } @@ -1888,11 +1873,10 @@ datalist[["main"]]$outcome8_2 <- labelled::labelled(datalist[["main"]]$outcome8 } ``` -```{r example-outcome8_2} -## data -datalist <- kobocruncher::kobo_data( system.file("demo_data.xlsx", #"test.xlsx", +```{r example-outcome8_2, message=TRUE, warning=FALSE} +## data, cf example fct_re_map() +datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", package = "IndicatorCalc")) - ## Apply indicator function on datalist datalist <- outcome8_2(datalist ) @@ -1946,13 +1930,6 @@ test_that("outcome8_2 works", { #' @param datalist A list with all hierarchical data frame for a survey data set. #' format is expected to match the Excel export synchronized from kobo to RILD #' and loaded with kobocruncher::kobo_data() -#' -#' @param mapper a list providing the mapping of the variables used for the -#' calculation - this mapper is potentially to be adjusted in relation with deviation -#' between the the standard XlsForm and the contextualized dataset -#' -#' @param chain Boolean TRUE/FALSE to tell whether the function should return -#' the dataset expanded with the variable or a single factor #' #' @importFrom dplyr mutate case_when #' @importFrom labelled labelled @@ -1962,67 +1939,79 @@ test_that("outcome8_2 works", { #' @export outcome9_1 <- function(datalist){ # Mapper , - mapper = list( +mapper = list( hierarchy = "main", variablemap = data.frame( label = -c("1.What type of dwelling does the household live in?", "2. Please observe if the door is lockable ?", -"3. Please observe if windows are lockable ?", "4. Please observe if shelter collapsed or partially collapsed ?", -"5. Please observe if the roof is damaged?", "6. Please observe if windows/doors are sealed to natural elements ?", -"7. Please observe if there is a leakage /rottenness in the walls / floors?", -"10. How many separate rooms do the members of your household occupy?", -"7. Does your household pay any rent?", "8. Can your household generally afford to pay the rent without any major financial distress?" +c("1.What type of dwelling does the household live in?", "2. Main material of the dwelling floor", +"3. Main material of the roof.", "4. Main material of the exterior walls", +"5. How many separate rooms do the members of your household occupy?", +"8. Does your household pay any rent?", "9. Can your household generally afford to pay the rent without any major financial distress?", +"What is the total number of persons in this household ? (including the respondent)" ) , variable = -c("DWE01", "SHEL01", "SHEL02", "SHEL03", "SHEL04", "SHEL05", -"SHEL06", "DWE05", "DWE08", "DWE09") +c("DWE01", "DWE02", "DWE03", "DWE04", "DWE05", "DWE08", "DWE09", +"HH01") ), modalitymap = data.frame( variable = c("DWE01", "DWE01", "DWE01", "DWE01", "DWE01", "DWE01", "DWE01", -"DWE01", "DWE01", "SHEL01", "SHEL01", "SHEL01", "SHEL02", "SHEL02", -"SHEL02", "SHEL03", "SHEL03", "SHEL03", "SHEL04", "SHEL04", "SHEL04", -"SHEL05", "SHEL05", "SHEL05", "SHEL06", "SHEL06", "SHEL06", "DWE05", -"DWE08", "DWE08", "DWE09", "DWE09", "DWE09", "DWE09") +"DWE01", "DWE01", "DWE01", "DWE02", "DWE02", "DWE02", "DWE02", +"DWE02", "DWE02", "DWE02", "DWE02", "DWE02", "DWE02", "DWE03", +"DWE03", "DWE03", "DWE03", "DWE03", "DWE03", "DWE03", "DWE03", +"DWE03", "DWE03", "DWE03", "DWE03", "DWE03", "DWE03", "DWE04", +"DWE04", "DWE04", "DWE04", "DWE04", "DWE04", "DWE04", "DWE04", +"DWE04", "DWE04", "DWE04", "DWE04", "DWE04", "DWE04", "DWE04", +"DWE04", "DWE05", "DWE08", "DWE08", "DWE09", "DWE09", "DWE09", +"DWE09", "HH01") , label = -c("House", "Tent", "Caravan", "Collective Center", "Worksite/Unfinished Home/ Abandoned Building", -"Farm Building", "School, mosque, church or other religious building", -"Garage, shop, workshop, or other structure not meant as residential space", -"Other (Specify)", "Yes", "No", "Not possible to observe", "Yes", -"No", "Not possible to observe", "Yes", "No", "Not possible to observe", -"Yes", "No", "Not possible to observe", "Yes", "No", "Not possible to observe", -"Yes", "No", "Not possible to observe", NA, "Yes", "No", "Always", -"Often", "Sometimes", "Never") +c("Apartment", "House", "Tent", "Caravan", "Collective Center", +"Worksite/Unfinished Home/ Abandoned Building", "Farm Building", +"School, mosque, church or other religious building", "Garage, shop, workshop, or other structure not meant as residential space", +"Other (Specify)", "Earth/sand", "Dung", "Wood planks", "Palm/bamboo", +"Parquet or polished wood", "Vinyl or asphalt strips", "Ceramic tiles", +"Cement", "Carpet", "Other (Specify)", "No roof", "Thatch/Palm leaf", +"Sod", "Rustic mat", "Palm/bamboo", "Wood planks", "Cardboard", +"Metal/tin", "Wood", "Calamine/Cement fibre", "Ceramic tiles", +"Cement", "Roofing shingles", "Other (Specify)", "No walls", +"Cane/Palm/ Trunks", "Dirt", "Bamboo with mud", "Stone with mud", +"Uncovered adobe", "Plywood", "Cardboard", "Reused wood", "Cement", +"Stone with lime/ cement", "Bricks", "Cement blocks", "Covered adobe", +"Wood planks/shingles", "Other (Specify)", NA, "Yes", "No", "Always", +"Often", "Sometimes", "Never", NA) , standard = -c("2", "3", "4", "5", "6", "7", "8", "9", "96", "1", "0", "98", -"1", "0", "98", "1", "0", "98", "1", "0", "98", "1", "0", "98", -"1", "0", "98", NA, "1", "0", "1", "2", "3", "4") - )) +c("1", "2", "3", "4", "5", "6", "7", "8", "9", "96", "1", "2", +"3", "4", "5", "6", "7", "8", "9", "96", "1", "2", "3", "4", +"5", "6", "7", "8", "9", "10", "11", "12", "13", "96", "1", "2", +"3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", +"15", "96", NA, "1", "0", "1", "2", "3", "4", NA) + )) ## So first we check that we have what we need in the data set based on the mapper check_map <- fct_check_map(datalist = datalist, mapper = mapper) - if (! isTRUE(check_map )) { + if ( check_map == FALSE) { cat( "There are missing data requirement to calculate Indicator Outcome 9.1") } else { #Only apartment and house datalist[["main"]]$dwe01_cat<- dplyr::case_when( - (datalist[["main"]]$DWE01 == 1 | - datalist[["main"]]$DWE01 == 2) ~ 1, + (as.integer(datalist[["main"]]$DWE01) == 1 | + as.integer(datalist[["main"]]$DWE01) == 2) ~ 1, TRUE ~ 0 ) + datalist[["main"]]$dwe01_cat <- labelled::labelled(datalist[["main"]]$dwe01_cat, labels = c('Yes' = 1, 'No' = 0 ), label = "Proportion of apartment and house") #unimproved floor when not earth,sand,clay,mud, dung or other datalist[["main"]]$dwe02_cat<- dplyr::case_when( - (datalist[["main"]]$DWE02 == 1 | - datalist[["main"]]$DWE02 == 2 | - datalist[["main"]]$DWE02 == 96) ~ 0, + (as.integer(datalist[["main"]]$DWE02) == 1 | + as.integer(datalist[["main"]]$DWE02) == 2 | + as.integer(datalist[["main"]]$DWE02) == 96) ~ 0, TRUE ~ 1 ) datalist[["main"]]$dwe02_cat <- labelled::labelled(datalist[["main"]]$dwe02_cat, labels = c('Yes' = 1, 'No' = 0 ), @@ -2031,12 +2020,12 @@ datalist[["main"]]$dwe02_cat <- labelled::labelled(datalist[["main"]]$dwe02_cat, #unimproved roof all options except metal,wood,ceramic tiles, cement, roofing shingles/sheets datalist[["main"]]$dwe03_cat<- dplyr::case_when( - (datalist[["main"]]$DWE03 == 8 | - datalist[["main"]]$DWE03 == 9 | - datalist[["main"]]$DWE03 == 10 | - datalist[["main"]]$DWE03 == 11 | - datalist[["main"]]$DWE03 == 12 | - datalist[["main"]]$DWE03 == 13 ) ~ 1 , + (as.integer(datalist[["main"]]$DWE03) == 8 | + as.integer(datalist[["main"]]$DWE03) == 9 | + as.integer(datalist[["main"]]$DWE03) == 10 | + as.integer(datalist[["main"]]$DWE03) == 11 | + as.integer(datalist[["main"]]$DWE03) == 12 | + as.integer(datalist[["main"]]$DWE03) == 13 ) ~ 1 , TRUE ~ 0) datalist[["main"]]$dwe03_cat <- labelled::labelled(datalist[["main"]]$dwe03_cat, labels = c('Yes' = 1, 'No' = 0 ), @@ -2045,12 +2034,12 @@ datalist[["main"]]$dwe03_cat <- labelled::labelled(datalist[["main"]]$dwe03_cat, #improved wall: cement,stone,bricks,cement blocks, covered adobe, wood planks datalist[["main"]]$dwe04_cat<- dplyr::case_when( - (datalist[["main"]]$DWE04 == 10| - datalist[["main"]]$DWE04 == 11| - datalist[["main"]]$DWE04 == 12| - datalist[["main"]]$DWE04 == 13| - datalist[["main"]]$DWE04 == 14| - datalist[["main"]]$DWE04 == 15) ~ 1, + ( as.integer(datalist[["main"]]$DWE04) == 10 | + as.integer(datalist[["main"]]$DWE04) == 11 | + as.integer(datalist[["main"]]$DWE04) == 12 | + as.integer(datalist[["main"]]$DWE04) == 13 | + as.integer(datalist[["main"]]$DWE04) == 14 | + as.integer(datalist[["main"]]$DWE04) == 15) ~ 1, TRUE ~ 0) datalist[["main"]]$dwe04_cat <- labelled::labelled(datalist[["main"]]$dwe04_cat, labels = c('Yes' = 1, 'No' = 0 ), @@ -2059,8 +2048,8 @@ datalist[["main"]]$dwe04_cat <- labelled::labelled(datalist[["main"]]$dwe04_cat, ####Calculate crowding index - overcrowded when more than 3 persons -datalist[["main"]]$crowding <- datalist[["main"]]$HH01/ - datalist[["main"]]$DWE05 +datalist[["main"]]$crowding <- as.integer(datalist[["main"]]$HH01) / + as.integer(datalist[["main"]]$DWE05) ##if crowding <= 3, not overcrowded datalist[["main"]]$dwe05_cat <- dplyr::case_when( datalist[["main"]]$crowding <= 3 ~ 1, @@ -2075,53 +2064,52 @@ datalist[["main"]]$dwe05_cat <- labelled::labelled(datalist[["main"]]$dwe05_cat, #affordable if HH pays rent and often and always without financial distress datalist[["main"]]$dwe09_cat <- dplyr::case_when( - (datalist[["main"]]$DWE08 == 1 & - (datalist[["main"]]$DWE09 == 1 | - datalist[["main"]]$DWE09 == 2 ) ) ~ 1, + (as.integer(datalist[["main"]]$DWE08) == 1 & + (as.integer(datalist[["main"]]$DWE09) == 1 | + as.integer(datalist[["main"]]$DWE09) == 2 ) ) ~ 1, - (datalist[["main"]]$DWE08 == 1 & - (datalist[["main"]]$DWE09 == 3 | - datalist[["main"]]$DWE09 == 4 ) ) ~ 0, + (as.integer(datalist[["main"]]$DWE08) == 1 & + (as.integer(datalist[["main"]]$DWE09) == 3 | + as.integer(datalist[["main"]]$DWE09) == 4 ) ) ~ 0, datalist[["main"]]$DWE08== 0 ~ 1) datalist[["main"]]$dwe09_cat <- labelled::labelled(datalist[["main"]]$dwe09_cat, labels = c('Yes' = 1, 'No' = 0 ), - label = "Proportion of affordable sheters - HH pays rent and often and always without financial distress") + label = "Proportion of affordable sheters - HH pays rent and often and always without financial distress") ####Combine all shelter indicators ##dwe01_cat / dwe02_cat / dwe03_cat / dwe04_cat / dwe05_cat / dwe09_cat -datalist[["main"]]$outcome9_1 < dplyr::case_when( - datalist[["main"]]$dwe01_cat== 0 | - datalist[["main"]]$dwe02_cat== 0 | - datalist[["main"]]$dwe03_cat== 0 | - datalist[["main"]]$dwe04_cat== 0 | - datalist[["main"]]$dwe05_cat== 0 | - datalist[["main"]]$dwe09_cat== 0 ~ 0, +datalist[["main"]]$outcome9_1 <- dplyr::case_when( + datalist[["main"]]$dwe01_cat == 0 | + datalist[["main"]]$dwe02_cat == 0 | + datalist[["main"]]$dwe03_cat == 0 | + datalist[["main"]]$dwe04_cat == 0 | + datalist[["main"]]$dwe05_cat == 0 | + datalist[["main"]]$dwe09_cat == 0 ~ 0, - datalist[["main"]]$dwe01_cat== 1 & - datalist[["main"]]$dwe02_cat== 1 & - datalist[["main"]]$dwe03_cat== 1 & - datalist[["main"]]$dwe04_cat== 1 & - datalist[["main"]]$dwe05_cat== 1 & - datalist[["main"]]$dwe09_cat== 1 ~ 1) + datalist[["main"]]$dwe01_cat == 1 & + datalist[["main"]]$dwe02_cat == 1 & + datalist[["main"]]$dwe03_cat == 1 & + datalist[["main"]]$dwe04_cat == 1 & + datalist[["main"]]$dwe05_cat == 1 & + datalist[["main"]]$dwe09_cat == 1 ~ 1) datalist[["main"]]$outcome9_1 <- labelled::labelled(datalist[["main"]]$outcome9_1, labels = c('Yes' = 1, 'No' = 0 ), - label = "Proportion of PoCs living in habitable - and affordable housing") + label = "Proportion of PoCs living in habitable and affordable housing") } return(datalist) } ``` -```{r example-outcome9_1} -## data -datalist <- kobocruncher::kobo_data( system.file("demo_data.xlsx", #"test.xlsx", +```{r example-outcome9_1, message=TRUE, warning=FALSE} +## data, cf example fct_re_map() +datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", package = "IndicatorCalc")) ## Apply indicator function on datalist datalist <- outcome9_1(datalist) @@ -2171,13 +2159,6 @@ test_that("outcome9_1 works", { #' @param datalist A list with all hierarchical data frame for a survey data set. #' format is expected to match the Excel export synchronized from kobo to RILD #' and loaded with kobocruncher::kobo_data() -#' -#' @param mapper a list providing the mapping of the variables used for the -#' calculation - this mapper is potentially to be adjusted in relation with deviation -#' between the the standard XlsForm and the contextualized dataset -#' -#' @param chain Boolean TRUE/FALSE to tell whether the function should return -#' the dataset expanded with the variable or a single factor #' #' @importFrom dplyr mutate case_when #' @importFrom labelled labelled @@ -2187,7 +2168,7 @@ test_that("outcome9_1 works", { #' @export outcome9_2 <- function(datalist ){ # Mapper , - mapper = list( +mapper = list( hierarchy = "main", variablemap = data.frame( label = @@ -2202,57 +2183,56 @@ c("LIGHT01", "LIGHT02", "LIGHT03") variable = c("LIGHT01", "LIGHT01", "LIGHT02", "LIGHT02", "LIGHT02", "LIGHT02", "LIGHT02", "LIGHT02", "LIGHT02", "LIGHT02", "LIGHT02", "LIGHT02", -"LIGHT02", "LIGHT02", "LIGHT02", "LIGHT03", "LIGHT03", "LIGHT03", +"LIGHT02", "LIGHT02", "LIGHT02", "LIGHT02", "LIGHT03", "LIGHT03", "LIGHT03", "LIGHT03", "LIGHT03", "LIGHT03", "LIGHT03", "LIGHT03", -"LIGHT03") +"LIGHT03", "LIGHT03") , label = c("Yes", "No", "Electricity (including solar mini-grids, hybrid mini-grids and national grid)", -"Solar home system", "Solar-powered lantern or flashlight", "Rechargeable flashlight, mobile, torch or lantern", -"Battery powered flashlight, torch or lantern", "Biogas lamp", -"LPG lamp", "Gasoline lamp", "Kerosene or paraffin lamp", "Oil lamp", -"Candle", "Open fire", "Other, specify", "No electricity in household", +"Electricity (from diesel generator)", "Solar home system", "Solar-powered lantern or flashlight", +"Rechargeable flashlight, mobile, torch or lantern", "Battery powered flashlight, torch or lantern", +"Biogas lamp", "LPG lamp", "Gasoline lamp", "Kerosene or paraffin lamp", +"Oil lamp", "Candle", "Open fire", "Other, specify", "No electricity in household", "National grid connection from [COMPANY]", "Local mini grid", "Solar home system", "Solar lantern", "Electric generator", "Rechargeable battery", "Dry cell battery / torch", "Other, specify", "Don't know") , standard = c("1", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", -"11", "12", "96", "0", "1", "2", "3", "4", "5", "6", "7", "96", -"98") - )) +"11", "12", "13", "96", "1", "2", "3", "4", "5", "6", "7", "8", +"96", "98") + )) ## So first we check that we have what we need in the data set based on the mapper check_map <- fct_check_map(datalist = datalist, mapper = mapper) - if (! isTRUE(check_map )) { + if ( check_map == FALSE) { cat( "There are missing data requirement to calculate Indicator Outcome 9.2") } else { datalist[["main"]]$outcome9_2 <- dplyr::case_when( - datalist[["main"]]$LIGHT01 == 1 & - (datalist[["main"]]$LIGHT02 == 1 | - datalist[["main"]]$LIGHT02 == 3 | - datalist[["main"]]$LIGHT02 == 4 | - datalist[["main"]]$LIGHT02 == 5 | - datalist[["main"]]$LIGHT02 == 6 | - datalist[["main"]]$LIGHT02 == 7 | - datalist[["main"]]$LIGHT02 == 8 ) ~ 1, + as.integer(datalist[["main"]]$LIGHT01) == 1 & + (as.integer(datalist[["main"]]$LIGHT02) == 1 | + as.integer(datalist[["main"]]$LIGHT02) == 3 | + as.integer(datalist[["main"]]$LIGHT02) == 4 | + as.integer(datalist[["main"]]$LIGHT02) == 5 | + as.integer(datalist[["main"]]$LIGHT02) == 6 | + as.integer(datalist[["main"]]$LIGHT02) == 7 | + as.integer(datalist[["main"]]$LIGHT02) == 8 ) ~ 1, TRUE ~ 0) datalist[["main"]]$outcome9_2 <- labelled::labelled(datalist[["main"]]$outcome9_2, labels = c('Yes' = 1, 'No' = 0 ), - label = "Proportion of PoC that have energy to - ensure lighting") + label = "Proportion of PoC that have energy to ensure lighting") } return(datalist) } ``` -```{r example-outcome9_2} -## data -datalist <- kobocruncher::kobo_data( system.file("demo_data.xlsx", #"test.xlsx", +```{r example-outcome9_2, message=TRUE, warning=FALSE} +## data, cf example fct_re_map() +datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", package = "IndicatorCalc")) ## Apply indicator function on datalist datalist <- outcome9_2(datalist ) @@ -2300,13 +2280,6 @@ test_that("outcome9_2 works", { #' @param datalist A list with all hierarchical data frame for a survey data set. #' format is expected to match the Excel export synchronized from kobo to RILD #' and loaded with kobocruncher::kobo_data() -#' -#' @param mapper a list providing the mapping of the variables used for the -#' calculation - this mapper is potentially to be adjusted in relation with deviation -#' between the the standard XlsForm and the contextualized dataset -#' -#' @param chain Boolean TRUE/FALSE to tell whether the function should return -#' the dataset expanded with the variable or a single factor #' #' @importFrom dplyr mutate case_when #' @importFrom labelled labelled @@ -2316,65 +2289,58 @@ test_that("outcome9_2 works", { #' @export outcome10_1 <- function(datalist){ # Mapper , - mapper = list( +mapper = list( hierarchy = "ind", variablemap = data.frame( label = -c("1. Do you have a *[National Child Immunisation Record]*, immunisation records from a private health provider or any other document where ${name_individual}’s vaccinations are written down?", -"2. Did you ever have a [National Child Immunisation Record] or immunisation records from a private health provider for ${name_individual}?", -"3. Has ${name_individual} ever received a measles containing vaccine(i.e. measles, MR or MMR)? That is a shot at the age of 9 months or older - to prevent (him/her) from getting measles", -"4. How many times was the measles vaccine received?") +"3. Has ${indiv_05Less_name} ever received a measles containing vaccine(i.e. measles, MR or MMR)? That is a shot at the age of 9 months or older - to prevent (him/her) from getting measles" , variable = -c("MMR01", "MMR02", "MMR03", "MMR04") +"MMR03" ), modalitymap = data.frame( variable = -c("MMR01", "MMR01", "MMR01", "MMR01", "MMR02", "MMR02", "MMR02", -"MMR03", "MMR03", "MMR03", "MMR04") +c("MMR03", "MMR03", "MMR03") , label = -c("Yes, has only card(s)", "Yes, has only other document", "Yes, has a card(s) and another document", -"No, has no cards and no other document", "Yes", "No", "Don't know", -"Yes", "No", "Don't know", NA) +c("Yes", "No", "Don't know") , standard = -c("1", "2", "3", "4", "1", "0", "98", "1", "0", "98", NA) - )) +c("1", "0", "98") + )) ## So first we check that we have what we need in the data set based on the mapper check_map <- fct_check_map(datalist = datalist, mapper = mapper) - if (! isTRUE(check_map )) { + if ( check_map == FALSE) { cat( "There are missing data requirement to calculate Indicator Outcome 10.1") } else { -datalist[["ind"]]$outcome10_1<- dplyr::case_when( - datalist[["ind"]]$MMR03 == 1 ~ 1, +datalist[["ind"]]$outcome10_1 <- dplyr::case_when( + as.integer(datalist[["ind"]]$MMR03) == 1 ~ 1, - datalist[["ind"]]$MMR03 == 0 | - datalist[["ind"]]$MMR03 == 98 ~ 0) + as.integer(datalist[["ind"]]$MMR03) == 0 | + as.integer(datalist[["ind"]]$MMR03) == 98 ~ 0) datalist[["ind"]]$outcome10_1 <- labelled::labelled(datalist[["ind"]]$outcome10_1, labels = c('Yes' = 1, 'No' = 0 ), - label = "Proportion of children aged 9 months - to five years who have received measles vaccination") + label = "Proportion of children aged 9 months to five years who have received measles vaccination") } return(datalist) } ``` -```{r example-outcome10_1} -## data -datalist <- kobocruncher::kobo_data( system.file("demo_data.xlsx", #"test.xlsx", +```{r example-outcome10_1, message=TRUE, warning=FALSE} +## data, cf example fct_re_map() +datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", package = "IndicatorCalc")) ## Apply indicator function on datalist datalist <- outcome10_1(datalist) ## Visualise value -fct_plot_indic_donut(indicator = datalist[["main"]]$outcome10_1, +fct_plot_indic_donut(indicator = datalist[["ind"]]$outcome10_1, iconunicode = "f140") ``` @@ -2420,13 +2386,6 @@ test_that("outcome10_1 works", { #' @param datalist A list with all hierarchical data frame for a survey data set. #' format is expected to match the Excel export synchronized from kobo to RILD #' and loaded with kobocruncher::kobo_data() -#' -#' @param mapper a list providing the mapping of the variables used for the -#' calculation - this mapper is potentially to be adjusted in relation with deviation -#' between the the standard XlsForm and the contextualized dataset -#' -#' @param chain Boolean TRUE/FALSE to tell whether the function should return -#' the dataset expanded with the variable or a single factor #' #' @importFrom dplyr mutate case_when #' @importFrom labelled labelled @@ -2436,7 +2395,7 @@ test_that("outcome10_1 works", { #' @export outcome10_2 <- function(datalist){ ## Mapper , - mapper = list( +mapper = list( hierarchy = "main", variablemap = data.frame( label = @@ -2464,34 +2423,34 @@ c("Yes", "No", "Yes", "No", "Health professional", "Doctor", standard = c("1", "0", "1", "0", "1", "2", "3", "4", "5", "6", "96", "98", "1", "2", "3", "96") - )) + )) ## So first we check that we have what we need in the data set based on the mapper check_map <- fct_check_map(datalist = datalist, mapper = mapper) - if (! isTRUE(check_map )) { + if ( check_map == FALSE) { cat( "There are missing data requirement to calculate Indicator Outcome 10.2") } else { datalist[["main"]]$outcome10_2 <- dplyr::case_when( - (datalist[["main"]]$BIR01 == 1 | - datalist[["main"]]$BIR02 == 1) & - (datalist[["main"]]$BIR03 == 1 | - datalist[["main"]]$BIR03 == 2 | - datalist[["main"]]$BIR03 == 3 ) ~ "1", + ( as.integer(datalist[["main"]]$BIR01) == 1 | + as.integer(datalist[["main"]]$BIR02) == 1) & + (as.integer(datalist[["main"]]$BIR03) == 1 | + as.integer(datalist[["main"]]$BIR03) == 2 | + as.integer(datalist[["main"]]$BIR03) == 3 ) ~ 1, - (datalist[["main"]]$BIR01 == 1 | - datalist[["main"]]$BIR02 == 1) & ( - datalist[["main"]]$BIR03 == 4 | - datalist[["main"]]$BIR03 == 5 | - datalist[["main"]]$BIR03 == 6 | - datalist[["main"]]$BIR03 == 96| - datalist[["main"]]$BIR03 == 98) ~ "0", + (as.integer(datalist[["main"]]$BIR01) == 1 | + as.integer(datalist[["main"]]$BIR02) == 1) & ( + as.integer(datalist[["main"]]$BIR03) == 4 | + as.integer(datalist[["main"]]$BIR03) == 5 | + as.integer(datalist[["main"]]$BIR03) == 6 | + as.integer(datalist[["main"]]$BIR03) == 96| + as.integer(datalist[["main"]]$BIR03) == 98) ~ 0, TRUE ~ NA_real_) datalist[["main"]]$outcome10_2 <- labelled::labelled(datalist[["main"]]$outcome10_2, - labels = c('Yes' = "1", 'No' = "0" ), + labels = c('Yes' = 1, 'No' = 0 ), label = "Proportion of births attended by skilled health personnel") } @@ -2499,9 +2458,9 @@ datalist[["main"]]$outcome10_2 <- labelled::labelled(datalist[["main"]]$outcome } ``` -```{r example-outcome10_2} -## data -datalist <- kobocruncher::kobo_data( system.file("demo_data.xlsx", #"test.xlsx", +```{r example-outcome10_2, message=TRUE, warning=FALSE} +## data, cf example fct_re_map() +datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", package = "IndicatorCalc")) ## Apply indicator function on datalist datalist <- outcome10_2(datalist ) @@ -2554,13 +2513,6 @@ test_that("outcome10_2 works", { #' @param datalist A list with all hierarchical data frame for a survey data set. #' format is expected to match the Excel export synchronized from kobo to RILD #' and loaded with kobocruncher::kobo_data() -#' -#' @param mapper a list providing the mapping of the variables used for the -#' calculation - this mapper is potentially to be adjusted in relation with deviation -#' between the the standard XlsForm and the contextualized dataset -#' -#' @param chain Boolean TRUE/FALSE to tell whether the function should return -#' the dataset expanded with the variable or a single factor #' #' @importFrom dplyr mutate case_when #' @importFrom labelled labelled @@ -2570,24 +2522,24 @@ test_that("outcome10_2 works", { #' @export outcome12_1 <- function(datalist ){ # Mapper , - mapper = list( +mapper = list( hierarchy = "main", variablemap = data.frame( label = c("1. What is the main source of drinking water for this household?", -"2. Where is this ${source} - ${source2} located?", "3. How long does it take to go there, get water, and come back, including waiting time?", +"2. Where is this ${source} - ${source2} located?", "Unit", "Time", "4. In the last 30 days, has there been any time when your household did not have sufficient quantities of drinking water when needed?" ) , variable = -c("DWA01", "DWA02", "DWA03", "DWA04") +c("DWA01", "DWA02", "DWA03a", "DWA03b", "DWA04") ), modalitymap = data.frame( variable = c("DWA01", "DWA01", "DWA01", "DWA01", "DWA01", "DWA01", "DWA01", "DWA01", "DWA01", "DWA01", "DWA01", "DWA01", "DWA01", "DWA01", "DWA01", "DWA01", "DWA01", "DWA01", "DWA02", "DWA02", "DWA02", -"DWA03", "DWA04", "DWA04") +"DWA03a", "DWA03a", "DWA03b", "DWA04", "DWA04") , label = c("Piped Into Dwelling", "Piped Into Yard/Plot", "Piped To Neighbor", @@ -2596,92 +2548,90 @@ c("Piped Into Dwelling", "Piped Into Yard/Plot", "Piped To Neighbor", "Rain Water Collection", "Tanker Truck/Water Vendor", "Cart With Small Tank/Drum", "Surface Water (River, Stream, Pond, Dam, Canal)", "Bottled Water", "Sachet Water", "Water Kiosk", "Other (Specify)", "Don't Know", -"In Own Dwelling", "In Own Yard/Plot", "Elsewhere", NA, "Yes", -"No") +"In Own Dwelling", "In Own Yard/Plot", "Elsewhere", "Minutes", +"Hours", NA, "Yes", "No") , standard = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", -"13", "14", "15", "16", "96", "98", "1", "2", "3", NA, "1", "0" -) - )) - +"13", "14", "15", "16", "96", "98", "1", "2", "3", "1", "2", +NA, "1", "0") + )) ## So first we check that we have what we need in the data set based on the mapper check_map <- fct_check_map(datalist = datalist, mapper = mapper) - if (! isTRUE(check_map )) { + if ( check_map == FALSE) { cat( "There are missing data requirement to calculate Indicator Outcome 12.1") } else { #convert hour into minutes datalist[["main"]]$time_DWA <- dplyr::case_when( - datalist[["main"]]$DWA03a == 1 ~ 1, - datalist[["main"]]$DWA03a == 2 ~ 60) + as.integer(datalist[["main"]]$DWA03a) == 1 ~ 1, + as.integer(datalist[["main"]]$DWA03a) == 2 ~ 60) -datalist[["main"]]$time_tot <- datalist[["main"]]$time_DWA * datalist[["main"]]$DWA03b +datalist[["main"]]$time_tot <- datalist[["main"]]$time_DWA * + as.integer(datalist[["main"]]$DWA03b) # reachable under 30 minutes datalist[["main"]]$reachableU30 <- dplyr::case_when( - datalist[["main"]]$time_tot > 30 ~ "0", - TRUE ~ "1") + datalist[["main"]]$time_tot > 30 ~ 0, + TRUE ~ 1) # improved source datalist[["main"]]$dwa_cond1 <- dplyr::case_when( - datalist[["main"]]$DWA01!="7" | - datalist[["main"]]$DWA01 !="9" | - datalist[["main"]]$DWA01 != "13" | - datalist[["main"]]$DWA01 != "96" | - datalist[["main"]]$DWA01 !="98" ~ "1", + as.integer(datalist[["main"]]$DWA01) != 7 | + as.integer(datalist[["main"]]$DWA01) != 9 | + as.integer(datalist[["main"]]$DWA01) != 13 | + as.integer(datalist[["main"]]$DWA01) != 96 | + as.integer(datalist[["main"]]$DWA01) != 98 ~ 1, - TRUE ~ "0") + TRUE ~ 0) -datalist[["main"]]$dwa_cond1 <- labelled::labelled(dwa_cond1 , - labels = c('Yes' = "1", 'No' = "0" ), +datalist[["main"]]$dwa_cond1 <- labelled::labelled(datalist[["main"]]$dwa_cond1 , + labels = c('Yes' = 1, 'No' = 0 ), label = "Water: improved source") ## Second condition datalist[["main"]]$dwa_cond2 <- dplyr::case_when( - datalist[["main"]]$DWA02 == "3" ~ "1", + as.integer(datalist[["main"]]$DWA02) == 3 ~ 1, #UNDER30 MIN - datalist[["main"]]$reachableU30 == "1" & + as.integer(datalist[["main"]]$reachableU30) == 1 & # NOT UNDER30 MIN - datalist[["main"]]$reachableU30 == "0" & - datalist[["main"]]$DWA02 == "3" ~ "0", + as.integer(datalist[["main"]]$reachableU30) == 0 & + as.integer(datalist[["main"]]$DWA02) == 3 ~ 0, # in the dwelling/yard/plot - datalist[["main"]]$DWA02 == "1" | - datalist[["main"]]$DWA02 == "2" ~ "1", + as.integer(datalist[["main"]]$DWA02) == 1 | + as.integer(datalist[["main"]]$DWA02) == 2 ~ 1, - TRUE ~ NA_character_ ) + TRUE ~ NA ) -datalist[["main"]]$dwa_cond2 <- labelled::labelled(dwa_cond2 , - labels = c('Yes' = "1", 'No' = "0" ), +datalist[["main"]]$dwa_cond2 <- labelled::labelled(datalist[["main"]]$dwa_cond2 , + labels = c('Yes' = 1, 'No' = 0 ), label = "Water accessible") ## Composite datalist[["main"]]$outcome12_1 <- dplyr::case_when( - (datalist[["main"]]$dwa_cond1=="1" & - datalist[["main"]]$dwa_cond2=="1" ) ~ "1", - TRUE ~ "0") + (datalist[["main"]]$dwa_cond1 == 1 & + datalist[["main"]]$dwa_cond2 == 1 ) ~ 1, + TRUE ~ 0) -datalist[["main"]]$outcome12_1 <- labelled::labelled(outcome12_1 , - labels = c('Yes' = "1", 'No' = "0" ), - label = "Proportion of PoC using at - least basic drinking water services") +datalist[["main"]]$outcome12_1 <- labelled::labelled(datalist[["main"]]$outcome12_1, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Proportion of PoC using at least basic drinking water services") } return(datalist) } ``` -```{r example-outcome12_1} -## data -datalist <- kobocruncher::kobo_data( system.file("demo_data.xlsx", #"test.xlsx", +```{r example-outcome12_1, message=TRUE, warning=FALSE} +## data, cf example fct_re_map() +datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", package = "IndicatorCalc")) - ## Apply indicator function on datalist datalist <- outcome12_1(datalist ) @@ -2725,13 +2675,6 @@ test_that("outcome12_1 works", { #' @param datalist A list with all hierarchical data frame for a survey data set. #' format is expected to match the Excel export synchronized from kobo to RILD #' and loaded with kobocruncher::kobo_data() -#' -#' @param mapper a list providing the mapping of the variables used for the -#' calculation - this mapper is potentially to be adjusted in relation with deviation -#' between the the standard XlsForm and the contextualized dataset -#' -#' @param chain Boolean TRUE/FALSE to tell whether the function should return -#' the dataset expanded with the variable or a single factor #' #' @importFrom dplyr mutate case_when #' @importFrom labelled labelled @@ -2741,9 +2684,9 @@ test_that("outcome12_1 works", { #' @export outcome12_2 <- function(datalist ){ ## mapper, - -mapper = list( + mapper = list( hierarchy = "main", + hierarchy = "ind", variablemap = data.frame( label = c("1. What is the MAIN type of toilet facility used by your household?", @@ -2785,51 +2728,51 @@ c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", ## So first we check that we have what we need in the data set based on the mapper check_map <- fct_check_map(datalist = datalist, mapper = mapper) - if (! isTRUE(check_map )) { + if ( check_map == FALSE) { cat( "There are missing data requirement to calculate Indicator Outcome 12.2") } else { datalist[["main"]]$toi_cond1 <- dplyr::case_when( - datalist[["main"]]$TOI01 == 1 | - datalist[["main"]]$TOI01 == 2 | - datalist[["main"]]$TOI01 == 3 | - datalist[["main"]]$TOI01 == 4 | - datalist[["main"]]$TOI01 == 5 | - datalist[["main"]]$TOI01 == 6 | - datalist[["main"]]$TOI01 == 7 | - datalist[["main"]]$TOI01 == 9 ~ 1, - - datalist[["main"]]$TOI01 == 8 | - datalist[["main"]]$TOI01 == 10 | - datalist[["main"]]$TOI01 == 11 | - datalist[["main"]]$TOI01 == 12 | - datalist[["main"]]$TOI01 == 96 ~ 0, + as.integer(datalist[["main"]]$TOI01) == 1 | + as.integer(datalist[["main"]]$TOI01) == 2 | + as.integer(datalist[["main"]]$TOI01) == 3 | + as.integer(datalist[["main"]]$TOI01) == 4 | + as.integer(datalist[["main"]]$TOI01) == 5 | + as.integer(datalist[["main"]]$TOI01) == 6 | + as.integer(datalist[["main"]]$TOI01) == 7 | + as.integer(datalist[["main"]]$TOI01) == 9 ~ 1, + + as.integer(datalist[["main"]]$TOI01) == 8 | + as.integer(datalist[["main"]]$TOI01) == 10 | + as.integer(datalist[["main"]]$TOI01) == 11 | + as.integer(datalist[["main"]]$TOI01) == 12 | + as.integer(datalist[["main"]]$TOI01) == 96 ~ 0, TRUE ~ NA_real_) #Unsafe disposal datalist[["main"]]$toi_cond2 <- dplyr::case_when( - datalist[["main"]]$TOI02 == 1 & - ( datalist[["main"]]$TOI03 == 1 | - datalist[["main"]]$TOI03 == 2 | - datalist[["main"]]$TOI03 == 3 | - datalist[["main"]]$TOI03 == 4 ) ~ 1, + as.integer(datalist[["main"]]$TOI02) == 1 & + ( as.integer(datalist[["main"]]$TOI03) == 1 | + as.integer(datalist[["main"]]$TOI03) == 2 | + as.integer(datalist[["main"]]$TOI03) == 3 | + as.integer(datalist[["main"]]$TOI03) == 4 ) ~ 1, - datalist[["main"]]$TOI02 == 1 & - (datalist[["main"]]$TOI03 ==5 | - datalist[["main"]]$TOI03 == 96 | - datalist[["main"]]$TOI03 == 98) ~ 0, + as.integer(datalist[["main"]]$TOI02) == 1 & + (as.integer(datalist[["main"]]$TOI03) ==5 | + as.integer(datalist[["main"]]$TOI03) == 96 | + as.integer(datalist[["main"]]$TOI03) == 98) ~ 0, - datalist[["main"]]$TOI02 == 2 ~ 0, - datalist[["main"]]$TOI02 == 98 ~ 0, + as.integer(datalist[["main"]]$TOI02) == 2 ~ 0, + as.integer(datalist[["main"]]$TOI02) == 98 ~ 0, TRUE ~ NA_real_) # toilet not shared with other households datalist[["main"]]$toi_cond3<- dplyr::case_when( - datalist[["main"]]$TOI05 == 1 ~ 0, - datalist[["main"]]$TOI05 == 0 ~ 1) + as.integer(datalist[["main"]]$TOI05) == 1 ~ 0, + as.integer(datalist[["main"]]$TOI05) == 0 ~ 1) ###Combine all three conditions below @@ -2852,12 +2795,10 @@ datalist[["main"]]$outcome12_2 <- labelled::labelled(datalist[["main"]]$outcome } ``` -```{r example-outcome12_2} - -## data -datalist <- kobocruncher::kobo_data( system.file("demo_data.xlsx", #"test.xlsx", +```{r example-outcome12_2, message=TRUE, warning=FALSE} +## data, cf example fct_re_map() +datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", package = "IndicatorCalc")) - ## Apply indicator function on datalist datalist <- outcome12_2(datalist) @@ -2872,7 +2813,7 @@ test_that("outcome12_2 works", { }) ``` -## outcome13_1 - Financial Inclusion +## outcome13_1 ```{r function-outcome13_1} #' outcome13_1 @@ -2901,13 +2842,6 @@ test_that("outcome12_2 works", { #' @param datalist A list with all hierarchical data frame for a survey data set. #' format is expected to match the Excel export synchronized from kobo to RILD #' and loaded with kobocruncher::kobo_data() -#' -#' @param mapper a list providing the mapping of the variables used for the -#' calculation - this mapper is potentially to be adjusted in relation with deviation -#' between the the standard XlsForm and the contextualized dataset -#' -#' @param chain Boolean TRUE/FALSE to tell whether the function should return -#' the dataset expanded with the variable or a single factor #' #' @importFrom dplyr mutate case_when #' @importFrom labelled labelled @@ -2943,25 +2877,24 @@ c("Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", , standard = c("1", "0", "1", "0", "1", "0", "1", "0", "1", "0") - )) - + )) ## So first we check that we have what we need in the data set based on the mapper check_map <- fct_check_map(datalist = datalist, mapper = mapper) - if (! isTRUE(check_map )) { + if ( check_map == FALSE) { cat( "There are missing data requirement to calculate Indicator Outcome 13.1") } else { datalist[["main"]]$outcome13_1 <- dplyr::case_when( - datalist[["main"]]$BANK01 == 1 | - datalist[["main"]]$BANK02 == 1 | - datalist[["main"]]$BANK03 == 1 | - datalist[["main"]]$BANK05 == 1 ~ 1, + as.integer(datalist[["main"]]$BANK01) == 1 | + as.integer(datalist[["main"]]$BANK02) == 1 | + as.integer(datalist[["main"]]$BANK03) == 1 | + as.integer(datalist[["main"]]$BANK05) == 1 ~ 1, - datalist[["main"]]$BANK01 == 0 & - datalist[["main"]]$BANK02 == 0 & - datalist[["main"]]$BANK03 == 0 & - datalist[["main"]]$BANK05 == 0 ~ 0, + as.integer(datalist[["main"]]$BANK01) == 0 & + as.integer(datalist[["main"]]$BANK02) == 0 & + as.integer(datalist[["main"]]$BANK03) == 0 & + as.integer(datalist[["main"]]$BANK05) == 0 ~ 0, TRUE ~ 0) datalist[["main"]]$outcome13_1 <- labelled::labelled(datalist[["main"]]$outcome13_1, @@ -2974,11 +2907,10 @@ datalist[["main"]]$outcome13_1 <- labelled::labelled(datalist[["main"]]$outcome } ``` -```{r example-outcome13_1} -## data -datalist <- kobocruncher::kobo_data( system.file("demo_data.xlsx", #"test.xlsx", +```{r example-outcome13_1, message=TRUE, warning=FALSE} +## data, cf example fct_re_map() +datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", package = "IndicatorCalc")) - ## Apply indicator function on datalist datalist <- outcome13_1(datalist ) @@ -3020,13 +2952,6 @@ test_that("outcome13_1 works", { #' @param datalist A list with all hierarchical data frame for a survey data set. #' format is expected to match the Excel export synchronized from kobo to RILD #' and loaded with kobocruncher::kobo_data() -#' -#' @param mapper a list providing the mapping of the variables used for the -#' calculation - this mapper is potentially to be adjusted in relation with deviation -#' between the the standard XlsForm and the contextualized dataset -#' -#' @param chain Boolean TRUE/FALSE to tell whether the function should return -#' the dataset expanded with the variable or a single factor #' #' @importFrom dplyr mutate case_when #' @importFrom labelled labelled @@ -3040,58 +2965,49 @@ mapper = list( hierarchy = "main", variablemap = data.frame( label = -c("11. Compared to this time last year, has your income increased, remained the same, or decreased?", -"12. Compared to last year at this time, do you feel you can afford more things, the same, or fewer things in terms of goods and services?" -) +"11. Compared to this time last year, do you think you can now afford more goods and services, the same, or fewer goods and services?" , variable = -c("INC01", "INC02") +"INC01" ), modalitymap = data.frame( variable = -c("INC01", "INC01", "INC01", "INC01", "INC02", "INC02", "INC02", -"INC02") +c("INC01", "INC01", "INC01", "INC01") , label = -c("Increased compared to previous year", "Been the same compared to previous year", -"Decreased compared to previous year", "Don't know ", "More ", -"The same ", "Fewer ", "Don't know ") +c("More", "The same", "Fewer", "Don't know") , standard = -c("1", "2", "3", "98", "1", "2", "3", "98") - )) - - +c("1", "2", "3", "98") + )) ## So first we check that we have what we need in the data set based on the mapper check_map <- fct_check_map(datalist = datalist, mapper = mapper) - if (! isTRUE(check_map )) { + if ( check_map == FALSE) { cat( "There are missing data requirement to calculate Indicator Outcome 13.2") } else { datalist[["main"]]$outcome13_2<- dplyr::case_when( - datalist[["main"]]$INC01 == 1 ~ 1, + as.integer(datalist[["main"]]$INC01) == 1 ~ 1, - datalist[["main"]]$INC01 == 2 | - datalist[["main"]]$INC01 == 3 | - datalist[["main"]]$INC01 == 98 ~ 0 ) + as.integer(datalist[["main"]]$INC01) == 2 | + as.integer(datalist[["main"]]$INC01) == 3 | + as.integer(datalist[["main"]]$INC01) == 98 ~ 0 ) datalist[["main"]]$outcome13_2 <- labelled::labelled(datalist[["main"]]$outcome13_2, labels = c('Yes' = 1, 'No' = 0 ), - label = "Proportion of PoC who self-report positive changes - in their income compared to previous year") + label = "Proportion of PoC who self-report positive changes in their income compared to previous year") } return(datalist) } ``` -```{r example-outcome13_2} -## data -datalist <- kobocruncher::kobo_data( system.file("demo_data.xlsx", #"test.xlsx", +```{r example-outcome13_2, message=TRUE, warning=FALSE} +## data, cf example fct_re_map() +datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", package = "IndicatorCalc")) - ## Apply indicator function on datalist datalist <- outcome13_2(datalist) @@ -3137,13 +3053,6 @@ test_that("outcome13_2 works", { #' @param datalist A list with all hierarchical data frame for a survey data set. #' format is expected to match the Excel export synchronized from kobo to RILD #' and loaded with kobocruncher::kobo_data() -#' -#' @param mapper a list providing the mapping of the variables used for the -#' calculation - this mapper is potentially to be adjusted in relation with deviation -#' between the the standard XlsForm and the contextualized dataset -#' -#' @param chain Boolean TRUE/FALSE to tell whether the function should return -#' the dataset expanded with the variable or a single factor #' #' @importFrom dplyr mutate case_when #' @importFrom labelled labelled @@ -3153,19 +3062,22 @@ test_that("outcome13_2 works", { #' @export outcome13_3 <- function(datalist ){ # mapper , - mapper = list( + +mapper = list( hierarchy = "main", variablemap = data.frame( label = -c("1. During the past 7 days, did you work for someone else for pay, for one or more hours?", -"2. During the past 7 days, did you run or do any kind of business, farming, or other activity to generate income?", -"3. During the past 7 days, did you help in a family business or farm?", -"4. Even though you did not work, during the past 7 days, did he/she have a paid job from which he/she was temporarily absent?", -"5. Even though you did not work, during the past 7 days, did he/she have a business or a helper job in a family business/farm from which he/she was temporarily absent?", -"6. Last week, did you do any work in…?", "7. Was this work that you mentioned in...?", +c("1. During the past 7 days, did ${name_selectedadult18} work for someone else for pay, for one or more hours?", +"2. During the past 7 days, did ${name_selectedadult18} run or do any kind of business, farming, or other activity to generate income?", +"3. During the past 7 days, did ${name_selectedadult18} help in a family business or farm?", +"4. Even though ${name_selectedadult18} did not work, during the past 7 days, did he/she have a paid job from which he/she was temporarily absent?", +"5. Even though ${name_selectedadult18} did not work, during the past 7 days, did he/she have a business or a helper job in a family business/farm from which he/she was temporarily absent?", +"6. Last week, did ${name_selectedadult18} do any work in…?", +"7. Was this work that ${name_selectedadult18} mentioned in...?", "8. Thinking about this work, are the products intended...?", -"9. During the last 30 days, did you do anything to find a paid job or try to start a business?", -"10. Could you start working within the next two weeks?") +"9. During the last 30 days, did ${name_selectedadult18} do anything to find a paid job or try to start a business?", +"10. Could ${name_selectedadult18} start working within the next two weeks?" +) , variable = c("UNEM01", "UNEM02", "UNEM03", "UNEM04", "UNEM05", "UNEM06", @@ -3188,70 +3100,91 @@ c("Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", standard = c("1", "0", "1", "0", "1", "0", "1", "0", "1", "0", "1", "2", "3", "1", "2", "3", "1", "2", "3", "4", "1", "0", "1", "0") - )) + )) ## So first we check that we have what we need in the data set based on the mapper check_map <- fct_check_map(datalist = datalist, mapper = mapper) - if (! isTRUE(check_map )) { + if ( check_map == FALSE) { cat( "There are missing data requirement to calculate Indicator Outcome 13.23") } else { -datalist[["main"]]$employed<- dplyr::case_when( +datalist[["main"]]$employed <- dplyr::case_when( - datalist[["main"]]$UNEM01 == 1 ~ 1, + as.integer(datalist[["main"]]$UNEM01) == 1 ~ 1, - datalist[["main"]]$UNEM02 == 1 & - datalist[["main"]]$UNEM07 == 3 ~ 1, + as.integer(datalist[["main"]]$UNEM02) == 1 & + as.integer(datalist[["main"]]$UNEM07) == 3 ~ 1, - datalist[["main"]]$UNEM04 == 1 ~ 1, + as.integer(datalist[["main"]]$UNEM04) == 1 ~ 1, - datalist[["main"]]$UNEM02 == 1 & - datalist[["main"]]$UNEM07 == 1 & - (datalist[["main"]]$UNEM08 == 1 | - datalist[["main"]]$UNEM08 == 2) ~ 1, + as.integer(datalist[["main"]]$UNEM02) == 1 & + as.integer(datalist[["main"]]$UNEM07) == 1 & + (as.integer(datalist[["main"]]$UNEM08) == 1 | + as.integer(datalist[["main"]]$UNEM08) == 2) ~ 1, - datalist[["main"]]$UNEM05 == 1 & - datalist[["main"]]$UNEM06 == 3 ~ 1, - datalist[["main"]]$UNEM05== 1 & - ( datalist[["main"]]$UNEM06 == 1 | - datalist[["main"]]$UNEM06 == 2) & - ( datalist[["main"]]$UNEM08 == 1 | - datalist[["main"]]$UNEM08 == 2) ~ 1, + as.integer(datalist[["main"]]$UNEM05) == 1 & + as.integer(datalist[["main"]]$UNEM06) == 3 ~ 1, + as.integer(datalist[["main"]]$UNEM05) == 1 & + ( as.integer(datalist[["main"]]$UNEM06) == 1 | + as.integer(datalist[["main"]]$UNEM06) == 2) & + ( as.integer(datalist[["main"]]$UNEM08) == 1 | + as.integer(datalist[["main"]]$UNEM08) == 2) ~ 1, TRUE ~ 0) + +datalist[["main"]]$employed <- labelled::labelled(datalist[["main"]]$employed, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Proportion employed") + + datalist[["main"]]$unemployed<- dplyr::case_when( - datalist[["main"]]$employed == 0 & - datalist[["main"]]$UNEM09 == 1 & - datalist[["main"]]$UNEM10 == 1 ~ 1, + as.integer(datalist[["main"]]$employed) == 0 & + as.integer(datalist[["main"]]$UNEM09) == 1 & + as.integer(datalist[["main"]]$UNEM10) == 1 ~ 1, TRUE ~ 0) -datalist[["main"]]$labour_force<- dplyr::case_when( - datalist[["main"]]$employed== 1 | - datalist[["main"]]$unemployed== 1 ~ 1) +datalist[["main"]]$unemployed <- labelled::labelled(datalist[["main"]]$unemployed, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Proportion unemployed") + +datalist[["main"]]$labour_force <- dplyr::case_when( + datalist[["main"]]$employed == 1 | + datalist[["main"]]$unemployed == 1 ~ 1) + + +datalist[["main"]]$labour_force <- labelled::labelled(datalist[["main"]]$labour_force, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Proportion forming labour force") + +datalist[["main"]]$outcome13_3 <- dplyr::case_when( + (datalist[["main"]]$unemployed == 1 & + datalist[["main"]]$labour_force == 1) ~ 1, + (datalist[["main"]]$employed == 1 & + datalist[["main"]]$labour_force == 1) ~ 0, + TRUE ~ NA_real_ ) + + +datalist[["main"]]$outcome13_3 <- labelled::labelled(datalist[["main"]]$outcome13_3, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Proportion of unemployed while being part of labour force") -datalist[["main"]]$outcome13_3 <- - datalist[["main"]]$unemployed/ - datalist[["main"]]$labour_force } return(datalist) } ``` -```{r example-outcome13_3} -## data -datalist <- kobocruncher::kobo_data( system.file("demo_data.xlsx", #"test.xlsx", +```{r example-outcome13_3, message=TRUE, warning=FALSE} +## data, cf example fct_re_map() +datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", package = "IndicatorCalc")) - -## mapper -mapper <- list("") - ## Apply indicator function on datalist -datalist <- outcome13_3(datalist, mapper ) +datalist <- outcome13_3(datalist ) +table( datalist[["main"]]$outcome13_3, useNA = "ifany") ## Visualise value fct_plot_indic_donut(indicator = datalist[["main"]]$outcome13_3, iconunicode = "f140") @@ -3294,13 +3227,6 @@ test_that("outcome13_3 works", { #' @param datalist A list with all hierarchical data frame for a survey data set. #' format is expected to match the Excel export synchronized from kobo to RILD #' and loaded with kobocruncher::kobo_data() -#' -#' @param mapper a list providing the mapping of the variables used for the -#' calculation - this mapper is potentially to be adjusted in relation with deviation -#' between the the standard XlsForm and the contextualized dataset -#' -#' @param chain Boolean TRUE/FALSE to tell whether the function should return -#' the dataset expanded with the variable or a single factor #' #' @importFrom dplyr mutate case_when #' @importFrom labelled labelled @@ -3310,60 +3236,81 @@ test_that("outcome13_3 works", { #' @export outcome14_1 <- function(datalist ){ ## Mapper , - mapper = list( + +mapper = list( hierarchy = "ind", variablemap = data.frame( label = -c("1. Does ${name_individual} have the documents below?", "2. Does ${name_individual} have any other document that establishes his/her legal identity?", +c("Passport?", "Birth certificate?", "Civil/government-issued ID card ?", +"Residency permit (both temporary and permanent)?", "Statelessness documentation?", +"Household card of address / family book/ marriage certificate?", +"Social security card?", "2. Does ${name_individual} have any other document that establishes his/her legal identity?", "3. Does ${name_individual} have a birth certificate?", "4. Has ${name_individual}'s birth been registered with civil authorities?", -"5. Does ${name_individual} have the documents below?", "6. Does ${name_individual} have any other document that establishes your legal identity?" +"Passport?", "Civil/government-issued ID card ?", "Residency permit (both temporary and permanent)?", +"Statelessness documentation?", "Household card of address / family book/ marriage certificate?", +"Social security card?", "6. Does ${name_individual} have any other document that establishes your legal identity?" ) , variable = -c("REG01", "REG02", "REG03", "REG04", "REG05", "REG06") +c("REG01a", "REG01b", "REG01c", "REG01d", "REG01e", "REG01f", +"REG01g", "REG02", "REG03", "REG04", "REG05a", "REG05b", "REG05c", +"REG05d", "REG05e", "REG05f", "REG06") ), modalitymap = data.frame( variable = -c("REG01", "REG01", "REG01", "REG02", "REG02", "REG02", "REG03", -"REG03", "REG03", "REG04", "REG04", "REG04", "REG04", "REG05", -"REG05", "REG05", "REG06", "REG06", "REG06") +c("REG01a", "REG01a", "REG01a", "REG01b", "REG01b", "REG01b", +"REG01c", "REG01c", "REG01c", "REG01d", "REG01d", "REG01d", "REG01e", +"REG01e", "REG01e", "REG01f", "REG01f", "REG01f", "REG01g", "REG01g", +"REG01g", "REG02", "REG02", "REG02", "REG03", "REG03", "REG03", +"REG04", "REG04", "REG04", "REG04", "REG05a", "REG05a", "REG05a", +"REG05b", "REG05b", "REG05b", "REG05c", "REG05c", "REG05c", "REG05d", +"REG05d", "REG05d", "REG05e", "REG05e", "REG05e", "REG05f", "REG05f", +"REG05f", "REG06", "REG06", "REG06") , label = c("Yes", "No", "Prefer not to respond", "Yes", "No", "Prefer not to respond", +"Yes", "No", "Prefer not to respond", "Yes", "No", "Prefer not to respond", +"Yes", "No", "Prefer not to respond", "Yes", "No", "Prefer not to respond", +"Yes", "No", "Prefer not to respond", "Yes", "No", "Prefer not to respond", "Yes", "No", "Don't know", "Yes", "No", "Don't know", "Prefer not to say", -"Yes", "No", "Prefer not to respond", "Yes", "No", "Prefer not to respond" -) +"Yes", "No", "Prefer not to respond", "Yes", "No", "Prefer not to respond", +"Yes", "No", "Prefer not to respond", "Yes", "No", "Prefer not to respond", +"Yes", "No", "Prefer not to respond", "Yes", "No", "Prefer not to respond", +"Yes", "No", "Prefer not to respond") , standard = -c("1", "0", "99", "1", "0", "99", "1", "0", "98", "1", "0", "98", -"99", "1", "0", "99", "1", "0", "99") +c("1", "0", "99", "1", "0", "99", "1", "0", "99", "1", "0", "99", +"1", "0", "99", "1", "0", "99", "1", "0", "99", "1", "0", "99", +"1", "0", "98", "1", "0", "98", "99", "1", "0", "99", "1", "0", +"99", "1", "0", "99", "1", "0", "99", "1", "0", "99", "1", "0", +"99", "1", "0", "99") )) ## So first we check that we have what we need in the data set based on the mapper check_map <- fct_check_map(datalist = datalist, mapper = mapper) - if (! isTRUE(check_map )) { + if ( check_map == FALSE) { cat( "There are missing data requirement to calculate Indicator Outcome 14.1") } else { datalist[["ind"]]$document_under5 <- dplyr::case_when( - datalist[["ind"]]$REG05a == 1 | #ind$REG05a - passport - datalist[["ind"]]$REG05b == 1 | #ind$REG05b - civil/government issued ID - datalist[["ind"]]$REG05c == 1 | #ind$REG05c - residency permit - datalist[["ind"]]$REG05d == 1 | #ind$REG05d - statelessness documentation - datalist[["ind"]]$REG05e == 1 | #ind$REG05e - household card of address/family book - datalist[["ind"]]$REG05f == 1 | #ind$REG05f - social security card - datalist[["ind"]]$REG06 == 1 | #ind$REG06 - any other document establishes identity - datalist[["ind"]]$REG03 == 1 #add birth certificate as additional document from REG03 + as.integer(datalist[["ind"]]$REG05a) == 1 | #ind$REG05a - passport + as.integer(datalist[["ind"]]$REG05b) == 1 | #ind$REG05b - civil/government issued ID + as.integer(datalist[["ind"]]$REG05c) == 1 | #ind$REG05c - residency permit + as.integer(datalist[["ind"]]$REG05d) == 1 | #ind$REG05d - statelessness documentation + as.integer(datalist[["ind"]]$REG05e) == 1 | #ind$REG05e - household card of address/family book + as.integer(datalist[["ind"]]$REG05f) == 1 | #ind$REG05f - social security card + as.integer(datalist[["ind"]]$REG06) == 1 | #ind$REG06 - any other document establishes identity + as.integer(datalist[["ind"]]$REG03) == 1 #add birth certificate as additional document from REG03 ~ 1, - datalist[["ind"]]$REG05a == 0 & - datalist[["ind"]]$REG05b == 0 & - datalist[["ind"]]$REG05c == 0 & - datalist[["ind"]]$REG05d == 0 & - datalist[["ind"]]$REG05e == 0 & - datalist[["ind"]]$REG05f == 0 & - datalist[["ind"]]$REG06 == 0 & - datalist[["ind"]]$REG03 == 0 ~ 0, + as.integer(datalist[["ind"]]$REG05a) == 0 & + as.integer(datalist[["ind"]]$REG05b) == 0 & + as.integer(datalist[["ind"]]$REG05c) == 0 & + as.integer(datalist[["ind"]]$REG05d) == 0 & + as.integer(datalist[["ind"]]$REG05e) == 0 & + as.integer(datalist[["ind"]]$REG05f) == 0 & + as.integer(datalist[["ind"]]$REG06) == 0 & + as.integer(datalist[["ind"]]$REG03) == 0 ~ 0, TRUE ~ NA_real_ ) ###Calculate valid identity documents for above 5 with REG01 and REG02 variables @@ -3379,23 +3326,23 @@ datalist[["ind"]]$document_under5 <- dplyr::case_when( #ind$REG02 # any other document establishes identity datalist[["ind"]]$document_above5 <- dplyr::case_when( - datalist[["ind"]]$REG01a == 1 | - datalist[["ind"]]$REG01b == 1 | - datalist[["ind"]]$REG01c == 1 | - datalist[["ind"]]$REG01d == 1 | - datalist[["ind"]]$REG01e == 1 | - datalist[["ind"]]$REG01f == 1 | - datalist[["ind"]]$REG01g == 1 | - datalist[["ind"]]$REG02 == 1 ~ 1, + as.integer(datalist[["ind"]]$REG01a) == 1 | + as.integer(datalist[["ind"]]$REG01b) == 1 | + as.integer(datalist[["ind"]]$REG01c) == 1 | + as.integer(datalist[["ind"]]$REG01d) == 1 | + as.integer(datalist[["ind"]]$REG01e) == 1 | + as.integer(datalist[["ind"]]$REG01f) == 1 | + as.integer(datalist[["ind"]]$REG01g) == 1 | + as.integer(datalist[["ind"]]$REG02) == 1 ~ 1, - datalist[["ind"]]$REG01a == 0 & - datalist[["ind"]]$REG01b == 0 & - datalist[["ind"]]$REG01c == 0 & - datalist[["ind"]]$REG01d == 0 & - datalist[["ind"]]$REG01e == 0 & - datalist[["ind"]]$REG01f == 0 & - datalist[["ind"]]$REG01g == 0 & - datalist[["ind"]]$REG02 == 0 ~ 0, + as.integer(datalist[["ind"]]$REG01a) == 0 & + as.integer(datalist[["ind"]]$REG01b) == 0 & + as.integer(datalist[["ind"]]$REG01c) == 0 & + as.integer(datalist[["ind"]]$REG01d) == 0 & + as.integer(datalist[["ind"]]$REG01e) == 0 & + as.integer(datalist[["ind"]]$REG01f) == 0 & + as.integer(datalist[["ind"]]$REG01g) == 0 & + as.integer(datalist[["ind"]]$REG02) == 0 ~ 0, TRUE ~ NA_real_) @@ -3416,16 +3363,16 @@ datalist[["ind"]]$outcome14_1 <- labelled::labelled(datalist[["ind"]]$outcome14 } ``` -```{r example-outcome14_1} -## data -datalist <- kobocruncher::kobo_data( system.file("demo_data.xlsx", #"test.xlsx", +```{r example-outcome14_1, message=TRUE, warning=FALSE} +## data, cf example fct_re_map() +datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", package = "IndicatorCalc")) ## Apply indicator function on datalist datalist <- outcome14_1(datalist ) ## Visualise value -fct_plot_indic_donut(indicator = datalist[["main"]]$outcome14_1, +fct_plot_indic_donut(indicator = datalist[["ind"]]$outcome14_1, iconunicode = "f140") ``` @@ -3468,13 +3415,6 @@ test_that("outcome14_1 works", { #' @param datalist A list with all hierarchical data frame for a survey data set. #' format is expected to match the Excel export synchronized from kobo to RILD #' and loaded with kobocruncher::kobo_data() -#' -#' @param mapper a list providing the mapping of the variables used for the -#' calculation - this mapper is potentially to be adjusted in relation with deviation -#' between the the standard XlsForm and the contextualized dataset -#' -#' @param chain Boolean TRUE/FALSE to tell whether the function should return -#' the dataset expanded with the variable or a single factor #' #' @importFrom dplyr mutate case_when #' @importFrom labelled labelled @@ -3484,120 +3424,105 @@ test_that("outcome14_1 works", { #' @export outcome16_1 <- function(datalist ){ # Mapper , - + mapper = list( hierarchy = "main", variablemap = data.frame( label = -c("1. Who owns the **land** that you currently live in?", "2. Do you have official documentation (legally, customarily or otherwise recognized by relevant authorities) for who owns the **land** that your current housing is built on?", -"3. What type of documents does your household have for the **land** you currently live in? ", -"4. Who owns the **housing** that you currently live in?", "5. Do you have official documentation (legally, customarily or otherwise recognized by relevant authorities) for who owns the **housing**?", -"6. What type of documents does your household have for the **housing** you currently live in? ", -"9. In the next 12 months, how likely or unlikely is it that you could lose your right to this housing and/or land, against your will?" +c("6. Who owns the dwelling that you currently live in?", "7. Who owns the land that your current dwelling is built on?", +"10. What type of documents does your household have for the housing you live in?", +"11. In the next 12 months, how likely or unlikely is it that you could lose your right to this housing and/or land, against your will?" ) , variable = -c("DWE06_land", "DWE06a_land", "DWE07_land", "DWE06_housing", -"DWE06a_housing", "DWE07_housing", "DWE10") +c("DWE06", "DWE07", "DWE10", "DWE11") ), modalitymap = data.frame( variable = -c("DWE06_land", "DWE06_land", "DWE06_land", "DWE06_land", "DWE06_land", -"DWE06_land", "DWE06_land", "DWE06_land", "DWE06_land", "DWE06_land", -"DWE06_land", "DWE06_land", "DWE06a_land", "DWE06a_land", "DWE06a_land", -"DWE07_land", "DWE07_land", "DWE07_land", "DWE07_land", "DWE07_land", -"DWE07_land", "DWE07_land", "DWE06_housing", "DWE06_housing", -"DWE06_housing", "DWE06_housing", "DWE06_housing", "DWE06_housing", -"DWE06_housing", "DWE06_housing", "DWE06_housing", "DWE06_housing", -"DWE06_housing", "DWE06_housing", "DWE06a_housing", "DWE06a_housing", -"DWE06a_housing", "DWE07_housing", "DWE07_housing", "DWE07_housing", -"DWE07_housing", "DWE07_housing", "DWE07_housing", "DWE07_housing", -"DWE10", "DWE10", "DWE10", "DWE10", "DWE10") +c("DWE06", "DWE06", "DWE06", "DWE06", "DWE06", "DWE06", "DWE06", +"DWE06", "DWE06", "DWE06", "DWE06", "DWE06", "DWE07", "DWE07", +"DWE07", "DWE07", "DWE07", "DWE07", "DWE07", "DWE07", "DWE07", +"DWE07", "DWE07", "DWE07", "DWE07", "DWE07", "DWE10", "DWE10", +"DWE10", "DWE10", "DWE10", "DWE10", "DWE10", "DWE11", "DWE11", +"DWE11", "DWE11", "DWE11") , label = c("Respondent or another HH member", "Unrelated Person", "Friend Or Relative", "Ngo/Non-Religious Charity", "Government Agency Or Municipality", "UNHCR", "Religious Organization/ Charity", "Foreign Government", -"Unowned/Squatting", "Other (Specify) |____|", "Don't know", -"Prefer not to respond", "Yes", "No", "Don't know", "Title deed", -"Certificate of customary ownership ", "Certificate of occupancy ", -"Certificate of hereditary acquisition listed in registry ", -"Rental contract ", "Lease registered", "Other (Specify) |_____|", +"Unowned/Squatting", "Other (Specify)", "Don't know", "Prefer not to respond", "Respondent or another HH member", "Unrelated Person", "Friend Or Relative", "Ngo/Non-Religious Charity", "Government Agency Or Municipality", "UNHCR", "Religious Organization/ Charity", "Foreign Government", -"Unowned/Squatting", "Other (Specify) |____|", "Don't know", -"Prefer not to respond", "Yes", "No", "Don't know", "Title deed", -"Certificate of customary ownership ", "Certificate of occupancy ", -"Certificate of hereditary acquisition listed in registry ", -"Rental contract ", "Lease registered", "Other (Specify) |_____|", -"Very unlikely", "Somewhat unlikely", "Somewhat likely", "Very likely", -"Don't Know") +"Unowned/Squatting", "Apartment Building", "Community", "Other(Specify)", +"Don't know", "Prefer not to respond", "Title deed", "Certificate of customary ownership", +"Certificate of occupancy", "Certificate of hereditary acquisition listed in registry", +"Rental contract", "Lease registered", "Other (Specify)", "Very unlikely", +"Somewhat unlikely", "Somewhat likely", "Very likely", "Don't Know" +) , standard = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "96", "98", "99", -"1", "0", "98", "1", "2", "3", "4", "5", "6", "96", "1", "2", -"3", "4", "5", "6", "7", "8", "9", "96", "98", "99", "1", "0", -"98", "1", "2", "3", "4", "5", "6", "96", "1", "2", "3", "4", -"99") +"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "96", +"98", "99", "1", "2", "3", "4", "5", "6", "96", "1", "2", "3", +"4", "99") )) - ## So first we check that we have what we need in the data set based on the mapper check_map <- fct_check_map(datalist = datalist, mapper = mapper) - if (! isTRUE(check_map )) { + if ( check_map == FALSE) { cat( "There are missing data requirement to calculate Indicator Outcome 16.1") } else { # likelihood of losing right for housing is unlikely datalist[["main"]]$housing_cond1<- dplyr::case_when( - (datalist[["main"]]$DWE11 == 1 | - datalist[["main"]]$DWE11 == 2 ) ~ 1, + (as.integer(datalist[["main"]]$DWE11) == 1 | + as.integer(datalist[["main"]]$DWE11) == 2 ) ~ 1, TRUE ~ 0) # have official documents to proof residency datalist[["main"]]$housing_cond2 <- dplyr::case_when( - datalist[["main"]]$DWE10 == 96 ~ 0, - datalist[["main"]]$DWE10 == 1 | - datalist[["main"]]$DWE10 == 2 | - datalist[["main"]]$DWE10 == 3 | - datalist[["main"]]$DWE10 == 4 | - datalist[["main"]]$DWE10 == 5 | - datalist[["main"]]$DWE10 == 6 ~ 1) + as.integer(datalist[["main"]]$DWE10) == 96 ~ 0, + as.integer(datalist[["main"]]$DWE10) == 1 | + as.integer(datalist[["main"]]$DWE10) == 2 | + as.integer(datalist[["main"]]$DWE10) == 3 | + as.integer(datalist[["main"]]$DWE10) == 4 | + as.integer(datalist[["main"]]$DWE10) == 5 | + as.integer(datalist[["main"]]$DWE10) == 6 ~ 1) # not un-owned or not squatting - datalist[["main"]]$housing_cond3<- dplyr::case_when( - datalist[["main"]]$DWE06 == 9 | - datalist[["main"]]$DWE06 == 96 | - datalist[["main"]]$DWE06 == 98 | - datalist[["main"]]$DWE06 == 99 | - datalist[["main"]]$DWE07 == 9 | - datalist[["main"]]$DWE07 == 96 | - datalist[["main"]]$DWE07 == 98 | - datalist[["main"]]$DWE07 == 99 ~ 0, + datalist[["main"]]$housing_cond3 <- dplyr::case_when( + as.integer(datalist[["main"]]$DWE06) == 9 | + as.integer(datalist[["main"]]$DWE06) == 96 | + as.integer(datalist[["main"]]$DWE06) == 98 | + as.integer(datalist[["main"]]$DWE06) == 99 | + as.integer(datalist[["main"]]$DWE07) == 9 | + as.integer(datalist[["main"]]$DWE07) == 96 | + as.integer(datalist[["main"]]$DWE07) == 98 | + as.integer(datalist[["main"]]$DWE07) == 99 ~ 0, TRUE ~ 1) datalist[["main"]]$outcome16_1<- dplyr::case_when( - datalist[["main"]]$housing_cond1 == 1 & - datalist[["main"]]$housing_cond2 == 1 & - datalist[["main"]]$housing_cond3 == 1 ~ 1, + as.integer(datalist[["main"]]$housing_cond1) == 1 & + as.integer(datalist[["main"]]$housing_cond2) == 1 & + as.integer(datalist[["main"]]$housing_cond3) == 1 ~ 1, - datalist[["main"]]$housing_cond1 == 0 | - datalist[["main"]]$housing_cond2 == 0 | - datalist[["main"]]$housing_cond3 == 0 ~ 0) + as.integer(datalist[["main"]]$housing_cond1) == 0 | + as.integer(datalist[["main"]]$housing_cond2) == 0 | + as.integer(datalist[["main"]]$housing_cond3) == 0 ~ 0) -datalist[["main"]]$utcome16_1 <- labelled::labelled(datalist[["main"]]$outcome16_1, - labels = c('Yes' = 1, 'No' = 0 ), - label = "Proportion of PoC with secure tenure rights and/or property - rights to housing and/or land") +datalist[["main"]]$outcome16_1 <- labelled::labelled(datalist[["main"]]$outcome16_1, + labels = c('Yes' = 1, 'No' = 0 ), + label = "Proportion of PoC with secure tenure rights and/or property rights to housing and/or land") } return(datalist) } ``` -```{r example-outcome16_1} -## data -datalist <- kobocruncher::kobo_data( system.file("demo_data.xlsx", #"test.xlsx", +```{r example-outcome16_1, message=TRUE, warning=FALSE} +## data, cf example fct_re_map() +datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", package = "IndicatorCalc")) + ## Apply indicator function on datalist datalist <- outcome16_1(datalist ) @@ -3643,13 +3568,6 @@ test_that("outcome16_1 works", { #' @param datalist A list with all hierarchical data frame for a survey data set. #' format is expected to match the Excel export synchronized from kobo to RILD #' and loaded with kobocruncher::kobo_data() -#' -#' @param mapper a list providing the mapping of the variables used for the -#' calculation - this mapper is potentially to be adjusted in relation with deviation -#' between the the standard XlsForm and the contextualized dataset -#' -#' @param chain Boolean TRUE/FALSE to tell whether the function should return -#' the dataset expanded with the variable or a single factor #' #' @importFrom dplyr mutate case_when #' @importFrom labelled labelled @@ -3659,35 +3577,46 @@ test_that("outcome16_1 works", { #' @export outcome16_2 <- function(datalist ){ # Mapper , - + mapper = list( hierarchy = "main", variablemap = data.frame( label = -"1. In the last 12 months, have you or any member of your household received any payment from ${countryname} national or local government from the following sources?" +c("Social protection cash benefit", "Parental benefit", "Disability benefit", +"Unemployment benefit", "Employment injury benefit", "Old-age pension", +"Other vulnerability benefit", "Social assistance cash benefit" +) , variable = -"SPF01" +c("SPF01a", "SPF01b", "SPF01c", "SPF01d", "SPF01e", "SPF01f", +"SPF01g", "SPF01h") ), modalitymap = data.frame( variable = -"SPF01" +c("SPF01a", "SPF01a", "SPF01a", "SPF01b", "SPF01b", "SPF01b", +"SPF01c", "SPF01c", "SPF01c", "SPF01d", "SPF01d", "SPF01d", "SPF01e", +"SPF01e", "SPF01e", "SPF01f", "SPF01f", "SPF01f", "SPF01g", "SPF01g", +"SPF01g", "SPF01h", "SPF01h", "SPF01h") , label = -NA_character_ +c("Yes", "No", "Don't know", "Yes", "No", "Don't know", "Yes", +"No", "Don't know", "Yes", "No", "Don't know", "Yes", "No", "Don't know", +"Yes", "No", "Don't know", "Yes", "No", "Don't know", "Yes", +"No", "Don't know") , standard = -NA_character_ - )) - +c("1", "0", "98", "1", "0", "98", "1", "0", "98", "1", "0", "98", +"1", "0", "98", "1", "0", "98", "1", "0", "98", "1", "0", "98" +) + )) ## So first we check that we have what we need in the data set based on the mapper check_map <- fct_check_map(datalist = datalist, mapper = mapper) - if (! isTRUE(check_map )) { + if ( check_map == FALSE) { cat( "There are missing data requirement to calculate Indicator Outcome 16.2") } else { -datalist[["main"]]$outcome16_2<- dplyr::case_when( +datalist[["main"]]$outcome16_2 <- dplyr::case_when( datalist[["main"]]$SPF01a == 1 | datalist[["main"]]$SPF01b == 1 | datalist[["main"]]$SPF01c == 1 | @@ -3698,22 +3627,24 @@ datalist[["main"]]$outcome16_2<- dplyr::case_when( datalist[["main"]]$SPF01h == 1 ~ 1, TRUE ~ 0) -datalist[["main"]]$outcome16_2 <- labelled::labelled(datalist[["main"]]$outcome16_2, + datalist[["main"]]$outcome16_2 <- labelled::labelled(datalist[["main"]]$outcome16_2, labels = c('Yes' = 1, 'No' = 0 ), label = "Proportion of Persons of Concern covered by social protection floors/systems") + } return(datalist) } ``` -```{r example-outcome16_2} -## data -datalist <- kobocruncher::kobo_data( system.file("demo_data.xlsx", #"test.xlsx", +```{r example-outcome16_2, message=TRUE, warning=FALSE} +## data, cf example fct_re_map() +datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", package = "IndicatorCalc")) ## Apply indicator function on datalist datalist <- outcome16_2(datalist) +table( datalist[["main"]]$outcome16_2, useNA = "ifany") ## Visualise value fct_plot_indic_donut(indicator = datalist[["main"]]$outcome16_2, iconunicode = "f140") @@ -3731,7 +3662,7 @@ test_that("outcome16_2 works", { ```{r development-inflate, eval=FALSE} # Keep eval=FALSE to avoid infinite loop in case you hit the knit button # Execute in the console directly -fusen::inflate(flat_file = "dev/indicators.Rmd", vignette_name = "Development") +fusen::inflate(flat_file = "dev/indicators.Rmd", vignette_name = "Indicators calculation functions") ``` diff --git a/dev/utilities.Rmd b/dev/utilities.Rmd index 2f2570d..6bfde76 100644 --- a/dev/utilities.Rmd +++ b/dev/utilities.Rmd @@ -1,5 +1,5 @@ --- -title: "Get Started" +title: "Supporting functions behind indicators calculation" output: html_document editor_options: chunk_output_type: console @@ -21,83 +21,566 @@ If it is the first time you use {fusen}, after 'description', you can directly r ```{r development-load} -# Load already included functions if relevant -pkgload::load_all(export_all = FALSE) -``` - - - -```{r development-dataset} # Run all this chunk in the console directly # There already is a dataset in the "inst/" directory # Make the dataset file available to the current Rmd during development pkgload::load_all(path = here::here(), export_all = FALSE) - - ``` -Indicator functions are designed to work based on data stored as a list - which is - the default structure for a complex hierarchical survey dataset with nested tables + +Indicator functions are designed to work based on data stored as a list - which is the default structure for a complex hierarchical survey dataset with nested tables. - The default export format from kobotoolbox includes variables names generated as a concatenation of groups and names. +The default export format from kobotoolbox includes variables names generated as a concatenation of groups and names. - The indicators calculation are based on specific patterns to be identified within - the variable names. This allow to handle cases where variables and questions would - have been shifted within the sequence of the questionnaire and through different - questions groups. +The indicators calculation are based on specific patterns to be identified within the variable names. This allow to handle cases where variables and questions would have been shifted within the sequence of the questionnaire and through different questions groups. - The indicator functions also check that the data content is the one expected. - A check log is written to keep track of all issues +The indicator functions also check that the data content is the one expected. + +A check log is displayed in the console to keep track of all issues +# Generating data from a specific form definition -# Data Wrangling +One key function is to generate a dummy dataset based on a specific form structure +To demonstrate the package we will use the standard questionnaire and then apply each indicator function to demonstrate them. + +We can then also re-use the function to create dummy data based on any form and then apply each indicator function to actually verify which indicators can be created based on the form content. -Each indicator calculation is based on predefined frame, variable name and variable value. +## fct_var_dummy + +```{r function-fct_var_dummy} +#' fct_var_dummy +#' +#' Append to an existing dataframe new Generate the variable based on 1. type, 2. constraint and 3.relevance +#' +#' @param frame dataframe object where new dummy varaible will be created... +#' @param name name of the variable to be created +#' @param type type of the variable to be created in line with xlsform - +#' one of the following 6 options: +#' "select_one", "select_multiple", "text", "integer", "numeric", "date" +#' +#' "start ", "end", "calculate", "geopoint","acknowledge", +#' "begin_group", "end_group", "begin_repeat", "end_repeat" , "note" +#' +#' @param list_opt a vector with all possible values for select_one and select_multiple +#' - can be null +#' @param constraint specific constraint - used for for numeric or integer +## @param seed random seed +#' +#' @return dataframe +#' +#' @importFrom withr with_seed +#' @importFrom dplyr mutate n tibble rename +#' @importFrom stats rlnorm +#' @importFrom tidyr unnest +#' +#' @noRd +#' +#' @export +fct_var_dummy <- function( frame, + name, + type, + list_opt, + constraint #, + # seed = 1976 + ) { + ## number of records to be generated depends on the initial frame.. + n <- nrow(frame) + #withr::with_seed(seed = seed) + # withr::with_seed( + # seed = seed, + # suppressWarnings( -Therefore each indicator function is organised in 3 steps: + # no content ########### + if( type %in% c("begin_group", "end_group", "begin_repeat", "end_repeat" , "note" )) { + res <- dplyr::tibble( + thisvar = NA) + names(res)[1] <- name + frame <- cbind(frame,res) + } + + + ## acknowledge ########### + if( type %in% c("acknowledge")) { + res <- dplyr::tibble( + thisvar = sample(c(0,1), n, replace = TRUE)) + names(res)[1] <- name + frame <- cbind(frame,res) + } + + + ## select one ########### + if( type %in% c("select_one") ) { + res <- dplyr::tibble( thisvar = sample(c(list_opt), n, replace = TRUE) ) + ## Now rename the variable + names(res)[1] <- name + frame <- cbind(frame,res) + } + + ## select multiple ########### + if( type %in% c("select_multiple")) { + for (modality in list_opt) { + res <- dplyr::tibble( + thisvar = sample(c(0,1), n, replace = TRUE)) + names(res)[1] <- paste0(name, "_",modality ) + frame <- cbind(frame,res) + } + } - * Apply fuzzy matching `fct_var_mapping()` to map within a specified XlsForm the expected variables for indicator calculation. The output is a convenient excel file that will need to be manually revised. The process is very similar than data cleaning with a cleaning log as implemented within [{cleaningtools}](https://edouard-legoupil.github.io/cleaningtools/) - - * Once the mapping as been revised, `fct_build_map()` is used to create the mapper with the precise list format used by the next function - - * Apply the mapping the function `fct_re_map()` to obtain the data in the exact format expected by the indicator calculation functions + ## stext ########### + if( type %in% c("text")) { + res <- dplyr::tibble( + thisvar = paste0( "FreeText_", + purrr::as_vector( + purrr::map(n, sample(LETTERS, 6)) |> + purrr::map(paste0, collapse = "")), + "-", + formatC(1:n, width = nchar(n) + 1, flag = "0") + )) + ## Now rename the variable + names(res)[1] <- name + frame <- cbind(frame,res) + } + + ## numeric ########### + if( type %in% c("numeric", "integer", "calculate", "geopoint")) { + ## TODO extract elements from constraint to define num1 an num2 + num1 <- 18 + num2 <- 95 + + res <- dplyr::tibble( + thisvar = sample(num1:num2, n, replace = TRUE)) + ## Now rename the variable + names(res)[1] <- name + frame <- cbind(frame,res) + } - * For each indicator, checking that standard frame/variable/modalities are present in the dataset with `fct_check_map()`, apply the indicator calculation . + ## date ########### + if( type %in% c("date", "start", "end")) { + res <- dplyr::tibble( + thisvar = Sys.time() - abs(rnorm(n, 0, sd = 2) * 365 * 24 * 3600)) + ## Now rename the variable + names(res)[1] <- name + frame <- cbind(frame,res) + } -## fct_var_mapping + # ) + # ) + + return(frame) +} +``` + +```{r example-fct_var_dummy} + +## let's initiate a dataframe with an index of n r records +n <- 384 +frame <- dplyr::tibble( + index = paste0( "ID-", + purrr::as_vector( + purrr::map(n, sample(LETTERS, 4)) |> + purrr::map(paste0, collapse = "")), + "-", + formatC(1:n, width = nchar(n) + 1, flag = "0") + )) + +## test inject select_one +frame <- fct_var_dummy( + frame = frame, + name = "testselect_one", + type = "select_one", + list_opt = c("alpha", "beta", "delta"), + constraint = NULL + ) +## test inject select_multiple in main +frame <- fct_var_dummy( + frame = frame, + name = "testselect_multiple", + type = "select_multiple", + list_opt = c("alpha", "beta", "delta"), + constraint = NULL + ) + +## test inject text +frame <- fct_var_dummy( + frame = frame, + name = "testtext", + type = "text", + list_opt = NULL, + constraint = NULL + ) + + +## test inject numeric +frame <- fct_var_dummy( + frame = frame, + name = "testnumeric", + type = "numeric", + list_opt = NULL, + constraint = NULL + ) + +## test inject date +frame <- fct_var_dummy( + frame = frame, + name = "testdate", + type = "date", + list_opt = NULL, + constraint = NULL + ) + +## Preview out out +knitr::kable(head(frame, 5)) +``` + +```{r tests-fct_var_dummy} +test_that("fct_var_dummy works", { + expect_true(inherits(fct_var_dummy, "function")) +}) +``` + + +## fct_kobo_dummy + -```{r function-fct_var_mapping} -#' fct_var_mapping +```{r function-fct_kobo_dummy} +#' fct_kobo_dummy #' -#' Create a diff to identify the mapping between expected variables / modalities and -#' what is has been configured in a specific xlsform. The function use a fuzzy matching -#' process to identify the best and second best match process... -#' It generates an excel file ready for manual review and that can be used as an input -#' for the next function in the pipeline fct_build_map() +#' @description Automatically produce an dummy dataset in line with the structure +#' of an XlsForm +#' Making decisions about research design and analysis strategies is often difficult before data is collected, +#' because it is hard to imagine the exact form data will take. +#' This function helps imagine what data will look like before they collect it. +#' samplesize is set per defautl at 500 records +#' +#' Supported Features: +#' - Generate a data set +#' - respects ODK structure "`relevant`" skip logic +#' (Some advanced functionality such as "coalesce()" not covered) +#' - respects "`constraint`" +#' - respects "`repeat`" adds ID column to link hierarchical data based on "`repeat_count`" +#' This function is a rewriting of +#' https://unhcr.github.io/koboloadeR/docs/reference/kobo_dummy.html +#' it also build on https://thinkr-open.github.io/fakir/ and +#' https://docs.ropensci.org/charlatan/ #' -#' @param xlsformpath path to the xlsform -#' @param mappingfile_out path to file to out the mapping +#' @param form file path of the XlsForm +#' @param n number of main records to be generated +#' @param nrepeat max random number of repeat records to be generated +#' when repeat_count is not mentionned +#' @param file file as xlsx where to save the resulting data #' -#' @import stringdist -#' @import sdplyr +#' @importFrom kobocruncher kobo_dico #' -#' @return a list with the best mapping... +#' +#' @return a data list with a series of dummy data #' #' @export -fct_var_mapping <- function(xlsformpath, - mappingfile_out){ - ## Pulling Comparison base ###### - IndicMap <- readxl::read_excel( system.file("IndicMap.xlsx", package = "IndicatorCalc")) - dico <- kobocruncher::kobo_dico(xlsformpath) +fct_kobo_dummy <- function(form, + n = 384, + file){ - # Comparing variables ########## - # names(IndicMap) - varMapIndic <- IndicMap |> - dplyr::select(QuestionVar, label, type) |> - dplyr::distinct() |> - tibble::as_tibble() + dico <- kobocruncher::kobo_dico(xlsformpath = form) + + ## Get the variables to extract + frame <- "main" + + conf <- data.frame( + name <- dico[["variables"]] |> + dplyr::filter(repeatvar == frame) |> + dplyr::filter(! is.na(name)) |> + dplyr::filter(!(type %in% c("note", "begin_group", "end_group","begin_repeat", "end_repeat") )) |> + dplyr::select(name), + + # dico[["variables"]]|> + # dplyr::filter(repeatvar == frame) |> + # dplyr::distinct(type) + type <- dico[["variables"]]|> + dplyr::filter(repeatvar == frame) |> + dplyr::filter(! is.na(name)) |> + dplyr::filter(!(type %in% c("note", "begin_group", "end_group","begin_repeat", "end_repeat") )) |> + dplyr::select(type), + + ## pulling list options... + list_opt <- dico[["variables"]] |> + dplyr::filter(repeatvar == frame) |> + dplyr::filter(! is.na(name)) |> + dplyr::filter(!(type %in% c("note", "begin_group", "end_group","begin_repeat", "end_repeat") )) |> + dplyr::select(list_name) , + #dplyr::left_join(modal, by = c("list_name")) |> + # dplyr::select( list_opt) + + + constraint <- dico[["variables"]]|> + dplyr::filter(repeatvar == frame) |> + dplyr::filter(! is.na(name)) |> + dplyr::filter(!(type %in% c("note", "begin_group", "end_group","begin_repeat", "end_repeat") )) |> + dplyr::select(constraint) + ) + + ## We start with the generation of the main frame + ## Build the main table and initiate it an index..... + main <- dplyr::tibble( + index = paste0( "ID-", + purrr::as_vector( + purrr::map(n, sample(LETTERS, 4)) |> + purrr::map(paste0, collapse = "")), + "-", + formatC(1:n, width = nchar(n) + 1, flag = "0") + )) + names(main)[1] <- "_index" + + ## then apply fct_var_dummy interactively... + for(i in (1:nrow(conf)) ) { + # i <- 1 + cat(paste0(i, "-", conf[i, c("type")], "-", conf[i, c("name")], "\n")) + + ## manage specific case when list name is not defined in ccoices but pulled from data.. + ## in such case we replace type by select_one by text + this.type <- conf[i, c("type")] + this.name <- conf[i, c("name")] + this.listname <- conf[i , c("list_name")] + this.constraint = conf[i, c("constraint")] + + ## cas there's no list matching.. + if( this.type %in% c("select_one", "select_multiple") & + !(this.listname %in% c( dico[["modalities"]] |> + dplyr::pull(list_name))) ) { + this.listname <- NULL + this.type <- "text" + } + + main <- fct_var_dummy( + frame = main, + name = this.name, + type = this.type, + list_opt = c(dico[["modalities"]] |> + dplyr::filter(list_name == this.listname ) |> + dplyr::pull(name) ), + constraint = this.constraint ) + } + + ## Still struggling with purrr + # test <- purrr::map( main, + # name, + # type, + # list_opt$list_opt , + # constraint , + # fct_var_dummy) + + ## TODO -- apply the relevance statement... + ## define when the variable should exist based on other elements + ## Need to convert the xlsform statement into R syntax + # relevant <- dico[["variables"]]|> + # dplyr::filter(repeatvar == "main") |> + # dplyr::pull(relevant) + + ## replace \" by ' + ## replace '=' by '==" + ## replace '!==' by '!=" (to fix previsous replace) + ## replace '${' '}' with nothing.. + ## replace 'or' with '|'.. + ## replace 'and' with '&'.. + + ## Now store in the list... + datalist <- list ( "main" = main) + + ## treat cases with repeat table.. ##### + repeatvar <- dico[["variables"]] |> + dplyr::select(repeatvar) |> + dplyr::filter(repeatvar != "main") |> + dplyr::distinct() |> + dplyr::pull() + ## need to separate the different repeat elements wthin the form... + for ( rep in repeatvar ) { + + confrep <- data.frame( + name <- dico[["variables"]] |> + dplyr::filter(repeatvar == rep) |> + dplyr::filter(! is.na(name)) |> + dplyr::filter(!(type %in% c("note", "begin_group", + "end_group","begin_repeat", "end_repeat") )) |> + dplyr::select(name), + + # dico[["variables"]]|> + # dplyr::filter(repeatvar == rep) |> + # dplyr::distinct(type) + type <- dico[["variables"]]|> + dplyr::filter(repeatvar == rep) |> + dplyr::filter(! is.na(name)) |> + dplyr::filter(!(type %in% c("note", "begin_group", + "end_group","begin_repeat", "end_repeat") )) |> + dplyr::select(type), + + ## pulling list options... + list_opt <- dico[["variables"]] |> + dplyr::filter(repeatvar == rep) |> + dplyr::filter(! is.na(name)) |> + dplyr::filter(!(type %in% c("note", "begin_group", + "end_group","begin_repeat", "end_repeat") )) |> + dplyr::select(list_name) , + #dplyr::left_join(modal, by = c("list_name")) |> + # dplyr::select( list_opt) + + + constraint <- dico[["variables"]]|> + dplyr::filter(repeatvar == rep) |> + dplyr::filter(! is.na(name)) |> + dplyr::filter(!(type %in% c("note", "begin_group", + "end_group","begin_repeat", "end_repeat") )) |> + dplyr::select(constraint) + ) + + ## We start with the generation of the main frame + ## Build the main table and initiate it an index..... + repframe <- dplyr::tibble( + index = paste0( "ID-", + purrr::as_vector( + purrr::map(n, sample(LETTERS, 4)) |> + purrr::map(paste0, collapse = "")), + "-", + formatC(1:n, width = nchar(n) + 1, flag = "0") + )) + + names(repframe)[1] <- "_parent_index" + ## Apply now `_index` + ## TODO -- check if we have a `repeat_count` to apply limitation.. + + ## then apply fct_var_dummy interactively... + for(i in (1:nrow(confrep)) ) { + # i <- 6 + cat(paste0(i, " in repeat -", rep, " ///", confrep[i, c("type")], "-", confrep[i, c("name")], "\n")) + + ## manage specific case when list name is not defined in ccoices but pulled from data.. + ## in such case we replace type by select_one by text + this.type <- confrep[i, c("type")] + this.name <- confrep[i, c("name")] + this.listname <- confrep[i , c("list_name")] + this.constraint = confrep[i, c("constraint")] + + if(this.type %in% c("select_one", "select_multiple") & + !(this.listname %in% c( dico[["modalities"]] |> + dplyr::pull(list_name))) ) { + this.listname <- NULL + this.type <- "text" + } + + repframe <- fct_var_dummy( + frame = repframe, + name = this.name, + type = this.type, + list_opt = c(dico[["modalities"]] |> + dplyr::filter(list_name == this.listname ) |> + dplyr::pull(name) ), + constraint = this.constraint ) + } + + + ## append to the repeat + datalist[[rep]] <- repframe + } + + + + + return(datalist) + } +``` + +```{r example-fct_kobo_dummy} +## generate dummy dataset for different form version + +## CAPI +form <- system.file("RMS_CAPI_v2.xlsx", package = "IndicatorCalc") +datalist <- fct_kobo_dummy(form, + n = 384, + file = NULL) +# openxlsx::write.xlsx(datalist, here::here("inst", "dummy_RMS_CAPI_v2.xlsx")) + +form <- system.file("RMS_CAPI_v3.xlsx", package = "IndicatorCalc") +datalist <- fct_kobo_dummy(form, + n = 384, + file = NULL) +# openxlsx::write.xlsx(datalist, here::here("inst", "dummy_RMS_CAPI_v3.xlsx")) + +## CATI +form <- system.file("RMS_CATI_v0.xlsx", package = "IndicatorCalc") +datalist <- fct_kobo_dummy(form, + n = 384, + file = NULL) +# openxlsx::write.xlsx(datalist, here::here("inst", "dummy_RMS_CATI_v0.xlsx")) + +form <- system.file("RMS_CATI_v3.xlsx", package = "IndicatorCalc") +datalist <- fct_kobo_dummy(form, + n = 384, + file = NULL) +# openxlsx::write.xlsx(datalist, here::here("inst", "dummy_RMS_CATI_v3.xlsx")) +``` + +```{r tests-fct_kobo_dummy} +test_that("fct_kobo_dummy works", { + expect_true(inherits(fct_kobo_dummy, "function")) +}) +``` + + + + +# Data Wrangling + +Each indicator calculation is based on predefined frame, variable name and variable value. Some data wrangling is therefore required before performing the calculations. + +The packages comes with tool to support this task. The original data can be transformed with the function fct_re_map(). The mapper object is build with the `fct_build_map()` function, which require 2 files: + + * `IndicatorRequirementFile` thi files is created from the orginal template and indicator scripts. It creates a mapping between question variables and indicator variables. + +* `mappingfile` this one is created once and is designed to double check that the mapping can actually be performed. To obtain this file, we Apply fuzzy matching `fct_var_mapping()` to map within a specified XlsForm the expected variables for indicator calculation. The output is a convenient excel file that will need to be manually revised. The process is very similar than data cleaning with a cleaning log as implemented within [{cleaningtools}](https://edouard-legoupil.github.io/cleaningtools/) + +The data wrangling is then done with the function `fct_re_map()` to obtain the data in the exact format expected by the indicator calculation functions + +Before doing indicator calculation, `fct_check_map()` checks that standard frame/variable/modalities are present in the dataset. + +## fct_var_mapping + +```{r function-fct_var_mapping} +#' fct_var_mapping +#' +#' Create a diff to identify the mapping between expected variables / modalities and +#' what is has been configured in a specific xlsform. The function use a fuzzy matching +#' process to identify the best and second best match process... +#' It generates an excel file ready for manual review and that can be used as an input +#' for the next function in the pipeline fct_build_map() +#' +#' When you open the excel, filter for all match where the matching_index_best is different than 0 +#' +#' If you can confirm the match, manually set it to 0 so that the matching can be confirmed. +#' If the matching is not the expected one you may take the second best match, and if correct, +#' paste it and set the index to 0 +#' +#' @param xlsformpath path to the xlsform +#' @param IndicatorRequirementFile path to the file where the standard mapping is depending on form version +#' @param mappingfile_out path to file to out the mapping +#' +#' @import stringdist +#' @import dplyr +#' +#' @return a list with the best mapping... +#' +#' @export +fct_var_mapping <- function(xlsformpath, + IndicatorRequirementFile, + mappingfile_out){ + ## Pulling Comparison base ###### + IndicMap <- readxl::read_excel(IndicatorRequirementFile ) + dico <- kobocruncher::kobo_dico(xlsformpath) + + # Comparing variables ########## + # names(IndicMap) + varMapIndic <- IndicMap |> + dplyr::select(QuestionVar, label, type) |> + dplyr::distinct() |> + tibble::as_tibble() ## Comparing based on variable name vec1 <- varMapIndic$QuestionVar @@ -274,10 +757,13 @@ fct_var_mapping <- function(xlsformpath, ```{r example-fct_var_mapping} # Test the function -form <- system.file("RMSCAPI.xlsx", package = "IndicatorCalc") +form <- system.file("RMS_CAPI_v2.xlsx", package = "IndicatorCalc") +IndicatorRequirementFile <- system.file("RMS_CAPI_v2_mapper.xlsx", package = "IndicatorCalc") # Get the map to revise mapper <- fct_var_mapping(xlsformpath = form, - mappingfile_out = here::here("inst", "map_to_revise.xlsx")) + IndicatorRequirementFile = IndicatorRequirementFile, + mappingfile_out = tempfile()) + # here::here("inst", "RMS_CAPI_v2_mapping.xlsx")) ``` ```{r tests-fct_var_mapping} @@ -292,93 +778,148 @@ test_that("fct_var_mapping works", { ```{r function-fct_build_map} #' fct_build_map #' -#' Quick helper to reformat the data from the mapping file into the mapping file to use with each indicator calculation +#' Quick helper to reformat the data from the mapping file into the mapping file +#' to use with each indicator calculation. T +#' +#' his needs to be done in two steps: +#' one for `ind` for individual level and +#' one for `main` for household level +#' +#' @param mappingfile full path to the xlsx file with the revise variable mapping generated with `fct_var_mapping()` #' -#' @param mappingfile full path to the xlsx file with the revise variable mapping +#' @param IndicatorRequirementFile path to the file where the standard mapping is depending on form version +#' +#' @param thisMeasureLevel can be `ind` for individual level or `main` for household level +#' +#' @import readxl +#' @import dplyr #' #' @return a list #' #' @export -fct_build_map <- function(mappingfile){ - +fct_build_map <- function(mappingfile, + IndicatorRequirementFile, + thisMeasureLevel){ + + IndicMap <- readxl::read_excel( IndicatorRequirementFile) + ## Merge the map... + MeasureLevelQ <- IndicMap |> + dplyr::select(MeasureLevel, QuestionVar) |> + dplyr::filter(MeasureLevel == thisMeasureLevel)|> + dplyr::distinct() + + revisemapIndic <- readxl::read_excel( mappingfile, + sheet = "result_matchInd") + + revisemapIndic2 <- MeasureLevelQ |> + dplyr::left_join(revisemapIndic, by = c("QuestionVar") ) |> + dplyr::rename(#QuestionVar, + #label, + mappattern = "best_name") |> + dplyr::select(QuestionVar, label, mappattern)|> + dplyr::distinct() - IndicMap <- readxl::read_excel( system.file("IndicMap.xlsx", package = "IndicatorCalc")) - mappingfile <- readxl::read_excel( mappingfile) - - listind <- IndicMap |> dplyr::select(IndName) |> dplyr::distinct() |> dplyr::pull() - filemap <- tempfile() - filemap <- tempfile() - if (file.exists(filemap)) file.remove(filemap) - ## Roving around indicators - for ( thisInd in listind ) { - # thisInd <- listind[1] - cat( paste0(thisInd, "\n"), file = filemap , sep = "\n", append = TRUE) - cat( paste0( "------\n"), file = filemap , sep = "\n", append = TRUE) - cat( paste0( "mapper = list("), file = filemap , sep = "\n", append = TRUE) - cat( paste0( " hierarchy = \"", - IndicMap |> - dplyr::filter(IndName == thisInd) |> - dplyr::select(MeasureLevel) |> - dplyr::distinct() |> - dplyr::pull(), - "\","), file = filemap , sep = "\n", append = TRUE) + # names( IndicMap) + # names(revisemapIndic2) + # names(revisemapMod) + + + revisemapMod <- readxl::read_excel( mappingfile, + sheet = "result_matchMod") + revisemapMod2 <- MeasureLevelQ |> + dplyr::left_join(revisemapMod, by = c("QuestionVar") ) |> + dplyr::rename(#variable = "QuestionVar", + # label= "label_mod" , + # standard = "name_mod" , + map = "best_name") |> + dplyr::select(QuestionVar, label_mod, name_mod, map) + + + + filemap <- tempfile() + filemaplab <- tempfile() + #filemap <- "dev/test1.R" + if (file.exists(filemap)) file.remove(filemap) + + cat( paste0( "thismapper = list("), file = filemap , sep = "\n", append = TRUE) + cat( paste0( " hierarchy = \"", thisMeasureLevel,"\","), file = filemap , sep = "\n", append = TRUE) + + ## Mapping variables - thisvar <- IndicMap |> - dplyr::filter(IndName == thisInd) |> - dplyr::select(QuestionVar, label) |> + thisvar <- revisemapIndic2 |> + dplyr::select(QuestionVar, label, mappattern) |> dplyr::distinct() cat( paste0( " variablemap = data.frame("), file = filemap , sep = "\n", append = TRUE) cat( paste0( " label = "), file = filemap , sep = "\n", append = TRUE) ## Looping around labels - dput( thisvar |> dplyr::pull(label) , file = "dev/lab.R" ) - file_str <- paste(readLines("dev/lab.R"), collapse="\n") + dput( thisvar |> dplyr::pull(label) , file = filemaplab ) + file_str <- paste(readLines(filemaplab), collapse="\n") + cat( paste0(file_str), file = filemap , sep = "\n", append = TRUE) + cat( paste0( " ,"), file = filemap , sep = "\n", append = TRUE) + cat( paste0( " mappattern = "), file = filemap , sep = "\n", append = TRUE) + ## Looping around mappattern + dput( thisvar |> dplyr::pull(mappattern) , file = filemaplab ) + file_str <- paste(readLines(filemaplab), collapse="\n") cat( paste0(file_str), file = filemap , sep = "\n", append = TRUE) cat( paste0( " ,"), file = filemap , sep = "\n", append = TRUE) cat( paste0( " variable = "), file = filemap , sep = "\n", append = TRUE) ## Looping around variable - dput( thisvar |> dplyr::pull(QuestionVar), file = "dev/lab.R" ) - file_str <- paste(readLines("dev/lab.R"), collapse="\n") + dput( thisvar |> dplyr::pull(QuestionVar), file = filemaplab ) + file_str <- paste(readLines(filemaplab), collapse="\n") cat( paste0(file_str), file = filemap , sep = "\n", append = TRUE) cat( paste0( " ),"), file = filemap , sep = "\n", append = TRUE) - ## Mapping modality - thismod <- IndicMap |> - dplyr::filter(IndName == thisInd) |> - dplyr::select(QuestionVar, label_mod, name_mod) |> + + ## Mapping modality + thismod <- revisemapMod2 |> + dplyr::select(QuestionVar, label_mod, name_mod, map) |> dplyr::filter(! (is.null(name_mod)) ) |> dplyr::distinct() cat( paste0( " modalitymap = data.frame("), file = filemap , sep = "\n", append = TRUE) cat( paste0( " variable = "), file = filemap , sep = "\n", append = TRUE) ## Looping around variable for modalities - dput( thismod |> dplyr::pull(QuestionVar) , file = "dev/lab.R" ) - file_str <- paste(readLines("dev/lab.R"), collapse="\n") + dput( thismod |> dplyr::pull(QuestionVar) , file = filemaplab ) + file_str <- paste(readLines(filemaplab), collapse="\n") cat( paste0(file_str), file = filemap , sep = "\n", append = TRUE) cat( paste0( " ,"), file = filemap , sep = "\n", append = TRUE) cat( paste0( " label = "), file = filemap , sep = "\n", append = TRUE) ## Looping around labels for modalities - dput( thismod |> dplyr::pull(label_mod) , file = "dev/lab.R" ) - file_str <- paste(readLines("dev/lab.R"), collapse="\n") + dput( thismod |> dplyr::pull(label_mod) , file = filemaplab ) + file_str <- paste(readLines(filemaplab), collapse="\n") + cat( paste0(file_str), file = filemap , sep = "\n", append = TRUE) + cat( paste0( " ,"), file = filemap , sep = "\n", append = TRUE) + cat( paste0( " map = "), file = filemap , sep = "\n", append = TRUE) + ## Looping around labels for modalities + dput( thismod |> dplyr::pull(map) , file = filemaplab ) + file_str <- paste(readLines(filemaplab), collapse="\n") cat( paste0(file_str), file = filemap , sep = "\n", append = TRUE) cat( paste0( " ,"), file = filemap , sep = "\n", append = TRUE) cat( paste0( " standard = "), file = filemap , sep = "\n", append = TRUE) ## Looping around standard for modalities - dput( thismod |> dplyr::pull(name_mod) , file = "dev/lab.R" ) - file_str <- paste(readLines("dev/lab.R"), collapse="\n") + dput( thismod |> dplyr::pull(name_mod) , file = filemaplab ) + file_str <- paste(readLines(filemaplab), collapse="\n") cat( paste0(file_str), file = filemap , sep = "\n", append = TRUE) cat( paste0( " ))"), file = filemap , sep = "\n", append = TRUE) - cat( paste0( "------\n"), file = filemap , sep = "\n", append = TRUE) - } - - - - - + source(filemap) + + return(thismapper) } ``` ```{r example-fct_build_map} -mappingfile = here::here("inst", "map_to_revise.xlsx") -mapper <- fct_build_map(mappingfile = mappingfile) +mappingfile <- system.file("RMS_CAPI_v2_mapping.xlsx", + package = "IndicatorCalc") + +IndicatorRequirementFile <- system.file("RMS_CAPI_v2_mapper.xlsx", + package = "IndicatorCalc") + +mappermain <- fct_build_map(mappingfile = mappingfile, + IndicatorRequirementFile = IndicatorRequirementFile, + thisMeasureLevel = "main") + +mapperind <- fct_build_map(mappingfile = mappingfile, + IndicatorRequirementFile = IndicatorRequirementFile, + thisMeasureLevel = "ind") ``` @@ -409,7 +950,7 @@ test_that("fct_build_map works", { #' @importFrom tibble tibble as_tibble_col #' @importFrom tidyr unnest #' -#' @keyword internal +#' @keywords internal #' #' @export fct_get_all_variable_names <- function(datalist){ @@ -461,6 +1002,7 @@ test_that("fct_get_all_variable_names works", { fct_re_map <- function(datalist, mapper){ varname <- fct_get_all_variable_names(datalist) + ## Loop around the variables within mapper for ( i in 1:nrow(mapper[["variablemap"]]) ) { # i <- 4 @@ -510,7 +1052,6 @@ fct_re_map <- function(datalist, mapper){ rm(thisvar, newlev) } } - return(datalist) } @@ -548,8 +1089,37 @@ datalist <- list(mainhousehold = data.frame( ) datalist <- fct_re_map(datalist = datalist, mapper = mapper ) -fct_check_map(datalist = datalist, mapper = mapper ) - +#fct_check_map(datalist = datalist, mapper = mapper ) + +## Now testing on a full remap... +## Dummy data created with fct_kobo_dummy +datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2.xlsx", + package = "IndicatorCalc")) + +## Mapping file created from Dummy data +mappingfile <- system.file("RMS_CAPI_v2_mapping.xlsx", + package = "IndicatorCalc") + +IndicatorRequirementFile <- system.file("RMS_CAPI_v2_mapper.xlsx", + package = "IndicatorCalc") + +## and now we remap both required variables for main and ind +mappermain <- fct_build_map(mappingfile = mappingfile, + IndicatorRequirementFile = IndicatorRequirementFile, + thisMeasureLevel = "main") +datalist <- fct_re_map(datalist = datalist, mapper = mappermain ) + +mapperind <- fct_build_map(mappingfile = mappingfile, + IndicatorRequirementFile = IndicatorRequirementFile, + thisMeasureLevel = "ind") +datalist <- fct_re_map(datalist = datalist, mapper = mapperind ) + + + +## Writing this in the installation folder of the packages to run all examples +# openxlsx::write.xlsx( list ( main = as.data.frame(datalist[["main"]]), +# ind = as.data.frame(datalist[["ind"]]) ), +# here::here("inst","dummy_RMS_CAPI_v2_mapped.xlsx")) ``` @@ -583,18 +1153,20 @@ test_that("fct_re_map works", { #' #' @return boolean flag TRUE or FALSE indicating if we can go forward for the indicator calculation #' +#' @import cli +#' #' @export fct_check_map <- function(datalist, mapper){ # rm(check ) check <- data.frame( check = c("")) ## Loop around the variables within mapper for ( i in 1:nrow(mapper[["variablemap"]]) ) { - # i <- 1 + # i <- 3 thisvar <- mapper[["variablemap"]][["variable"]][[i]] if ( is.null( datalist[[mapper[["hierarchy"]] ]] [[ thisvar ]] ) ) - {cli::cli_alert_info(paste0( thisvar ," standard variable was not found in the dataset.\n")) + {cli::cli_alert_danger(paste0( thisvar ," not found in the dataset.\n")) check <- rbind( check , check = c(FALSE)) } else { @@ -602,14 +1174,22 @@ fct_check_map <- function(datalist, mapper){ mod <- mapper[["modalitymap"]] |> dplyr::filter( variable == thisvar ) |> dplyr::pull(standard) + ## If numeric - mod is NA + if( anyNA(mod) ) { + + cli::cli_alert_success(paste0( thisvar ," \n")) + check <- rbind( check , check = c(TRUE)) + } else { if( any(levels(as.factor(datalist[[mapper[["hierarchy"]] ]] [[ thisvar ]])) %in% mod ) ) - { cat(paste0( thisvar ," is in the dataset and has at least one of the expected modality for calculation\n")) + { + cli::cli_alert_success(paste0( thisvar ," \n")) check <- rbind( check , check = c(TRUE)) } else { - cli::cli_alert_info(paste0( thisvar , " standard variable in the dataset misses at least one response among : ", mod)) + cli::cli_alert_info(paste0( thisvar , " misses responses options among : ", mod)) check <- rbind( check , check = c(FALSE)) } } + } } ## Check if have any fasel finalcheck <- if( FALSE %in% check$check ) { paste0(FALSE)} else { paste0(TRUE)} @@ -670,7 +1250,7 @@ test_that("fct_check_map works", { }) ``` - +# Plot ## fct_plot_indic_donut @@ -691,7 +1271,7 @@ test_that("fct_check_map works", { #' #' @return ggplot2 object #' -#' @importFrom ggplot2 ggplot labs aes expansion +#' @import ggplot2 #' @importFrom unhcrthemes scale_fill_unhcr_d theme_unhcr #' @importFrom ggforce geom_arc_bar #' @importFrom ggtext geom_richtext @@ -712,6 +1292,10 @@ fct_plot_indic_donut <- function(indicator, # Loading data #indicator <- datalist[["main"]]$shelter + if (is.null(indicator)) { + cat("No value was supplied for plotting...") + } else { + df2 <-sjmisc::frq(indicator)[[1]] #df2$label <- factor(df2$label, levels = c( "Yes","No")) @@ -725,19 +1309,26 @@ plot <- ggplot2::ggplot(df2) + fill = label ), stat = "pie", size = 1, color = "#FFFFFF" ) + - ggtext::geom_richtext( + ggtext::geom_textbox( x = 0, y = 0, label = paste0( "&#x", - iconunicode,";
", - sjlabelled::get_label(indicator), + iconunicode,";
", + sjlabelled::get_label(indicator) , "
", round(df2 |> dplyr::filter(val == 1) |> dplyr::pull(raw.prc), 2), "%"), size = 16 , - fill = NA, label.color = NA ) + + + # hjust = 0, vjust = 1, + halign = 0.5, # centered text + width = grid::unit(0.60, "npc"), # 73% of plot panel width + box.colour = NA, + fill = NA#, + # label.color = NA + ) + ggplot2::labs( #title = sjlabelled::get_label(x ), subtitle = subtitle_chart, @@ -753,23 +1344,33 @@ plot <- ggplot2::ggplot(df2) + #plot return(plot) +} } ``` -```{r example-fct_plot_indic_donut} +```{r example-fct_plot_indic_donut, fig.height=3, fig.width=3} test <- data.frame( shelter = rbinom(20, 1, 0.5)) |> dplyr::mutate( shelter = labelled::labelled( shelter, - labels = c( "Yes" = 1, "No" = 0), - label = "Access to adequate shelter")) + labels = c( "Yes" = 1, "No" = 0), +label = "Access to adequate shelter also testing a long title to see if it wraps well")) fct_plot_indic_donut(indicator = test$shelter, subtitle_chart = NULL, caption_chart = NULL, ordered_threhold = NULL, iconunicode = "e54f") + +## test no value +test2 <- NULL + +fct_plot_indic_donut(indicator = test2, + subtitle_chart = NULL, + caption_chart = NULL, + ordered_threhold = NULL, + iconunicode = "e54f") ``` ```{r tests-fct_plot_indic_donut} @@ -795,6 +1396,7 @@ test_that("fct_plot_indic_donut works", { #' @param rbm the RBM variable name - that can match SDG #' @param years years to filter the chart - for instance c(2000,2022) #' +#' @import ggplot2 #' @importFrom SDGsR get_indicator #' @importFrom countrycode countrycode #' @importFrom janitor clean_names @@ -992,10 +1594,10 @@ sdg_rbm <- structure(list( ``` ```{r example-fct_plot_rbm_sdg} -fct_plot_rbm_sdg( country = "BRA", - rbm = "impact2_2", - years = c(2000, 2022)) + - unhcrthemes::theme_unhcr(font_size = 10) +# fct_plot_rbm_sdg( country = "BRA", +# rbm = "impact2_2", +# years = c(2000, 2022)) + +# unhcrthemes::theme_unhcr(font_size = 10) ``` ```{r tests-fct_plot_rbm_sdg} @@ -1004,7 +1606,8 @@ test_that("fct_plot_rbm_sdg works", { }) ``` - +# Export Indicator table + ## fct_compass_table ```{r function-fct_compass_table} @@ -1017,686 +1620,267 @@ test_that("fct_plot_rbm_sdg works", { #' have a datalist object with all the correct calculation inside. #' #' The output can conveniently be upoaded as an indicator ressource in ridl -#' -#' @param country iso3 code for the country (easier to recall than the M49 used in the API) -#' @param operation operation name -#' @param year year to use to extract the baseline from Population Statistics -#' @param population_type The list of population type for baseline calculation -#' @param population_rms The list of population type covered by RMS -#' @param rms_indicator list with indicators and their related frame to pull the value -#' @param ridl name of ridl data container to push the data to -#' @param publish yes / no -#' -#' @import refugees -#' @importFrom janitor clean_names -#' -#' @return frame with all compass indicators -#' -#' @export -#' -fct_compass_table <- function(country , - operation, - year , - population_type, - population_rms, - rms_indicator, - ridl, - publish) { -# -# -# totalBaseline <- refugees::population |> -# filter(CountryAsylumCode == country, -# Year == 2022 , -# Population.type %in% population_type) |> -# group_by(CountryAsylumName) |> -# summarise(Value = sum(Value, na.rm = TRUE)) |> -# ungroup() |> -# dplyr::pull( Value) -# -# rms_indicator = as.data.frame(rms_indicator) -# -# ## Initialise the data frame -# compass <- data.frame( t(c( a = operation, -# b = population_rms, -# c = "Percent", -# d = totalBaseline, -# e = 10, -# f = 10, -# g = "XXX" )) ) -# -# ## append all the values -# for (i in (1:nrow(rms_indicator))) { -# # i <- 3 -# t <- as.data.frame(eval(parse(text= paste0("table( datalist[[\"", -# rms_indicator[i,1] , -# "\"]]$", -# rms_indicator[i, 2] ,")" ) ))) -# #cat( paste0(rms_indicator[i,3] , "\n")) -# -# #class(t) -# if( nrow(t) == 0) { -# t1 <- 0 } else if( nrow(t |> dplyr::filter(Var1 ==1 )) == 1) { -# t1 <- eval(parse(text= paste0("as.data.frame( prop.table(table( datalist[[\"", -# rms_indicator[i,1],"\"]]$", -# rms_indicator[i,2],"))) |> -# dplyr::filter(Var1 ==1 ) |> dplyr::pull(Freq)"))) } else { -# t1 <- 0} -# compass1 <- as.data.frame(t(c( a = operation, -# b = population_rms, -# c = "Percent", -# d = totalBaseline, -# e = round(t1 * 100,2), -# f = round(totalBaseline *t1), -# g = rms_indicator[i,3] ))) -# #str(compass) -# #str(compass1) -# compass <- rbind( compass,compass1) -# rm(compass1) -# } -# -# compass <- compass |> -# as.data.frame() |> -# dplyr::slice(-1) |> -# dplyr::mutate (Plan = a , -# `Population Type (operational)` = b , -# `Show As` = c, -# `Baseline Num.` = f, -# `Baseline Den.` = d, -# `%` = e , -# `Indicator` = g)|> -# dplyr::select(Plan, -# `Indicator`, -# `Population Type (operational)`, -# `Show As`, -# `Baseline Num.`, -# `Baseline Den.`, -# `%` ) -# -# ## And now saving -# ## Create a new workbook -# wb <- openxlsx::createWorkbook() -# ## add the cleaning log to the file -# openxlsx::addWorksheet(wb, "Compass") -# openxlsx::writeData(wb, "Compass", -# compass, withFilter = TRUE) -# ## Save workbook -# openxlsx::saveWorkbook(wb, -# file = here::here(paste0("compass_", -# country,"_", -# stringr::str_replace_all(string=population_rms, pattern=" ", repl=""), -# ".xlsx") ), -# overwrite = TRUE) -# -# -# ## Now push to RILD -# if( publish == "yes"){ -# p <- riddle::dataset_show(param$ridl) -# list_of_resources <- p[["resources"]][[1]] -# -# time <- format(Sys.Date(), '%d%b%y') -# ### Publish the analysis plan #### -# namecompass = paste0("compass_", country,"_", -# stringr::str_replace_all(string=population_rms, pattern=" ", repl="")) -# ### Check if the name is already in the resources -# if(namecompass %in% list_of_resources$name) { -# ## get the resource id -# resourceid <- list_of_resources |> -# dplyr::filter ( name == namecompass) |> -# dplyr::pull(id) -# ## get the new resource version -# curversion <- list_of_resources |> -# dplyr::filter ( name == namecompass) |> -# dplyr::pull(version) -# -# ## Build resource metadata -# metadatacompass <- riddle::resource_metadata( -# type = "attachment", -# url = paste0(namecompass, ".xlsx"), -# name = namecompass, -# description = paste0("Compass output generated from RMS on ", time, -# ". Built using kobocruncher "), -# format = "xlsx", -# version = (curversion + 1), -# visibility = "public", -# file_type = "other", -# ## Revise here based on the name from your crunching report -# upload = httr::upload_file(here::here(paste0(namecompass, ".xlsx"))) -# ) -# riddle::resource_update(id = resourceid, -# res_metadata = metadatacompass) -# } else { -# -# metadatacompass <- riddle::resource_metadata( -# type = "attachment", -# url = paste0(namecompass, ".xlsx"), -# name = namecompass, -# description = paste0("Compass output generated from RMS on ", time, -# ". Built using kobocruncher "), -# format = "xlsx", -# visibility = "public", -# file_type = "other", -# ## Revise here based on the name from your crunching report -# upload = httr::upload_file(here::here(paste0(namecompass, ".xlsx"))) -# ) -# riddle::resource_create(package_id = p$id, -# res_metadata = metadatacompass) -# } -# -# } -# -# return(compass) - -} -``` - -```{r example-fct_compass_table} -# compass <- export_compass_fill( country = "ECU", -# operation = "Ecuador ABC", -# year = 2022, -# population_type = c("REF","ASY", "OIP"), -# population_rms = "Refugees and Asylum-seekers", -# rms_indicator = rbind( -# c("main", "impact2_2", "2.2 Proportion of PoCs residing in physically safe and -# secure settlements with access to basic facilities"), -# c("main", "impact2_3", "2.3 Proportion of PoC with access to health services"), -# c("P2.S3", "impact3_2a", "3.2a Proportion of PoC enrolled in primary education" ), -# c("P2.S3", "impact3_2b", "3.2b Proportion of PoC enrolled in secondary education" ), -# c("main", "impact3_3", "3.3 Proportion of PoC feeling safe walking alone in their neighborhood (related SDG 16.1.4)." ), -# c("S2", "outcome1_2", "1.2 Proportion of children under 5 years of age whose births -# have been registered with a civil authority. [SDG 16.9.1 - Tier 1]" ), -# c("S2", "outcome1_3", "1.3 Proportion of PoC with legally recognized identity documents or credentials [GCR 4.2.2]." ), -# c("main", "outcome4_1", "4.1 Proportion of PoC who know where to access available GBV services." ), -# c("main", "outcome4_2", "4.2 Proportion of POCs who do not accept violence against women." ), -# c("main", "outcome8_2", "8.2 Proportion of PoC with primary reliance on clean (cooking) fuels and technology [SDG 7.1.2 Tier 1]" ), -# c("main", "outcome9_1", "9.1 Proportion of PoCs living in habitable and affordable housing." ), -# c("main", "outcome9_2", "9.2 Proportion of PoC that have energy to ensure lighting (close to Sphere)." ), -# c("main","outcome12_1", "12.1 Proportion of PoC using at least basic drinking water services (SDG)." ), -# # c("main" , "outcome12_2", "12.2 Proportion of PoC with access to a safe household toilet (SDG)." ), -# c("main", "outcome13_1", "13.1. Proportion of PoC with an account at a bank or other -# financial institution or with a mobile-money-service provider [SDG 8.10.2 Tier 1]." ), -# c("main", "outcome13_2", "13.2. Proportion of PoC who self-report positive changes in their income compared to previous year." ), -# c("main", "outcome13_3", "13.3 Proportion of PoC (working age) who are unemployed." ), -# c("main", "outcome16_1", "16.1. Proportion of PoC with secure tenure rights and/or -# property rights to housing and/or land [revised SDG indicator 1.4.2]." )#, -# # c("main", "outcome16_2", "16.2. Proportion of PoC covered by social protection floors/systems [SDG 1.3.1]." ) -# ), -# ridl = params$ridl, -# publish = params$publish ) -``` - -```{r tests-fct_compass_table} -#test_that("fct_compass_table works", { expect_true(inherits(fct_compass_table, "function")) }) -``` - - - - - - -# Generating data from a specific form definition - -One key function is to generate a dummy dataset based on a specific form structure - -To demonstrate the package we will use the standard questionnaire and then apply - each indicator function to demonstrate them. - -We can then also re-use the function to create dummy data based on any form and then apply - each indicator function to actually verify which indicators can be created based - on the form content. - -## fct_var_dummy - -```{r function-fct_var_dummy} -#' fct_var_dummy -#' -#' Append to an existing dataframe new Generate the variable based on 1. type, 2. constraint and 3.relevance -#' -#' @param frame dataframe object where new dummy varaible will be created... -#' @param name name of the variable to be created -#' @param type type of the variable to be created in line with xlsform - -#' one of the following 6 options: -#' "select_one", "select_multiple", "text", "integer", "numeric", "date" -#' -#' "start ", "end", "calculate", "geopoint","acknowledge", -#' "begin_group", "end_group", "begin_repeat", "end_repeat" , "note" -#' -#' @param list_opt a vector with all possible values for select_one and select_multiple -#' - can be null -#' @param constraint specific constraint - used for for numeric or integer -## @param seed random seed -#' -#' @return dataframe -#' -#' @importFrom withr with_seed -#' @importFrom dplyr mutate n tibble rename -#' @importFrom stats rlnorm -#' @importFrom tidyr unnest -#' -#' @noRd -#' -#' @export -fct_var_dummy <- function( frame, - name, - type, - list_opt, - constraint #, - # seed = 1976 - ) { - ## number of records to be generated depends on the initial frame.. - n <- nrow(frame) - #withr::with_seed(seed = seed) - # withr::with_seed( - # seed = seed, - # suppressWarnings( - - # no content ########### - if( type %in% c("begin_group", "end_group", "begin_repeat", "end_repeat" , "note" )) { - res <- dplyr::tibble( - thisvar = NA) - names(res)[1] <- name - frame <- cbind(frame,res) - } - - - ## acknowledge ########### - if( type %in% c("acknowledge")) { - res <- dplyr::tibble( - thisvar = sample(c(0,1), n, replace = TRUE)) - names(res)[1] <- name - frame <- cbind(frame,res) - } - - - ## select one ########### - if( type %in% c("select_one") ) { - res <- dplyr::tibble( thisvar = sample(c(list_opt), n, replace = TRUE) ) - ## Now rename the variable - names(res)[1] <- name - frame <- cbind(frame,res) - } - - ## select multiple ########### - if( type %in% c("select_multiple")) { - for (modality in list_opt) { - res <- dplyr::tibble( - thisvar = sample(c(0,1), n, replace = TRUE)) - names(res)[1] <- paste0(name, "_",modality ) - frame <- cbind(frame,res) - } - } - - ## stext ########### - if( type %in% c("text")) { - res <- dplyr::tibble( - thisvar = paste0( "FreeText_", - purrr::as_vector( - purrr::map(n, sample(LETTERS, 6)) |> - purrr::map(paste0, collapse = "")), - "-", - formatC(1:n, width = nchar(n) + 1, flag = "0") - )) - ## Now rename the variable - names(res)[1] <- name - frame <- cbind(frame,res) - } - - ## numeric ########### - if( type %in% c("numeric", "integer", "calculate", "geopoint")) { - ## TODO extract elements from constraint to define num1 an num2 - num1 <- 18 - num2 <- 95 - - res <- dplyr::tibble( - thisvar = sample(num1:num2, n, replace = TRUE)) - ## Now rename the variable - names(res)[1] <- name - frame <- cbind(frame,res) - } - - ## date ########### - if( type %in% c("date", "start ", "end")) { - res <- dplyr::tibble( - thisvar = Sys.time() - abs(rnorm(n, 0, sd = 2) * 365 * 24 * 3600)) - ## Now rename the variable - names(res)[1] <- name - frame <- cbind(frame,res) - } - - # ) - # ) - - return(frame) -} -``` - -```{r example-fct_var_dummy} - -## let's initiate a dataframe with an index of n r records -n <- 384 -frame <- dplyr::tibble( - index = paste0( "ID-", - purrr::as_vector( - purrr::map(n, sample(LETTERS, 4)) |> - purrr::map(paste0, collapse = "")), - "-", - formatC(1:n, width = nchar(n) + 1, flag = "0") - )) - -## test inject select_one -frame <- fct_var_dummy( - frame = frame, - name = "testselect_one", - type = "select_one", - list_opt = c("alpha", "beta", "delta"), - constraint = NULL - ) -## test inject select_multiple in main -frame <- fct_var_dummy( - frame = frame, - name = "testselect_multiple", - type = "select_multiple", - list_opt = c("alpha", "beta", "delta"), - constraint = NULL - ) - -## test inject text -frame <- fct_var_dummy( - frame = frame, - name = "testtext", - type = "text", - list_opt = NULL, - constraint = NULL - ) - - -## test inject numeric -frame <- fct_var_dummy( - frame = frame, - name = "testnumeric", - type = "numeric", - list_opt = NULL, - constraint = NULL - ) - -## test inject date -frame <- fct_var_dummy( - frame = frame, - name = "testdate", - type = "date", - list_opt = NULL, - constraint = NULL - ) - -## Preview out out -knitr::kable(head(frame, 5)) -``` - -```{r tests-fct_var_dummy} -test_that("fct_var_dummy works", { - expect_true(inherits(fct_var_dummy, "function")) -}) -``` - - -## fct_kobo_dummy - - -```{r function-fct_kobo_dummy} -#' fct_kobo_dummy -#' -#' @description Automatically produce an dummy dataset in line with the structure -#' of an XlsForm -#' Making decisions about research design and analysis strategies is often difficult before data is collected, -#' because it is hard to imagine the exact form data will take. -#' This function helps imagine what data will look like before they collect it. -#' samplesize is set per defautl at 500 records -#' -#' Supported Features: -#' - Generate a data set -#' - respects ODK structure "`relevant`" skip logic -#' (Some advanced functionality such as "coalesce()" not covered) -#' - respects "`constraint`" -#' - respects "`repeat`" adds ID column to link hierarchical data based on "`repeat_count`" -#' This function is a rewriting of -#' https://unhcr.github.io/koboloadeR/docs/reference/kobo_dummy.html -#' it also build on https://thinkr-open.github.io/fakir/ and -#' https://docs.ropensci.org/charlatan/ -#' -#' @param form file path of the XlsForm -#' @param n number of main records to be generated -#' @param nrepeat max random number of repeat records to be generated -#' when repeat_count is not mentionned -#' @param file file as xlsx where to save to the -#' -#' @importFrom kobocruncher kobo_dico -#' -#' -#' @return a data list with a series of dummy data -#' -#' @export -fct_kobo_dummy <- function(form, - n = 384, - file){ - - dico <- kobocruncher::kobo_dico(xlsformpath = form) - - ## Get the variables to extract - frame <- "main" - - conf <- data.frame( - name <- dico[["variables"]] |> - dplyr::filter(repeatvar == frame) |> - dplyr::select(name), - - # dico[["variables"]]|> - # dplyr::filter(repeatvar == frame) |> - # dplyr::distinct(type) - type <- dico[["variables"]]|> - dplyr::filter(repeatvar == frame) |> - dplyr::select(type), - - ## pulling list options... - list_opt <- dico[["variables"]] |> - dplyr::filter(repeatvar == frame) |> - dplyr::select(list_name) , - #dplyr::left_join(modal, by = c("list_name")) |> - # dplyr::select( list_opt) - - - constraint <- dico[["variables"]]|> - dplyr::filter(repeatvar == frame) |> - dplyr::select(constraint) - ) - - ## We start with the generation of the main frame - ## Build the main table and initiate it an index..... - main <- dplyr::tibble( - index = paste0( "ID-", - purrr::as_vector( - purrr::map(n, sample(LETTERS, 4)) |> - purrr::map(paste0, collapse = "")), - "-", - formatC(1:n, width = nchar(n) + 1, flag = "0") - )) - names(main)[1] <- "_index" - - ## then apply fct_var_dummy interactively... - for(i in (1:nrow(conf)) ) { - # i <- 6 - cat(paste0(i, "-", conf[i, c("type")], "-", conf[i, c("name")], "\n")) - - ## manage specific case when list name is not defined in ccoices but pulled from data.. - ## in such case we replace type by select_one by text - this.type <- conf[i, c("type")] - this.name <- conf[i, c("name")] - this.listname <- conf[i , c("list_name")] - this.constraint = conf[i, c("constraint")] - - if( !(this.listname %in% c( dico[["modalities"]] |> dplyr::pull(list_name))) ) { - this.listname <- NULL - this.type <- "text" - } - - main <- fct_var_dummy( - frame = main, - name = this.name, - type = this.type, - list_opt = c(dico[["modalities"]] |> - dplyr::filter(list_name == this.listname ) |> - dplyr::pull(name) ), - constraint = this.constraint ) - } - - ## Still struggling with purrr - # test <- purrr::map( main, - # name, - # type, - # list_opt$list_opt , - # constraint , - # fct_var_dummy) - - ## TODO -- apply the relevance statement... - ## define when the variable should exist based on other elements - ## Need to convert the xlsform statement into R syntax - # relevant <- dico[["variables"]]|> - # dplyr::filter(repeatvar == "main") |> - # dplyr::pull(relevant) - - ## replace \" by ' - ## replace '=' by '==" - ## replace '!==' by '!=" (to fix previsous replace) - ## replace '${' '}' with nothing.. - ## replace 'or' with '|'.. - ## replace 'and' with '&'.. - - ## Now store in the list... - datalist <- list ( "main" = main) - - ## treat cases with repeat table.. ##### - repeatvar <- dico[["variables"]] |> - dplyr::select(repeatvar) |> - dplyr::filter(repeatvar != "main") |> - dplyr::distinct() |> - dplyr::pull() - ## need to separate the different repeat elements wthin the form... - for ( rep in repeatvar ) { - - confrep <- data.frame( - name <- dico[["variables"]] |> - dplyr::filter(repeatvar == rep) |> - dplyr::select(name), - - # dico[["variables"]]|> - # dplyr::filter(repeatvar == rep) |> - # dplyr::distinct(type) - type <- dico[["variables"]]|> - dplyr::filter(repeatvar == rep) |> - dplyr::select(type), - - ## pulling list options... - list_opt <- dico[["variables"]] |> - dplyr::filter(repeatvar == rep) |> - dplyr::select(list_name) , - #dplyr::left_join(modal, by = c("list_name")) |> - # dplyr::select( list_opt) - - - constraint <- dico[["variables"]]|> - dplyr::filter(repeatvar == rep) |> - dplyr::select(constraint) - ) - - ## We start with the generation of the main frame - ## Build the main table and initiate it an index..... - repframe <- dplyr::tibble( - index = paste0( "ID-", - purrr::as_vector( - purrr::map(n, sample(LETTERS, 4)) |> - purrr::map(paste0, collapse = "")), - "-", - formatC(1:n, width = nchar(n) + 1, flag = "0") - )) - - names(repframe)[1] <- "_parent_index" - ## Apply now `_index` - ## TODO -- check if we have a `repeat_count` to apply limitation.. - - ## then apply fct_var_dummy interactively... - for(i in (1:nrow(confrep)) ) { - # i <- 6 - cat(paste0(i, " in repeat -", rep, " ///", confrep[i, c("type")], "-", confrep[i, c("name")], "\n")) - - ## manage specific case when list name is not defined in ccoices but pulled from data.. - ## in such case we replace type by select_one by text - this.type <- confrep[i, c("type")] - this.name <- confrep[i, c("name")] - this.listname <- confrep[i , c("list_name")] - this.constraint = confrep[i, c("constraint")] - - if( !(this.listname %in% c( dico[["modalities"]] |> dplyr::pull(list_name))) ) { - this.listname <- NULL - this.type <- "text" - } - - repframe <- fct_var_dummy( - frame = repframe, - name = this.name, - type = this.type, - list_opt = c(dico[["modalities"]] |> - dplyr::filter(list_name == this.listname ) |> - dplyr::pull(name) ), - constraint = this.constraint ) - } - - - ## append to the repeat - datalist[[rep]] <- repframe - } - - - +#' +#' @param country iso3 code for the country (easier to recall than the M49 used in the API) +#' @param operation operation name +#' @param year year to use to extract the baseline from Population Statistics +#' @param population_type The list of population type for baseline calculation +#' @param population_rms The list of population type covered by RMS +#' @param rms_indicator list with indicators and their related frame to pull the value +#' @param ridl name of ridl data container to push the data to +#' @param publish yes / no +#' +#' @import refugees +#' @importFrom janitor clean_names +#' +#' @return frame with all compass indicators +#' +#' @export +#' +fct_compass_table <- function(country , + operation, + year , + population_type, + population_rms, + rms_indicator, + ridl, + publish) { +# +# +# totalBaseline <- refugees::population |> +# filter(CountryAsylumCode == country, +# Year == 2022 , +# Population.type %in% population_type) |> +# group_by(CountryAsylumName) |> +# summarise(Value = sum(Value, na.rm = TRUE)) |> +# ungroup() |> +# dplyr::pull( Value) +# +# rms_indicator = as.data.frame(rms_indicator) +# +# ## Initialise the data frame +# compass <- data.frame( t(c( a = operation, +# b = population_rms, +# c = "Percent", +# d = totalBaseline, +# e = 10, +# f = 10, +# g = "XXX" )) ) +# +# ## append all the values +# for (i in (1:nrow(rms_indicator))) { +# # i <- 3 +# t <- as.data.frame(eval(parse(text= paste0("table( datalist[[\"", +# rms_indicator[i,1] , +# "\"]]$", +# rms_indicator[i, 2] ,")" ) ))) +# #cat( paste0(rms_indicator[i,3] , "\n")) +# +# #class(t) +# if( nrow(t) == 0) { +# t1 <- 0 } else if( nrow(t |> dplyr::filter(Var1 ==1 )) == 1) { +# t1 <- eval(parse(text= paste0("as.data.frame( prop.table(table( datalist[[\"", +# rms_indicator[i,1],"\"]]$", +# rms_indicator[i,2],"))) |> +# dplyr::filter(Var1 ==1 ) |> dplyr::pull(Freq)"))) } else { +# t1 <- 0} +# compass1 <- as.data.frame(t(c( a = operation, +# b = population_rms, +# c = "Percent", +# d = totalBaseline, +# e = round(t1 * 100,2), +# f = round(totalBaseline *t1), +# g = rms_indicator[i,3] ))) +# #str(compass) +# #str(compass1) +# compass <- rbind( compass,compass1) +# rm(compass1) +# } +# +# compass <- compass |> +# as.data.frame() |> +# dplyr::slice(-1) |> +# dplyr::mutate (Plan = a , +# `Population Type (operational)` = b , +# `Show As` = c, +# `Baseline Num.` = f, +# `Baseline Den.` = d, +# `%` = e , +# `Indicator` = g)|> +# dplyr::select(Plan, +# `Indicator`, +# `Population Type (operational)`, +# `Show As`, +# `Baseline Num.`, +# `Baseline Den.`, +# `%` ) +# +# ## And now saving +# ## Create a new workbook +# wb <- openxlsx::createWorkbook() +# ## add the cleaning log to the file +# openxlsx::addWorksheet(wb, "Compass") +# openxlsx::writeData(wb, "Compass", +# compass, withFilter = TRUE) +# ## Save workbook +# openxlsx::saveWorkbook(wb, +# file = here::here(paste0("compass_", +# country,"_", +# stringr::str_replace_all(string=population_rms, pattern=" ", repl=""), +# ".xlsx") ), +# overwrite = TRUE) +# +# +# ## Now push to RILD +# if( publish == "yes"){ +# p <- riddle::dataset_show(param$ridl) +# list_of_resources <- p[["resources"]][[1]] +# +# time <- format(Sys.Date(), '%d%b%y') +# ### Publish the analysis plan #### +# namecompass = paste0("compass_", country,"_", +# stringr::str_replace_all(string=population_rms, pattern=" ", repl="")) +# ### Check if the name is already in the resources +# if(namecompass %in% list_of_resources$name) { +# ## get the resource id +# resourceid <- list_of_resources |> +# dplyr::filter ( name == namecompass) |> +# dplyr::pull(id) +# ## get the new resource version +# curversion <- list_of_resources |> +# dplyr::filter ( name == namecompass) |> +# dplyr::pull(version) +# +# ## Build resource metadata +# metadatacompass <- riddle::resource_metadata( +# type = "attachment", +# url = paste0(namecompass, ".xlsx"), +# name = namecompass, +# description = paste0("Compass output generated from RMS on ", time, +# ". Built using kobocruncher "), +# format = "xlsx", +# version = (curversion + 1), +# visibility = "public", +# file_type = "other", +# ## Revise here based on the name from your crunching report +# upload = httr::upload_file(here::here(paste0(namecompass, ".xlsx"))) +# ) +# riddle::resource_update(id = resourceid, +# res_metadata = metadatacompass) +# } else { +# +# metadatacompass <- riddle::resource_metadata( +# type = "attachment", +# url = paste0(namecompass, ".xlsx"), +# name = namecompass, +# description = paste0("Compass output generated from RMS on ", time, +# ". Built using kobocruncher "), +# format = "xlsx", +# visibility = "public", +# file_type = "other", +# ## Revise here based on the name from your crunching report +# upload = httr::upload_file(here::here(paste0(namecompass, ".xlsx"))) +# ) +# riddle::resource_create(package_id = p$id, +# res_metadata = metadatacompass) +# } +# +# } +# +# return(compass) - return(datalist) - } +} ``` -```{r example-fct_kobo_dummy} -form <- system.file("RMSCAPI.xlsx", package = "IndicatorCalc") -datalist <- fct_kobo_dummy(form, - n = 384, - file = NULL) - -## Save this to use it for testing the package... - -openxlsx::write.xlsx(datalist, here::here("inst", "demo_data.xlsx")) +```{r example-fct_compass_table} +# compass <- export_compass_fill( country = "ECU", +# operation = "Ecuador ABC", +# year = 2022, +# population_type = c("REF","ASY", "OIP"), +# population_rms = "Refugees and Asylum-seekers", +# rms_indicator = rbind( +# c("main", "impact2_2", "2.2 Proportion of PoCs residing in physically safe and +# secure settlements with access to basic facilities"), +# c("main", "impact2_3", "2.3 Proportion of PoC with access to health services"), +# c("P2.S3", "impact3_2a", "3.2a Proportion of PoC enrolled in primary education" ), +# c("P2.S3", "impact3_2b", "3.2b Proportion of PoC enrolled in secondary education" ), +# c("main", "impact3_3", "3.3 Proportion of PoC feeling safe walking alone in their neighborhood (related SDG 16.1.4)." ), +# c("S2", "outcome1_2", "1.2 Proportion of children under 5 years of age whose births +# have been registered with a civil authority. [SDG 16.9.1 - Tier 1]" ), +# c("S2", "outcome1_3", "1.3 Proportion of PoC with legally recognized identity documents or credentials [GCR 4.2.2]." ), +# c("main", "outcome4_1", "4.1 Proportion of PoC who know where to access available GBV services." ), +# c("main", "outcome4_2", "4.2 Proportion of POCs who do not accept violence against women." ), +# c("main", "outcome8_2", "8.2 Proportion of PoC with primary reliance on clean (cooking) fuels and technology [SDG 7.1.2 Tier 1]" ), +# c("main", "outcome9_1", "9.1 Proportion of PoCs living in habitable and affordable housing." ), +# c("main", "outcome9_2", "9.2 Proportion of PoC that have energy to ensure lighting (close to Sphere)." ), +# c("main","outcome12_1", "12.1 Proportion of PoC using at least basic drinking water services (SDG)." ), +# # c("main" , "outcome12_2", "12.2 Proportion of PoC with access to a safe household toilet (SDG)." ), +# c("main", "outcome13_1", "13.1. Proportion of PoC with an account at a bank or other +# financial institution or with a mobile-money-service provider [SDG 8.10.2 Tier 1]." ), +# c("main", "outcome13_2", "13.2. Proportion of PoC who self-report positive changes in their income compared to previous year." ), +# c("main", "outcome13_3", "13.3 Proportion of PoC (working age) who are unemployed." ), +# c("main", "outcome16_1", "16.1. Proportion of PoC with secure tenure rights and/or +# property rights to housing and/or land [revised SDG indicator 1.4.2]." )#, +# # c("main", "outcome16_2", "16.2. Proportion of PoC covered by social protection floors/systems [SDG 1.3.1]." ) +# ), +# ridl = params$ridl, +# publish = params$publish ) ``` -```{r tests-fct_kobo_dummy} -test_that("fct_kobo_dummy works", { - expect_true(inherits(fct_kobo_dummy, "function")) -}) +```{r tests-fct_compass_table} +#test_that("fct_compass_table works", { expect_true(inherits(fct_compass_table, "function")) }) ``` + + + # Utilities -## fct_require +## fct_build_requirement -```{r function-fct_require} -#' fct_require +```{r function-fct_build_requirement} +#' fct_build_requirement #' #' Write in a file the variable requirement for each single indicator functions #' -#' The function is used to conveniently keep the mapping between the excel tables used to show requirement by -#' each functions +#' The function is used to conveniently keep the mapping between the excel +#' tables used to show variable requirements for each indicator calculation #' +#' It expects a specific table within the xlsform called `Indicator_to_question` + +#' @param xlsformpath path to the xlsform #' @return print in console #' #' @export -fct_require <- function(){ - - - IndicMap <- readxl::read_excel( system.file("IndicMap.xlsx", package = "IndicatorCalc")) +fct_build_requirement <- function(xlsformpath){ + + mappers <- readxl::read_excel(xlsformpath, + sheet = "Indicator_to_question") |> + dplyr::select(IndName, MeasureLevel, QuestionVar) + + dico <- kobocruncher::kobo_dico( xlsformpath ) + + # View(dico[["variables"]]) + # names(dico[["variables"]]) + + mappers <- mappers |> + dplyr::left_join( dico[["variables"]] |> + dplyr::select(name, label, + type, list_name, name_or), + by= c("QuestionVar" = "name_or")) + + + IndicMap <- mappers |> + dplyr::left_join( dico[["modalities"]] |> + dplyr::select(name, label, list_name ) |> + dplyr::rename( name_mod = name, + label_mod = label), + by= c("list_name")) + + #writexl::write_xlsx(IndicMap, here::here( "inst", "IndicMap.xlsx")) + # IndicMap <- readxl::read_excel( system.file("IndicMap.xlsx", + # package = "IndicatorCalc")) ## Below is the format we are trying to replicate @@ -1721,80 +1905,99 @@ fct_require <- function(){ # ) # ) + filemap <- tempfile() + filemaplab <- tempfile() listind <- IndicMap |> dplyr::select(IndName) |> dplyr::distinct() |> dplyr::pull() - if (file.exists("dev/mapper.R")) file.remove("dev/mapper.R") + if (file.exists(filemap)) file.remove(filemap) ## Roving around indicators for ( thisInd in listind ) { # thisInd <- listind[1] - cat( paste0("# --- ", thisInd, "\n"), file = "dev/mapper.R" , sep = "\n", append = TRUE) - cat( paste0( "# -----------------------------\n"), file = "dev/mapper.R" , sep = "\n", append = TRUE) - cat( paste0( "mapper = list("), file = "dev/mapper.R" , sep = "\n", append = TRUE) + cat( paste0("# --- ", thisInd, "\n"), file = filemap , sep = "\n", append = TRUE) + cat( paste0( "# -----------------------------\n"), file = filemap , sep = "\n", append = TRUE) + cat( paste0( "mapper = list("), file = filemap , sep = "\n", append = TRUE) cat( paste0( " hierarchy = \"", IndicMap |> dplyr::filter(IndName == thisInd) |> dplyr::select(MeasureLevel) |> dplyr::distinct() |> dplyr::pull(), - "\","), file = "dev/mapper.R" , sep = "\n", append = TRUE) + "\","), file = filemap , sep = "\n", append = TRUE) ## Mapping variables thisvar <- IndicMap |> dplyr::filter(IndName == thisInd) |> dplyr::select(QuestionVar, label) |> dplyr::distinct() - cat( paste0( " variablemap = data.frame("), file = "dev/mapper.R" , sep = "\n", append = TRUE) - cat( paste0( " label = "), file = "dev/mapper.R" , sep = "\n", append = TRUE) + cat( paste0( " variablemap = data.frame("), file = filemap , sep = "\n", append = TRUE) + cat( paste0( " label = "), file = filemap , sep = "\n", append = TRUE) ## Looping around labels - dput( thisvar |> dplyr::pull(label) , file = "dev/lab.R" ) - file_str <- paste(readLines("dev/lab.R"), collapse="\n") - cat( paste0(file_str), file = "dev/mapper.R" , sep = "\n", append = TRUE) - cat( paste0( " ,"), file = "dev/mapper.R" , sep = "\n", append = TRUE) - cat( paste0( " variable = "), file = "dev/mapper.R" , sep = "\n", append = TRUE) + dput( thisvar |> dplyr::pull(label) , file = filemaplab ) + file_str <- paste(readLines(filemaplab), collapse="\n") + cat( paste0(file_str), file = filemap , sep = "\n", append = TRUE) + cat( paste0( " ,"), file = filemap , sep = "\n", append = TRUE) + cat( paste0( " variable = "), file = filemap , sep = "\n", append = TRUE) ## Looping around variable - dput( thisvar |> dplyr::pull(QuestionVar), file = "dev/lab.R" ) - file_str <- paste(readLines("dev/lab.R"), collapse="\n") - cat( paste0(file_str), file = "dev/mapper.R" , sep = "\n", append = TRUE) - cat( paste0( " ),"), file = "dev/mapper.R" , sep = "\n", append = TRUE) + dput( thisvar |> dplyr::pull(QuestionVar), file = filemaplab ) + file_str <- paste(readLines(filemaplab), collapse="\n") + cat( paste0(file_str), file = filemap , sep = "\n", append = TRUE) + cat( paste0( " ),"), file = filemap , sep = "\n", append = TRUE) ## Mapping modality thismod <- IndicMap |> dplyr::filter(IndName == thisInd) |> dplyr::select(QuestionVar, label_mod, name_mod) |> dplyr::filter(! (is.null(name_mod)) ) |> dplyr::distinct() - cat( paste0( " modalitymap = data.frame("), file = "dev/mapper.R" , sep = "\n", append = TRUE) - cat( paste0( " variable = "), file = "dev/mapper.R" , sep = "\n", append = TRUE) + cat( paste0( " modalitymap = data.frame("), file = filemap , sep = "\n", append = TRUE) + cat( paste0( " variable = "), file = filemap , sep = "\n", append = TRUE) ## Looping around variable for modalities - dput( thismod |> dplyr::pull(QuestionVar) , file = "dev/lab.R" ) - file_str <- paste(readLines("dev/lab.R"), collapse="\n") - cat( paste0(file_str), file = "dev/mapper.R" , sep = "\n", append = TRUE) - cat( paste0( " ,"), file = "dev/mapper.R" , sep = "\n", append = TRUE) - cat( paste0( " label = "), file = "dev/mapper.R" , sep = "\n", append = TRUE) + dput( thismod |> dplyr::pull(QuestionVar) , file = filemaplab ) + file_str <- paste(readLines(filemaplab), collapse="\n") + cat( paste0(file_str), file = filemap , sep = "\n", append = TRUE) + cat( paste0( " ,"), file = filemap , sep = "\n", append = TRUE) + cat( paste0( " label = "), file = filemap , sep = "\n", append = TRUE) ## Looping around labels for modalities - dput( thismod |> dplyr::pull(label_mod) , file = "dev/lab.R" ) - file_str <- paste(readLines("dev/lab.R"), collapse="\n") - cat( paste0(file_str), file = "dev/mapper.R" , sep = "\n", append = TRUE) - cat( paste0( " ,"), file = "dev/mapper.R" , sep = "\n", append = TRUE) - cat( paste0( " standard = "), file = "dev/mapper.R" , sep = "\n", append = TRUE) + dput( thismod |> dplyr::pull(label_mod) , file = filemaplab ) + file_str <- paste(readLines(filemaplab), collapse="\n") + cat( paste0(file_str), file = filemap , sep = "\n", append = TRUE) + cat( paste0( " ,"), file = filemap , sep = "\n", append = TRUE) + cat( paste0( " standard = "), file = filemap , sep = "\n", append = TRUE) ## Looping around standard for modalities - dput( thismod |> dplyr::pull(name_mod) , file = "dev/lab.R" ) - file_str <- paste(readLines("dev/lab.R"), collapse="\n") - cat( paste0(file_str), file = "dev/mapper.R" , sep = "\n", append = TRUE) - cat( paste0( " ))"), file = "dev/mapper.R" , sep = "\n", append = TRUE) - cat( paste0( "# -----------------------------\n"), file = "dev/mapper.R" , sep = "\n", append = TRUE) + dput( thismod |> dplyr::pull(name_mod) , file = filemaplab ) + file_str <- paste(readLines(filemaplab), collapse="\n") + cat( paste0(file_str), file = filemap , sep = "\n", append = TRUE) + cat( paste0( " ))"), file = filemap , sep = "\n", append = TRUE) + cat( paste0( "# -----------------------------\n"), file = filemap , sep = "\n", append = TRUE) } - + cat(filemap) + return(IndicMap) } ``` -```{r example-fct_require} +```{r example-fct_build_requirement} ## Write in dev/mapper.R each of the function parameters - as recorded in system.file("IndicMap.xlsx", package = "IndicatorCalc") -fct_require() +xlsformpath <- system.file("RMS_CAPI_v2.xlsx", package = "IndicatorCalc") +RMS_CAPI_v2_mapper <- fct_build_requirement(xlsformpath ) +# openxlsx::write.xlsx(RMS_CAPI_v2_mapper, here::here("inst", "RMS_CAPI_v2_mapper.xlsx")) + +xlsformpath <- system.file("RMS_CAPI_v3.xlsx", package = "IndicatorCalc") +RMS_CAPI_v3_mapper <- fct_build_requirement(xlsformpath ) +# openxlsx::write.xlsx(RMS_CAPI_v3_mapper, here::here("inst", "RMS_CAPI_v3_mapper.xlsx")) + +## CATI +xlsformpath <- system.file("RMS_CATI_v0.xlsx", package = "IndicatorCalc") +RMS_CATI_v0_mapper <- fct_build_requirement(xlsformpath ) +# openxlsx::write.xlsx(RMS_CATI_v0_mapper, here::here("inst", "RMS_CATI_v0_mapper.xlsx")) + +xlsformpath <- system.file("RMS_CATI_v3.xlsx", package = "IndicatorCalc") +RMS_CATI_v3_mapper <- fct_build_requirement(xlsformpath ) +# openxlsx::write.xlsx(RMS_CATI_v3_mapper, here::here("inst", "RMS_CATI_v3_mapper.xlsx")) + ``` -```{r tests-fct_require} -test_that("fct_require works", { - expect_true(inherits(fct_require, "function")) +```{r tests-fct_build_requirement} +test_that("fct_build_requirement works", { + expect_true(inherits(fct_build_requirement, "function")) }) ``` diff --git a/docs/404.html b/docs/404.html index defb26b..5be4d99 100644 --- a/docs/404.html +++ b/docs/404.html @@ -19,7 +19,6 @@ - Contributor Covenant Code of Conduct • IndicatorCalcContributor Covenant Code of Conduct • IndicatorCalc @@ -24,6 +24,7 @@ @@ -46,7 +47,7 @@
diff --git a/docs/LICENSE-text.html b/docs/LICENSE-text.html index 9e13160..4ff27cf 100644 --- a/docs/LICENSE-text.html +++ b/docs/LICENSE-text.html @@ -1,5 +1,5 @@ -License • IndicatorCalcLicense • IndicatorCalc @@ -24,6 +24,7 @@ @@ -46,7 +47,7 @@
diff --git a/docs/LICENSE.html b/docs/LICENSE.html index dd42165..7e7ff7b 100644 --- a/docs/LICENSE.html +++ b/docs/LICENSE.html @@ -1,5 +1,5 @@ -MIT License • IndicatorCalcMIT License • IndicatorCalc @@ -24,6 +24,7 @@ @@ -46,7 +47,7 @@
diff --git a/docs/articles/development.html b/docs/articles/development.html index 363d82d..243263d 100644 --- a/docs/articles/development.html +++ b/docs/articles/development.html @@ -7,6 +7,12 @@ Development • IndicatorCalc + + + + + + @@ -44,6 +50,7 @@
-

fct_re_map +

impact3_2a

-mapper = list(
-            hierarchy = "main",
-            variablemap = data.frame(
-              label = c("Does this household use anything for lighting?",
-                        "What source of electricity is used most of the time in this household?"),
-              variable = c("LIGHT01", 
-                           "LIGHT03"),
-              mappattern = c("LIGHT01", 
-                           "LIGHT03") ),
-            modalitymap = data.frame(
-              variable = c( "LIGHT01", 
-                            "LIGHT03", "LIGHT03", "LIGHT03"),
-              label = c( "yes",
-                "No electricity in household", "Other, specify", "Don't know"),
-              standard = c( "1",
-                           "1", "96", "98"),
-              map = c("yes",
-                      "Noelec", "Other", "Dontknow")
-            )
-          )
-
-
-## One variable is not correctly 
-datalist <- list(mainhousehold = data.frame(
-                group.LIGHT01 = c("yes",  "yes",  "no", "yes",  "yes",
-                                  "no", "yes",  "yes",  "yes"),
-                group.LIGHT03 = c("Noelec", "Other", "Dontknow", "Noelec", "Other",
-                                  "Dontknow", "Nuclear", "Other", "Dontknow"))
-             )
-datalist <- fct_re_map(datalist = datalist, mapper = mapper )
-#> Mapped levels for LIGHT01 are now: 1, no
-#> Mapped levels for LIGHT03 are now: 1, 96, 98, Nuclear
-
-fct_check_map(datalist = datalist, mapper = mapper )
-#> LIGHT01 is in the dataset and has at least one of the expected modality for calculation
-#> LIGHT03 is in the dataset and has at least one of the expected modality for calculation
+## data, cf example fct_re_map() +datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", + package = "IndicatorCalc")) +#> Warning: Unknown or uninitialised column: `_parent_index`. +## Apply calculation +datalist <- impact3_2a(datalist ) +#> EDU01 +#> EDU02 +#> EDU03 +#> EDU04 +#> HH07 misses responses options among : NA +#> There are missing data requirement to calculate Indicator Impact 3.2.a + +## Visualise value +fct_plot_indic_donut(indicator = datalist[["ind"]]$impact3_2a, + iconunicode = "f140") +#> Warning: Unknown or uninitialised column: `impact3_2a`. +#> No value was supplied for plotting...
-

fct_plot_indic_donut +

impact3_2b

-test <- data.frame(
-  shelter = rbinom(20, 1, 0.5)) |> 
-  dplyr::mutate( shelter = 
-  labelled::labelled( shelter,
-                      labels = c( "Yes" = 1, "No" = 0),
-                      label = "Access to adequate shelter")) 
-  
-fct_plot_indic_donut(indicator = test$shelter,
-                     subtitle_chart = NULL,
-                     caption_chart = NULL,
-                     ordered_threhold = NULL,
-                     iconunicode = "e54f") 
-

-
+## data, cf example fct_re_map() +datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", + package = "IndicatorCalc")) +#> Warning: Unknown or uninitialised column: `_parent_index`. +## Apply calculation +datalist <- impact3_2b(datalist ) +#> EDU01 +#> EDU02 +#> EDU03 +#> EDU04 +#> HH07 misses responses options among : NA +#> There are missing data requirement to calculate Indicator Impact 3.2.b + +## Visualise value +fct_plot_indic_donut(indicator = datalist[["ind"]]$impact3_2b, + iconunicode = "f140") +#> Warning: Unknown or uninitialised column: `impact3_2b`. +#> No value was supplied for plotting...
-
-

Impact Indicators -

-

inter_electricity +

impact3_3

-datalist <- kobocruncher::kobo_data( system.file("test.xlsx", 
+## data, cf example  fct_re_map()
+datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", 
                                                  package = "IndicatorCalc"))
-#> Warning: Expecting logical in G1286 / R1286C7: got 'no'
-mapper <- list(
-            hierarchy = "main",
-            variablemap = data.frame(
-              label = c("Does this household use anything for lighting?",
-       "What source of electricity is used most of the time in this household?"),
-              variable = c("LIGHT01", 
-                           "LIGHT03"),
-              mappattern = c("LIGHT01", 
-                           "LIGHT03") ),
-            modalitymap = data.frame(
-              variable = c( "LIGHT01", 
-                            "LIGHT03", "LIGHT03", "LIGHT03"),
-              label = c( "yes",
-                        "No electricity in household", "Other, specify", "Don't know"),
-              standard = c( "1",
-                           "1", "96", "98"),
-              map = c("yes",
-                      "1", "96", "98"))) 
-
-datalist <- inter_electricity( datalist =datalist, mapper = mapper  )
-#>  LIGHT01 standard variable was not found in the dataset.
-#>  LIGHT03 standard variable was not found in the dataset.
-#> Mapped levels for LIGHT01 are now: 1, no, notell
-#>  LIGHT03 variable has more than one variable pattern match in the dataset. We will take the first one but good to check...
-#> Mapped levels for LIGHT03 are now: 1, 2, 3, 4, 5, 6, 7, 96, 98
-
-
-table(datalist[["main"]]$electricity)
-#> 
-#>    0    1 
-#>   81 1209
-fct_plot_indic_donut(indicator = datalist[["main"]]$electricity,
-                     iconunicode = "f0e7") 
-

+#> Warning: Unknown or uninitialised column: `_parent_index`. +## Apply calculation +datalist <- impact3_3(datalist) +#> SAF01 + +## Visualise value +fct_plot_indic_donut(indicator = datalist[["ind"]]$impact3_3, + iconunicode = "f140") +#> Warning: Unknown or uninitialised column: `impact3_3`. +#> No value was supplied for plotting...
+
+
+

Outcome indicators +

-

inter_healthcare +

outcome1_2

-datalist <- kobocruncher::kobo_data( system.file("test.xlsx",
+## data, cf example  fct_re_map()
+datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", 
                                                  package = "IndicatorCalc"))
-#> Warning: Expecting logical in G1286 / R1286C7: got 'no'
-
-mapper <-  list(
-            hierarchy = "main",
-            variablemap = data.frame(
-              label = c(
-"In general, when anyone in your household is sick, where do they go to seek care?",
-"How long it takes to go there when you use the mode of transport mentioned above?"),
-              variable = c("HEA01", 
-                           "HEA03"),
-              mappattern = c("HEA01", 
-                           "HEA03") ),
-            modalitymap = data.frame(
-              variable = c( "HEA01", "HEA01" ),
-              label = c(  "Other, specify", "Don't know"),
-              standard = c("96", "98" ),
-              map = c("96", "98" )))
-
-datalist <- inter_healthcare(datalist, mapper )
-#>  HEA01 standard variable was not found in the dataset.
-#>  HEA03 standard variable was not found in the dataset.
-#>  HEA01 variable has more than one variable pattern match in the dataset. We will take the first one but good to check...
-#> Mapped levels for HEA01 are now: 1, 2, 3, 4, 5, 96, 98
-#> Mapped levels for HEA03 are now: 0, 1, 10, 100, 12, 120, 13, 15, 18, 2, 20, 22, 25, 3, 30, 300, 35, 4, 40, 45, 5, 50, 55, 6, 60, 7, 70, 75, 8, 90, 900
-
-table(datalist[["main"]]$healthcare)
-#> 
-#>    0    1 
-#>   93 1197
-fct_plot_indic_donut(indicator = datalist[["main"]]$healthcare,
-                     iconunicode = "f479") 
-

+#> Warning: Unknown or uninitialised column: `_parent_index`. + +## Apply indicator function on datalist +datalist <- outcome1_2(datalist) +#> REG03 +#> REG04 +#> HH07 misses responses options among : NA +#> There are missing data requirement to calculate Indicator Outcome 1.2 +table(datalist[["ind"]]$outcome1_2, useNA = "ifany") +#> Warning: Unknown or uninitialised column: `outcome1_2`. +#> < table of extent 0 > +## Visualise value +fct_plot_indic_donut(indicator = datalist[["ind"]]$outcome1_2, + iconunicode = "f140") +#> Warning: Unknown or uninitialised column: `outcome1_2`. +#> No value was supplied for plotting... +fct_plot_indic_donut(indicator = datalist[["ind"]]$birthCertificate, + iconunicode = "f140") +#> Warning: Unknown or uninitialised column: `birthCertificate`. +#> No value was supplied for plotting... +fct_plot_indic_donut(indicator = datalist[["ind"]]$birthRegistered, + iconunicode = "f140") +#> Warning: Unknown or uninitialised column: `birthRegistered`. +#> No value was supplied for plotting...
-

inter_drinkingwater +

outcome1_3

-datalist <- kobocruncher::kobo_data( system.file("test.xlsx",
+## data, cf example  fct_re_map()
+datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", 
                                                  package = "IndicatorCalc"))
-#> Warning: Expecting logical in G1286 / R1286C7: got 'no'
-
-## in the contextualised form - DWA03a has been skipped and all results are in min... 
-## only manual transformation can adjust this before we use the mapper..
-
-datalist[["main"]]$DWA03a  <- "1" 
-
-datalist[["main"]]$DWA03b <- 
-  datalist[["main"]]$VulnerabilityScoring.BasicNeeds.DWA03
-
-# now the mapper
-mapper <-  list(
-            hierarchy = "main",
-            variablemap = data.frame(
-              label = c(
-   "What is the main source of drinking water for this household?",
-  "Where is this source located?",
-  "Unit used to measure time to access",
-  "How long does it take to go there, wait get water, and come back?"),
-              variable = c("DWA01", 
-                           "DWA02", 
-                           "DWA03a", 
-                           "DWA03b" ),
-              mappattern = c("DWA01", 
-                           "DWA02", 
-                           "DWA03a", 
-                           "DWA03b" ) ),
-            modalitymap = data.frame(
-             variable = c("DWA01",  "DWA01", "DWA01","DWA01",  "DWA01", 
-                   "DWA02","DWA02", "DWA02",
-                   "DWA03a","DWA03a"),
-             label = c( 
-                ##DWA01
-                "Unprotected Dug Well", 
-                "Unprotected Spring",
-                "Surface Water (River, Stream, Pond, Dam, Canal)",
-                "Other, specify",
-                "Don't know",
-                ##DWA02
-                "In Own Dwelling", 
-                "In Own Yard/Plot",
-                "Elsewhere",
-                ## DWA03a
-                "Minutes", 
-                "Hours"    ),
-             standard = c( "7", "9", "13", "96", "98",
-                           "1", "2", "3",
-                           "1", "2"),
-             map = c( "7", "9", "13", "96", "98",
-                       "1", "2", "3",
-                        "1", "2") ) 
-         )
-  
-datalist <- inter_drinkingwater(datalist, mapper )
-#>  DWA01 standard variable was not found in the dataset.
-#>  DWA02 standard variable was not found in the dataset.
-#> DWA03a is in the dataset and has at least one of the expected modality for calculation
-#>  DWA03b standard variable in the dataset misses at least one response among :
-#>  DWA01 variable has more than one variable pattern match in the dataset. We will take the first one but good to check...
-#> The following `from` values were not present in `x`: 9
-#> Mapped levels for DWA01 are now: 1, 11, 12, 13, 14, 15, 2, 3, 4, 5, 7, 96, 98
-#>  DWA02 variable has more than one variable pattern match in the dataset. We will take the first one but good to check...
-#> Mapped levels for DWA02 are now: 1, 2, 3
-#> The following `from` values were not present in `x`: 2
-#> Mapped levels for DWA03a are now: 1
-#> Mapped levels for DWA03b are now: 0, 10, 15, 2, 3, 30, 5, 90
-
-## Indicator summary
-table(datalist[["main"]]$drinkingwater, useNA = "ifany")
-#> 
-#>    0    1 
-#> 1204   86
-fct_plot_indic_donut(indicator = datalist[["main"]]$drinkingwater,
-                     iconunicode = "e006") 
-

+#> Warning: Unknown or uninitialised column: `_parent_index`. +## Apply indicator function on datalist +datalist <- outcome1_3(datalist) +#> REG01a +#> REG01b +#> REG01c +#> REG01d +#> REG01e +#> REG01f +#> REG01g +#> REG02 +#> REG03 +#> REG05a +#> REG05b +#> REG05c +#> REG05d +#> REG05e +#> REG05f +#> REG06 + +## Visualise value +fct_plot_indic_donut(indicator = datalist[["ind"]]$outcome1_3, + iconunicode = "f140")
+

 
-## Check auxilliary
-table(datalist[["main"]]$dwa_cond1, useNA = "ifany")
-#> 
-#>    0    1 
-#>   69 1221
-table(datalist[["main"]]$reachableU30, useNA = "ifany")
-#> 
-#>    0    1 
-#>    1 1289
-table(datalist[["main"]]$DWA02, useNA = "ifany")
-#> 
-#>    1    2    3 <NA> 
-#>   42   34   11 1203
-table(datalist[["main"]]$dwa_cond2, useNA = "ifany")
+fct_plot_indic_donut(indicator = datalist[["ind"]]$document_above5,
+                     iconunicode = "f140")    
+

+
+
+fct_plot_indic_donut(indicator = datalist[["ind"]]$document_under5,
+                     iconunicode = "f140")    
+

+
+
+

outcome4_1 +

+
+## data, cf example  fct_re_map()
+datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", 
+                                                 package = "IndicatorCalc"))
+#> Warning: Unknown or uninitialised column: `_parent_index`.
+## Apply indicator function on datalist
+datalist <- outcome4_1(datalist )
+#>  GBV01a
+#>  GBV01b
+#>  GBV01c
+#>  GBV01d
+
+## Visualise value
+fct_plot_indic_donut(indicator = datalist[["main"]]$outcome1_4,
+                     iconunicode = "f140")   
+#> Warning: Unknown or uninitialised column: `outcome1_4`.
+#> No value was supplied for plotting...
+
+
+

outcome4_2 +

+
+## data, cf example  fct_re_map()
+datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", 
+                                                 package = "IndicatorCalc"))
+#> Warning: Unknown or uninitialised column: `_parent_index`.
+## Apply indicator function on datalist
+datalist <- outcome4_2(datalist)
+#>  VAW01a
+#>  VAW01b
+#>  VAW01c
+#>  VAW01d
+#>  VAW01e
+
+## Visualise value
+fct_plot_indic_donut(indicator = datalist[["main"]]$outcome4_2,
+                     iconunicode = "f140")   
+

+
+
+

outcome5_2 +

+
+## data, cf example  fct_re_map()
+datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", 
+                                                 package = "IndicatorCalc"))
+#> Warning: Unknown or uninitialised column: `_parent_index`.
+## Apply indicator function on datalist
+datalist <- outcome5_2(datalist  )
+#>  COMM01
+#>  COMM02 misses responses options among : NA
+#>  COMM03
+#>  COMM04
+#> There are missing data requirement to calculate Indicator Outcome 5.2
+
+## Visualise value
+fct_plot_indic_donut(indicator = datalist[["ind"]]$outcome5_2,
+                     iconunicode = "f140")   
+#> Warning: Unknown or uninitialised column: `outcome5_2`.
+#> No value was supplied for plotting...
+
+
+

outcome8_2 +

+
+## data, cf example  fct_re_map()
+datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", 
+                                                 package = "IndicatorCalc"))
+#> Warning: Unknown or uninitialised column: `_parent_index`.
+## Apply indicator function on datalist
+datalist <- outcome8_2(datalist )
+#>  COOK01
+#>  COOK02 not found in the dataset.
+#>  COOK03 not found in the dataset.
+#> There are missing data requirement to calculate Indicator Outcome 8.2
+
+## Visualise value
+fct_plot_indic_donut(indicator = datalist[["main"]]$outcome8_2,
+                     iconunicode = "f140")   
+#> Warning: Unknown or uninitialised column: `outcome8_2`.
+#> No value was supplied for plotting...
+
+
+

outcome9_1 +

+
+## data, cf example  fct_re_map()
+datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", 
+                                                 package = "IndicatorCalc"))
+#> Warning: Unknown or uninitialised column: `_parent_index`.
+## Apply indicator function on datalist
+datalist <- outcome9_1(datalist)
+#>  DWE01
+#>  DWE02
+#>  DWE03
+#>  DWE04
+#>  DWE05 misses responses options among : NA
+#>  DWE08
+#>  DWE09
+#>  HH01 misses responses options among : NA
+#> There are missing data requirement to calculate Indicator Outcome 9.1
+
+## Visualise value
+fct_plot_indic_donut(indicator = datalist[["main"]]$outcome9_1,
+                     iconunicode = "f140")   
+#> Warning: Unknown or uninitialised column: `outcome9_1`.
+#> No value was supplied for plotting...
+
+
+

outcome9_2 +

+
+## data, cf example  fct_re_map()
+datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", 
+                                                 package = "IndicatorCalc"))
+#> Warning: Unknown or uninitialised column: `_parent_index`.
+## Apply indicator function on datalist
+datalist <- outcome9_2(datalist )
+#>  LIGHT01
+#>  LIGHT02
+#>  LIGHT03
+
+## Visualise value
+fct_plot_indic_donut(indicator = datalist[["main"]]$outcome9_2,
+                     iconunicode = "f140")   
+

+
+
+

outcome10_1 +

+
+## data, cf example  fct_re_map()
+datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", 
+                                                 package = "IndicatorCalc"))
+#> Warning: Unknown or uninitialised column: `_parent_index`.
+
+## Apply indicator function on datalist
+datalist <- outcome10_1(datalist)
+#>  MMR03
+
+## Visualise value
+fct_plot_indic_donut(indicator = datalist[["ind"]]$outcome10_1,
+                     iconunicode = "f140")   
+

+
+
+

outcome10_2 +

+
+## data, cf example  fct_re_map()
+datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", 
+                                                 package = "IndicatorCalc"))
+#> Warning: Unknown or uninitialised column: `_parent_index`.
+## Apply indicator function on datalist
+datalist <- outcome10_2(datalist  )
+#>  BIR01
+#>  BIR02
+#>  BIR03
+#>  BIR04
+
+## Visualise value
+fct_plot_indic_donut(indicator = datalist[["main"]]$outcome10_2,
+                     iconunicode = "f140")   
+

+
+
+

outcome12_1 +

+
+## data, cf example  fct_re_map()
+datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", 
+                                                 package = "IndicatorCalc"))
+#> Warning: Unknown or uninitialised column: `_parent_index`.
+## Apply indicator function on datalist
+datalist <- outcome12_1(datalist  )
+#>  DWA01
+#>  DWA02
+#>  DWA03a
+#>  DWA03b misses responses options among : NA
+#>  DWA04
+#> There are missing data requirement to calculate Indicator Outcome 12.1
+
+## Visualise value
+fct_plot_indic_donut(indicator = datalist[["main"]]$outcome12_1,
+                     iconunicode = "f140")   
+#> Warning: Unknown or uninitialised column: `outcome12_1`.
+#> No value was supplied for plotting...
+
+
+

outcome12_2 +

+
+## data, cf example  fct_re_map()
+datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", 
+                                                 package = "IndicatorCalc"))
+#> Warning: Unknown or uninitialised column: `_parent_index`.
+## Apply indicator function on datalist
+datalist <- outcome12_2(datalist)
+#>  TOI01
+#>  TOI02
+#>  TOI03 not found in the dataset.
+#>  TOI04 not found in the dataset.
+#>  TOI05 not found in the dataset.
+#> There are missing data requirement to calculate Indicator Outcome 12.2
+
+## Visualise value
+fct_plot_indic_donut(indicator = datalist[["main"]]$outcome12_2,
+                     iconunicode = "f140")   
+#> Warning: Unknown or uninitialised column: `outcome12_2`.
+#> No value was supplied for plotting...
+
+
+

outcome13_1 - Financial Inclusion +

+
+## data, cf example  fct_re_map()
+datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", 
+                                                 package = "IndicatorCalc"))
+#> Warning: Unknown or uninitialised column: `_parent_index`.
+## Apply indicator function on datalist
+datalist <- outcome13_1(datalist )
+#>  BANK01
+#>  BANK02
+#>  BANK03
+#>  BANK04
+#>  BANK05
+
+## Visualise value
+fct_plot_indic_donut(indicator = datalist[["main"]]$outcome13_1,
+                     iconunicode = "f140")   
+

+
+
+

outcome13_2 +

+
+## data, cf example  fct_re_map()
+datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", 
+                                                 package = "IndicatorCalc"))
+#> Warning: Unknown or uninitialised column: `_parent_index`.
+## Apply indicator function on datalist
+datalist <- outcome13_2(datalist)
+#>  INC01
+
+## Visualise value
+fct_plot_indic_donut(indicator = datalist[["main"]]$outcome13_2,
+                     iconunicode = "f140")   
+

+
+
+

outcome13_3 +

+
+## data, cf example  fct_re_map()
+datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", 
+                                                 package = "IndicatorCalc"))
+#> Warning: Unknown or uninitialised column: `_parent_index`.
+## Apply indicator function on datalist
+datalist <- outcome13_3(datalist )
+#>  UNEM01
+#>  UNEM02
+#>  UNEM03
+#>  UNEM04
+#>  UNEM05
+#>  UNEM06
+#>  UNEM07
+#>  UNEM08
+#>  UNEM09
+#>  UNEM10
+
+table( datalist[["main"]]$outcome13_3, useNA = "ifany")
 #> 
 #>    0    1 <NA> 
-#>    1   86 1203
+#> 334 12 38 +## Visualise value +fct_plot_indic_donut(indicator = datalist[["main"]]$outcome13_3, + iconunicode = "f140")
+

-

inter_shelter +

outcome14_1

-
-datalist <- kobocruncher::kobo_data( system.file("test.xlsx", 
+
+## data, cf example  fct_re_map()
+datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", 
                                                  package = "IndicatorCalc"))
-#> Warning: Expecting logical in G1286 / R1286C7: got 'no'
-
-mapper <- list(
-            hierarchy = "main",
-            variablemap = data.frame(
-              label = c(
-                "What type of dwelling does the household live in?",
-"Main material of the dwelling floor",
-"Main material of the roof",
-"Main material of the exterior walls",
-"How many separate rooms do the members of your household occupy?",
-"What is the total number of persons in this household?"),
-              variable = c("DWE01","DWE02","DWE03","DWE04","DWE05", 
-                           "HH01"),
-              mappattern = c("DWE01","DWE02","DWE03","DWE04","DWE05", 
-                           "progres_groupsize") ),
-            modalitymap = data.frame(
-              variable = c( "DWE01","DWE01",
-                            "DWE02","DWE02","DWE02",
-                            "DWE03","DWE03","DWE03","DWE03","DWE03","DWE03",
-                            "DWE04","DWE04","DWE04","DWE04","DWE04","DWE04"),
-              label = c(  "Apartment", "House", # DWE01
-                         "Earth/sand", "Dung", "Other (Specify)", #DWE02
-                         
-                         "Metal/tin", "Wood", "Calamine/Cement fibre", 
-                         "Ceramic tiles", "Cement", "Roofing shingles",#DWE03
-                         
-                         "Cement", "Stone with lime/ cement", "Bricks", 
-                         "Cement blocks", "Covered adobe", "Wood planks/shingles" # DWE04
-                         ),
-              standard = c( "1","2",
-                           "1", "2","96",
-                           "8","9","10","11","12","13",
-                           "10","11","12","13","14","15"),
-              map = c("1","2",
-                           "1", "2","96",
-                           "8","9","10","11","12","13",
-                           "10","11","12","13","14","15"))) 
-## Calculate
-datalist <-  inter_shelter(datalist, mapper)
-#>  DWE01 standard variable was not found in the dataset.
-#>  DWE02 standard variable was not found in the dataset.
-#>  DWE03 standard variable was not found in the dataset.
-#>  DWE04 standard variable was not found in the dataset.
-#>  DWE05 standard variable was not found in the dataset.
-#>  HH01 standard variable was not found in the dataset.
-#>  DWE01 variable has more than one variable pattern match in the dataset. We will take the first one but good to check...
-#> Mapped levels for DWE01 are now: 1, 2, 3, 5, 6, 7, 8, 9, 96
-#>  DWE02 variable has more than one variable pattern match in the dataset. We will take the first one but good to check...
-#> Mapped levels for DWE02 are now: 1, 2, 3, 5, 6, 7, 8, 96
-#>  DWE03 variable has more than one variable pattern match in the dataset. We will take the first one but good to check...
-#> Mapped levels for DWE03 are now: 1, 10, 11, 12, 13, 2, 4, 6, 7, 8, 9, 96
-#>  DWE04 variable has more than one variable pattern match in the dataset. We will take the first one but good to check...
-#> Mapped levels for DWE04 are now: 1, 10, 11, 12, 13, 14, 15, 2, 3, 4, 5, 6, 7, 8, 9, 96
-#> Mapped levels for DWE05 are now: 1, 2, 3, 4, 5, 6
-#> Mapped levels for HH01 are now: 1, 10, 11, 12, 2, 3, 4, 5, 6, 7, 8, 9
-# Tabulate
-table(datalist[["main"]]$dwe01_cat)
-#> 
-#>   0   1 
-#> 639 651
-table(datalist[["main"]]$dwe02_cat)
-#> 
-#>    0    1 
-#>   55 1235
-table(datalist[["main"]]$dwe03_cat)
-#> 
-#>    0    1 
-#>  154 1136
-table(datalist[["main"]]$dwe04_cat)
-#> 
-#>    0    1 
-#>  189 1101
-table(datalist[["main"]]$dwe05_cat)
-#> 
-#>   0   1 
-#> 409 881
-table(datalist[["main"]]$shelter)
-#> 
-#>   0   1 
-#> 860 430
-#plot
-fct_plot_indic_donut(datalist[["main"]]$shelter,
-                     iconunicode = "e54f") 
-

+#> Warning: Unknown or uninitialised column: `_parent_index`. + +## Apply indicator function on datalist +datalist <- outcome14_1(datalist ) +#> REG01a +#> REG01b +#> REG01c +#> REG01d +#> REG01e +#> REG01f +#> REG01g +#> REG02 +#> REG03 +#> REG04 +#> REG05a +#> REG05b +#> REG05c +#> REG05d +#> REG05e +#> REG05f +#> REG06 + +## Visualise value +fct_plot_indic_donut(indicator = datalist[["ind"]]$outcome14_1, + iconunicode = "f140")
+

-

impact_2_2 +

outcome16_1

-
-datalist <- kobocruncher::kobo_data( system.file("test.xlsx",
+
+## data, cf example  fct_re_map()
+datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", 
                                                  package = "IndicatorCalc"))
-#> Warning: Expecting logical in G1286 / R1286C7: got 'no'
-
-#Healthcare
-mapper <-  list(
-            hierarchy = "main",
-            variablemap = data.frame(
-              label = c(
-  "In general, when anyone in your household is sick, 
-   where do they go to seek care?",
-  "How long does it take to go there when you use the mode of transport 
-  that you mentioned above?"),
-              variable = c("HEA01", 
-                           "HEA03"),
-              mappattern = c("HEA01", 
-                           "HEA03") ),
-            modalitymap = data.frame(
-              variable = c( "HEA01", "HEA01" ),
-              label = c(  "Other, specify", "Don't know"),
-              standard = c("96", "98" ),
-              map = c("96", "98" )))
-
-datalist <- inter_healthcare(datalist, mapper )
-#>  HEA01 standard variable was not found in the dataset.
-#>  HEA03 standard variable was not found in the dataset.
-#>  HEA01 variable has more than one variable pattern match in the dataset. We will take the first one but good to check...
-#> Mapped levels for HEA01 are now: 1, 2, 3, 4, 5, 96, 98
-#> Mapped levels for HEA03 are now: 0, 1, 10, 100, 12, 120, 13, 15, 18, 2, 20, 22, 25, 3, 30, 300, 35, 4, 40, 45, 5, 50, 55, 6, 60, 7, 70, 75, 8, 90, 900
-
-## Electricity
-mapper <- list(
-            hierarchy = "main",
-            variablemap = data.frame(
-              label = c("Does this household use anything for lighting?",
-       "What source of electricity is used most of the time in this household?"),
-              variable = c("LIGHT01", 
-                           "LIGHT03"),
-              mappattern = c("LIGHT01", 
-                           "LIGHT03") ),
-            modalitymap = data.frame(
-              variable = c( "LIGHT01", 
-                            "LIGHT03", "LIGHT03", "LIGHT03"),
-              label = c( "yes",
-                        "No electricity in household", "Other, specify", "Don't know"),
-              standard = c( "1",
-                           "1", "96", "98"),
-              map = c("yes",
-                      "1", "96", "98"))) 
-
-datalist <- inter_electricity( datalist =datalist, mapper = mapper  )
-#>  LIGHT01 standard variable was not found in the dataset.
-#>  LIGHT03 standard variable was not found in the dataset.
-#> Mapped levels for LIGHT01 are now: 1, no, notell
-#>  LIGHT03 variable has more than one variable pattern match in the dataset. We will take the first one but good to check...
-#> Mapped levels for LIGHT03 are now: 1, 2, 3, 4, 5, 6, 7, 96, 98
-
-## Drinking Water
-## in the contextualised form - DWA03a has been skipped and all results are in min... 
-## only manual transformation can adjust this before we use the mapper..
-
-datalist[["main"]]$DWA03a  <- "1" 
-
-datalist[["main"]]$DWA03b <- 
-  datalist[["main"]]$VulnerabilityScoring.BasicNeeds.DWA03
-
-# now the mapper
-mapper <-  list(
-            hierarchy = "main",
-            variablemap = data.frame(
-              label = c(
-               "What is the main source of drinking water for this household?",
-               "Where is this source located?",
-               "Unit used to measure time to access",
-               "How long does it take to go there, get water, and come back,
-                including waiting time?"),
-              variable = c("DWA01", 
-                           "DWA02", 
-                           "DWA03a", 
-                           "DWA03b" ),
-              mappattern = c("DWA01", 
-                           "DWA02", 
-                           "DWA03a", 
-                           "DWA03b" ) ),
-            modalitymap = data.frame(
-             variable = c("DWA01",  "DWA01", "DWA01","DWA01",  "DWA01", 
-                   "DWA02","DWA02", "DWA02",
-                   "DWA03a","DWA03a"),
-             label = c( 
-                ##DWA01
-                "Unprotected Dug Well", 
-                "Unprotected Spring",
-                "Surface Water (River, Stream, Pond, Dam, Canal)",
-                "Other, specify",
-                "Don't know",
-                ##DWA02
-                "In Own Dwelling", 
-                "In Own Yard/Plot",
-                "Elsewhere",
-                ## DWA03a
-                "Minutes", 
-                "Hours"    ),
-             standard = c( "7", "9", "13", "96", "98",
-                           "1", "2", "3",
-                           "1", "2"),
-             map = c( "7", "9", "13", "96", "98",
-                       "1", "2", "3",
-                        "1", "2") ) 
-         )
-  
-datalist <- inter_drinkingwater(datalist, mapper )
-#>  DWA01 standard variable was not found in the dataset.
-#>  DWA02 standard variable was not found in the dataset.
-#> DWA03a is in the dataset and has at least one of the expected modality for calculation
-#>  DWA03b standard variable in the dataset misses at least one response among :
-#>  DWA01 variable has more than one variable pattern match in the dataset. We will take the first one but good to check...
-#> The following `from` values were not present in `x`: 9
-#> Mapped levels for DWA01 are now: 1, 11, 12, 13, 14, 15, 2, 3, 4, 5, 7, 96, 98
-#>  DWA02 variable has more than one variable pattern match in the dataset. We will take the first one but good to check...
-#> Mapped levels for DWA02 are now: 1, 2, 3
-#> The following `from` values were not present in `x`: 2
-#> Mapped levels for DWA03a are now: 1
-#> Mapped levels for DWA03b are now: 0, 10, 15, 2, 3, 30, 5, 90
-
-##Shelter
-mapper <- list(
-            hierarchy = "main",
-            variablemap = data.frame(
-              label = c(
-                "What type of dwelling does the household live in?",
-"Main material of the dwelling floor",
-"Main material of the roof",
-"Main material of the exterior walls",
-"How many separate rooms do the members of your household occupy?",
-"What is the total number of persons in this household?"),
-              variable = c("DWE01","DWE02","DWE03","DWE04","DWE05", 
-                           "HH01"),
-              mappattern = c("DWE01","DWE02","DWE03","DWE04","DWE05", 
-                           "progres_groupsize") ),
-            modalitymap = data.frame(
-              variable = c( "DWE01","DWE01",
-                            "DWE02","DWE02","DWE02",
-                            "DWE03","DWE03","DWE03","DWE03","DWE03","DWE03",
-                            "DWE04","DWE04","DWE04","DWE04","DWE04","DWE04"),
-              label = c(  "Apartment", "House", # DWE01
-                         "Earth/sand", "Dung", "Other (Specify)", #DWE02
-                         
-                         "Metal/tin", "Wood", "Calamine/Cement fibre", 
-                         "Ceramic tiles", "Cement", "Roofing shingles",#DWE03
-                         
-                         "Cement", "Stone with lime/ cement", "Bricks", 
-                         "Cement blocks", "Covered adobe", "Wood planks/shingles" # DWE04
-                         ),
-              standard = c( "1","2",
-                           "1", "2","96",
-                           "8","9","10","11","12","13",
-                           "10","11","12","13","14","15"),
-              map = c("1","2",
-                           "1", "2","96",
-                           "8","9","10","11","12","13",
-                           "10","11","12","13","14","15"))) 
-## Calculate
-datalist <-  inter_shelter(datalist, mapper)
-#>  DWE01 standard variable was not found in the dataset.
-#>  DWE02 standard variable was not found in the dataset.
-#>  DWE03 standard variable was not found in the dataset.
-#>  DWE04 standard variable was not found in the dataset.
-#>  DWE05 standard variable was not found in the dataset.
-#>  HH01 standard variable was not found in the dataset.
-#>  DWE01 variable has more than one variable pattern match in the dataset. We will take the first one but good to check...
-#> Mapped levels for DWE01 are now: 1, 2, 3, 5, 6, 7, 8, 9, 96
-#>  DWE02 variable has more than one variable pattern match in the dataset. We will take the first one but good to check...
-#> Mapped levels for DWE02 are now: 1, 2, 3, 5, 6, 7, 8, 96
-#>  DWE03 variable has more than one variable pattern match in the dataset. We will take the first one but good to check...
-#> Mapped levels for DWE03 are now: 1, 10, 11, 12, 13, 2, 4, 6, 7, 8, 9, 96
-#>  DWE04 variable has more than one variable pattern match in the dataset. We will take the first one but good to check...
-#> Mapped levels for DWE04 are now: 1, 10, 11, 12, 13, 14, 15, 2, 3, 4, 5, 6, 7, 8, 9, 96
-#> Mapped levels for DWE05 are now: 1, 2, 3, 4, 5, 6
-#> Mapped levels for HH01 are now: 1, 10, 11, 12, 2, 3, 4, 5, 6, 7, 8, 9
-
-## and now impact
-
-mapper <-   list(
-            hierarchy = "main",
-            variablemap = data.frame(
-              label = c("Access to shelter", 
-                        "Access to electricity", 
-                         "Access to drinking water", 
-                         "Access to  healthcare"),
-              variable = c("shelter", 
-                           "electricity", 
-                           "drinkingwater", 
-                           "healthcare"),
-              mappattern = c("shelter", 
-                           "electricity", 
-                           "drinkingwater", 
-                           "healthcare") ),
-            modalitymap = data.frame(
-              variable = c( "shelter", "shelter",
-                           "electricity",  "electricity", 
-                           "drinkingwater", "drinkingwater", 
-                           "healthcare","healthcare"),
-              label = c( "Yes","No",
-                       "Yes","No",
-                       "Yes","No",
-                       "Yes","No"),
-              standard = c( "1","0",
-                           "1","0",
-                           "1","0",
-                           "1","0"),
-              map = c("1","0",
-                           "1","0",
-                           "1","0",
-                           "1","0")))
- 
-datalist <-  impact_2_2(datalist, mapper)
-#> shelter is in the dataset and has at least one of the expected modality for calculation
-#> electricity is in the dataset and has at least one of the expected modality for calculation
-#> drinkingwater is in the dataset and has at least one of the expected modality for calculation
-#> healthcare is in the dataset and has at least one of the expected modality for calculation
+#> Warning: Unknown or uninitialised column: `_parent_index`.
 
-fct_plot_indic_donut(indicator = datalist[["main"]]$impact2_2,
-                     iconunicode = "f140") 
-

+## Apply indicator function on datalist +datalist <- outcome16_1(datalist ) +#> DWE06 +#> DWE07 +#> DWE10 +#> DWE11 + +## Visualise value +fct_plot_indic_donut(indicator = datalist[["main"]]$outcome16_1, + iconunicode = "f140")
+

+
+
+

outcome16_2 +

+
+## data, cf example  fct_re_map()
+datalist <- kobocruncher::kobo_data( system.file("dummy_RMS_CAPI_v2_mapped.xlsx", 
+                                                 package = "IndicatorCalc"))
+#> Warning: Unknown or uninitialised column: `_parent_index`.
+## Apply indicator function on datalist
+datalist <- outcome16_2(datalist)
+#>  SPF01a
+#>  SPF01b
+#>  SPF01c
+#>  SPF01d
+#>  SPF01e
+#>  SPF01f
+#>  SPF01g
+#>  SPF01h
+
+table( datalist[["main"]]$outcome16_2, useNA = "ifany")
+#> 
+#>   0   1 
+#>  17 367
+## Visualise value
+fct_plot_indic_donut(indicator = datalist[["main"]]$outcome16_2,
+                     iconunicode = "f140")   
+

+