knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
#===============================================================================
# This code:
# 1) Create a file that can be passed to the "Covid Limits" function*
# 2) Runs the "Covid Limits" function
# 3) Sets Chart titles and Sub-titles for the Google Studio Charts
# 4) Saves the data needed for the Google Studio Charts

# For example purposes, the data is currently extracted from the 
# New York Times Github site reporting COIVD cases and deaths for 
# all US States and Territories


# * We need to pass the following to the "Covid limits" function:

#   COLUMN      DESCRIPTION                   EXAMPLE DATA COLUMN NAME
#   PLACE:      location                      us_states$state
#   DATE:       date                          us_states$date
#   EVENTS:     cumulative number of events   us_states$deaths
#   NEW_EVENTS: Number of new daily events    us_states$new_deaths

#===============================================================================


#===============================================================================
# Set up all the library connections we need
#===============================================================================
rm(list = ls())
library(utils)
library(httr)
library(DT)
library(tidyverse)
library(broom)
library(readr)
require(dplyr)
library(dplyr)
library(lubridate)
library(googlesheets4)

#===============================================================================
# Run the Function Code if you need to refresh it
#===============================================================================

library("shewhart.hybrid")
# load_all()
#===============================================================================

# Read the data from the NY Times GitHub Site
# us_states <- read_csv("https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-states.csv")
#===============================================================================
# Run the limit calculations using the function "Covid_limits"
#===============================================================================

ca = us_states %>% filter(state == "California")
US_limits <- Covid_limits(ca$state, ca$date, ca$deaths, ca$new_deaths)

US_limits2 = Covid_limits2(ca %>% select(date, new_deaths) %>% rename(new_events = new_deaths))
library(plotly)
US_limits$day = 1:nrow(US_limits)
plot_ly(
  data = US_limits2,  #%>% filter(date %within% interval(ymd("2021-01-01"), ymd("2021-02-01"))),
  x = ~date, # if we use `date` you'll get artifacts in the exponential curves where dates were skipped
  y = ~new_events,
  name = "adjusted reported deaths",
  type = "scatter",
  mode = "lines+markers") %>%
  layout(
    yaxis = list(range = list(0, 1500))
  ) %>%
  add_trace(
    y = ~MIDLINEa,
    name = "Midline a",
    mode = "lines"
  ) %>%
  add_trace(
    y = ~MIDLINEb,
    name = "Midline b",
    mode = "lines"
  ) %>%
  add_trace(
    mode = "lines",
    name = "upper a",
    y = ~UPPERa
  ) %>%
  add_trace(
    mode = "lines",
    name = "upper b",
    y = ~UPPERb
  ) %>%
  add_trace(
    mode = "lines",
    name = "lower a",
    y = ~LOWERa
  ) %>% 
  add_trace(
    mode = "lines",
    name = "lower b", 
    y = ~LOWERb
  )
#===============================================================================
# Create Chart Title and Sub-Titles
#
#   Chart_Title:  Chart Title to show in the Google Studio chart display
#   Epoch_txt:    Description of the current phase/Epoch
#   Sub_Title:    Sub-Title to show in the Google Studio chart display
#===============================================================================


# US_limits$Chart_Title <- paste(US_limits$place,": Unadjusted Daily Covid-19 Reported Deaths")

US_limits$Epoch_txt <- if_else(US_limits$EPOCH == 1, "1: Pre-Growth in Daily Reported Deaths",
                               if_else(US_limits$EPOCH == 2, "2: Growth in Daily Reported Deaths",
                                       if_else(US_limits$EPOCH == 3, "3: Plateau or Descent in Daily Reported Deaths",
                                               if_else(US_limits$EPOCH == 4, "4: Stability After Descent in Daily Reported Deaths", ""))))

# US_limits$Sub_Title <- paste(US_limits$place, " is in Epoch ", US_limits$Epoch_txt)

#===============================================================================
# Save "US_limits to Google Sheets
#===============================================================================

# ToGoogle <- gs4_find("xxCOVID US States")
# write_sheet(US_limits, ss = ToGoogle, sheet = 4)


UCLA-PHP/shewhart.hybrid documentation built on Oct. 2, 2022, 8:48 p.m.