Advertisement
Guest User

Untitled

a guest
Apr 15th, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1.  
  2. owid = read.csv(paths$owid, stringsAsFactors = F)
  3. owid$Date = as.Date(owid$Date, format = "%b %d, %Y")
  4. colnames(owid)[4:5] = c("Tests", "Cases")
  5.  
  6. sel = c("AUT", "AUS", "DEU", "USA", "GBR", "FRA", "ESP", "ISL", "BRA",
  7. "BRA", "LVA", "NLD", "BEL", "ISL")
  8.  
  9. owid = owid[owid$Code %in% sel, ]
  10.  
  11. codes = unique(owid$Code)
  12. owid2 = NULL
  13. for(i in 1:length(codes)){
  14. sub = owid[owid$Code == codes[i], ]
  15. sub = sub[sub$Cases >= 100, ]
  16. idx_na = which(!is.na(sub$Tests))
  17.  
  18. if(length(idx_na) > 4){
  19.  
  20. sub = sub[idx_na[1]:tail(idx_na, 1), ]
  21. sub$Tests = approx(sub$Date, sub$Tests, n = nrow(sub))$y
  22. owid2 = rbind(owid2, sub)
  23. }
  24. }
  25. rownames(owid2) = NULL
  26.  
  27.  
  28. codes = unique(owid2$Code)
  29. slopes = as.data.frame(matrix(nrow = length(codes), ncol = 2))
  30. colnames(slopes) = c("Cases", "Tests")
  31. par(mfrow = c(3, 4))
  32. for(i in 1:length(codes)){
  33. sub = owid2[owid2$Code == codes[i], ]
  34. sub = sub[1:20,]
  35. sub$idx = 1:nrow(sub)
  36. slopes[i, 1] = coef(lm(log(Cases)~idx, sub[1:20,]))[2]
  37. slopes[i, 2] = coef(lm(log(Tests)~idx, sub[1:20,]))[2]
  38.  
  39. par(bg = "Black", fg = "White")
  40.  
  41. plot(sub$Date, sub$Tests,
  42. col.lab = "White",
  43. col.axis = "White",
  44. col.main = "White",
  45. ylim = c(1, max(sub$Tests, na.rm = T)),
  46. xlab = "",
  47. ylab = "# per day",
  48. type = "b",
  49. col = "Light Blue",
  50. log = "y",
  51. main = sub$Entity[1])
  52. lines(sub$Date, sub$Cases, type = "b", col = "Red")
  53.  
  54. abline(h = axTicks(2),
  55. v = axTicks.Date(1, trak$date),
  56. lty = 3,
  57. col = "Light Grey")
  58. legend("bottomright",
  59. legend = c(paste0("Tests (", round(slopes[i, 2], 2), ")"),
  60. paste0("Cases (", round(slopes[i, 1], 2), ")")),
  61. bty = "n",
  62. col = c("Light Blue", "Red"),
  63. lwd = 2)
  64.  
  65. }
  66.  
  67. slopes = log(2)/slopes
  68.  
  69. hist(slopes$Cases,
  70. col.lab = "White",
  71. col.axis = "White",
  72. col.main = "White",
  73. col = rgb(1, 0, 0, 0.75),
  74. xlab = "log(2)/slope",
  75. xaxs = "i",
  76. yaxs = "i",
  77. main = "Cases",
  78. breaks = seq(0, 16, by = 1))
  79. hist(slopes$Tests, add = T,
  80. col.lab = "White",
  81. col.axis = "White",
  82. col.main = "White",
  83. col = rgb(0, 0, 1, 0.5),
  84. xlab = "log(2)/Tests",
  85. xaxs = "i",
  86. yaxs = "i",
  87. main = "Tests",
  88. breaks = seq(0, 16, by = 1))
  89.  
  90. legend("topright", legend = c("Tests", "Cases"),
  91. bty = "n",
  92. col = c("Blue", "Red"),
  93. lwd = 2)
  94.  
  95.  
  96.  
  97. plot(slopes$Tests, slopes$Cases,
  98. col.lab = "White",
  99. col.axis = "White",
  100. col.main = "White",
  101. xlab = "log(2)/slope(Tests)",
  102. ylab = "log(2)/slope(Cases)",
  103. type = "p",
  104. col = "Red",
  105. main = "Tests vs Cases")
  106. abline(h = axTicks(2),
  107. v = axTicks(1),
  108. lty = 3,
  109. col = "Light Grey")
  110.  
  111. m = round(median(slopes$Cases/slopes$Tests, na.rm = T), 2)
  112. lines(slopes$Tests, m*slopes$Tests, col = "Red")
  113.  
  114. legend("topleft", legend = paste0(m, "*log(2)/slope(Tests)"),
  115. bty = "n",
  116. col = c("Red"),
  117. lwd = 2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement