library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.2.1     v purrr   0.3.3
## v tibble  2.1.3     v dplyr   0.8.3
## v tidyr   1.0.0     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.4.0
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(readxl)

covid <- read_xlsx("C:/Dropbox/DRPH/P9489 Application of Epi Research Methods II/Data/COVID-2019.xlsx")

covid <- covid %>% mutate(dno = row_number())

covid
ggplot(data = covid, aes(x = Date, y = RoW_Confirmed)) + geom_line(color = "#00AFBB", size = 0.5)

ds <- length(covid$Confirmed) # all observations to date
d2 <- ds-10  # look back 10 days in the model
lastday <- 101 # 04/30/2020

x <- c(1:ds) #as.numeric(covid$Date)
y <- covid$RoW_Confirmed[1:ds]

plot(x, y)
fit <- lm(y ~ x + I(x^3) + I(x^2)-1)
points(x, predict(fit), type="l")

summary(fit)
## 
## Call:
## lm(formula = y ~ x + I(x^3) + I(x^2) - 1)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -645.83 -308.28  -89.04  231.08 1166.45 
## 
## Coefficients:
##         Estimate Std. Error t value Pr(>|t|)    
## x      127.45866   24.73376   5.153 8.22e-06 ***
## I(x^3)   0.32592    0.03294   9.895 4.57e-12 ***
## I(x^2) -12.90055    1.84756  -6.982 2.60e-08 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 381.4 on 38 degrees of freedom
## Multiple R-squared:  0.964,  Adjusted R-squared:  0.9612 
## F-statistic: 339.5 on 3 and 38 DF,  p-value: < 2.2e-16
curve(fit$coefficients[1]*x + fit$coefficients[3]*x^2 + fit$coefficients[2]*x^3, d2, (lastday))

fit$coefficients[1]*(lastday) + fit$coefficients[3]*(lastday)^2 + fit$coefficients[2]*(lastday)^3
##        x 
## 217068.1
x <- c(d2:ds) #as.numeric(covid$Date)
y <- covid$RoW_Confirmed[d2:ds]

plot(x, y)
fit <- lm(y ~ x + I(x^3) + I(x^2)-1)
points(x, predict(fit), type="l")

summary(fit)
## 
## Call:
## lm(formula = y ~ x + I(x^3) + I(x^2) - 1)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -175.25  -99.61   21.11   85.86  179.25 
## 
## Coefficients:
##         Estimate Std. Error t value Pr(>|t|)    
## x      1580.2161   167.0141   9.462 1.28e-05 ***
## I(x^3)    1.5440     0.1279  12.073 2.05e-06 ***
## I(x^2)  -97.5965     9.2706 -10.528 5.78e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 134 on 8 degrees of freedom
## Multiple R-squared:  0.999,  Adjusted R-squared:  0.9987 
## F-statistic:  2772 on 3 and 8 DF,  p-value: 2.099e-12
fit$coefficients[1:3]
##           x      I(x^3)      I(x^2) 
## 1580.216087    1.543963  -97.596461
curve(fit$coefficients[1]*x + fit$coefficients[3]*x^2 + fit$coefficients[2]*x^3, d2, (lastday))

fit$coefficients[1]*(lastday) + fit$coefficients[3]*(lastday)^2 + fit$coefficients[2]*(lastday)^3
##        x 
## 754766.7