Skip to content

Commit 89c75a4

Browse files
Merge pull request #3 from WorldHealthOrganization/feature/composite-indicators
Feature/composite indicators
2 parents 6515e63 + c191b5a commit 89c75a4

19 files changed

Lines changed: 482 additions & 304 deletions

DESCRIPTION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ Collate:
4141
'indicators-iodine.R'
4242
'measurements.R'
4343
'concepts.R'
44-
'indicators-iron-deficiency-anaemia.R'
44+
'indicators-composite.R'
4545
'indicators-export.R'
46+
'indicators-iron-deficiency-anaemia.R'
4647
'package-micronutrients.R'
4748
'prevalence.R'
4849
'utils.R'

NAMESPACE

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,31 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
S3method(format,composite_indicator)
34
S3method(format,indicator)
45
S3method(format,vctrs_measurement)
6+
S3method(indicator_abbreviated_name,composite_indicator)
7+
S3method(indicator_abbreviated_name,indicator)
8+
S3method(indicator_agg_prevalence_categories,composite_indicator)
9+
S3method(indicator_agg_prevalence_categories,indicator)
10+
S3method(indicator_drop_columns,composite_indicator)
11+
S3method(indicator_drop_columns,indicator)
12+
S3method(indicator_export_value_name,composite_indicator)
13+
S3method(indicator_export_value_name,indicator)
14+
S3method(indicator_name,composite_indicator)
15+
S3method(indicator_name,indicator)
16+
S3method(indicator_prevalence_categories,composite_indicator)
17+
S3method(indicator_prevalence_categories,indicator)
18+
S3method(indicator_rename_columns,composite_indicator)
19+
S3method(indicator_rename_columns,indicator)
20+
S3method(indicator_reorder_columns,composite_indicator)
21+
S3method(indicator_reorder_columns,indicator)
22+
S3method(indicator_value_concept,composite_indicator)
23+
S3method(indicator_value_concept,indicator)
524
S3method(iodine_is_implausible,default)
625
S3method(levels,indicator)
26+
S3method(prevalence_report_short,composite_indicator)
27+
S3method(prevalence_report_short,indicator)
28+
S3method(print,composite_indicator)
729
S3method(print,indicator)
830
S3method(required_concepts,indicator)
931
S3method(required_concepts,indicator_adjustment)
@@ -23,7 +45,6 @@ export(adjustment_ferritin_arithmetic_correction)
2345
export(adjustment_ferritin_cutoff)
2446
export(adjustment_ferritin_regression_correction)
2547
export(adjustment_ferritin_rm_agp_crp)
26-
export(adjustment_none)
2748
export(indicator_anaemia)
2849
export(indicator_ferritin)
2950
export(indicator_ida)

