Skip to content

Instantly share code, notes, and snippets.

@affans
Created June 11, 2021 02:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save affans/fc56c6b4b841071b747fbf4db1a6935d to your computer and use it in GitHub Desktop.
Save affans/fc56c6b4b841071b747fbf4db1a6935d to your computer and use it in GitHub Desktop.
## Affan's reproduce
library(distributions3)
library(ggplot2)
library(tidyverse)
library(R.utils)
library(zoo)
library(tictoc)
#load("formatted_US_death_data.rds")
#US.deaths<-US.deaths %>% filter(DailyDeaths>=0)
# Seyed's data now
US.deaths = fread("./USData.csv")
US.deaths$DailyDeaths = as.numeric(US.deaths$DailyDeaths)
US.deaths$Date = as.Date(US.deaths$Date)
#Best-Guess Estimate for Infection Fatality Ratio (Derived from Lancet Paper)
Cases_Distr <- function(Deaths){
ifr = rlnorm(1,meanlog = log(0.2), 0.25)
ifr2 = rlnorm(1,meanlog = log(2.58), 0.212)
#Deaths$Infections <- Deaths$DailyDeaths/ifr*100 ###Move this to the end
df = data.frame(InfectedDate=NA,Country=NA,Count=NA)
for (i in 1:nrow(Deaths)){
x<-Deaths[i,]
#print(x$Infections)
lags <- rlnorm(x$DailyDeaths, meanlog = log(13.2), sdlog = 0.24)
incubationperiods = rlnorm(x$DailyDeaths, 1.434, 0.661)
lags = lags + incubationperiods
lagdates <- x$Date - lags
if (length(lagdates)==0){
next
} else{
s <- data.frame(Count=summary(as.factor(as.character(lagdates))))
s$InfectedDate <-rownames(s)
s$Country=x$Country
s<-s[c(2,3,1)]
df <- rbind(df,s)
}
}
df <- df %>% group_by(InfectedDate,Country) %>% summarize(Infected = sum(Count)) %>% arrange(InfectedDate) %>%
filter(InfectedDate!="(Other)")
#print(df[1:10,])
df$InfectedDate<-as.Date(df$InfectedDate)
df$Country<-as.factor(df$Country)
df$Infected = 0.31*df$Infected/(ifr/100) + 0.69*df$Infected/(ifr2/100)
return(df)
}
#Deaths = data.frame (US.deaths)
# add country
MCRuns <- function(Deaths,n){
trials <- data.frame(InfectedDate=NA,Country=NA,Infected=NA,run=NA)
pb <- txtProgressBar(min = 0, max = n, style = 3)
for (i in seq(1,n,1)){
setTxtProgressBar(pb, i)
draw <- Cases_Distr(Deaths)
draw$run <- i
trials <- bind_rows(trials,draw)
names(trials)<-c("InfectedDate","Country","Infected","run")
}
return(trials)
}
tic()
lancet <- MCRuns(US.deaths, n=30)
toc()
lln2 = lancet %>% group_by(InfectedDate) %>% summarise(infmean = mean(Infected))
plot(lln2$infmean)
sum(na.omit(lln2$infmean))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment