forked from impact-initiatives/analysistools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
432 lines (346 loc) · 12.1 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
---
output: github_document
editor_options:
markdown:
wrap: 72
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# analysistools
<!-- badges: start -->
[![Contributor
Covenant](https://img.shields.io/badge/Contributor%20Covenant-2.1-4baaaa.svg)](code_of_conduct.md)
[![check-standard](https://github.com/impact-initiatives/analysistools/actions/workflows/check-standard.yaml/badge.svg)](https://github.com/impact-initiatives/analysistools/actions/workflows/check-standard.yaml)
[![Codecov test
coverage](https://codecov.io/gh/impact-initiatives/analysistools/branch/main/graph/badge.svg)](https://app.codecov.io/gh/impact-initiatives/analysistools?branch=main)
<!-- badges: end -->
The goal of analysistools is to ...
## Installation
You can install the development version of analysistools from
[GitHub](https://github.com/) with:
``` r
# install.packages("devtools")
devtools::install_github("impact-initiatives/analysistools")
```
## Example
```{r load}
library(analysistools)
```
### How to add weights
```{r}
shorter_df <- analysistools_MSNA_template_data[, c(
"admin1",
"admin2",
"expenditure_debt",
"income_v1_salaried_work",
"wash_drinkingwatersource",
grep("edu_learning_conditions_reasons_v1", names(analysistools_MSNA_template_data), value = T)
)]
example_sample <- data.frame(
strata = c("admin1a", "admin1b", "admin1c"),
population = c(30000, 50000, 80000)
)
weighted_shorter_df <- shorter_df %>%
add_weights(example_sample,
strata_column_dataset = "admin1",
strata_column_sample = "strata",
population_column = "population"
)
weighted_shorter_df[, c("admin1", "weights")] %>% head()
```
### How to perform a descriptive analysis (mean, median, proportions)
The *create_analysis* function needs a survey design from *srvyr*.
```{r}
example_design <- srvyr::as_survey(weighted_shorter_df, strata = admin1, weights = weights)
```
If only the design is provided, it will perform mean, median and
proportions.
```{r}
ex1_results <- create_analysis(design = example_design, sm_separator = "/")
```
It should return an object with 3 elements: - the results table (in a
long format and analysis key), - the dataset used, - the list of
analysis performed.
```{r}
names(ex1_results)
```
```{r}
ex1_results[["results_table"]] %>% head()
```
```{r}
ex1_results[["loa"]] %>% head()
```
#### Grouping variables
The group_var can be used to defined the different grouping, independent
variables. For example: - one variable
```{r}
ex2_results <- create_analysis(design = srvyr::as_survey(shorter_df), group_var = "admin1", sm_separator = "/")
ex2_results[["loa"]]
```
- two variables separately
```{r}
ex3_results <- create_analysis(design = srvyr::as_survey(shorter_df), group_var = c("admin1", "admin2"), sm_separator = "/")
ex3_results[["loa"]]
```
- two variables combined
```{r}
ex4_results <- create_analysis(design = srvyr::as_survey(shorter_df), group_var = "admin1, admin2", sm_separator = "/")
ex4_results[["loa"]]
```
### How to perform a descriptive analysis with a *list of analysis*
```{r}
ex5_results <- create_analysis(design = srvyr::as_survey(shorter_df), loa = analysistools_MSNA_template_loa, sm_separator = "/")
ex5_results[["loa"]]
```
### How to perform specfic analysis
#### Mean
This is a basic example which shows you how to calculate the mean:
```{r mean}
somedata <- data.frame(
aa = 1:10,
bb = rep(c("a", "b"), 5),
weights = rep(c(.5, 1.5), 5),
stratas = rep(c("strata_a", "strata_b"), 5)
)
me_design <- srvyr::as_survey(somedata)
create_analysis_mean(me_design, analysis_var = "aa")
create_analysis_mean(me_design, group_var = "bb", analysis_var = "aa")
me_design_w <- srvyr::as_survey(somedata, weights = weights)
create_analysis_mean(me_design_w, analysis_var = "aa")
create_analysis_mean(me_design_w, group_var = "bb", analysis_var = "aa")
```
#### Median
This is a basic example which shows you how to calculate the median:
```{r median}
somedata <- data.frame(
aa = 1:10,
bb = rep(c("a", "b"), 5),
weights = rep(c(.5, 1.5), 5),
stratas = rep(c("strata_a", "strata_b"), 5)
)
me_design <- srvyr::as_survey(somedata)
create_analysis_median(me_design, analysis_var = "aa")
create_analysis_median(me_design, group_var = "bb", analysis_var = "aa")
me_design_w <- srvyr::as_survey(somedata, weights = weights)
create_analysis_median(me_design_w, analysis_var = "aa")
create_analysis_median(me_design_w, group_var = "bb", analysis_var = "aa")
```
#### Proportion
##### Select one
This is a basic example which shows you how to calculate the proportion
for select one:
```{r proportion}
somedata <- data.frame(
groups = sample(c("group_a", "group_b"),
size = 100,
replace = TRUE
),
value = sample(c("a", "b", "c"),
size = 100, replace = TRUE,
prob = c(.6, .4, .1)
)
)
create_analysis_prop_select_one(srvyr::as_survey(somedata, strata = groups),
group_var = NA,
analysis_var = "value",
level = .95
)
create_analysis_prop_select_one(srvyr::as_survey(somedata, strata = groups),
group_var = "groups",
analysis_var = "value",
level = .95
)
```
##### Select multiple
```{r proportion select multiple}
somedata <- data.frame(
groups = sample(c("group_a", "group_b"), size = 100, replace = T),
smvar = rep(NA_character_, 100),
smvar.option1 = sample(c(TRUE, FALSE), size = 100, replace = T, prob = c(.7, .3)),
smvar.option2 = sample(c(TRUE, FALSE), size = 100, replace = T, prob = c(.6, .4)),
smvar.option3 = sample(c(TRUE, FALSE), size = 100, replace = T, prob = c(.1, .9)),
smvar.option4 = sample(c(TRUE, FALSE), size = 100, replace = T, prob = c(.8, .2)),
uuid = 1:100 %>% as.character()
) %>%
cleaningtools::recreate_parent_column(uuid = "uuid", sm_separator = ".")
somedata <- somedata$data_with_fix_concat
create_analysis_prop_select_multiple(srvyr::as_survey(somedata),
group_var = NA,
analysis_var = "smvar",
level = 0.95
)
create_analysis_prop_select_multiple(srvyr::as_survey(somedata),
group_var = "groups",
analysis_var = "smvar",
level = 0.95
)
```
#### Ratios
This is a basic example which shows you how to calculate the ratio
between 2 numeric variables:
```{r}
school_ex <- data.frame(
hh = c("hh1", "hh2", "hh3", "hh4"),
num_children = c(3, 0, 2, NA),
num_enrolled = c(3, NA, 0, NA),
num_attending = c(1, NA, NA, NA),
group = c("a", "a", "b", "b")
)
me_design <- srvyr::as_survey(school_ex)
```
Default value will give a ratio of 0.2 as there are 1 child out of 5
attending school. In the hh3, the NA is present because there is a skip
logic, there cannot be a child attending as none are enrolled. The
number of household counted, n, is equal to 2, as there are 2 households
only having child.
```{r}
create_analysis_ratio(me_design,
analysis_var_numerator = "num_attending",
analysis_var_denominator = "num_children"
)
```
If numerator_NA_to_0 is set to FALSE, ratio will be 1/3, as hh3 with 2
children and NA for attending will be removed with the na.rm = T inside
the survey_ratio calculation. The number of household used in the
calculation is 1.
```{r}
create_analysis_ratio(me_design,
analysis_var_numerator = "num_attending",
analysis_var_denominator = "num_children",
numerator_NA_to_0 = FALSE
)
```
If filter_denominator_0 is set to FALSE, ratio will be 0.2 as there are
1 child out of 5 attending school. In the hh3, the NA is present because
there is a skip logic, there cannot be a child attending as none are
enrolled. The number of household counted, n, is equal to 3 instead 2.
The household with 0 child is counted in the n.
```{r}
create_analysis_ratio(me_design,
analysis_var_numerator = "num_attending",
analysis_var_denominator = "num_children",
numerator_NA_to_0 = FALSE
)
```
For weigths and group:
```{r}
set.seed(8988)
somedata <- data.frame(
groups = rep(c("a", "b"), 50),
children_518 = sample(0:5, 100, replace = TRUE),
children_enrolled = sample(0:5, 100, replace = TRUE)
) %>%
dplyr::mutate(children_enrolled = ifelse(children_enrolled > children_518,
children_518,
children_enrolled
))
somedata[["weights"]] <- ifelse(somedata$groups == "a", 1.33, .67)
create_analysis_ratio(srvyr::as_survey(somedata, weights = weights, strata = groups),
group_var = NA,
analysis_var_numerator = "children_enrolled",
analysis_var_denominator = "children_518",
level = 0.95
)
create_analysis_ratio(srvyr::as_survey(somedata, weights = weights, strata = groups),
group_var = "groups",
analysis_var_numerator = "children_enrolled",
analysis_var_denominator = "children_518",
level = 0.95
)
```
### How to review results
The logic behind reviewing analysis is to compare the results from 2
independent analysis of the same variables using the *review_analysis*.
In this example, the results table to be review and the dataset are
loaded.
```{r}
results_to_review <- analysistools::analysistools_MSNA_template_with_ratio_results_table$results_table
dataset_to_analyse <- analysistools::analysistools_MSNA_template_data
```
The list of analysis from the results can be reproduced with
*create_loa_from_results* and the analysis key. This *loa* can be used
to create a new analysis to be compared with.
```{r}
me_loa <- create_loa_from_results(results_to_review)
me_analysis <- create_analysis(srvyr::as_survey(dataset_to_analyse),
loa = me_loa,
sm_separator = "/")
```
The new results and the results to be reviewed are bound together by the
*analysis_key*.
```{r}
binded_results <- results_to_review %>%
dplyr::full_join(me_analysis$results_table, by ="analysis_key")
```
```{r}
review_results <- review_analysis(binded_results,
stat_columns_to_review = c("stat.x", "stat_low.x", "stat_upp.x"),
stat_columns_to_compare_with = c("stat.y", "stat_low.y", "stat_upp.y"))
review_results$review_table %>%
dplyr::group_by(stat) %>%
dplyr::summarise(prop_correct = mean(review_check))
review_results$review_table %>%
dplyr::group_by(stat, review_comment) %>%
dplyr::tally(sort = T)
review_results$review_table %>%
dplyr::filter(!review_check) %>%
dplyr::select(analysis_type,analysis_var,group_var) %>%
dplyr::distinct()
```
```{r}
analysis_key_column <- c("mean @/@ income ~/~ NA @/@ NA ~/~ NA",
"prop_select_one @/@ water_source ~/~ tap_water @/@ district ~/~ district_a",
"prop_select_one @/@ water_source ~/~ tap_water @/@ district ~/~ district_a ~/~ population ~/~ displaced",
"prop_select_multiple @/@ source_information ~/~ relatives @/@ NA ~/~ NA")
test_analysis_results <- data.frame(
test = c(
"test equality",
"test difference",
"test Missing in y",
"test Missing in x"
),
stat_col.x = c(0, 1, 2, NA),
stat_col.y = c(0, 2, NA, 3),
analysis_key = analysis_key_column
)
review_results2 <- review_analysis(test_analysis_results,
stat_columns_to_review = "stat_col.x",
stat_columns_to_compare_with = "stat_col.y")
review_results2$review_table %>%
dplyr::group_by(stat) %>%
dplyr::summarise(prop_correct = mean(review_check))
review_results2$review_table %>%
dplyr::group_by(stat, review_comment) %>%
dplyr::tally(sort = T)
review_results2$review_table %>%
dplyr::filter(!review_check) %>%
dplyr::select(review_check, analysis_type,analysis_var,group_var) %>%
dplyr::distinct()
```
### Converting the analysis index into a table
This is is how to turn the analysis index into a table
```{r create_analysis_key_table}
resultstable <- data.frame(analysis_index = c(
"mean @/@ v1 ~/~ NA @/@ NA ~/~ NA",
"mean @/@ v1 ~/~ NA @/@ gro ~/~ A",
"mean @/@ v1 ~/~ NA @/@ gro ~/~ B"
))
key_table <- create_analysis_key_table(resultstable, "analysis_index")
key_table
```
You can then unite the analysis and grouping variables if needed.
```{r}
unite_variables(key_table)
```
## Code of Conduct
Please note that the analysistools project is released with a
[Contributor Code of
Conduct](https://impact-initiatives.github.io/analysistools/CODE_OF_CONDUCT.html).
By contributing to this project, you agree to abide by its terms.