Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #17

Merged
merged 4 commits into from
May 22, 2024
Merged

Dev #17

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: impactR.analysis
Title: Analyzing survey data (collected with Kobo)
Version: 0.0.3
Version: 0.0.4
Authors@R: c(
person("Noblet", "Guillaume", email = "[email protected]", role = c("aut", "cre"))
)
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export(auto_kobo_analysis)
export(auto_svy_analysis)
export(kobo_analysis)
export(kobo_analysis_from_dap)
export(kobo_analysis_from_dap_group)
export(kobo_mean)
export(kobo_mean_all)
export(kobo_median)
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# impactR.analysis 0.0.4

* Update: the default choices_sep is now "/".
* Update: analysis key has been updated (see #11).
* New: `kobo_analysis_from_dap_group()` is a wrapper around the `kobo_analysis_from_dap()` function, over a list of grouping vectors (#15).

# impactR.analysis 0.0.3

* Breaking change and new: the `kobo_*()` family functions make use of the `svy_*()` family and Kobo tools. Previous functions have been renamed and updated `prop_*`.
Expand Down
3 changes: 2 additions & 1 deletion R/auto_kobo_analysis.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
#'
#' @export
#'
auto_kobo_analysis <- function(design, survey, choices, group = NULL, group_key_sep = " -/- ", na_rm = TRUE, vartype = "ci", level = 0.95, choices_sep = "_", label_survey = TRUE, bind = TRUE){
auto_kobo_analysis <- function(design, survey, choices, group = NULL, group_key_sep = " -/- ", na_rm = TRUE, vartype = "ci", level = 0.95, choices_sep = "/", label_survey = TRUE, bind = TRUE){


num_median_an <- kobo_median_all(design, survey, group, group_key_sep, label_survey, na_rm, vartype, level)
num_mean_an <- kobo_mean_all(design, survey, group, group_key_sep, label_survey, na_rm, vartype, level)
Expand Down
2 changes: 1 addition & 1 deletion R/kobo_analysis.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#'
#' @export
#'
kobo_analysis <- function(design, analysis, vars, survey, choices = NULL, group = NULL, group_key_sep = " -/- ", label_survey = TRUE, label_choices = TRUE, na_rm = TRUE, vartype = "ci", level = 0.95, ratio_key_sep = " -/- ", choices_sep = "_"){
kobo_analysis <- function(design, analysis, vars, survey, choices = NULL, group = NULL, group_key_sep = " -/- ", label_survey = TRUE, label_choices = TRUE, na_rm = TRUE, vartype = "ci", level = 0.95, ratio_key_sep = " -/- ", choices_sep = "/"){


analysis_type <- c("mean", "median", "select_multiple", "select_one", "ratio")
Expand Down
50 changes: 47 additions & 3 deletions R/kobo_analysis_from_dap.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#' @title Kobo survey analysis from a data analysis plan
#' Kobo survey analysis from a data analysis plan
#'
#' [kobo_analysis_from_dap] performs a survey analysis from a data analysis plan, while [kobo_analysis_from_dap_group] is a wrapper around the former to run multiple grouped analyses at once.
#'
#' @param design A srvyr::design object.
#' @param dap A well formatted data analysis plan.#'
Expand All @@ -21,7 +23,7 @@
#'
#' @export
#'
kobo_analysis_from_dap <- function(design, dap, survey, choices, group = NULL, level = 0.95, choices_sep = "_"){
kobo_analysis_from_dap <- function(design, dap, survey, choices, group = NULL, level = 0.95, choices_sep = "/"){

#------ Checks

Expand Down Expand Up @@ -77,6 +79,48 @@ kobo_analysis_from_dap <- function(design, dap, survey, choices, group = NULL, l


return(analysis)
#

}



#' @rdname kobo_analysis_from_dap
#'
#' @param l_group A list of vectors of variables to group by.
#' @param no_group If TRUE, the analysis without grouping is run.
#'
#' @export
kobo_analysis_from_dap_group <- function(design, dap, survey, choices, l_group, no_group = TRUE, level = 0.95, choices_sep = "/"){

#------ Checks

# Check if l_group is a list
if (!is.list(l_group)) rlang::abort("'l_group' should be a list.")

# Check if all elements are vectors of variables that exist in the design
purrr::map(l_group, \(x) if_not_in_stop(design$variables, x, "design"))

#------ Run analysis

# Map over group list
analysis <- purrr::map(
l_group,
\(x) {

an <- kobo_analysis_from_dap(design = design, dap = dap, survey = survey, choices = choices, group = x, level = level, choices_sep = choices_sep)

return(an)

},
.progress = TRUE
)

if(no_group) analysis[["no_grouping"]] <- kobo_analysis_from_dap(design = design, dap = dap, survey = survey, choices = choices, level = level, choices_sep = choices_sep)

# Bind all analyses together
analysis <- purrr::list_rbind(analysis)


return(analysis)
}

2 changes: 1 addition & 1 deletion R/kobo_ratio.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#' @return A character vector of select_one questions.
#'
#' @export
kobo_ratio <- function(design, nums, denoms, ratio_key_sep = " -/- ", survey, group = NULL, group_key_sep = " ~/~ ", label_survey = TRUE, na_rm = TRUE, vartype = "ci", level = 0.95){
kobo_ratio <- function(design, nums, denoms, ratio_key_sep = " -/- ", survey, group = NULL, group_key_sep = " -/- ", label_survey = TRUE, na_rm = TRUE, vartype = "ci", level = 0.95){


#------ Gather arguments
Expand Down
2 changes: 1 addition & 1 deletion R/kobo_select_multiple.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#' @return A character vector of select_one questions.
#'
#' @export
kobo_select_multiple <- function(design, vars, survey, choices = NULL, choices_sep = "_", group = NULL, group_key_sep = " -/- ", label_survey = TRUE, label_choices = TRUE, na_rm = TRUE, vartype = "ci", level = 0.95){
kobo_select_multiple <- function(design, vars, survey, choices = NULL, choices_sep = "/", group = NULL, group_key_sep = " -/- ", label_survey = TRUE, label_choices = TRUE, na_rm = TRUE, vartype = "ci", level = 0.95){

#------ Check

Expand Down
2 changes: 1 addition & 1 deletion R/kobo_select_multiple_all.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#' @return A character vector of select_one questions.
#'
#' @export
kobo_select_multiple_all <- function(design, survey, choices, group = NULL, group_key_sep = " -/- ", choices_sep = "_", label_survey = TRUE, label_choices = TRUE, na_rm = TRUE, vartype = "ci", level = 0.95){
kobo_select_multiple_all <- function(design, survey, choices, group = NULL, group_key_sep = " -/- ", choices_sep = "/", label_survey = TRUE, label_choices = TRUE, na_rm = TRUE, vartype = "ci", level = 0.95){

select_multiples <- impactR.kobo::get_survey_select_multiple(survey)

Expand Down
2 changes: 1 addition & 1 deletion R/svy_analysis.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#'
#' @export
#'
svy_analysis <- function(design, analysis, vars, group = NULL, group_key_sep = " -/- ", na_rm = TRUE, vartype = "ci", level = 0.95, ratio_key_sep = " ~/~ ", interact_key_sep = " -/- ", quantiles = c(0.25, 0.5, 0.75), ak = TRUE, ak_overall_sep = " @/@ ", ak_main_sep = " -/-", ak_var_to_value_sep = " %/% ", ...){
svy_analysis <- function(design, analysis, vars, group = NULL, group_key_sep = " -/- ", na_rm = TRUE, vartype = "ci", level = 0.95, ratio_key_sep = " -/- ", interact_key_sep = " -/- ", quantiles = c(0.25, 0.5, 0.75), ak = TRUE, ak_overall_sep = " @/@ ", ak_main_sep = " -/-", ak_var_to_value_sep = " %/% ", ...){


analysis_type <- c("mean", "median", "proportion", "quantile", "ratio", "interact")
Expand Down
4 changes: 2 additions & 2 deletions man/auto_kobo_analysis.Rd

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

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

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

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

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

20 changes: 18 additions & 2 deletions man/kobo_analysis_from_dap.Rd

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

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

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

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

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

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

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

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

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

5 changes: 3 additions & 2 deletions vignettes/kobo_analysis.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ The `auto_kobo_analysis()` function runs all the above functions but `kobo_ratio
While all these functions provide a quick workflow for analyzing survey data, the recommend way is to provide a data analysis plan and use functions `kobo_analysis()` or `kobo_analysis_dap()` (see below), which allows for finer analyses (e.g. providing ratios, labels of indicators, etc., beyond types as defined in the Kobo tool.)

## Make your own analysis

```{r kobo-analysis, eval = F}
# Calculate a mean
kobo_analysis(design, analysis = "mean", vars = c("c_total_3_17_femmes", "e_abandont_3a_4a_fille"), survey)
Expand Down Expand Up @@ -126,10 +127,10 @@ Then, to run the analysis, do the following:

```{r kobo-analysis-from-dap, eval = F}
# Default
kobo_analysis_from_dap(design, analysis_dap, survey, choices)
kobo_analysis_from_dap(design, analysis_dap, survey, choices, choices_sep = "_")

# Grouped and confidence level of 0.99
kobo_analysis_from_dap(design, analysis_dap, survey, choices, group = "milieu", level = 0.99)
kobo_analysis_from_dap(design, analysis_dap, survey, choices, group = "milieu", level = 0.99, choices_sep = "_")

```

Loading