-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcovid-19 prediction.R
33 lines (24 loc) · 949 Bytes
/
covid-19 prediction.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
# read data from a csv file
data = read.csv("covid19.csv", header = T)
# view data
head(data)
# create samples from the data
samples = sample(1:16, size = 16*0.8)
## Predict model based on cases and days
# build an exponential regression model
model = lm(log(Cases) ~ Day + I(Day^2) , data = data[samples,])
# look at the summary of the model
summary(model)
# save model
saveRDS(model, file = "Day_TotalCases.RDS")
# predict for new data
prediction = exp(predict(model, data.frame(Day = 1:21)))
# put actual and predictions into a data frame
d2 = data.frame(Day = 1:21, floor(prediction), actual = c(data$Cases, rep(NA, 5)))
d2
#Plot the data
ggplot(d2[,1:2], aes(x = Day, y = prediction)) +
geom_point(colour = "blue") +
geom_point(na.omit(d2[,-2]), colour = "red", mapping = aes(x = Day, y = actual)) +
ggtitle("COVID-19 Prediction for Michigan: Red - Actual & Blue - Prediction") +
theme(plot.title = element_text(hjust = 0.5))