This code will do the following:
Download the daily update file from Mass.gov, which catalogues the new confirmed cases Read the file and clean the data so it can be analyzed by the R code Create a dataframe summarizing totals for the week Plot the summary Create a second dataframe which plots the data in a log tranfirmation
library(dplyr)
library(lubridate)
library(ggplot2)
library(readxl)
library(tidyverse)
library(data.table)
The data is downloaded from the Mass.Gov website. The filename recently changed so the code parses the file name based on the date. Once downloaded the file gets a simpler name of “cases.xlsx”.
df2<- ""
df3<- ""
df5<- ""
p <- ""
setwd("~/Documents/R Coding")
FirstPart = "https://www.mass.gov/doc/covid-19-raw-data-"
a <- Sys.Date()
t <- as.numeric(format(Sys.time(), "%H"))
a <- fifelse(t > 17, yes = a, no = a-1)
DatePart<- gsub("(\\D)0", "\\1", format(as.Date(a[1]), "%B-%d-%Y"))
EndPart <- "/download"
LinkPart <- str_to_lower(paste(FirstPart,DatePart,EndPart, sep = ""))
download.file(LinkPart, "cases.xlsx")
df2 <- read_excel("cases.xlsx", sheet = "Cases (Report Date)")
This cleans the date column for further analysis
df2$Date <- format(as.POSIXct(df2$Date,format='%m/%d/%Y %H:%M:%S'),format='%m/%d/%Y')
df2$realdate <- as.Date(parse_date_time(df2$Date, c('mdy')))
This chunk limits the data to September 6 on. The weeks run from Sunday through Saturday. We then summarize the data by week and format the week display to fit the axis.
df3 <- subset(df2, realdate > "2020-09-06")
df5 <- as_tibble(df3 %>% dplyr::group_by(week = cut(realdate, "weeks", start.on.monday = FALSE)) %>% dplyr::summarize(value = sum(`Positive New`)))
df5$dispdate <- format(as.Date(df5$week), format = "%m-%d")
df5 %>% print(n = Inf)
## # A tibble: 21 x 3
## week value dispdate
## <fct> <dbl> <chr>
## 1 2020-09-06 1813 09-06
## 2 2020-09-13 2502 09-13
## 3 2020-09-20 2693 09-20
## 4 2020-09-27 3982 09-27
## 5 2020-10-04 3784 10-04
## 6 2020-10-11 4305 10-11
## 7 2020-10-18 6120 10-18
## 8 2020-10-25 8498 10-25
## 9 2020-11-01 10415 11-01
## 10 2020-11-08 15532 11-08
## 11 2020-11-15 16861 11-15
## 12 2020-11-22 17333 11-22
## 13 2020-11-29 28150 11-29
## 14 2020-12-06 32085 12-06
## 15 2020-12-13 32031 12-13
## 16 2020-12-20 28803 12-20
## 17 2020-12-27 32256 12-27
## 18 2021-01-03 39946 01-03
## 19 2021-01-10 36095 01-10
## 20 2021-01-17 28147 01-17
## 21 2021-01-24 23424 01-24
p <- ggplot(df5, aes(x = as.Date(week), y = value)) + xlab("Week") + geom_point() + ggtitle("Massachusetts Weekly Covid Postives Fall and Winter 2020") + scale_y_continuous(name="Total New Cases", limits=c(0, 40000))
print(p)
df5 <- mutate(df5, value1 = log(df5$value))
p <- ggplot(df5, aes(x = as.Date(week), y = log(value))) + xlab("Week") + geom_point() + geom_smooth(method = "lm") + ggtitle("Massachusetts Weekly Covid Postives Fall 2020 - Log Scale") + scale_y_continuous(name="Log Total New Positive Cases", limits=c(6.5, 11))
df5
## # A tibble: 21 x 4
## week value dispdate value1
## <fct> <dbl> <chr> <dbl>
## 1 2020-09-06 1813 09-06 7.50
## 2 2020-09-13 2502 09-13 7.82
## 3 2020-09-20 2693 09-20 7.90
## 4 2020-09-27 3982 09-27 8.29
## 5 2020-10-04 3784 10-04 8.24
## 6 2020-10-11 4305 10-11 8.37
## 7 2020-10-18 6120 10-18 8.72
## 8 2020-10-25 8498 10-25 9.05
## 9 2020-11-01 10415 11-01 9.25
## 10 2020-11-08 15532 11-08 9.65
## # … with 11 more rows
p