---
title: "Physician Report"
author: "Rian DeFaccio, for BH Analytics LLC"
format: 
  html:
    toc: true
    toc-location: left
    embed-resources: true
theme: "../common/css/zephyr.scss"
params:
  StaffID: 45
---

```{r setup, include=FALSE}
knitr::opts_knit$set(warning = FALSE, message = FALSE, echo = FALSE)
knitr::opts_chunk$set(warning = FALSE, message = FALSE, echo = FALSE)
```

```{r}
# load libraries and adjust settings
library(tidyverse)
```

```{r}
# import data and some data cleaning and formatting
clinics <- read.csv('../data/healthcare2/clinic.csv')
staff <- read.csv('../data/healthcare2/Staff.csv') %>%
  rename(StaffFirstName = FirstName, StaffLastName = LastName)
visits <- read.csv('../data/healthcare2/OutpatientVisit.csv') %>%
  left_join(staff, by = 'StaffID') %>%
  left_join(clinics, by = 'ClinicCode')
disease_map <- read.csv('../data/healthcare2/DiseaseMap.csv') %>%
  mutate(Condition = str_replace_all(Condition, "_", " ") %>%
           str_replace('Liver', 'Liver ') %>%
           str_to_title() %>%
           str_replace('With', 'with') %>%
           str_replace('Hiv', 'HIV'))

visits$VisitDateDT <- ymd(visits$VisitDate)
visits$VisitMonth  <- strftime(visits$VisitDateDT, '%Y-%m')
```

```{r}
staff_first_name <- staff %>%
  filter(StaffID == params$StaffID) %>%
  pull(StaffFirstName)
staff_name <- staff %>%
  filter(StaffID == params$StaffID) %>%
  mutate(FullName = paste(StaffFirstName, StaffLastName)) %>%
  pull(FullName)
staff_hosp <- visits %>% 
  filter(StaffID == params$StaffID) %>%
  slice(1) %>% 
  pull(ClinicName)
```


This report is for `r staff_name`, who works at `r staff_hosp`.

## Workload Over Time

How has `r staff_first_name`'s workload changed over time? 

How does this compare to other physicians at his clinic?

```{r}
# filter visits to just the current physician
current_staff_visits <- visits %>% filter(StaffID == params$StaffID)

staff_workload <- current_staff_visits %>%
  group_by(VisitMonth) %>%
  count() %>%
  rename(Month = VisitMonth, `Number of Visits` = n)

hosp_workload <- visits %>%
  filter(ClinicName == staff_hosp) %>%
  group_by(StaffID, VisitMonth) %>%
  count() %>%
  ungroup() %>%
  group_by(VisitMonth) %>%
  summarise(mean_visits = mean(n))

ggplot(staff_workload, 
       aes(x = ym(Month), y = `Number of Visits`)) +
  geom_col() +
  stat_smooth(data = hosp_workload, 
            aes(x = ym(VisitMonth), y = mean_visits),
            color = 'red', se = FALSE) +
  theme_minimal() +
  theme(panel.grid.minor = element_blank(),
          panel.grid.major.x = element_blank()) +
  xlab('Month') +
  ggtitle(paste('Workload over Time for', staff_name),
          subtitle = paste('Compared to Average Physician at', staff_hosp))
```

## Common Diagnoses

What conditions does `r staff_first_name` encounter most often?

```{r}
# build plot of common diagnoses for this staff 
current_staff_visits %>%
  select(VisitID, starts_with('ICD')) %>%
  pivot_longer(
    cols = starts_with('ICD'),
    names_to = 'ICDPosition',
    values_to = 'ICD10') %>%
  left_join(disease_map, by = 'ICD10') %>%
  group_by(Condition) %>%
  count() %>%
  filter(!is.na(Condition)) %>%
  ggplot(aes(x = reorder(Condition, n), y = n)) +
  geom_col() +
  coord_flip() +
  xlab('Disease Condition') + ylab('Number of Diagnoses') +
  theme_minimal() +
  ggtitle(paste('All Diagnoses for', staff_name)) +
  scale_y_continuous(label = scales::comma)
```



























