-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcovid-r-report.Rmd
123 lines (87 loc) · 3.04 KB
/
covid-r-report.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
---
title: "England COVID-19 plot"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library('jsonlite')
library('ggplot2')
library('tidyverse')
library('lubridate')
library('xts')
```
## Execute code
Run
```{r, eval=F, echo=T}
main-script.R
source("main-script.R")
```
## Positive test cases
The positive test cases for each local authority (LA) are given.
```{r lacovid, echo=FALSE}
# Open Rproj to setwd() nicely
source("includes.R")
coronavirusdata <-'https://c19downloads.azureedge.net/downloads/json/coronavirus-cases_latest.json'
jsonData<- fromJSON(coronavirusdata)
# Local Authority (LA) data
data<-jsonData$ltlas
data$date<-as.Date(data$specimenDate, "%Y-%m-%d")
# stores images here
imagesFolder <-'media'
inputDataFolder<-'indata'
localLAs <-levels(as.factor(data$areaName))
# plot charts per LA
plots <- lapply(localLAs,FNplotting)
```
## The estimated R value for England is given. This is based on
$R=\frac {d\ln(N)}{dt}$
where ${ln(N)}$ is the natural log of the cumulative number of test cases.
This is taken from [wikipedia](https://en.wikipedia.org/wiki/Basic_reproduction_number#Estimation_methods) as a quick option; I'm sure it's not perfect.
```{r rvalengland, echo=FALSE}
#############################################
# Estimate and plot R for whole of England
aggregateByDate<-aggregate(data$dailyLabConfirmedCases, by=list(Category=data$specimenDate), FUN=sum)
# Category= date e.g. 2020-04-20
# x is sum across all LAs
# was hoping to see something in autocorrelation at about lag =14 (2 weeks)
# acf(aggregateByDate$x,max.lag=40)
# Remove N/A and set to zero
aggregateByDate<-aggregateByDate %>%
fill(x)
# change daily totals to Weekly for R value by week estimation.
# Too much variation in daily rates
aggregatesAsXTS <- as.xts(aggregateByDate$x,order.by=as.Date(aggregateByDate$Category))
weeklySum<-apply.weekly(aggregatesAsXTS ,sum)
# length of x for finite difference
xlen=length(weeklySum[,1])
h<-seq(1,xlen)
weeklyCumSum = cumsum(weeklySum[,1])
weeklyLNCumSum = log(cumsum(weeklySum[,1]))
r<-finite.differences(h, coredata(weeklyLNCumSum))
area="England"
path<-paste("./",imagesFolder,"/R-value-for-",area,".png", sep="")
df <-data.frame(index(weeklyLNCumSum), r)
colnames(df)<-c('date','R value')
title <-paste('Estimated R value for',area)
p <- ggplot(data=df, aes(x = date, y = `R value`))+geom_point() +ggtitle(title)+theme(title =element_text(size=7 ), axis.text.x = element_text(color = "grey20", size = 8, angle = 45, hjust = .5, vjust = .5, face = "plain"),
panel.grid.minor = element_blank())
print(p)
# save England R plot
ggsave(
path,
plot = p,
device = "png",
scale = 1,
width = 8,
height = 4,
units = c("in"),
dpi = 150,
limitsize = TRUE,
)
```
## The estimated R value for each LA is given
```{r rvals, echo=FALSE}
#############################################
# Estimate and plot R by week for each local authority (LA)
plotsForRVals <- lapply(localLAs,FNplotR)
```