knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.height=8, fig.width=10
)
library(covid19br)
library(tidyverse)
library(covid19br)
library(geobr)
library(cowplot)
library(knitr)
library(DT)

# Data for comparisons:
used_date  <- max(covid_br$date)

n_sus      <- covid_br %>% filter(date == used_date) %>% pull(suspected_cases)
n_des      <- covid_br %>% filter(date == used_date) %>% pull(not_confirmed_cases)
n_total    <- covid_br %>% filter(date == used_date) %>% pull(confirmed_cases)
n_death    <- covid_br %>% filter(date == used_date) %>% pull(deaths)

# raw data
tab_stat <- covid_regions %>% filter(date == used_date) %>% 
  dplyr::select(Estado = state, "Casos suspeitos" = suspected_cases, 
         "Casos confirmados" = confirmed_cases,
         "Casos descartados" = not_confirmed_cases) %>% 
  arrange(Estado) %>% as.data.frame()
# Function to generate predictions from (log(2) regression models)
#' @export
predic_covid <- function(model, date_interval){
  future <-
    data.frame(
      date = date_interval, by = 1)
  pred <-exp(predict(model, newdata = future, interval = "confidence"))
  future <- cbind(future, pred)
  return(future)
}

# Function to generate predicted vs observed trends in covid
#' @export
plot_future <-
  function(obs, pred, pred_lab = "Previsto", obs_lab = "Observado", ylab = "Casos confirmados covid-19", xlab = "Data",
         title = "covid-19 | Brazil",
         subtitle = "Corona virus spreads exponentially",
         caption = "Data source: Minstério da Saúde | Predicted on 2020-03-15") {
  gpred <- ggplot(pred, aes(y = fit, x = date)) +
    geom_ribbon(data = pred, aes(ymax = upr, ymin = lwr, x = date), fill = "gray", alpha = .5) +
    geom_line(data = pred, aes(y = fit, color = pred_lab), lty = "dashed") +
    geom_line(data = obs,
              aes(y = confirmed_cases, x = date,
                  color = obs_lab)) +
    geom_point(data = obs,
               aes(y = confirmed_cases, x = date,
                   color = obs_lab)) +
    scale_color_manual(values = c(2,1), name = "") +
    scale_x_date(date_breaks = "7 days", date_labels =  "%d %b") +
    theme_cowplot(font_size = 20) +
    labs(y = ylab,
         x = xlab,
         title = title,
         subtitle = subtitle,
         caption =  caption) +
    theme(
      plot.title = element_text(size = 22),
      plot.subtitle = element_text(size = 18),
      plot.caption = element_text(size = 12, hjust = 1))
  return(gpred)
}

The data used in the following analysis were updated on :r used_date

Summary

Confirmed covid-19 cases in Brazil

library(covid19br)
# calculate doubling time:

mod10    <- lm(log10(confirmed_cases + 1) ~ date, covid_br)
modlog   <- lm(log(confirmed_cases + 1) ~ date, covid_br)
dtime    <- log(2)/modlog$coef[2]   # time to double
dtime10  <- 1/mod10$coef[2]         # time to x 10

# Predictions (next 10 days)
mod15   <- lm(log(confirmed_cases + 1) ~ date, covid_br %>% filter(date <= "2020-03-15"))

future <- predic_covid(mod15, date_interval = seq(as.Date("2020-03-10"), as.Date("2020-03-20"), 1))

gpred <- plot_future(obs = covid_br, pred = future, pred_lab = "Predicted", obs_lab = "Observed",
                     xlab = "Date", ylab = "Confirmed cases of covid-19",
                     title = "covid-19 | Brazil", 
                     subtitle = "Corona virus spreads exponentially",
                     caption = "Data source: Minstério da Saúde | Predicted on 2020-03-15") 

# Plots------
# Curva de crescimento no Brasil
g2 <- ggplot(covid_br, aes(y = confirmed_cases, x = date, label = confirmed_cases)) +
  geom_area(fill = "tomato", alpha = 0.2) +
  geom_line(color = "tomato", size = 3) +
  geom_label() +
  scale_x_date(date_breaks = "3 days", date_labels =  "%d %b") +
  geom_segment(aes(x = as.Date("2020-02-26"), y = 150, xend = as.Date ("2020-02-26"), yend = 5),
               arrow = arrow(type =  "closed", length = unit(0.3, "cm"))) +
  annotate(geom='text', x = as.Date("2020-02-26"), y = 160,
           label = "First case confirmed (26 of Feb - 2020)", hjust =  0, size = 7) +
  theme_cowplot(font_size = 20) +
  labs(y = "Confirmed covid-19 cases",
       x = "Date",
       title = "covid-19",
       subtitle = ("The number of cases is growing exponentially"),
       caption = paste("Source: Ministério da Saúde | updated", used_date)) +
     theme( 
    plot.title = element_text(size = 22),
  plot.subtitle = element_text(size = 18),
  plot.caption = element_text(size = 12, hjust = 1)) ;g2


  # Tempo para dobrar o número de casos
g3 <- ggplot(covid_br, aes(y = confirmed_cases + 1, x = date)) +
  geom_point(color = "black", size = 5, shape = 21, fill = "tomato", alpha = .75) +
  scale_y_continuous(trans = "log10") +
  geom_smooth(method = lm, color = "tomato") +
scale_x_date(date_breaks = "3 days", date_labels =  "%d %b") +
  theme_cowplot(font_size = 20) +
  labs(y = "Confirmed covid-19 cases [log10]",
       x = "Date",
       title = "covid-19",
       subtitle = paste("Every ", round(dtime10, 1), "days the number of cases increases 10 times!"),
       caption = paste("Source: Ministério da Saúde | updated", used_date)) +
    theme( 
    plot.title = element_text(size = 22),
  plot.subtitle = element_text(size = 18),
  plot.caption = element_text(size = 12, hjust = 1)) ;g3

Predicted vs observed confirmed cases

gpred

Number of cases by state

# Compare between states
ggplot(covid_states %>% filter(date == used_date), 
       aes(y = confirmed_cases, x = reorder(state, confirmed_cases), label = confirmed_cases)) +
  geom_col(fill = "tomato", color = "red", alpha  = .3, size = 0.5) +
  geom_label() +
  coord_flip() +
  theme_cowplot(font_size = 20) +
  labs(y = "Confirmed covid-19 cases",
       x = "")

Raw data

datatable(tab_stat)


paternogbc/covid19br documentation built on March 17, 2021, 5:57 p.m.