-
Notifications
You must be signed in to change notification settings - Fork 0
/
dpSARS-COV2.R
136 lines (104 loc) · 3.54 KB
/
dpSARS-COV2.R
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
library(RCurl)
library(jsonlite)
library(tidyverse)
library(gridExtra)
#
covid_df<-jsonlite::fromJSON(RCurl::getURL("https://coronavirus-tracker-api.herokuapp.com/all"))
myexfn<-function(country_code){
myn<-which(covid_df$confirmed$locations$country_code==country_code)
crapdates<-names(covid_df$confirmed$locations$history[myn[1],]) %>% as.Date(format="%m/%d/%y")
cvalues<-as.numeric(base::colSums(covid_df$confirmed$locations$history[myn,]))
retdat<-data.frame(Date=crapdates,Cvalues=cvalues) %>%
dplyr::arrange(Date)
colnames(retdat)<-c("Date",country_code)
return(retdat)
}
if(FALSE){
purrr::map(.x=list("CH","IT","DE","FR","GR"),.f=myexfn) %>%
reduce(.f=dplyr::full_join,by="Date")
jnk<-which(covid_df$confirmed$locations$country_code=="GB")
covid_df$confirmed$locations$province[jnk]
}
purrr::map(.x=list("CH","IT","DE","FR","GB","ES"),.f=myexfn) %>%
reduce(.f=dplyr::full_join,by="Date") %>%
tidyr::gather(Country,Value,-Date) %>%
ggplot(aes(x=Date,y=Value,colour=Country)) +
geom_point() +
geom_line() +
scale_y_log10() +
labs(x=NULL,y="Confirmed cases (log scale)",
title="COVID-19 confirmed cases (CSSE John Hopkins)") +
theme(legend.position="bottom")
purrr::map(.x=list("CH","IT","DE","FR","GB","ES","US"),.f=myexfn) %>%
reduce(.f=dplyr::full_join,by="Date") %>%
dplyr::select(-Date) %>%
dplyr::mutate_all(~c(NA,diff(log(.)))) %>%
tail(10)
purrr::map(.x=list("CH","IT","DE","FR","GB","ES","US"),.f=myexfn) %>%
reduce(.f=dplyr::full_join,by="Date") %>% tail(10)
(
mydf<-myexfn("CH") %>%
dplyr::filter(CH > 100) %>%
dplyr::mutate(
Label=format(Date,"%m-%d"),
t=1:n(),
dCH=CH-dplyr::lag(CH,1),
gr=dCH/CH,
egr=c(NA,base::diff(log(CH)))
)
)
summary(myfit<-lm(log(CH)~t,data=mydf))
mydf$fit<-exp(myfit$fitted.values)
mydf$dfit<-c(NA,diff(mydf$fit))
mydf
mydbl<-log(2)/myfit$coefficients[2]
#library(scales)
#show_col(hue_pal()(4))
P1<-mydf %>%
ggplot() +
geom_point(aes(x=t,y=CH),color="#F8766D") +
geom_point(aes(x=t,y=fit),color="#00BFC4") +
geom_line(aes(x=t,y=fit),color="#00BFC4") +
scale_y_log10() +
annotate("text", x=5, y=5000, label=sprintf("Doubling time: %.2f days",mydbl))
P2 <- mydf %>%
dplyr::select(Label,dCH,dfit) %>%
tidyr::gather(Variable,Value,-Label) %>%
ggplot(aes(x=Label,y=Value,fill=Variable)) +
geom_bar(stat="identity",position=position_dodge()) +
theme(legend.position="bottom",axis.text.x = element_text(angle = 90))
grid.arrange(P1,P2)
if (FALSE) {
jnk<-read_csv("https://raw.githubusercontent.com/daenuprobst/covid19-cases-switzerland/master/covid19_cases_switzerland.csv")
(
mydf<-jnk %>%
dplyr::select(Date,CH) %>%
dplyr::mutate(
Label=format(Date,"%m-%d"),
t=1:n(),
dCH=CH-dplyr::lag(CH,1),
gr=dCH/CH,
egr=c(NA,base::diff(log(CH)))
)
)
summary(myfit<-lm(log(CH)~t,data=mydf))
mydf$fit<-exp(myfit$fitted.values)
mydf$dfit<-c(NA,diff(mydf$fit))
mydf
mydbl<-log(2)/myfit$coefficients[2]
P1<-mydf %>%
ggplot() +
geom_point(aes(x=t,y=CH),color="#F8766D") +
geom_point(aes(x=t,y=fit),color="#00BFC4") +
geom_line(aes(x=t,y=fit),color="#00BFC4") +
scale_y_log10() +
annotate("text", x=5, y=5000, label=sprintf("Doubling time: %.2f days",mydbl))
P2 <- mydf %>%
dplyr::select(Label,dCH,dfit) %>%
tidyr::gather(Variable,Value,-Label) %>%
ggplot(aes(x=Label,y=Value,fill=Variable)) +
geom_bar(stat="identity",position=position_dodge()) +
theme(legend.position="bottom",axis.text.x = element_text(angle = 90))
grid.arrange(P1,P2)
jnk$CH
}