-
Notifications
You must be signed in to change notification settings - Fork 0
/
cohort_bmc_gen.R
110 lines (82 loc) · 3.27 KB
/
cohort_bmc_gen.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
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
#' @md
#' takes cohort of drug exposures,
#' joins to vocabulary table,
#' creates xwalk between drug and level
#'
#' @param drug_tbl defaults to `cdm_tbl('drug_exposure')`
#' @param concept_tbl defaults to `vocabulary_tbl('concept')`
#'
#' @return the original drug_tbl with an extra
#' column called `rxnorm_level`
#'
find_concept_names <- function(fact_tbl,
fact_concept_id,
concept_field,
concept_tbl=vocabulary_tbl('concept')) {
fact_tbl %>%
rename(concept_id = !!sym(fact_concept_id)) %>%
inner_join(
select(concept_tbl,
concept_id,
!!sym(concept_field)),
by=c('concept_id')
) %>% rename(concept_type=!!sym(concept_field))
}
#' compute numbers proportion of rows and patients at
#' each level of rxnorm drug level
#'
#' @param drug_tbl_list_args A list of lists where list name is a description of
#' what the table contains;
#' 1. The first element of the nested list is the table that will be computed;
#' 2. The second element of the nested list is a description of the table
#' @param check_string string that contains a description of the table
#'
check_bmc_gen <- function(fact_tbl_list_args,
check_string='bmc') {
results <- list()
for(i in 1:length(fact_tbl_list_args)) {
fact_tbl_name <- paste0(names(fact_tbl_list_args[i]))
xwalk <-
find_concept_names(fact_tbl = fact_tbl_list_args[[i]][[1]],
fact_concept_id = fact_tbl_list_args[[i]][[2]],
concept_field = fact_tbl_list_args[[i]][[5]])
total_cts <-
xwalk %>%
summarise(total_rows=n(),
total_pts=n_distinct(person_id)) %>% collect_new()
grps <- dplyr::group_vars(xwalk)
concept_grpd <- c(grps, 'concept_type')
concept_cts <-
xwalk %>%
group_by(!!! syms(concept_grpd)) %>%
summarise(concept_rows=n(),
concept_pts=n_distinct(person_id)) %>%
rename('concept' = !!sym(concept_grpd)) %>%
collect_new()
if(length(concept_grpd) > 1) {
props <-
concept_cts %>%
left_join(total_cts) %>% ungroup() %>%
mutate(row_proportions=round(concept_rows/total_rows,2)) %>%
mutate(person_proportions=round(concept_pts/total_pts,2)) %>%
add_meta(check_lib = check_string) %>%
mutate(check_name = fact_tbl_list_args[[i]][[4]]) %>%
mutate(check_desc = fact_tbl_list_args[[i]][[3]]) %>%
mutate(check_desc_short =fact_tbl_name)
} else {
props <-
concept_cts %>%
mutate(total_rows=total_cts$total_rows,
total_pts=total_cts$total_pts) %>%
#left_join(total_cts) %>% ungroup() %>%
mutate(row_proportions=round(concept_rows/total_rows,2)) %>%
mutate(person_proportions=round(concept_pts/total_pts,2)) %>%
add_meta(check_lib = check_string) %>%
mutate(check_name = fact_tbl_list_args[[i]][[4]]) %>%
mutate(check_desc = fact_tbl_list_args[[i]][[3]]) %>%
mutate(check_desc_short=fact_tbl_name)
}
results[[paste0(check_string,'_',names(fact_tbl_list[i]))]] <- props
}
results
}