############################ # Determine missing packages and load them: required_packages <- c("rstan", "ggplot2","xtable", "MCMCvis", "rjags", "coda", "MCMCpack", "BiasedUrn", "rvest", "runjags", "MASS", "HDInterval", "gridGraphics", "gridExtra", "forestplot", "latex2exp","RCurl","rriskDistributions") not_installed <- required_packages[!(required_packages %in% installed.packages()[ , "Package"])] if(length(not_installed)) install.packages(not_installed) suppressWarnings(lapply(required_packages, require, character.only = TRUE)) ############################ # create functions: invlog <- function(x){exp(x)/(1+exp(x))} logit <- function(x){log(x/(1-x))} cloglog <- function(x){log(-log(1-x))} icloglog <- function(x){1 - exp(-exp(x))} HDIofMCMC = function( sampleVec , credMass=0.95 ) { # Computes highest density interval from a sample of representative values, # estimated as shortest credible interval. # Arguments: # sampleVec # is a vector of representative values from a probability distribution. # credMass # is a scalar between 0 and 1, indicating the mass within the credible # interval that is to be estimated. # Value: # HDIlim is a vector containing the limits of the HDI sortedPts = sort( sampleVec ) ciIdxInc = ceiling( credMass * length( sortedPts ) ) nCIs = length( sortedPts ) - ciIdxInc ciWidth = rep( 0 , nCIs ) for ( i in 1:nCIs ) { ciWidth[ i ] = sortedPts[ i + ciIdxInc ] - sortedPts[ i ] } HDImin = sortedPts[ which.min( ciWidth ) ] HDImax = sortedPts[ which.min( ciWidth ) + ciIdxInc ] HDIlim = c( HDImin , HDImax ) return( HDIlim ) } ############################ # stan options options(mc.cores = parallel::detectCores()) rstan_options(auto_write = TRUE) # Set number of iterations MCMCiter <- 10000 set.seed(123) ################################################# ################################################# ### BUILD THE DATASET ################################################# ################################################# x <- getURL("https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/owid-covid-data.csv") owid <- read.csv(text = x) #write.csv(owid, file= '~/Desktop/UBC/RECODID_ZIKV/COVID/Rcode/owid_july16_2020.csv') # only Europe, and only first week of May: EUR_may1_all <- (owid[owid[,"continent"]%in%"Europe" & owid[,"date"]%in%c("2020-05-01","2020-05-02","2020-05-03","2020-05-04","2020-05-05","2020-05-06","2020-05-07") ,]) # only need certain variables: EUR_may1 <- EUR_may1_all[,c("iso_code", "location", "date", "total_cases", "total_tests", "total_cases_per_million", "aged_70_older", "hospital_beds_per_thousand", "population_density")] # take a quick look at test_units: EUR_may1_all[ EUR_may1_all[,"date"]%in%c("2020-05-01") ,c("iso_code", "location", "date", "total_cases", "total_tests", "total_cases_per_million", "aged_70_older", "hospital_beds_per_thousand", "tests_units")] # only need earliest data available during first week of May: country <- unique(EUR_may1_all[,"iso_code"]) EUR_j <- NULL for(j in 1:length(unique(EUR_may1_all[,"iso_code"]))){ EUR_j <- rbind(EUR_j, na.omit(EUR_may1[EUR_may1[,"iso_code"]%in%country[j],])[1,]) } EUR_may1 <- na.omit(EUR_j) dim(EUR_may1) # Note that France, Sweden and others are missing due to lack of testing data: EUR_may1_all[EUR_may1_all[,"location"]%in%c("France", "Sweden"),] # Add population data: EUR_may1[,"Population"] <- (EUR_may1[,"total_cases"]/EUR_may1[,"total_cases_per_million"])*(1000000) # obtain deaths data from +14 days EUR_may1[,"total_deaths"] <- NULL country <- unique(EUR_may1_all[,"iso_code"]) for(icountry in country){ test_date <- EUR_may1[EUR_may1[, "iso_code"]%in%icountry, "date"] death_date <- as.Date(test_date) +14 EUR_may1[EUR_may1[, "iso_code"]%in%icountry, "total_deaths"] <- owid[owid[,"continent"]=="Europe" & owid[, "iso_code"]%in%c(icountry) & owid[,"date"]%in%paste(death_date), c("total_deaths")] } EUR_dat<-EUR_may1 # restrict countries to EEA country club + GB + CHE EEAclub <- c("Austria", "Belgium", "Bulgaria", "Croatia", "Cyprus", "Czech Republic", "Denmark", "Estonia", "Finland", "France", "Germany", "Greece", "Hungary", "Ireland", "Italy", "Latvia", "Lithuania", "Luxembourg", "Malta", "Netherlands", "Poland", "Portugal", "Romania", "Slovakia", "Slovenia", "Spain", "Sweden", "Iceland", "Liechtenstein", "Norway", "Switzerland", "United Kingdom") EUR_dat <- EUR_dat[EUR_dat[,"location"]%in%EEAclub,] # remove Belgium: EUR_dat <- EUR_dat[EUR_dat[,"location"]!="Belgium",] EUR_dat # adding data from CJC data on CVD rates (Botly et al. (2020)) cjc_dat<-getURL("https://raw.githubusercontent.com/harlanhappydog/COVID19IFR/master/CJC_data.csv") CJC_data <- read.csv(text = cjc_dat) EUR_dat0<-EUR_dat EUR_dat<-merge(EUR_dat, CJC_data[,c("Location", "X65.years..both.","CVD..Both.sexes")], by.x="location", by.y="Location", all.x=TRUE) uhc_data<-getURL("https://raw.githubusercontent.com/harlanhappydog/COVID19IFR/master/UHC.csv") UHC_dat<-read.csv(text = uhc_data) EUR_dat00<-EUR_dat EUR_dat<-merge(EUR_dat, UHC_dat[,c("Country.Code", "UHC_2017")], by.x="iso_code", by.y="Country.Code", all.x=TRUE) EUR_dat[,"UHC"] <- EUR_dat[, "UHC_2017"] EUR_dat<-EUR_dat[, !colnames(EUR_dat)%in%c("UHC_2017")] #EUR_dat<-EUR_dat0 # incorporate seroprevalence studies zz <- getURL("https://raw.githubusercontent.com/harlanhappydog/COVID19IFR/master/serostudies_dates.csv") phi1_all <- read.csv(text=zz)[,1:18] # adjustement to create "aged_70_older" variable phi1_all[,"aged_70_older"] <- 100-phi1_all[,"aged_70_younger"] phi1_all # Find Beta parameters to match IR CI numbers for(k in 1:dim(phi1_all)[1]){ IR_CI <- as.numeric(phi1_all[k,c("IR_low", "IR_high")]/100) ab_param <- get.beta.par(p=c(0.025,0.975),q=IR_CI, plot=FALSE, show.output=FALSE) phi1_all[k,"a_par"] <- ab_param[1] phi1_all[k,"b_par"] <- ab_param[2]} # impute equivalent CC and T numbers (based on beta parameter values phi1_all[,"CC"]<-round(phi1_all[,"a_par"]) phi1_all[,"T"]<-round(phi1_all[,"a_par"]+phi1_all[,"b_par"]) # what is the implied IFR for each of the sero-studies: IFR_raw <- t(apply(rbind(1: dim(phi1_all)[1]),2,function(x){ c(100*prop.test(x=phi1_all[x,"D"], n=phi1_all[x,"P"]*phi1_all[x,c("IR_high")]/100)$conf.int[1], 100*prop.test(x=phi1_all[x,"D"], n=phi1_all[x,"P"]*phi1_all[x,c("IR_low")]/100)$conf.int[2])})) colnames(IFR_raw) <- c("IFR_low", "IFR_high") phi1_all <- cbind(phi1_all , IFR_raw) # prepare to merge the two datasets together: kprime <- dim(phi1_all)[1] colnames(EUR_dat)<-c( "iso_code", "Location", "date", "CC", "T", "total_cases_per_million", "aged_70_older", "hospital_beds_per_thousand", "population_density", "P", "D", "aged_65_older", "CVD", "UHC") phi1_all$iso_code <- c("CHE","DEU","LUX","HRV","CHE") phi1_all<-merge(phi1_all, EUR_dat[,c("iso_code", "UHC","aged_65_older", "CVD"), ], by="iso_code", all.x=TRUE, all.y=FALSE) phi1_all phi1_all<-phi1_all[order(phi1_all$k),] phi1_all$sero<-1 EUR_dat$sero<-0 # merge two datasets: fullEUR <- rbind(phi1_all[,c("sero", "iso_code", "Location", "date","T", "CC","P","D", "aged_70_older", "hospital_beds_per_thousand", "population_density", "UHC", "aged_65_older", "CVD")], EUR_dat[,c("sero", "iso_code", "Location", "date", "T", "CC","P","D","aged_70_older","hospital_beds_per_thousand", "population_density","UHC", "aged_65_older","CVD")]) rownames(fullEUR)<-1:dim(fullEUR)[1] # clean up : fullEUR$CC<-round(fullEUR$CC) fullEUR$P<-round(fullEUR$P) fullEUR$aged_70_older <-round(fullEUR$aged_70_older) fullEUR$aged_65_older <- as.numeric(as.character( sub("%","", as.character(fullEUR$aged_65_older)) )) fullEUR$UHC <- as.numeric(as.character(fullEUR$UHC)) fullEUR$CVD <- as.numeric(as.character( sub("%","", as.character(fullEUR$CVD)) )) #fullEUR$sero <- c(rep(1, dim(phi1_all)[1]), rep(0, dim(fullEUR)[1]-dim(phi1_all)[1])) ## add additional covariate: days_since_first_10infections fullEUR1<-fullEUR fullEUR1 fullEUR1[, "days_since_first_10infections"]<-NULL for(x in 1:dim(fullEUR1)[1]){ con <- (fullEUR1$iso_code)[x] caseall<-owid[owid[,"total_cases"]>=10 & owid[,"continent"]=="Europe", c("date","iso_code", "location", "total_cases")] fullEUR1[x, "days_since_first_10infections"]<- as.numeric(0+as.Date(fullEUR1[x,"date"] ) - as.Date(as.character((caseall[caseall[,"iso_code"]%in%c(con),][1,"date"])))) } ## add additional covariate: days between first case and social distancing fullEUR2<-fullEUR1 d<-NULL for(country in unique(fullEUR2$iso_code)){ days_till_lockdown1<-as.numeric( as.Date(na.omit(owid[owid[,"iso_code"]%in%country & owid[,"stringency_index"]>=20 , c("date","location","total_cases","stringency_index")])[1,"date"]) - as.Date(na.omit(owid[owid[,"iso_code"]%in%country & owid[,"total_cases"]>=1 , c("date","location","total_cases","stringency_index")])[1,"date"]) ) d1<-data.frame(days_till_lockdown=days_till_lockdown1, iso_code=country) d<-rbind(d,d1) } d fullEUR3<-merge(fullEUR2, d, by="iso_code", all.x=TRUE, all.y=FALSE) #fullEUR3 <- na.omit(data.frame(fullEUR3[order(fullEUR3$k),])) dim(fullEUR3) #plot(fullEUR3 [,"days_since_first_10infections"], fullEUR3 [,"population_density"]) #text(fullEUR3[,"Location"], x=fullEUR3 [,"days_since_first_10infections"], y=fullEUR3 [,"days_till_lockdown"]) # reorder the groups in the correct location fullEUR3$Location<-as.factor(as.character(fullEUR3$Location)) fullEUR4 <- fullEUR3[order(1-fullEUR3$sero, fullEUR3$Location),] # scale all covariates: fullEUR4$scale_aged_70_older <- scale(log(fullEUR4$aged_70_older)) fullEUR4$scale_hospital_beds_per_thousand <- scale(log(fullEUR4$hospital_beds_per_thousand)) fullEUR4$scale_days_since_first_10infections <- scale(log(fullEUR4$days_since_first_10infections)) fullEUR4$scale_days_till_lockdown <- scale(log(fullEUR4$days_till_lockdown+1-min(fullEUR4$days_till_lockdown,na.rm=TRUE))) fullEUR4$scale_UHC <- scale(log(fullEUR4$UHC)) fullEUR4$scale_CVD <- scale(log(fullEUR4$CVD)) fullEUR4$scale_aged_65_older <- scale(log(fullEUR4$aged_65_older)) fullEUR4$scale_population_density <- scale(log(fullEUR4$population_density)) fullEUR4 fullEUR4[,"k"] <- 1:dim(fullEUR4)[1] fullEUR4[,"study_names"]<- factor(paste(fullEUR4[,"k"],fullEUR4[,"Location"], sep="- "), levels=c(paste(fullEUR4[,"k"],fullEUR4[,"Location"], sep="- "))) fullEUR4[,"study_names"] fullEUR<-fullEUR4 ## to print: #fullEUR <- na.omit(fullEUR4) fullEUR <- droplevels (fullEUR) fullEUR write.csv(fullEUR, '~/Desktop/UBC/RECODID_ZIKV/COVID/Rcode/EUROPE_DATA.csv') # Printing data tables for manuscript: printEUR<-na.omit(fullEUR[,c("Location", "date", "T", "CC", "P", "D", "aged_70_older", "hospital_beds_per_thousand" , "days_since_first_10infections", "days_till_lockdown", "population_density")]) toprint <- printEUR[,c("Location", "date", "T", "CC", "P", "D")] rownames(toprint)<-1:dim(toprint)[1] print(xtable(toprint, digits=c(rep(0,6), rep(2,(dim(toprint)[2]-6)), 0) ),include.rownames=T) toprint2 <- printEUR[,c("Location", "aged_70_older", "hospital_beds_per_thousand" , "days_since_first_10infections", "days_till_lockdown", "population_density")] rownames(toprint2)<-1:dim(toprint2)[1] print(xtable(toprint2, digits=c(rep(0,3), 2, rep(0,3)) ),include.rownames=T) ################################################# ################################################# ### WRITE OUT MODELS IN STAN ################################################# ################################################# ################################################# ### FIT MODEL WITH ONLY STERO-STUDIES ################################################# stan_mod_simple <- " data { int kprime; int K; int D[K]; int CC[K]; int T[K]; int P[K]; real lambda; } parameters { real gamma; vector[K] phi; vector[K] cloglogIFR; vector[K] cloglogIR; real theta; real beta; real predictIFR; real predictIR; real sd_sig; real sd_tau; } transformed parameters { real icloglogtheta; real icloglogbeta; vector[K] IR; vector[K] IFR; icloglogtheta = 1 - exp(-exp(theta)); icloglogbeta = 1 - exp(-exp(beta)); IR = 1 - exp(-exp(cloglogIR)); IFR = 1 - exp(-exp(cloglogIFR)); } model { gamma ~ exponential(lambda); phi ~ uniform(1, (1+gamma)); cloglogIFR ~ normal(theta, sd_tau); cloglogIR ~ normal(beta, sd_sig); predictIFR ~ normal(theta, sd_tau); predictIR ~ normal(beta, sd_sig); icloglogtheta ~ uniform(0, 1); icloglogbeta ~ uniform(0, 1); sd_sig ~ normal(0, 1); sd_tau ~ normal(0, 0.1); for (k in 1:kprime){ CC[k] ~ binomial(T[k], IR[k]); } if(kprime kprime; int K; int Q; int H; int D[K]; int CC[K]; int T[K]; int P[K]; real lambda; matrix[K,Q] Z; matrix[K,H] X; } parameters { real gamma; vector[K] phi; vector[K] cloglogIFR; vector[K] cloglogIR; real theta; real beta; vector[Q] thetavec; vector[H] betavec; real predictIFR; real predictIR; real sd_sig; real sd_tau; } transformed parameters { real icloglogtheta; real icloglogbeta; vector[K] IR; vector[K] IFR; icloglogtheta = 1 - exp(-exp(theta)); icloglogbeta = 1 - exp(-exp(beta)); IR = 1 - exp(-exp(cloglogIR)); IFR = 1 - exp(-exp(cloglogIFR)); } model { thetavec ~ normal(0, 1); betavec ~ normal(0, 1); gamma ~ exponential(lambda); phi ~ uniform(1, (1+gamma)); cloglogIFR ~ normal(Z*thetavec + theta, sd_tau); cloglogIR ~ normal(X*betavec + beta, sd_sig); icloglogtheta ~ uniform(0, 1); icloglogbeta ~ uniform(0, 1); predictIFR ~ normal(theta, sd_tau); predictIR ~ normal(beta, sd_sig); sd_sig ~ normal(0, 1); sd_tau ~ normal(0, 0.1); for (k in 1:kprime){ CC[k] ~ binomial(T[k], IR[k]); } if(kprime "2020-02-01" & as.Date(as.character(lockdown[,"Date"]), "%Y%m%d")< "2020-04-01" & lockdown[,"CountryCode"]%in%fullEUR$iso_code,c("Date", "CountryCode","H2_Testing.policy"),] library("tidyverse") testingdays_wide <- testingdays %>% spread("Date","H2_Testing.policy") testing_avg <- data.frame(CountryCode=testingdays_wide[,1],testing_avg=rowMeans(testingdays_wide[,-1])) phivars<-cbind(apply(cbind(1:dim(OB)[1]), 1, function(ii) paste("phi[",ii,"]", sep=""))) phivars<-phivars[-c(1:kprime)] phi_est<-data.frame(CountryCode =OB$iso_code[-c(1:kprime)], phi= summary(MAAD)$summary[phivars,"mean"]) compare_testing<-merge(testing_avg,phi_est, by="CountryCode", all=TRUE) compare_testing2<-merge(compare_testing, fullEUR[fullEUR[,"sero"]==0,], by.x="CountryCode", by.y= "iso_code") gg <- ggplot(compare_testing2, mapping= aes(x= phi, y = testing_avg))+ geom_point(aes(size=P), shape = 21, color="white", fill = "lightblue")+ ggrepel::geom_text_repel(aes(label = Location), size = 3, segment.color = NA, point.padding = unit(0.1, "lines")) + theme_classic() + # This scales area to size (not radius), specifies max size, and hides legend scale_size_area(max_size = 20, guide = FALSE) library(dplyr); library(ggplot2) legend_bubbles <- data.frame( label = c("5m", "15m", "40m"), size = c(5E6, 15E6, 40E6) ) %>% mutate(radius = sqrt(size / pi)) gg + scale_y_continuous(limits = c(0, 1.75))+geom_point(data = legend_bubbles, # The "radius/301" was trial and error. Better way? aes(x = 1.65, y = 0.0125 + (radius/345)/140, size = size), shape = 21, color = "black", fill = NA) + geom_text(data = legend_bubbles, size = 2.5, aes(x = 1.65, y = 0.027 + (2.15 * radius/351)/140, label = label)) + annotate("text", x = 1.65, y = 0.24, label = "Population", fontface = "bold") + labs(y= "H2 index", x=expression(phi[k]), size=3) + theme(axis.text=element_text(size=12), axis.title=element_text(size=14)) #+geom_smooth(span=2, se=FALSE, mapping = aes(weight = P))