R/adjustments-export.R

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#' Indicator adjustment methods
2+
#'
3+
#' This can be used to signal no adjustment for an indicator.
14
#' @rdname adjustments
25
#' @include indicators-ferritin.R
36
#' @export
@@ -25,12 +28,3 @@ adjustment_ferritin_rm_agp_crp <- function() {
2528
adjustment_ferritin_cutoff <- function() {
2629
ferritin_adjustment_cutoff
2730
}
28-
29-
#' Indicator adjustment methods
30-
#'
31-
#' This can be used to signal no adjustment for an indicator.
32-
#' @rdname adjustments
33-
#' @export
34-
adjustment_none <- function() {
35-
no_adjustment
36-
}

R/classifications.R

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#' @param CRP (Optional) A vector of C-reactive protein (CRP) measurements (in mg/L), used to adjust for inflammation.
1616
#' @param AGP (Optional) A vector of alpha-1-acid glycoprotein (AGP) measurements (in g/L), used to adjust for inflammation.
1717
#' @param ferritin (Optional) A vector of ferritin measurements (in \\u00b5gg/L).
18-
#' @param ida (Optional) A vector of iron deficiency measurements.
1918
#' @param iodine (Optional) A vector of iodine measurements (in \\u00b5gg/L).
2019
#' @param haemoglobin (Optional) A vector of haemoglobin measurements (in g/L).
2120
#' @param altitude (Optional) A numeric vector representing elevation above sea level (in meters), used to adjust for altitude-related effects. Elevation is a compulsory variable and it should always be reported in the dataset. Even when no elevation data is collected, a variable for 'elevation' should be created and set as "0" for all individuals without reported elevation. When elevation is not reported, that individual case will be excluded from the analysis and considered as 'missing'
@@ -59,7 +58,6 @@ individual_classification <- function(
5958
CRP = NULL,
6059
AGP = NULL,
6160
ferritin = NULL,
62-
ida = NULL,
6361
iodine = NULL,
6462
haemoglobin = NULL,
6563
altitude = NULL,
@@ -77,7 +75,6 @@ individual_classification <- function(
7775
AGP = AGP,
7876
iodine = iodine,
7977
ferritin = ferritin,
80-
ida = ida,
8178
haemoglobin = haemoglobin,
8279
altitude = altitude,
8380
is_smoker = is_smoker,
@@ -98,7 +95,6 @@ classify_data_internal <- function(
9895
CRP = NULL,
9996
AGP = NULL,
10097
ferritin = NULL,
101-
ida = NULL,
10298
iodine = NULL,
10399
haemoglobin = NULL,
104100
altitude = NULL,
@@ -109,6 +105,7 @@ classify_data_internal <- function(
109105
malaria = NULL,
110106
.format_column_names = TRUE
111107
) {
108+
indicators <- flatten_indicators(indicators)
112109
validate_indicators(indicators)
113110
concept_list <- concepts_from_args(
114111
sex = sex,
@@ -119,7 +116,6 @@ classify_data_internal <- function(
119116
AGP = AGP,
120117
iodine = iodine,
121118
ferritin = ferritin,
122-
ida = ida,
123119
haemoglobin = haemoglobin,
124120
altitude = altitude,
125121
is_smoker = is_smoker,
@@ -131,7 +127,12 @@ classify_data_internal <- function(
131127
cols <- names(concept_list$values)
132128

133129
values <- lapply(indicators, function(x) {
134-
concept_list$values[[x$value_concept]]
130+
value_concept <- indicator_value_concept(x)
131+
if (is.null(value_concept)) {
132+
NULL
133+
} else {
134+
concept_list$values[[value_concept]]
135+
}
135136
})
136137
results <- indicators_compute_all(indicators, values, concept_list$values)
137138
names(results) <- NULL
@@ -147,7 +148,13 @@ classify_data_internal <- function(
147148

148149
validate_indicators <- function(indicators) {
149150
stopifnot(is.list(indicators))
150-
stopifnot(all(vapply(indicators, is_indicator, logical(1L))))
151+
stopifnot(all(vapply(
152+
indicators,
153+
function(x) {
154+
is_indicator(x) || is_composite_indicator(x)
155+
},
156+
logical(1L)
157+
)))
151158
}
152159

153160
not_null <- function(x) {

R/concepts.R

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -269,16 +269,6 @@ concepts <- concepts_list(
269269
prototype = measurement_mcg_l(NA_real_),
270270
is_implausible = is_ferritin_implausible
271271
),
272-
## ida ----
273-
concept(
274-
key = "ida",
275-
label = "Iron deficiency",
276-
accept = concept_acceptor(iron_deficiency_acceptor, "TODO"),
277-
standardizer = identity,
278-
validator = iron_deficiency_validator,
279-
prototype = NA
280-
# is_implausible = iron_deficiency_implausible
281-
),
282272
## AGP ----
283273
concept(
284274
key = "AGP",

R/indicators-composite.R

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
composite_indicator <- function(
2+
name,
3+
abbreviated_name,
4+
compute_function,
5+
prevalence_categories,
6+
requires_indicators
7+
) {
8+
structure(
9+
list(
10+
name = name,
11+
compute_function = compute_function,
12+
abbreviated_name = abbreviated_name,
13+
prevalence_categories = prevalence_categories,
14+
required_indicators = requires_indicators
15+
),
16+
class = "composite_indicator"
17+
)
18+
}
19+
20+
is_composite_indicator <- function(x) {
21+
inherits(x, "composite_indicator")
22+
}
23+
24+
#' @exportS3Method
25+
indicator_name.composite_indicator <- function(indicator) {
26+
indicator$name
27+
}
28+
29+
#' @exportS3Method
30+
indicator_value_concept.composite_indicator <- function(x) {
31+
NULL
32+
}
33+
34+
#' @exportS3Method
35+
indicator_abbreviated_name.composite_indicator <- function(indicator) {
36+
indicator$abbreviated_name
37+
}
38+
39+
#' @exportS3Method
40+
prevalence_report_short.composite_indicator <- function(indicator) {
41+
TRUE
42+
}
43+
44+
#' @exportS3Method
45+
indicator_prevalence_categories.composite_indicator <- function(indicator) {
46+
indicator$prevalence_categories
47+
}
48+
49+
#' @exportS3Method
50+
indicator_agg_prevalence_categories.composite_indicator <- function(indicator) {
51+
NULL
52+
}
53+
54+
#' @exportS3Method
55+
indicator_rename_columns.composite_indicator <- function(
56+
indicator,
57+
report_type
58+
) {
59+
list(
60+
short = NULL,
61+
long = NULL
62+
)
63+
}
64+
65+
#' @exportS3Method
66+
indicator_reorder_columns.composite_indicator <- function(
67+
indicator,
68+
report_type
69+
) {
70+
list(
71+
short = NULL,
72+
long = NULL
73+
)
74+
}
75+
76+
#' @exportS3Method
77+
indicator_drop_columns.composite_indicator <- function(indicator, report_type) {
78+
NULL
79+
}
80+
81+
#' @exportS3Method
82+
indicator_export_value_name.composite_indicator <- function(indicator) {
83+
indicator$abbreviated_name
84+
}
85+
86+
87+
#' @export
88+
format.composite_indicator <- function(x, ...) {
89+
paste0(
90+
"Indicator: ",
91+
x$name
92+
)
93+
}
94+
95+
#' @export
96+
print.composite_indicator <- function(x, ...) {
97+
cat(format(x), "\n")
98+
}

R/indicators-export.R

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,34 @@
66
#' @include indicators-anaemia.R
77
#' @rdname indicators
88
#' @export
9-
indicator_anaemia <- function() anaemia_indicator
9+
indicator_anaemia <- function() {
10+
anaemia_indicator
11+
}
1012

1113
#' @param ferritin_adjustment the adjustment method for ferritin.
1214
#' @include indicators-ferritin.R
1315
#' @rdname indicators
1416
#' @export
15-
indicator_ferritin <- function(ferritin_adjustment = adjustment_none()) {
16-
ferritin_indicator(ferritin_adjustment)
17+
indicator_ferritin <- function(ferritin_adjustment) {
18+
stopifnot(is_adjustment(ferritin_adjustment))
19+
list(
20+
ferritin_indicator(no_adjustment),
21+
ferritin_indicator(ferritin_adjustment)
22+
)
1723
}
1824

19-
#' @param ferritin_adjustment the adjustment method for ferritin.
2025
#' @rdname indicators
21-
#' @include indicators-iron-deficiency-anaemia.R
2226
#' @export
23-
indicator_ida <- function(ferritin_adjustment = adjustment_none()) {
24-
ida_indicator(ferritin_adjustment)
27+
indicator_ida <- function() {
28+
list(
29+
ida_indicator_unadjusted,
30+
ida_indicator_adjusted
31+
)
2532
}
2633

2734
#' @include indicators-iodine.R
2835
#' @rdname indicators
2936
#' @export
30-
indicator_iodine <- function() iodine_indicator
37+
indicator_iodine <- function() {
38+
iodine_indicator
39+
}

0 commit comments

Comments
 (0)