-
Notifications
You must be signed in to change notification settings - Fork 0
/
Covid_Project.Rmd
124 lines (85 loc) · 3.45 KB
/
Covid_Project.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
122
123
124
---
title: "MA2 Project"
output: word_document
---
```{r setup, include=FALSE}
knitr::opts_knit$set(root.dir = getwd())
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r, include=TRUE, echo=TRUE}
# c.data = read.csv("data/Covid19-FinalData.csv", header=T)
c.data = read.csv("data/Covid-Final.csv", header=T)
str(c.data)
c.data_eda=c.data
c.data$Year = as.factor(c.data$Year)
c.data$Week = as.factor(c.data$Week)
head(c.data,1000)
```
```{r}
library(dplyr)
library(tidyverse)
library(broom)
# Dont need since we dont look at whole years data
# ReturnWeeks <-function(year){
# if (year==2015){
# print(year)
# }
# else {
# ReturnWeeks(year-1)
# print(year)
# }
# }
# filter(c.data_eda,Year==2015)$Week
state_trend = filter(c.data_eda,Year<2020) %>% group_by(State) %>% do(model = lm(Deaths ~ Year, data = .)) %>% rowwise() %>% tidy(model)
state_trend
death_stdv=filter(c.data_eda,Year<2020) %>% group_by(State) %>% do(as.data.frame(t(sd(.$Deaths))))
death_stdv
# filter(state_trend,State=='Alabama')$estimate[2]*2020+filter(state_trend,State=='Alabama')$estimate[1]
state_equations=spread(select(state_trend,State,term,estimate),term,estimate)
# c.data_eda$Estimates = c.data_eda %>% rowwise(.) %>% do(filter(state_equations,State==.$State)$"(Intercept)"+filter(state_equations,State==.$State)$Year*.$Year)
#this takes a little while
est = c.data_eda %>% rowwise(.) %>%
do(as.data.frame(t(filter(state_equations,State==.$State)$"(Intercept)"+filter(state_equations,State==.$State)$Year*.$Year)))
est$V1
c.data_eda$Estimated=est$V1
c.eda= inner_join(c.data_eda,death_stdv, by="State")
# write.csv(c.eda,'data/CVT.csv')
# filter(select(state_equations,"(Intercept)"),"(Intercept)"==2019)
#
# filter(state_equations,State=='Alabama')$"(Intercept)"+filter(state_equations,State=='Alabama')$Year*2020
#
#
# state_equations$"(Intercept)"+state_equations$Year*2020
```
```{r, include=TRUE, echo=TRUE}
#poisson
#we should instead create a weekly baseline trend indexed by state rather then treat each week as
#then test for seasonal (my impression is those are small) and see if we can just collapse
#the deaths to a weekly baseline with std deviation
c.glm_p = glm(Deaths~Week+Covid+PopDensity+Population+Year+TAVG,
data=c.data, family=poisson)
summary(c.glm_p)
print("The AIC of Poisson Log linear Model is ")
AIC(c.glm_p)
print("The BIC of Poisson Log linear Model is ")
BIC(c.glm_p)
```
```{r, include=TRUE, echo=TRUE}
#Poisson with RE
library(lme4)
m.re1 = glmer(Deaths~Week+Covid+PopDensity+Population+Year+TAVG+(1|State),
data=c.data, family=poisson)
summary(m.re1)
print("The AIC of Poisson Log linear Model with different Intercepts is ")
AIC(m.re1)
print("The BIC of Poisson Log linear Model with different Intercepts is ")
BIC(m.re1)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.