Introduction

This is a very simple second grade model of cumulative cases in Costa Rica and Panama. Updated to april first of 2020.

day_1 = 1
day_proj = 30
setwd("C:/Users/darnel.bolanos/Desktop/Dataset")
covid_19 <- read.csv("Coronavirus_CR.csv", sep = ";", na.strings = "NA")
  
names(covid_19)[1] <- "Day"

#model generation
model_nli_CR <- lm(Confirmed_CR ~ poly(Day, 2), data = covid_19)
model_nli_Pan <- lm(Confirmed_Pan ~ poly(Day, 2), data = covid_19, na.action = na.omit)

#days of dataset
day_values <- seq(day_1, day_proj, 1)

#model predictions
pred_nli_CR <- data.frame(
    conf_nli_CR =
      (predict(model_nli_CR, list(Day = day_values))))
pred_nli_Pan <- data.frame(
    conf_nli_Pan =
      (predict(model_nli_Pan, list(Day = day_values))))

pred_values <- cbind(pred_nli_Pan, pred_nli_CR)

pred_values <- pred_values %>%
    mutate_if(is.numeric, ~round(.,0))
  
pred_values$Day_pred <- day_values

sub_day <- covid_19 %>%
    filter(Day >= day_1, Day <= day_proj) %>%
    select(Day, Confirmed_CR, Confirmed_Pan)
  
xlab <- day_values
xlab[seq(2, length(day_values), by = 2)] <- " "
  
y_label <- round(max(pred_values$conf_nli_Pan), -2)
  
cols <- c("CostaRica" = "darkred", "CostaRicaPredicted" = "darkblue",
          "Panama" = "darkorange", "PanamaPredicted" = "darkgreen")
#initial plot
ggplot(data = pred_values, aes(x = Day_pred)) +
  
#second grade model plot
geom_line(aes(y = conf_nli_CR, colour = "CostaRicaPredicted"),
          size = 0.8, alpha = 0.8) +
geom_point(aes(y = conf_nli_CR, colour = "CostaRicaPredicted"), size = 1.4,
            alpha = 0.7) +
geom_line(aes(y = conf_nli_Pan, colour = "PanamaPredicted"),
          size = 0.8, alpha = 0.8) +
geom_point(aes(y = conf_nli_Pan, colour = "PanamaPredicted"), size = 1.4,
            alpha = 0.7) +
#real data
geom_line(aes(Day, Confirmed_CR, colour = "CostaRica"), data = sub_day,
          size = 0.8, alpha = 0.8) +
geom_point(aes(Day, Confirmed_CR, colour = "CostaRica"), data = sub_day,
           size = 1.8, alpha = 0.7) +
geom_line(aes(Day, Confirmed_Pan, color = "Panama"), data = sub_day,
          size = 0.8, alpha = 0.8) +
geom_point(aes(Day, Confirmed_Pan, color = "Panama"), data = sub_day,
           size = 1.8, alpha = 0.7) +
#legend
scale_color_manual(name = NULL, values = cols) +

#labels
geom_label_repel(aes(x = Day_pred, y = conf_nli_CR, label = conf_nli_CR),
                 fill = "blue", color = "white",
                 direction = 'y',
                 nudge_y = -0.8, nudge_x = 0.2,
                 size = 2, force = 10,
                 segment.size = 0.5,
                 segment.colour = "blue")+
geom_label_repel(aes(Day, Confirmed_CR,label = Confirmed_CR),
                 data = sub_day, fill = "darkred",
                 direction = 'y',
                 nudge_y = -0.1, nudge_x = -0.2,
                 colour = "white",
                 size = 2, force = 10,
                 segment.size = 0.5,
                 segment.colour = "red",
                 min.segment.length = 0.2) +
geom_label_repel(aes(x = Day_pred, y = conf_nli_Pan, label = conf_nli_Pan),
                 fill = "darkgreen", color = "white",
                 direction = 'y',
                 nudge_y = 0.8, nudge_x = -0.2,
                 size = 2, force = 10,
                 segment.size = 0.5,
                 segment.colour = "green")+
geom_label_repel(aes(Day, Confirmed_Pan, label = Confirmed_Pan),
                 data = sub_day, fill = "darkorange",
                 direction = 'y',
                 nudge_y = 0.1, nudge_x = 0.2,
                 colour = "white",
                 size = 2, force = 10,
                 segment.size = 0.5,
                 segment.colour = "orange",
                 min.segment.length = 0.2) +
  
  #scales
scale_x_continuous(breaks = day_values, labels = xlab) +
scale_y_continuous(breaks = seq(0, y_label, 100)) +
  
  #labels
labs(x = "Days since first case", y = "Number of cases",
     title = "COVID-19 estimation: Costa Rica and Panama") +
  
  #theme general
theme_minimal() +
  
  #theme specific
theme(panel.grid = element_line(),
      panel.grid.major.y = element_line(color = "#2b2b2b",
                                        linetype = "dotted",
                                        size = 0.15),
      panel.grid.major.x = element_blank(),
      panel.grid.minor.x = element_blank(),
      panel.grid.minor.y = element_blank(),
      axis.line.y = element_blank(),
      axis.line.x = element_line(color = "#2b2b2b",
                                 size = 0.15),
      axis.ticks = element_line(),
      axis.ticks.x = element_line(color = "#2b2b2b",
                                  size = 0.15),
      axis.ticks.y = element_blank(),
      axis.ticks.length = unit(5, "pt"),
      plot.margin = unit(rep(0.5, 4), "cm"),
      axis.text.y = element_text(margin = margin(r = -5)),
      plot.title = element_text(margin = margin(b = 15)),
      legend.position = c(0.10, 0.85),
      legend.background = element_rect(colour = "gray"),
      legend.key = element_blank()
      )