-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCOVID-19 Spread curve.Rmd
109 lines (75 loc) · 2.44 KB
/
COVID-19 Spread curve.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
---
title: "COVID-19 Spread curve"
author: "Pastor Soto"
date: "10/17/2021"
output: html_document
---
```{r warning=FALSE}
library(MASS)
library(nnet)
library(readr)
library(investr)
library(ggplot2)
#install.packages("BiocManager"); BiocManager::install("xcms")
library(xcms)
```
```{r warning=FALSE}
#options(scipen = 999)
################################
file_path = "Data"
confirmed_cases <- read_csv(file.path(file_path, "covid.csv"))
confirmed_cases$date <- as.Date(confirmed_cases$date, format = "%d/%m/%Y")
a=confirmed_cases
a$x=seq(length(confirmed_cases$date))
a$y=a$total_cases
gauss_model <- nls(new~SSgauss(x, mu, sigma, h), data = a) #nls to choose the best fit parameters
xnew <- 174:300 #prediction range
#create logistic model
y.predict.gauss <- predict(gauss_model, newdata = list(x=xnew))
#predict.gaus_model
## with CI
#range of the graph
xnew <- 1:300
#create the confidence interval for the logaritmic model
y.pred.conf.gauss_model <- investr::predFit(gauss_model, newdata=list(x=xnew), interval = 'confidence')
#Plot both models with the real data
date1 <- seq(as.Date("2020-02-22"), as.Date("2020-12-17"), by="1 day")
y.pred.conf.gauss_model <- data.frame(y.pred.conf.gauss_model, date1)
```
```{r warning=FALSE}
ggplot(y.pred.conf.gauss_model, aes(date1,fit))+
geom_line()+
geom_point(data=a, aes(date,new,colour="red"))+
xlab("")+
ylab("New cases daily")+
scale_x_date(date_labels = "%Y-%m-%d")+
geom_linerange(aes(ymin=lwr, ymax=upr))
```
```{r}
##Logistic model
log_model <- nls(y~SSlogis(x, phi1, phi2, phi3), data = a) #nls to choose the best fit parameters
summary(log_model)
xnew <- 174:350 #prediction range
#create logistic model
y.predict.log_model <- predict(log_model, newdata = list(x=xnew))
#predict.log_model
## with CI
#range of the graph
xnew <- 1:350
#create the confidence interval for the logaritmic model
y.pred.conf.log_model <- investr::predFit(log_model, newdata=list(x=xnew), interval = 'confidence')
tail(y.pred.conf.log_model)
#Plot both models with the real data
date1 <- seq(as.Date("2020-02-22"), as.Date("2021-02-05"), by="1 day")
y.pred.conf.log_model <- data.frame(y.pred.conf.log_model, date1)
```
```{r}
ggplot(y.pred.conf.log_model, aes(date1,fit))+
geom_line()+
geom_point(data=a, aes(date,y, color = "red"))+
xlab("")+
ylab("Total number of confimed cases")+
labs(color = "Logis Model")+
scale_x_date(date_labels = "%Y-%m-%d")+
geom_linerange(aes(ymin=lwr, ymax=upr))
```