dev/incercare.R

library(tidyverse)
library(hrbrthemes)

# folder <- system.file(package = "rocheshiny", "extdata")
# folder <- "inst/extdata/"
# 
# lab_file <- file.path(folder, "Random_LabValuesInfo_2020.tsv")
# patient_file <- file.path(folder, "Random_PatientLevelInfo_2020.tsv")
# 
# cols_to_title_case_lab <- c("bmrkr2", "lbcat", "avisit")
# 
# lab_data <- read_tsv(lab_file, 
#                      col_types = cols(
#                          STUDYID = col_character(),
#                          USUBJID = col_character(),
#                          BMRKR1 = col_double(),
#                          BMRKR2 = col_character(),
#                          LBTESTCD = col_character(),
#                          LBTEST = col_character(),
#                          LBCAT = col_character(),
#                          AVAL = col_double(),
#                          AVALU = col_character(),
#                          AVISIT = col_character()
#                      )) %>% 
#     janitor::clean_names() %>% 
#     mutate_at(.vars = cols_to_title_case_lab, .funs = str_to_title) 

# lb_test <- load_lab_data()
# all_equal(lab_data, lb_test)

# cols_to_title_case_pat <- c("sex", "race")
# 
# patient_data <- read_tsv(patient_file,
#                          col_types = cols(
#                              STUDYID = col_character(),
#                              USUBJID = col_character(),
#                              AGE = col_double(),
#                              SEX = col_character(),
#                              RACE = col_character(),
#                              ACTARM = col_character(),
#                              ACTARMCD = col_character()
#                          )) %>% 
#     janitor::clean_names() %>% 
#     mutate_at(.vars = cols_to_title_case_pat, str_to_title) %>% 
#     mutate(race = str_replace_all(race, "Or", "or"))
    

# all_data <- lab_data %>% 
#     left_join(patient_data, by = c("studyid", "usubjid")) %>% 
#     group_by(usubjid, lbtestcd) %>% 
#     mutate(order = row_number()) %>% 
#     ungroup() %>% 
#     mutate(visit = str_remove_all(avisit, pattern = "Week [1-5] "))

lab_data <- load_lab_data()
patient_data <- load_patient_data()
all_data <-  left_join(lab_data, patient_data, by = c("studyid", "usubjid"))
# all_equal(all_data, all_data2)

# ex <- all_data %>% 
#     filter(usubjid == "AB12345-BRA-1-id-120")
# 
# ex_alt <- ex %>% 
#     filter(lbtestcd == "ALT")
# 
# ex_crp <- ex %>% 
#     filter(lbtestcd == "CRP")
# 
# ex_iga <- ex %>% 
#     filter(lbtestcd == "IGA")

# ggplot(ex_alt, aes(x = order, y = aval)) +
#     geom_line() +
#     labs(y = paste0(ex_alt$lbtestcd, "( ", ex_alt$avalu, ")"),
#          title = ex_alt$lbtest,
#          x = "Visit") +
#     theme_ipsum(grid = "XY") +
#     scale_x_continuous(breaks = 1:7) +
#     scale_y_continuous(limits = c(20, 80))

alt_data <- all_data %>% 
    filter(lbtestcd == "ALT")

# ggplot(alt_data, aes(order, aval, col = sex)) +
#     geom_point(alpha = 0.05) +
#     geom_smooth()

# demographic data
# sex
# patient_data %>% 
#     count(sex) %>% 
#     ggplot(aes(sex, n)) +
#     geom_col() +
#     geom_text(aes(label = scales::comma(n)), hjust = 0.5, nudge_y = 12) +
#     theme_ipsum(grid = "Y") +
#     labs(y = "Count",
#          x = "Sex",
#          title = "Number of subjects by sex")

p1 <- plot_sex(patient_data)
p1
# patient_data %>% 
#     count(race) %>% 
#     arrange(n) %>% 
#     mutate(race = factor(race, levels = race)) %>% 
#     ggplot(aes(race, n)) +
#     geom_col() +
#     coord_flip() +
#     theme_ipsum(grid = "X") +
#     labs(x = "Race",
#          y = "Count",
#          title = "Number of subjects by race")


p2 <- plot_race(patient_data)
p2

# patient_data %>% 
#     count(actarm) %>% 
#     ggplot(aes(actarm, n)) +
#     geom_col() +
#     labs(x = "",
#          y = "Count",
#          title = "Number of subjects by treatment group") +
#     theme_ipsum(grid = "Y")

p3 <- plot_treatment_group(patient_data)
p3

ggplot(patient_data, aes(sex, age)) +
    geom_boxplot() +
    labs(title = "Age distribution by sex",
         y = "Age (years)",
         x = "Sex") +
    theme_ipsum(grid = "Y")

p4 <- plot_age_sex(patient_data)
p4

# laboratory data

# data <- all_data %>% 
#     filter(lbtestcd == "ALT") %>% 
#     group_by(lbtestcd, actarm, order, avalu, lbtest, lbcat, avisit) %>% 
#     summarise(lower = min(aval),
#               upper = max(aval),
#               mean = mean(aval)) %>% 
#     ungroup()
# 
# ggplot(data, aes(order, y = mean, col = actarm)) +
#     geom_line(aes(group = actarm)) +
#     # geom_pointrange(aes(ymin = lower, ymax = upper)) +
#     theme_ipsum() +
#     scale_colour_ipsum("Treatment group") +
#     geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.1) +
#     labs(y = glue::glue("{unique(data$lbtestcd)} ({unique(data$avalu)})"),
#          x = "Visit",
#          title = glue::glue("{unique(data$lbtest)}"),
#          subtitle = glue::glue("{unique(data$lbcat)}")) +
#     scale_x_continuous(breaks = data$order,
#                        labels = data$avisit) +
#     theme(axis.text.x = element_text(angle = 45,
#                                      hjust = 1,
#                                      vjust = 1))


p_lab1 <- plot_lab(all_data, test = "ALT")
p_lab2 <- plot_lab(all_data, test = "CRP")
p_lab3 <- plot_lab(all_data, test = "IGA")

p_lab1
p_lab2
p_lab3
dragosmg/rocheshiny documentation built on April 12, 2020, 12:22 a.m.