-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplied_stats_project.Rmd
163 lines (127 loc) · 3.2 KB
/
applied_stats_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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
---
title: "Applied Stats II Project"
author: "Zhenzhao Tu"
date: "3/6/2022"
output:
html_document:
toc: yes
df_print: paged
pdf_document:
toc: yes
---
# Import the dataset
```{r, error = TRUE}
covid = read.csv("Covid_data.csv")
covid
```
```{r, error=TRUE}
last_day = covid[covid$Date == '2022-02-04', ]
last_day
```
```{r, error=TRUE}
library(dplyr)
#library(poweRlaw)
desc_tot_case = last_day %>% arrange(desc(Total.cases))
#desc_tot_case['rate'] = desc_tot_case['Total.deaths']/desc_tot_case['Total.cases']
country <- desc_tot_case[["Country"]]
tot_case <- desc_tot_case[["Total.cases"]]
head_coun = head(country, 30)
head_case = head(tot_case, 30)
barplot(head_case, names.arg = head_coun, las=2)
#desc_tot_case
#aov_test = aov(rate ~ Country, data = head(desc_tot_case, 100))
#summary(aov_test)
```
```{r, error=TRUE}
us = covid[covid$Country == 'US', ]
us['days'] <- c(1:745)
us
```
```{r, error=TRUE}
mo_rate = last_day['Total.deaths']/last_day['Total.cases']
#mo_rate['days'] <- c(1:280)
mo_rate['Country'] = last_day['Country']
mo_rate[mo_rate$Total.deaths < 0.03 & mo_rate$Total.deaths != 0, ]
#mo_rate[mo_rate$Total.deaths != 0.0000000000, ]
#plot(mo_rate$days, mo_rate$Total.deaths)
#lm_test = lm(mo_rate$Total.deaths ~ mo_rate$days)
#summary(lm_test)
#na.omit(mo_rate)
#mo_rate
aov_test = aov(Total.deaths ~ Country, data = head(mo_rate, 10))
summary(aov_test)
```
```{r, error=TRUE}
head_new = head(us$New.cases, 600)
head_day = head(us$days, 600)
tail_new = tail(us$New.cases, 150)
tail_day = tail(us$days, 150)
new_case = lm(New.cases ~ days, data = us)
plot(New.cases ~ days, data = us)
abline(new_case, col = 'red', lwd=3)
abline(v = 600, col="blue", lwd=3, lty=2)
summary(new_case)
new_case_600 = lm(head_new ~ head_day)
plot(head_new ~ head_day)
abline(new_case_600)
summary(new_case_600)
new_case_150 = lm(tail_new ~ tail_day)
plot(tail_new ~ tail_day)
abline(new_case_150)
summary(new_case_150)
```
```{r, error = TRUE}
coef_stderr <- function(...) {
sqrt(diag(vcov(...)))
}
deg_free <- function(fit) {
n <- length(resid(fit))
k <- length(coef(fit))
n - k
}
coef_conf_int <- function(fit, conf.level = 0.95) {
beta <- coef(fit)
se <- coef_stderr(fit)
nu <- deg_free(fit)
tstar <- qt((1 - conf.level) / 2, df = nu, lower.tail = FALSE)
moe <- se * tstar
cbind(lower = beta - moe, upper = beta + moe)
}
```
```{r, error = TRUE}
coef_tstat <- function(fit, beta0 = 0) {
se <- coef_stderr(fit)
beta <- coef(fit)
(beta - beta0) / se
}
coef_conf_int(tot_death)
pt(coef_tstat(tot_death, beta0 = 0.01396)[2], df = deg_free(tot_death))
```
```{r, error=TRUE}
tot_death <- lm(Total.deaths ~ Total.cases, data = us)
plot(Total.deaths ~ Total.cases, data = us, pch = 1)
abline(tot_death, col="red", lwd=3)
summary(tot_death)
```
```{r, error=TRUE}
library(forecast)
plot(Total.cases ~ days, data = us, type='l')
fit <- auto.arima(us$Total.cases)
pred = forecast(fit, 50)
checkresiduals(fit)
plot(pred, type='l')
summary(fit)
```
```{r, error=TRUE}
head_coun
head_case
sum_ten = covid[covid$Country%in%head_coun, ]
```
```{r, error=TRUE}
aov_test = aov(Total.cases ~ Country, data = sum_ten)
summary(aov_test)
```
```{r, error=TRUE}
boxplot(Total.cases ~ Country, data = sum_ten)
901391/76354040
```