-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIntroToRWithCovidEDA.Rmd
More file actions
237 lines (169 loc) · 7.25 KB
/
IntroToRWithCovidEDA.Rmd
File metadata and controls
237 lines (169 loc) · 7.25 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
---
title: "Intro to R and COVID-19 EDA"
author: "Julia Layne"
date: "6/29/2020"
output: html_document
---
# Resources for Learning R
### [DataCamp Intro To R](https://www.datacamp.com/courses/free-introduction-to-r)
- Truly introductory
- Code In Browser, has hints
### [Install R](https://cran.revolutionanalytics.com)
- I grabbed the Texas Mirror, but any will work on this page
- [R Install Mirrors](https://cran.r-project.org/mirrors.html)
### [RStudio](https://rstudio.com)
### [RStudio Cloud](https://rstudio.cloud/)
- Allows you to use R before installing. You can work on projects from any computer with a browser.
- [Out of Beta August 2020 - Plans and Pricing](https://rstudio.cloud/plans/free)
### [FreeCodeCamp Intro Video](https://www.youtube.com/watch?v=_V8eKsto3Ug)
- Made by Barton Poulson
- [His R Intro](https://datalab.cc/tools/r01)
- [Intro to R Playlist](https://www.youtube.com/watch?list=PLkk92zzyru5OOYKXfC4OWzc4Lzo_lBOLP&time_continue=22&v=Cl-9aLU292Y&feature=emb_title), I suggest starting at the plot. You can go back to the previous
# R Basics
This is an R Markdown file. It lets you run by code block or output a report with knittr
## Math
```{r}
print("1 + 1")
1 + 1
print("13* 13")
13* 13
print("25 / 5")
25 / 5
print("21 / 5")
21 / 5
print("21 %% 5")
21 %% 5
print("5^3")
5^3
```
## Variables
```{r}
5 -> five
thirteen = 13
print("1 + 1")
1 + 1
print("13* 13")
thirteen* thirteen
print("25 / 5")
(20 + five) / five
print("21 / 5")
21 / five
print("21 %% 5")
21 %% five
print("5^3")
five^3
```
## Vectors
```{r}
names <- c('test','Myocarditis','beta coronavirus','novel corona 2019','Encephalitis','Hepatitis A', 'influenza', 'coronaitis')
names
which_name <- function(name) {
type <- 'Neither'
if(grepl('corona', name,ignore.case = TRUE)){
type <-'corona'
}
if(grepl('itis', name,ignore.case = TRUE)){
type <-'itis'
}
type
}
#testing top dataset to confirm it is working
sapply(names, which_name,simplify = TRUE)
```
# COVID DATASET
> - From the [John Hopkins dataset](https://github.com/CSSEGISandData/COVID-19) avaiable on GitHub
> - COVID-19/csse_covid_19_data/csse_covid_19_daily_reports/
## Read in Files
Change with new COVID file for most up to date info
```{r}
covid_raw = read.csv("06-28-2020-us.csv",header = TRUE)
#covid_ts_confirmed = read.csv(file.choose(),header = TRUE)
```
## Look at Raw Data
```{r}
head(covid_raw)
#covid_ts_confirmed
```
# Plot Data
Initial lookinto ploting data points
Note here
```{r}
#install.packages("tidyverse")
library(tidyverse)
library(ggplot2)
library(GGally)
library(plotly)
plot(covid_raw)
byState10 <- covid_raw %>% head(10) %>% ggplot(aes(x=Deaths, y=Confirmed, fill=Province_State)) + geom_point(aes(fill=Province_State)) + ggtitle("Deaths vs Confirmed Cases in First Ten Provinces")
ggplotly(byState10)
byState <- covid_raw %>% ggplot(aes(x=Deaths, y=Confirmed, fill=Province_State)) + geom_point(aes(fill=Province_State)) + ggtitle("Deaths vs Confirmed Cases per Province")
ggplotly(byState)
byStateNoNY <- filter(covid_raw, Province_State != 'New York' & Province_State != 'New Jersey') %>% ggplot(aes(x=Deaths, y=Confirmed, fill=Province_State)) + geom_point(aes(fill=Province_State)) + ggtitle("Deaths vs Confirmed Cases per Province")
ggplotly(byStateNoNY)
#covid_raw %>% head(10) %>% ggplot(aes(x=Deaths, y=Confirmed, fill=Province_State) + geom_point(aes(fill=Province_State)) + ggtitle("Deaths vs Confirmed Cases in Top Countries (Minus China)")
```
```{r}
statesAbove700Deaths <- filter(covid_raw, Deaths >= 700) %>% ggplot(aes(x=Deaths, y=Confirmed, fill=Province_State)) + geom_point(aes(fill=Province_State)) + ggtitle("Over 700 Mortality States")
ggplotly(statesAbove700Deaths)
statesAbove700Deaths <- filter(covid_raw, Deaths < 700) %>% ggplot(aes(x=Deaths, y=Confirmed, fill=Province_State)) + geom_point(aes(fill=Province_State)) + ggtitle("Under 700 Mortality States")
ggplotly(statesAbove700Deaths)
```
# Map data using Fips codes
```{r}
library(e1071)
library(usmap)
UScovid_dataset <- filter(covid_raw, Country_Region == 'US' & FIPS != 'NA')
UScovid_dataset
#UScovid_dataset$fips <- fips(brew_count_by_state$state)
attach(UScovid_dataset)
UScovid_dataset_fips <- UScovid_dataset[order(FIPS),]
detach(UScovid_dataset)
UScovid_dataset_fips$fips = UScovid_dataset_fips$FIPS
plot_usmap(data = UScovid_dataset_fips,
values = "Deaths",
color = rgb(.2, .7, 1)) +
labs(title = "Covid Deaths by State",
subtitle = "Count of Covid19 Deaths per state") +
scale_fill_continuous(low = "white", high = rgb(.2, .7, 1),
name = "Deaths per state", label = scales::comma) + theme(legend.position = "right")
plot_usmap(data = filter(UScovid_dataset_fips, Province_State != 'New York'), values = "Deaths", color = rgb(.2, .7, 1)) +
labs(title = "Covid Deaths by State (New York Removed)", subtitle = "Count of Covid19 Deaths per state") +
scale_fill_continuous(low = "white", high = rgb(.2, .7, 1), name = "Deaths per state", label = scales::comma) + theme(legend.position = "right")
plot_usmap(data = filter(UScovid_dataset_fips, Province_State != 'New York' & Province_State != 'New Jersey'), values = "Deaths", color = rgb(.2, .7, 1)) +
labs(title = "Covid Deaths by State (New York Removed)", subtitle = "Count of Covid19 Deaths per state") +
scale_fill_continuous(low = "white", high = rgb(.2, .7, 1), name = "Deaths per state", label = scales::comma) + theme(legend.position = "right")
```
# Previous Work - Finding the top Countries by Confirmed Cases
## Read in Files
Change with new COVID file for most up to date info
```{r}
covid_raw_world = read.csv("06-28-2020.csv",header = TRUE)
#covid_ts_confirmed = read.csv(file.choose(),header = TRUE)
head(covid_raw_world)
```
```{r}
confirmed_by_country <- covid_raw_world%>% group_by(Country_Region) %>% tally(Confirmed, name = "Confirmed", sort = TRUE)
head(confirmed_by_country)
deaths_by_country <- covid_raw_world%>% group_by(Country_Region) %>% tally(Deaths, name = "Deaths", sort = TRUE)
head(deaths_by_country)
totals <- merge(confirmed_by_country, deaths_by_country, by="Country_Region")
head(totals)
```
Then reordered by Confirmed
```{r}
#order(totals$Confirmed, decreasing = TRUE)
#totals$Confirmed
#totals[180,]
top_to_least <- totals[order(totals$Confirmed, decreasing = TRUE),]
head(top_to_least)
```
```{r}
top10Confirmed <- top_to_least %>% head(10) %>% ggplot(aes(x=Deaths, y=Confirmed, fill=Country_Region)) + geom_point(aes(fill=Country_Region)) + ggtitle("Deaths vs Confirmed Cases in Top countries")
ggplotly(top10Confirmed)
# At the time, China was the highest and I wanted to look at the rest, now it is much different
top10ConfirmedMinusUS <- subset(top_to_least, Country_Region != "US") %>% head(10) %>% ggplot(aes(x=Deaths, y=Confirmed, fill=Country_Region)) + geom_point(aes(fill=Country_Region)) + ggtitle("Deaths vs Confirmed Cases in Top Countries (Minus US)")
ggplotly(top10ConfirmedMinusUS)
# Now removing US instead
top10ConfirmedMinusUSandB <- subset(top_to_least, Country_Region != "US" & Country_Region != "Brazil") %>% head(10) %>% ggplot(aes(x=Deaths, y=Confirmed, fill=Country_Region)) + geom_point(aes(fill=Country_Region)) + ggtitle("Deaths vs Confirmed Cases in Top Countries (Minus US and Brazil)")
ggplotly(top10ConfirmedMinusUSandB)
```