#' Create regression table.
#' Last updated on 2020-06-22
#'
#' @param medications A tibble
#' @param covariates A tibble
#' @return A list with two tibbles \code{regression_table_output}
#' @export
create_regression_table <- function(medications, covariates) {
#keep only last drug exposure
medications1 <- medications %>%
arrange(person_id, desc(date)) %>%
distinct(person_id, rxcui_ingr)
##add `count` column in preparation to make regression table
medications1$count <- 1
##pivot to make regression table
regression_drugs <- medications1 %>%
pivot_wider(names_from = rxcui_ingr,
values_from = count,
values_fill = list(count = 0))
#process `covariates`
covariates <- covariates %>%
select(person_id, gender, dob, race, groupc)
covariates1 <- medications %>%
select(person_id, start_date, obs_length) %>%
distinct()
covariates1 <- inner_join(covariates,
covariates1,
by = "person_id")
##calculate age
covariates1$age <- interval(covariates1$dob, covariates1$start_date)
covariates1$age <- time_length(covariates1$age, "year")
covariates1 <- covariates1 %>%
filter(age >= 18 & age < 90)
#normalize covariates
##age
v1 <- covariates1$age
v2 <- (v1-min(v1))/(max(v1)-min(v1))
covariates1$age_n <- v2
##obs_length
v1 <- covariates1$obs_length
v2 <- (v1-min(v1))/(max(v1)-min(v1))
covariates1$obs_length_n <- v2
##convert binary variables
covariates1$is_m <- if_else(covariates1$gender == "M", 1, 0)
covariates1$is_w <- if_else(covariates1$race == "W", 1, 0)
##select normalized columns only
covariates1 <- covariates1 %>%
select(person_id, is_m, is_w, age_n, obs_length_n, groupc)
#merge regression_drugs with covariates
regression_table <- inner_join(covariates1,
regression_drugs,
by = "person_id")
return(regression_table)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.