Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- owid = read.csv(paths$owid, stringsAsFactors = F)
- owid$Date = as.Date(owid$Date, format = "%b %d, %Y")
- colnames(owid)[4:5] = c("Tests", "Cases")
- sel = c("AUT", "AUS", "DEU", "USA", "GBR", "FRA", "ESP", "ISL", "BRA",
- "BRA", "LVA", "NLD", "BEL", "ISL")
- owid = owid[owid$Code %in% sel, ]
- codes = unique(owid$Code)
- owid2 = NULL
- for(i in 1:length(codes)){
- sub = owid[owid$Code == codes[i], ]
- sub = sub[sub$Cases >= 100, ]
- idx_na = which(!is.na(sub$Tests))
- if(length(idx_na) > 4){
- sub = sub[idx_na[1]:tail(idx_na, 1), ]
- sub$Tests = approx(sub$Date, sub$Tests, n = nrow(sub))$y
- owid2 = rbind(owid2, sub)
- }
- }
- rownames(owid2) = NULL
- codes = unique(owid2$Code)
- slopes = as.data.frame(matrix(nrow = length(codes), ncol = 2))
- colnames(slopes) = c("Cases", "Tests")
- par(mfrow = c(3, 4))
- for(i in 1:length(codes)){
- sub = owid2[owid2$Code == codes[i], ]
- sub = sub[1:20,]
- sub$idx = 1:nrow(sub)
- slopes[i, 1] = coef(lm(log(Cases)~idx, sub[1:20,]))[2]
- slopes[i, 2] = coef(lm(log(Tests)~idx, sub[1:20,]))[2]
- par(bg = "Black", fg = "White")
- plot(sub$Date, sub$Tests,
- col.lab = "White",
- col.axis = "White",
- col.main = "White",
- ylim = c(1, max(sub$Tests, na.rm = T)),
- xlab = "",
- ylab = "# per day",
- type = "b",
- col = "Light Blue",
- log = "y",
- main = sub$Entity[1])
- lines(sub$Date, sub$Cases, type = "b", col = "Red")
- abline(h = axTicks(2),
- v = axTicks.Date(1, trak$date),
- lty = 3,
- col = "Light Grey")
- legend("bottomright",
- legend = c(paste0("Tests (", round(slopes[i, 2], 2), ")"),
- paste0("Cases (", round(slopes[i, 1], 2), ")")),
- bty = "n",
- col = c("Light Blue", "Red"),
- lwd = 2)
- }
- slopes = log(2)/slopes
- hist(slopes$Cases,
- col.lab = "White",
- col.axis = "White",
- col.main = "White",
- col = rgb(1, 0, 0, 0.75),
- xlab = "log(2)/slope",
- xaxs = "i",
- yaxs = "i",
- main = "Cases",
- breaks = seq(0, 16, by = 1))
- hist(slopes$Tests, add = T,
- col.lab = "White",
- col.axis = "White",
- col.main = "White",
- col = rgb(0, 0, 1, 0.5),
- xlab = "log(2)/Tests",
- xaxs = "i",
- yaxs = "i",
- main = "Tests",
- breaks = seq(0, 16, by = 1))
- legend("topright", legend = c("Tests", "Cases"),
- bty = "n",
- col = c("Blue", "Red"),
- lwd = 2)
- plot(slopes$Tests, slopes$Cases,
- col.lab = "White",
- col.axis = "White",
- col.main = "White",
- xlab = "log(2)/slope(Tests)",
- ylab = "log(2)/slope(Cases)",
- type = "p",
- col = "Red",
- main = "Tests vs Cases")
- abline(h = axTicks(2),
- v = axTicks(1),
- lty = 3,
- col = "Light Grey")
- m = round(median(slopes$Cases/slopes$Tests, na.rm = T), 2)
- lines(slopes$Tests, m*slopes$Tests, col = "Red")
- legend("topleft", legend = paste0(m, "*log(2)/slope(Tests)"),
- bty = "n",
- col = c("Red"),
- lwd = 2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement