library(kableExtra, warn.conflicts=F, quietly=T)
library(dplyr, warn.conflicts=F, quietly=T)
library(ggplot2, warn.conflicts=F, quietly=T)
library(EpiEstim, warn.conflicts=F, quietly=T)

Background 1

Background 2

linelist <- read.csv("https://raw.githubusercontent.com/blopman/epi569/master/Rushmore_Noro_LineList.csv") 
# Read in the data

# Changing the onset date variable into the R date format
linelist$onset_date <- as.Date(as.character(linelist$onset_date), format="%m/%d/%Y")
#code to answer Q1#
table (linelist$lab.confirmed)
## 
##   N   Y 
## 103   6
table (linelist$hospitalized)
## 
##   N 
## 109
table(linelist$gender)
## 
##  F  M 
## 77 32
table (linelist$case_type)
## 
##            Staff Student_MPHyear1 Student_MPHyear2      Student_PhD 
##                7               67               28                7
median (linelist$age)
## [1] 24
IQR (linelist$age)
## [1] 3
tapply(linelist$age, linelist$case_type, median)
##            Staff Student_MPHyear1 Student_MPHyear2      Student_PhD 
##               56               23               25               30
tapply(linelist$age, linelist$case_type, IQR)
##            Staff Student_MPHyear1 Student_MPHyear2      Student_PhD 
##             12.5              2.0              2.0              7.0

Question 1 (0.5pt)

# Plotting the epidemic curve

epicurve <- as.data.frame(linelist %>% group_by(onset_date) %>% summarize(incidence=n()))
## `summarise()` ungrouping output (override with `.groups` argument)
## `summarise()` ungrouping output (override with `.groups` argument)
plot1 <- ggplot(data=epicurve, aes(x=onset_date, y=incidence, labs=FALSE)) + 
         geom_bar(stat="identity", fill="gray45") +
         scale_x_date(date_breaks = "2 days", date_labels = "%m/%d") +
         scale_y_continuous(breaks = seq(0,15, by = 2)) +
         theme(axis.text.x = element_text(angle = 45)) +
         labs(x = "Symptom Onset Date", y = "Number of Cases", title = "Epi Curve for Rushmore Academy Norovirus Outbreak")
plot1

Question 2 (1pt)

Background 3

# First, convert the data to a format that can be used in the EpiEstim wallinga_teunis function 
# Data must be in the following format: 
### 1 column for symptom onset dates in ascending order, including dates on which 0 cases were reported, titled "dates"
### 1 column for case counts (incidence) titled "I"
### Note: to calculate an Rt estimate for day day 1 of the outbreak, we must start our epi curve 2 days prior the first symptom onset date

epicurve2 <- epicurve %>% arrange(onset_date) %>% rename(dates = onset_date, I = incidence)

all.dates <- as.data.frame(seq(as.Date("2019-10-14"), by = "day", length.out = 45))
names(all.dates) <- "dates"

epicurve.epiestim <- merge(x=epicurve2, y=all.dates, by="dates", all="TRUE")
epicurve.epiestim <- epicurve.epiestim %>% mutate(I = ifelse(is.na(I), 0, I)) 


# Next, run the code below to estimate Rt, along with 95% confidence intervals for Rt estimates
# This requires that we specify the mean and standard deviation of the serial interval  
# An offset gamma distribution will be used for the serial interval (by default)

mean_si <- 3.6   
std_si <- 2.0

estimates <- wallinga_teunis(epicurve.epiestim$I, 
                             method="parametric_si",
                             config = list(t_start = seq(3, 45), 
                                           t_end = seq(3, 45),
                                           mean_si = mean_si, 
                                           std_si = std_si, 
                                           n_sim = 1000))


# You can examine the serial interval distribution using the code below

plot(estimates$si_distr)

# Lastly, use the code below to plot the Rt estimates and 95% CIs over the epi curve to examine trends

plot2.data <- cbind(epicurve, estimates$R$`Mean(R)`,
                    estimates$R$`Quantile.0.025(R)`, estimates$R$`Quantile.0.975(R)`)
names(plot2.data) <- c("dates", "I", "R", "lowerCI", "upperCI")

plot2 <- ggplot(data=plot2.data, aes(x=dates, y=I, labs=FALSE)) + 
  geom_bar(stat="identity", fill="gray45") +
  scale_x_date(date_breaks = "2 days", date_labels = "%m/%d") +
  scale_y_continuous(breaks = seq(0,15, by = 2)) +
  theme(axis.text.x = element_text(angle = 45)) +
  labs(x = "Symptom Onset Date", 
       y = "Number of Cases",
       title = "Epi Curve for Rushmore Academy Norovirus Outbreak") +
  geom_hline(aes(yintercept=1), colour="red", linetype="dashed", size=0.5) +
  geom_errorbar(data=plot2.data, aes(ymax=upperCI, ymin=lowerCI, width=0.6),stat="identity", size=0.8, show.legend=FALSE) +
  geom_line(data=plot2.data[!is.na(plot2.data$R),],aes(x=dates, y=R), color='blue', size=0.5) +
  geom_point(data = plot2.data, aes(x=dates, y=R), size=1.2, show.legend=FALSE) 
plot2

Question 3 (1pt)

Question 4 (1pt)

mean_si <- 3.6# Change this to answer Question 4   
std_si <- 2.0

estimates <- wallinga_teunis(epicurve.epiestim$I, 
                             method="parametric_si",
                             config = list(t_start = seq(3, 45), 
                                           t_end = seq(3, 45),
                                           mean_si = mean_si, 
                                           std_si = std_si, 
                                           n_sim = 1000))

plot2.data <- cbind(epicurve, estimates$R$`Mean(R)`,
                    estimates$R$`Quantile.0.025(R)`, estimates$R$`Quantile.0.975(R)`)
names(plot2.data) <- c("dates", "I", "R", "lowerCI", "upperCI")

plot2 <- ggplot(data=plot2.data, aes(x=dates, y=I, labs=FALSE)) + 
  geom_bar(stat="identity", fill="gray45") +
  scale_x_date(date_breaks = "4 days", date_labels = "%m/%d") +
  scale_y_continuous(breaks = seq(0,15, by = 2)) +
  theme(axis.text.x = element_text(angle = 45)) +
  labs(x = "Symptom Onset Date", 
       y = "Number of Cases",
       title = "Epi Curve for Rushmore Academy Norovirus Outbreak") +
  geom_hline(aes(yintercept=1), colour="red", linetype="dashed", size=0.5) +
  geom_errorbar(data=plot2.data, aes(ymax=upperCI, ymin=lowerCI, width=0.6),stat="identity", size=0.8, show.legend=FALSE) +
  geom_line(data=plot2.data[!is.na(plot2.data$R),],aes(x=dates, y=R), color='blue', size=0.5) +
  geom_point(data = plot2.data, aes(x=dates, y=R), size=1.2, show.legend=FALSE) 
plot2

Question 5 (1pt)

Question 6 (0.5 pt)