-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex_disparity_calcs.R
67 lines (59 loc) · 1.87 KB
/
index_disparity_calcs.R
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
library(glue)
library(dplyr)
library(tidyverse)
get_disparity_ratio <- function(activity_by_type_clusters_stg1) {
options(scipen=999) # disable scientific notation
#options(scipen=0)
activity_type_decile <- activity_by_type_clusters_stg1
# Metrics 2 - 9 listed as use GP list size 16+ as denominator
# else uses CHD prevalence
by_list_total <- c(
"2", "5",
"6", "7",
"8", "9"
)
# Produces a string to be used in an if(else) in later code to match against
# the metric numbers listed in by_list_total
by_list_regex <- glue("^metric({paste(by_list_total, collapse = '|')})_total$")
# Data manipulation code --------------------------------------------------
activity_long <- activity_type_decile |>
pivot_longer(
cols = c(starts_with("metric"), -metric1_total),
names_to = "metric_name",
values_to = "metric_total"
) |>
mutate(
total_column = ifelse(
str_detect(metric_name, by_list_regex),
list_size_total,
metric1_total
),
total_rate = metric_total / total_column * 1000
) |>
select(
quantile = cluster2,
metric1_total,
metric_name,
metric_total,
total_rate
) |>
group_by(metric_name) |>
# arrange() required for min() and max() functions to calculate
# largest_value and smallest_value
arrange(
metric_name,
quantile
) |>
mutate(
largest_value = max(total_rate),
smallest_value = min(total_rate),
abs_range = largest_value - smallest_value, # not used in this particular code
rel_range = largest_value / smallest_value,
disparity_ratio = total_rate / largest_value,
abs_diff = abs(total_rate - largest_value)
) |>
ungroup()
return(activity_long)
}
table_data <- activity_long |>
select(cluster=quantile, metric_name, total_rate,disparity_ratio, largest_value)