Help Fitting Model

If anyone can help solve all the errors, or if provide an easier way to estimate the parameters...

library(deSolve) # using the "ode" function

#initial Values 
initial_values <- c(
  S <- 327200000, #Initial Susceptible 66575226 (England total population)
  E <- 10, #Initial Exposed at time=0
  I <- 1, #Initial Infected at time=0
  R <- 0, #Initial Recovered at time=0
  D <- 0 #Initial Deaths at time=0
)


parameters_values <- c(
  alpha = 1/997500000,
  beta  = 1/1000, # infectious contact rate (/person/day)
  gamma = 1/14, # recovery rate (/day)
  eta = 1/280
)

time_values <- seq(0, 113) # days

SEIRD <- function(S, E, I, R, D,
                  alpha, beta, gamma, eta,
                  n1=200){
  Out1 <- matrix(0, ncol = 5, nrow = n1)
  for(i in 1:n1){
    Sn <- S
    En <- E
    In <- I
    Rn <- R
    Dn <- D
    S <- max(0, Sn - alpha*En*Sn)
    E <- max(0, En + alpha*En*Sn - gamma*En) #Exposed group
    I <- max(0, In + gamma*En - beta*In - eta*In)
    R <- max(0, Rn + eta*In)
    D <- max(0, Dn + eta*In)
    Out1[i,1] <- S
    Out1[i,2] <- E
    Out1[i,3] <- I
    Out1[i,4] <- R
    Out1[i,5] <- D
  }
  Out2 <- data.frame (S = Out1[,1],
                      E = Out1[,2],
                      I = Out1[,3],
                      R = Out1[,4],
                      D = Out1[,5])
  return(Out2)
}


alpha = 1/997500000
beta  = 1/1000 # infectious contact rate (/person/day)
gamma = 1/14 # recovery rate (/day)
eta = 1/280
Out1 <- SEIRD( S, E, I, R, D,
               alpha, beta, gamma, eta,
               n1 = 200)

plot(1:200, Out1$S,
     type = "l", 
     ylim = c( 0,997500000), 
     col = "purple")
lines(1:200, Out1$E, col = "orange")
lines(1:200, Out1$I, col = "red")
lines(1:200, Out1$R, col = "seagreen") 
lines(1:200, Out1$D, col = "black")

# adding a legend:
legend("right", c("Susceptible", "Exposed","Infected", "recovered", "Deceased"),
       col = c("purple", "orange", "red", "seagreen", "black"), lty = 1, bty = "n")

sir_equations <- function(time, variables, parameters) {
  Sn <- S
  En <- E
  In <- I
  Rn <- R
  Dn <- D
  with(as.list(c(variables, parameters)), {
    S <- max(0, Sn - alpha*En*Sn)
    E <- max(0, En + alpha*En*Sn - gamma*En) #Exposed group
    I <- max(0, In + gamma*En - beta*In - eta*In)
    R <- max(0, Rn + eta*In)
    D <- max(0, Dn + eta*In)
    return(list(c(S, E, I, R, D)))
  })
}


sir_values_1 <- ode(
  y = initial_values,
  times = time_values,
  func = sir_equations,
  parms = parameters_values 
)


sir_values_1
sir_values_1 <- as.data.frame(sir_values_1)
sir_values_1


(66575226 + 1) * parameters_values["beta"] / parameters_values["gamma"]

sir_1 <- function(alpha, beta, gamma, eta, S0, E0, I0, R0, D0, times) {
  require(deSolve) # for the "ode" function
  
  # the differential equations:
  sir_equations <- function(time, variables, parameters) {
    with(as.list(c(variables, parameters)), {
      S <- max(0, Sn - alpha*En*Sn)
      E <- max(0, En + alpha*En*Sn - gamma*En) #Exposed group
      I <- max(0, In + gamma*En - beta*In - eta*In)
      R <- max(0, Rn + eta*In)
      D <- max(0, Dn + eta*In)
      return(list(c(S, E, I, R, D)))
    })
  }}
  
  # the parameters values:
  parameters_values <- c(alpha = alpha, beta  = beta, gamma = gamma, eta = eta )
  
  # the initial values of variables:
  initial_values <- c( S0 <- S, E0 <- E,  I0 <- I, R0 <- R, D0 <- D)
  
  # solving
  time_values <- seq(0, 113) # days
  times <- time_values
  out <- ode(initial_values, times, sir_equations, parameters_values)
  
  # returning the output:
  as.data.frame(out)

flu <- read.csv("COVIDdataset1.csv", 
                   header = TRUE)

with(flu, plot(Date, Cases, pch = 19, col = "red", ylim = c(0, 1)))
predictions <- sir_1(alpha = 1/997500000, beta = 1/1000, gamma = 1/14, eta = 1/280, S0 = 1000, E0 = 10, I0 = 1, R0 = 0, D0 = 0, times = flu$Date)
with(predictions, lines(time, I, col = "red"))

model_fit <- function(alpha, beta, data, N = 327200000, ...) {
  I0 <- data$Cases[1] # initial number of infected (from data)
  times <- data$Date   # time points (from data)
  # model's predictions:
  predictions <- sir_1 (alpha = alpha, beta  = beta, gamma = gamma, eta = eta,   # parameters
                       S0 = N - I0, E0 = E0, I0 = I0, R0 = 0, D = D0, # variables' intial values
                       times = time_values)              # time points
  # plotting the observed prevalences:
  with(data, plot(Date, Cases, ...))
  # adding the model-predicted prevalence:
  with(predictions, lines(time, I, col = "red"))
}

model_fit(alpha = 1/997500000, beta = 1/1000, gamma = 1/14, eta = 1/280, flu, pch = 19, col = "red", ylim = c(0, 100000))


predictions <- sir_1(alpha = 1/997500000, beta = 1/1000, gamma = 1/14, eta = 1/280, S0 = 1000, E0 = 10, I0 = 1, R0 = 0, D0 = 0, times = flu$Date)
predictions


sum((predictions$I - flu$Cases)^2)

# the observed prevalences:
with(flu, plot(Date, Cases, pch = 19, col = "red", ylim = c(0, 1000)))
# the model-predicted prevalences:
with(predictions, lines(time, I, col = "red", type = "o"))
# the "errors":
segments(flu$Date, flu$Cases, predictions$time, predictions$I)

ss <- function(alpha, beta, data = flu, N = 763) {
  I0 <- data$Cases[1]
  times <- data$Date
  predictions <- sir_1(alpha = alpha, beta = beta, gamma = gamma, eta = eta,   # parameters
                       S0 = N - I0, E0 = E0, I0 = I0, R0 = 0, D = D0, # variables' intial values
                       times = time_values)                # time points
  sum((predictions$I[-1] - data$Cases[-1])^2)
}

ss(alpha = 1/997500000, beta = 1/1000, gamma = 1/14, eta = 1/280)

alpha_val <- seq(from = 0.0016, to = 1/1000, le = 100)
ss_val <- sapply(alpha_val, ss, beta = 0.5)

min_ss_val <- min(ss_val)
min_ss_val

alpha_hat <- alpha_val[ss_val == min_ss_val]
alpha_hat

plot(alpha_val, ss_val, type = "l", lwd = 2,
     xlab = expression(paste("infectious contact rate ", alpha)),
     ylab = "sum of squares")
# adding the minimal value of the sum of squares:
abline(h = min_ss_val, lty = 2, col = "grey")
# adding the estimate of alpha:
abline(v = alpha_hat, lty = 2, col = "grey")


beta_val <- seq(from = 0.4, to = 0.575, le = 100)
ss_val <- sapply(beta_val, function(x) ss(alpha_hat, x))
(min_ss_val <- min(ss_val))

(beta_hat <- beta_val[ss_val == min_ss_val])

plot(beta_val, ss_val, type = "l", lwd = 2,
     xlab = expression(paste("recovery rate ", beta)),
     ylab = "sum of squares")
abline(h = min_ss_val, lty = 2, col = "grey")
abline(v = beta_hat, lty = 2, col = "grey")

n <- 10 # number of parameter values to try
alpha_val <- seq(from = 0.002, to = 0.0035, le = n)
beta_val <- seq(from = 0.3, to = 0.65, le = n)
param_val <- expand.grid(alpha_val, beta_val)
ss_val <- with(param_val, Map(ss, Var1, Var2))
ss_val <- matrix(unlist(ss_val), n)
persp(alpha_val, beta_val, -ss_val, theta = 40, phi = 30,
      xlab = "alpha", ylab = "beta", zlab =  = "-sum of squares")


n <- 30 # number of parameters values
alpha_val <- seq(from = 0.002, to = 0.0035, le = n)
beta_val <- seq(from = 0.3, to = 0.65, le = n)
# calculating the sum of squares:
param_val <- expand.grid(alpha_val, beta_val)
ss_val <- with(param_val, Map(ss, Var1, Var2))
ss_val <- unlist(ss_val)

# minimum sum of squares and parameters values:
(ss_min <- min(ss_val))

ind <- ss_val == ss_min
(alpha_hat <- param_val$Var1[ind])

(beta_hat <- param_val$Var2[ind])


# visualizing the sum of squares profile:
ss_val <- matrix(ss_val, n)
image(alpha_val, beta_val, ss_val,
      xlab = expression(paste("infectious contact rate ", alpha, " (/person/Date)")),
      ylab = expression(paste("recovery rate ", beta, " (/Date)")))
contour(alpha_val, beta_val,ss_val, add = TRUE)
points(alpha_hat, beta_hat, pch = 3)
box(bty = "o")


ss(alpha = 1/997500000, beta = 1/1000, gamma = 1/14, eta = 1/280)
ss2 <- function(x) {
  ss(alpha = x[1], beta = x[2],  gamma = x[3], eta = x[4])
}

ss2(c(1/997500000, 1/1000, 1/14, 1/128))

starting_param_val <- c(1/997500000, 1/1000, 1/14, 1/128)
ss_optim <- optim(starting_param_val, ss2)


ss_optim
ss_optim$value
ss_optim$par


mLL <- function(alpha, beta, sigma, Date, Cases, N = 763) {
  alpha <- exp(alpha) # to make sure that the parameters are positive
  beta <- exp(beta)
  sigma <- exp(sigma)
  I0 <- Cases[1] # initial number of infectious
  observations <- Cases[-1] # the fit is done on the other data points
  predictions <- sir_1(alpha = alpha, beta = beta, gamma = gamma, eta = eta,
                       S0 = N - I0, E0 = E0 I0 = I0, R0 = 0, D = D0, times = Date)
  predictions <- predictions$I[-1] # removing the first point too
  # returning minus log-likelihood:
  -sum(dnorm(x = observations, mean = predictions, sd = sigma, log = TRUE))
}

library(bbmle) # for "mle2", "coef", "confint", "vcov", "logLik", "profile", "summary", "plot.profile.mle2"

starting_param_val <- list(alpha = 1/997500000, beta = 1/1000, gamma = 1/14, eta = 1/280, sigma = 1)
estimates <- mle2(minuslogl = mLL, start = lapply(starting_param_val, log),
                  method = "Nelder-Mead", data = c(flu, N = 763))

summary(estimates)
exp(coef(estimates))
exp(confint(estimates))
vcov(estimates)
-logLik(estimates)


prof <- profile(estimates)
plot(prof, main = NA)


N <- 10000 # total population size
time_points <- seq(min(flu$Date), max(flu$Date), le = 100) # vector of time points
I0 <- flu$Cases[1] # initial number of infected
param_hat <- exp(coef(estimates)) # parameters estimates
# model's best predictions:
best_predictions <- sir_1(alpha = param_hat["alpha"], beta = param_hat["beta"], gamma = param_hat["gamma"], eta = param_hat["eta"],
                          S0 = N - I0, E0 = E0 I0 = I0, R0 = 0, D = D0, time_points)$I
# confidence interval of the best predictions:
cl <- 0.95 # confidence level
cl <- (1 - cl) / 2
lwr <- qnorm(p = cl, mean = best_predictions, sd = param_hat["sigma"])
upr <- qnorm(p = 1 - cl, mean = best_predictions, sd = param_hat["sigma"])
# layout of the plot:
plot(time_points, time_points, ylim = c(0, max(upr)), type = "n",
     xlab = "time (Dates)", ylab = "prevalence")
# adding the predictions' confidence interval:
sel <- time_points >= 1 # predictions start from the second data point
polygon(c(time_points[sel], rev(time_points[sel])), c(upr[sel], rev(lwr[sel])),
        border = NA, col = adjustcolor("red", alpha.f = 0.1))
# adding the model's best predictions:
lines(time_points, best_predictions, col = "red")
# adding the observed data:
with(flu, points(Date, Cases, pch = 19, col = "red"))


#Poisson Distribution of Errors

#mLL <- function(alpha, beta, sigma, Date, Cases, N = 10000) {
mLL_pois <- function(alpha, Date, Cases, N = 1000) {
  alpha <- exp(alpha) # to make sure that the parameters are positive
  beta <- 1/1000
  gamma <- 1/14
  eta <- 1/280
    
 
  #  sigma <- exp(sigma)
  I0 <- Cases[1] # initial number of infectious
  observations <- Cases[-1] # the fit is done on the other data points
  predictions <- sir_1(alpha = alpha, beta = 1/1000, gamma = 1/14, eta = 1/280,
                       S0 = N - I0, E0 = E0 I0 = I0, R0 = 0, D = D0, times = Date)
  predictions <- predictions$I[-1] # removing the first point too
  if (any(predictions < 0)) return(NA) # safety
  # returning minus log-likelihood:
  #  -sum(dnorm(x = observations, mean = predictions, sd = sigma, log = TRUE))
  -sum(dpois(x = observations, lambda = predictions, log = TRUE))
}

starting_param_val <- list(alpha = 1/997500000)
estimates_pois <- mle2(minuslogl = mLL_pois,
                       start = lapply(starting_param_val, log),
                       data = c(flu$Cases, N = 1000))

exp(coef(estimates))
exp(coef(estimates_pois))
exp(confint(estimates))
exp(confint(estimates_pois))
vcov(estimates_pois)

# points estimates:
param_hat <- exp(coef(estimates_pois))
# model's best predictions:
best_predictions <- sir_1(alpha = param_hat["alpha"], beta = param_hat["beta"],
                          S0 = N - I0, E0 = E0 I0 = I0, R0 = 0, D = D0, time_points)$I
# confidence interval of the best predictions:
cl <- 0.95 # confidence level
cl <- (1 - cl) / 2
lwr <- qpois(p = cl, lambda = best_predictions)
upr <- qpois(p = 1 - cl, lambda = best_predictions)
# layout of the plot:
plot(time_points, time_points, ylim = c(0, max(upr)), type = "n",
     xlab = "time (Dates)", ylab = "prevalence")
# adding the predictions' confidence interval:
sel <- time_points >= 1 # predictions start from the second data point
polygon(c(time_points[sel], rev(time_points[sel])), c(upr[sel], rev(lwr[sel])),
        border = NA, col = adjustcolor("red", alpha.f = 0.1))
# adding the model's best predictions:
lines(time_points, best_predictions, col = "red")
# adding the observed data:
with(flu, points(Date, Cases, pch = 19, col = "red"))

-logLik(estimates)
-logLik(estimates_pois)

'Can't get past

cannot open file 'COVIDdataset1.csv': No such file or directory

I am unable to attach the dataset to this in the excel file form.

Date Deaths Total Deaths Cases Total Cases
6/3/2020 1 1 0 0
7/3/2020 1 2 0 0
8/3/2020 0 2 0 0
9/3/2020 1 3 0 0
10/3/2020 4 7 0 0
11/3/2020 0 7 0 0
12/3/2020 2 9 0 0
13/3/2020 1 10 0 0
14/3/2020 18 28 0 0
15/3/2020 15 43 0 0
16/3/2020 22 65 0 0
17/3/2020 16 81 0 0
18/3/2020 34 115 0 0
19/3/2020 43 158 0 0
20/3/2020 36 194 0 0
21/3/2020 56 250 1,035 1,035
22/3/2020 35 285 665 1,700
23/3/2020 74 359 967 2,667
24/3/2020 149 508 1,427 4,094
25/3/2020 187 695 1,452 5,546
26/3/2020 183 878 2,129 7,675
27/3/2020 284 1,162 2,890 10,565
28/3/2020 294 1,456 2,556 13,121
29/3/2020 214 1,670 2,502 15,623
30/3/2020 374 2,044 2,665 18,288
31/3/2020 382 2,426 3,250 21,538
1/4/2020 670 3,096 4,567 26,105
2/4/2020 652 3,748 4,522 30,627
3/4/2020 714 4,462 4,672 35,299
4/4/2020 760 5,222 4,000 39,299
5/4/2020 644 5,866 6,199 45,498
6/4/2020 568 6,434 4,143 49,641
7/4/2020 1,038 7,472 3,888 53,529
8/4/2020 1,034 8,506 5,865 59,394
9/4/2020 1,110 9,616 4,675 64,069
10/4/2020 1,152 10,768 5,706 69,775
11/4/2020 840 11,608 5,234 75,009
12/4/2020 686 12,294 5,288 80,297
13/4/2020 744 13,038 4,342 84,639
14/4/2020 1,047 14,085 5,252 89,891
15/4/2020 842 14,927 4,605 94,496
16/4/2020 1,029 15,956 4,618 99,114
17/4/2020 936 16,892 5,599 104,713
18/4/2020 1,115 18,007 5,526 110,239
19/4/2020 498 18,505 5,850 116,089
20/4/2020 559 19,064 4,676 120,765
21/4/2020 1,173 20,237 4,301 125,066
22/4/2020 837 21,074 4,451 129,517
23/4/2020 727 21,801 4,583 134,100
24/4/2020 1,006 22,807 5,386 139,486
25/4/2020 843 23,650 4,913 144,399
26/4/2020 420 24,070 4,463 148,862
27/4/2020 338 24,408 4,310 153,172
28/4/2020 911 25,319 3,996 157,168
29/4/2020 795 26,114 4,076 161,244
30/4/2020 674 26,788 6,032 167,276
1/5/2020 740 27,528 6,201 173,477
2/5/2020 621 28,149 4,806 178,283
3/5/2020 315 28,464 4,339 182,622
4/5/2020 288 28,752 3,985 186,607
5/5/2020 694 29,446 4,406 191,013
6/5/2020 652 30,098 6,111 197,124
7/5/2020 540 30,638 5,614 202,738
8/5/2020 627 31,265 4,649 207,387
9/5/2020 345 31,610 3,896 211,283
10/5/2020 269 31,879 3,923 215,206
11/5/2020 211 32,090 3,877 219,083
12/5/2020 630 32,720 3,403 222,486
13/5/2020 496 33,216 3,242 225,728
14/5/2020 428 33,644 3,446 229,174
15/5/2020 384 34,028 3,560 232,734
16/5/2020 480 34,508 3,451 236,185
17/5/2020 170 34,678 3,562 239,747
18/5/2020 160 34,838 2,684 242,431
19/5/2020 548 35,386 2,412 244,843
20/5/2020 369 35,755 2,472 247,315
21/5/2020 338 36,093 2,615 249,930
22/5/2020 358 36,451 3,287 253,217
23/5/2020 283 36,734 2,959 256,176
24/5/2020 441 37,175 2,409 258,585
25/5/2020 122 37,297 1,625 260,210
26/5/2020 136 37,433 2,004 262,214
27/5/2020 443 37,876 2,013 264,227
28/5/2020 416 38,292 1,887 266,114
29/5/2020 373 38,665 2,095 268,209
30/5/2020 230 38,895 2,445 270,654
31/5/2020 115 39,010 1,936 272,590
1/6/2020 111 39,121 1,570 274,160
2/6/2020 326 39,447 1,613 275,773
3/6/2020 365 39,812 1,871 277,644
4/6/2020 177 39,989 1,805 279,449
5/6/2020 358 40,347 1,650 281,099
6/6/2020 207 40,554 1,557 282,656
7/6/2020 77 40,631 1,326 283,982
8/6/2020 55 40,686 1,205 285,187
9/6/2020 289 40,975 1,387 286,574
10/6/2020 250 41,225 1,003 287,577
11/6/2020 152 41,377 1,266 288,843
12/6/2020 204 41,581 1,541 290,384
13/6/2020 183 41,764 1,425 291,809
14/6/2020 36 41,800 1,514 293,323
15/6/2020 38 41,838 1,056 294,379
16/6/2020 236 42,074 1,279 295,658
17/6/2020 184 42,258 1,115 296,773
18/6/2020 137 42,395 1,218 297,991
19/6/2020 173 42,568 1,346 299,337
20/6/2020 130 42,698 1,295 300,632
21/6/2020 43 42,741 1,221 301,853
22/6/2020 15 42,756 958 302,811
23/6/2020 171 42,927 874 303,685
24/6/2020 154 43,081 653 304,338
25/6/2020 149 43,230 1,118 305,456
26/6/2020 186 43,414 1,006 306,462

data as code for convenience

structure(list(Date = c(
  "6/3/2020", "7/3/2020", "8/3/2020", "9/3/2020",
  "10/3/2020", "11/3/2020", "12/3/2020", "13/3/2020", "14/3/2020",
  "15/3/2020", "16/3/2020", "17/3/2020", "18/3/2020", "19/3/2020",
  "20/3/2020", "21/3/2020", "22/3/2020", "23/3/2020", "24/3/2020",
  "25/3/2020", "26/3/2020", "27/3/2020", "28/3/2020", "29/3/2020",
  "30/3/2020", "31/3/2020", "1/4/2020", "2/4/2020", "3/4/2020",
  "4/4/2020", "5/4/2020", "6/4/2020", "7/4/2020", "8/4/2020", "9/4/2020",
  "10/4/2020", "11/4/2020", "12/4/2020", "13/4/2020", "14/4/2020",
  "15/4/2020", "16/4/2020", "17/4/2020", "18/4/2020", "19/4/2020",
  "20/4/2020", "21/4/2020", "22/4/2020", "23/4/2020", "24/4/2020",
  "25/4/2020", "26/4/2020", "27/4/2020", "28/4/2020", "29/4/2020",
  "30/4/2020", "1/5/2020", "2/5/2020", "3/5/2020", "4/5/2020",
  "5/5/2020", "6/5/2020", "7/5/2020", "8/5/2020", "9/5/2020", "10/5/2020",
  "11/5/2020", "12/5/2020", "13/5/2020", "14/5/2020", "15/5/2020",
  "16/5/2020", "17/5/2020", "18/5/2020", "19/5/2020", "20/5/2020",
  "21/5/2020", "22/5/2020", "23/5/2020", "24/5/2020", "25/5/2020",
  "26/5/2020", "27/5/2020", "28/5/2020", "29/5/2020", "30/5/2020",
  "31/5/2020", "1/6/2020", "2/6/2020", "3/6/2020", "4/6/2020",
  "5/6/2020", "6/6/2020", "7/6/2020", "8/6/2020", "9/6/2020", "10/6/2020",
  "11/6/2020", "12/6/2020", "13/6/2020", "14/6/2020", "15/6/2020",
  "16/6/2020", "17/6/2020", "18/6/2020", "19/6/2020", "20/6/2020",
  "21/6/2020", "22/6/2020", "23/6/2020", "24/6/2020", "25/6/2020",
  "26/6/2020"
), Deaths = c(
  1L, 1L, 0L, 1L, 4L, 0L, 2L, 1L, 18L,
  15L, 22L, 16L, 34L, 43L, 36L, 56L, 35L, 74L, 149L, 187L, 183L,
  284L, 294L, 214L, 374L, 382L, 670L, 652L, 714L, 760L, 644L, 568L,
  1038L, 1034L, 1110L, 1152L, 840L, 686L, 744L, 1047L, 842L, 1029L,
  936L, 1115L, 498L, 559L, 1173L, 837L, 727L, 1006L, 843L, 420L,
  338L, 911L, 795L, 674L, 740L, 621L, 315L, 288L, 694L, 652L, 540L,
  627L, 345L, 269L, 211L, 630L, 496L, 428L, 384L, 480L, 170L, 160L,
  548L, 369L, 338L, 358L, 283L, 441L, 122L, 136L, 443L, 416L, 373L,
  230L, 115L, 111L, 326L, 365L, 177L, 358L, 207L, 77L, 55L, 289L,
  250L, 152L, 204L, 183L, 36L, 38L, 236L, 184L, 137L, 173L, 130L,
  43L, 15L, 171L, 154L, 149L, 186L
), "Total Deaths" = c(
  1L, 2L, 2L,
  3L, 7L, 7L, 9L, 10L, 28L, 43L, 65L, 81L, 115L, 158L, 194L, 250L,
  285L, 359L, 508L, 695L, 878L, 1162L, 1456L, 1670L, 2044L, 2426L,
  3096L, 3748L, 4462L, 5222L, 5866L, 6434L, 7472L, 8506L, 9616L,
  10768L, 11608L, 12294L, 13038L, 14085L, 14927L, 15956L, 16892L,
  18007L, 18505L, 19064L, 20237L, 21074L, 21801L, 22807L, 23650L,
  24070L, 24408L, 25319L, 26114L, 26788L, 27528L, 28149L, 28464L,
  28752L, 29446L, 30098L, 30638L, 31265L, 31610L, 31879L, 32090L,
  32720L, 33216L, 33644L, 34028L, 34508L, 34678L, 34838L, 35386L,
  35755L, 36093L, 36451L, 36734L, 37175L, 37297L, 37433L, 37876L,
  38292L, 38665L, 38895L, 39010L, 39121L, 39447L, 39812L, 39989L,
  40347L, 40554L, 40631L, 40686L, 40975L, 41225L, 41377L, 41581L,
  41764L, 41800L, 41838L, 42074L, 42258L, 42395L, 42568L, 42698L,
  42741L, 42756L, 42927L, 43081L, 43230L, 43414L
), Cases = c(
  0L,
  0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1035L,
  665L, 967L, 1427L, 1452L, 2129L, 2890L, 2556L, 2502L, 2665L,
  3250L, 4567L, 4522L, 4672L, 4000L, 6199L, 4143L, 3888L, 5865L,
  4675L, 5706L, 5234L, 5288L, 4342L, 5252L, 4605L, 4618L, 5599L,
  5526L, 5850L, 4676L, 4301L, 4451L, 4583L, 5386L, 4913L, 4463L,
  4310L, 3996L, 4076L, 6032L, 6201L, 4806L, 4339L, 3985L, 4406L,
  6111L, 5614L, 4649L, 3896L, 3923L, 3877L, 3403L, 3242L, 3446L,
  3560L, 3451L, 3562L, 2684L, 2412L, 2472L, 2615L, 3287L, 2959L,
  2409L, 1625L, 2004L, 2013L, 1887L, 2095L, 2445L, 1936L, 1570L,
  1613L, 1871L, 1805L, 1650L, 1557L, 1326L, 1205L, 1387L, 1003L,
  1266L, 1541L, 1425L, 1514L, 1056L, 1279L, 1115L, 1218L, 1346L,
  1295L, 1221L, 958L, 874L, 653L, 1118L, 1006L
), "Total Cases" = c(
  0L,
  0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1035L,
  1700L, 2667L, 4094L, 5546L, 7675L, 10565L, 13121L, 15623L, 18288L,
  21538L, 26105L, 30627L, 35299L, 39299L, 45498L, 49641L, 53529L,
  59394L, 64069L, 69775L, 75009L, 80297L, 84639L, 89891L, 94496L,
  99114L, 104713L, 110239L, 116089L, 120765L, 125066L, 129517L,
  134100L, 139486L, 144399L, 148862L, 153172L, 157168L, 161244L,
  167276L, 173477L, 178283L, 182622L, 186607L, 191013L, 197124L,
  202738L, 207387L, 211283L, 215206L, 219083L, 222486L, 225728L,
  229174L, 232734L, 236185L, 239747L, 242431L, 244843L, 247315L,
  249930L, 253217L, 256176L, 258585L, 260210L, 262214L, 264227L,
  266114L, 268209L, 270654L, 272590L, 274160L, 275773L, 277644L,
  279449L, 281099L, 282656L, 283982L, 285187L, 286574L, 287577L,
  288843L, 290384L, 291809L, 293323L, 294379L, 295658L, 296773L,
  297991L, 299337L, 300632L, 301853L, 302811L, 303685L, 304338L,
  305456L, 306462L
)), row.names = c(NA, -113L), class = c(
  "tbl_df",
  "tbl", "data.frame"
)) -> flu
2 Likes
library(deSolve) # using the "ode" function

#initial Values 
initial_values <- c(
  S <- 327200000, #Initial Susceptible 66575226 (England total population)
  E <- 10, #Initial Exposed at time=0
  I <- 1, #Initial Infected at time=0
  R <- 0, #Initial Recovered at time=0
  D <- 0 #Initial Deaths at time=0
)


parameters_values <- c(
  alpha = 1/997500000,
  beta  = 1/1000, # infectious contact rate (/person/day)
  gamma = 1/14, # recovery rate (/day)
  eta = 1/280
)

time_values <- seq(0, 113) # days

SEIRD <- function(S, E, I, R, D,
                  alpha, beta, gamma, eta,
                  n1=200){
  Out1 <- matrix(0, ncol = 5, nrow = n1)
  for(i in 1:n1){
    Sn <- S
    En <- E
    In <- I
    Rn <- R
    Dn <- D
    S <- max(0, Sn - alpha*En*Sn)
    E <- max(0, En + alpha*En*Sn - gamma*En) #Exposed group
    I <- max(0, In + gamma*En - beta*In - eta*In)
    R <- max(0, Rn + eta*In)
    D <- max(0, Dn + eta*In)
    Out1[i,1] <- S
    Out1[i,2] <- E
    Out1[i,3] <- I
    Out1[i,4] <- R
    Out1[i,5] <- D
  }
  Out2 <- data.frame (S = Out1[,1],
                      E = Out1[,2],
                      I = Out1[,3],
                      R = Out1[,4],
                      D = Out1[,5])
  return(Out2)
}


alpha = 1/997500000
beta  = 1/1000 # infectious contact rate (/person/day)
gamma = 1/14 # recovery rate (/day)
eta = 1/280
Out1 <- SEIRD( S, E, I, R, D,
               alpha, beta, gamma, eta,
               n1 = 200)

plot(1:200, Out1$S,
     type = "l", 
     ylim = c( 0,997500000), 
     col = "purple")
lines(1:200, Out1$E, col = "orange")
lines(1:200, Out1$I, col = "red")
lines(1:200, Out1$R, col = "seagreen") 
lines(1:200, Out1$D, col = "black")

# adding a legend:
legend("right", c("Susceptible", "Exposed","Infected", "recovered", "Deceased"),
       col = c("purple", "orange", "red", "seagreen", "black"), lty = 1, bty = "n")

sir_equations <- function(time, variables, parameters) {
  Sn <- S
  En <- E
  In <- I
  Rn <- R
  Dn <- D
  with(as.list(c(variables, parameters)), {
    S <- max(0, Sn - alpha*En*Sn)
    E <- max(0, En + alpha*En*Sn - gamma*En) #Exposed group
    I <- max(0, In + gamma*En - beta*In - eta*In)
    R <- max(0, Rn + eta*In)
    D <- max(0, Dn + eta*In)
    return(list(c(S, E, I, R, D)))
  })
}


sir_values_1 <- ode(
  y = initial_values,
  times = time_values,
  func = sir_equations,
  parms = parameters_values 
)


sir_values_1
sir_values_1 <- as.data.frame(sir_values_1)
sir_values_1


(66575226 + 1) * parameters_values["beta"] / parameters_values["gamma"]

sir_1 <- function(alpha, beta, gamma, eta, S0, E0, I0, R0, D0, times) {
  require(deSolve) # for the "ode" function
  
  # the differential equations:
  sir_equations <- function(time, variables, parameters) {
    with(as.list(c(variables, parameters)), {
      S <- max(0, Sn - alpha*En*Sn)
      E <- max(0, En + alpha*En*Sn - gamma*En) #Exposed group
      I <- max(0, In + gamma*En - beta*In - eta*In)
      R <- max(0, Rn + eta*In)
      D <- max(0, Dn + eta*In)
      return(list(c(S, E, I, R, D)))
    })
  }}
  
  # the parameters values:
  parameters_values <- c(alpha = alpha, beta  = beta, gamma = gamma, eta = eta )
  
  # the initial values of variables:
  initial_values <- c( S0 <- S, E0 <- E,  I0 <- I, R0 <- R, D0 <- D)
  
  # solving
  time_values <- seq(0, 113) # days
  times <- time_values
  out <- ode(initial_values, times, sir_equations, parameters_values)
  
  # returning the output:
  as.data.frame(out)

structure(list(Date = c(
  "6/3/2020", "7/3/2020", "8/3/2020", "9/3/2020",
  "10/3/2020", "11/3/2020", "12/3/2020", "13/3/2020", "14/3/2020",
  "15/3/2020", "16/3/2020", "17/3/2020", "18/3/2020", "19/3/2020",
  "20/3/2020", "21/3/2020", "22/3/2020", "23/3/2020", "24/3/2020",
  "25/3/2020", "26/3/2020", "27/3/2020", "28/3/2020", "29/3/2020",
  "30/3/2020", "31/3/2020", "1/4/2020", "2/4/2020", "3/4/2020",
  "4/4/2020", "5/4/2020", "6/4/2020", "7/4/2020", "8/4/2020", "9/4/2020",
  "10/4/2020", "11/4/2020", "12/4/2020", "13/4/2020", "14/4/2020",
  "15/4/2020", "16/4/2020", "17/4/2020", "18/4/2020", "19/4/2020",
  "20/4/2020", "21/4/2020", "22/4/2020", "23/4/2020", "24/4/2020",
  "25/4/2020", "26/4/2020", "27/4/2020", "28/4/2020", "29/4/2020",
  "30/4/2020", "1/5/2020", "2/5/2020", "3/5/2020", "4/5/2020",
  "5/5/2020", "6/5/2020", "7/5/2020", "8/5/2020", "9/5/2020", "10/5/2020",
  "11/5/2020", "12/5/2020", "13/5/2020", "14/5/2020", "15/5/2020",
  "16/5/2020", "17/5/2020", "18/5/2020", "19/5/2020", "20/5/2020",
  "21/5/2020", "22/5/2020", "23/5/2020", "24/5/2020", "25/5/2020",
  "26/5/2020", "27/5/2020", "28/5/2020", "29/5/2020", "30/5/2020",
  "31/5/2020", "1/6/2020", "2/6/2020", "3/6/2020", "4/6/2020",
  "5/6/2020", "6/6/2020", "7/6/2020", "8/6/2020", "9/6/2020", "10/6/2020",
  "11/6/2020", "12/6/2020", "13/6/2020", "14/6/2020", "15/6/2020",
  "16/6/2020", "17/6/2020", "18/6/2020", "19/6/2020", "20/6/2020",
  "21/6/2020", "22/6/2020", "23/6/2020", "24/6/2020", "25/6/2020",
  "26/6/2020"
), Deaths = c(
  1L, 1L, 0L, 1L, 4L, 0L, 2L, 1L, 18L,
  15L, 22L, 16L, 34L, 43L, 36L, 56L, 35L, 74L, 149L, 187L, 183L,
  284L, 294L, 214L, 374L, 382L, 670L, 652L, 714L, 760L, 644L, 568L,
  1038L, 1034L, 1110L, 1152L, 840L, 686L, 744L, 1047L, 842L, 1029L,
  936L, 1115L, 498L, 559L, 1173L, 837L, 727L, 1006L, 843L, 420L,
  338L, 911L, 795L, 674L, 740L, 621L, 315L, 288L, 694L, 652L, 540L,
  627L, 345L, 269L, 211L, 630L, 496L, 428L, 384L, 480L, 170L, 160L,
  548L, 369L, 338L, 358L, 283L, 441L, 122L, 136L, 443L, 416L, 373L,
  230L, 115L, 111L, 326L, 365L, 177L, 358L, 207L, 77L, 55L, 289L,
  250L, 152L, 204L, 183L, 36L, 38L, 236L, 184L, 137L, 173L, 130L,
  43L, 15L, 171L, 154L, 149L, 186L
), "Total Deaths" = c(
  1L, 2L, 2L,
  3L, 7L, 7L, 9L, 10L, 28L, 43L, 65L, 81L, 115L, 158L, 194L, 250L,
  285L, 359L, 508L, 695L, 878L, 1162L, 1456L, 1670L, 2044L, 2426L,
  3096L, 3748L, 4462L, 5222L, 5866L, 6434L, 7472L, 8506L, 9616L,
  10768L, 11608L, 12294L, 13038L, 14085L, 14927L, 15956L, 16892L,
  18007L, 18505L, 19064L, 20237L, 21074L, 21801L, 22807L, 23650L,
  24070L, 24408L, 25319L, 26114L, 26788L, 27528L, 28149L, 28464L,
  28752L, 29446L, 30098L, 30638L, 31265L, 31610L, 31879L, 32090L,
  32720L, 33216L, 33644L, 34028L, 34508L, 34678L, 34838L, 35386L,
  35755L, 36093L, 36451L, 36734L, 37175L, 37297L, 37433L, 37876L,
  38292L, 38665L, 38895L, 39010L, 39121L, 39447L, 39812L, 39989L,
  40347L, 40554L, 40631L, 40686L, 40975L, 41225L, 41377L, 41581L,
  41764L, 41800L, 41838L, 42074L, 42258L, 42395L, 42568L, 42698L,
  42741L, 42756L, 42927L, 43081L, 43230L, 43414L
), Cases = c(
  0L,
  0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1035L,
  665L, 967L, 1427L, 1452L, 2129L, 2890L, 2556L, 2502L, 2665L,
  3250L, 4567L, 4522L, 4672L, 4000L, 6199L, 4143L, 3888L, 5865L,
  4675L, 5706L, 5234L, 5288L, 4342L, 5252L, 4605L, 4618L, 5599L,
  5526L, 5850L, 4676L, 4301L, 4451L, 4583L, 5386L, 4913L, 4463L,
  4310L, 3996L, 4076L, 6032L, 6201L, 4806L, 4339L, 3985L, 4406L,
  6111L, 5614L, 4649L, 3896L, 3923L, 3877L, 3403L, 3242L, 3446L,
  3560L, 3451L, 3562L, 2684L, 2412L, 2472L, 2615L, 3287L, 2959L,
  2409L, 1625L, 2004L, 2013L, 1887L, 2095L, 2445L, 1936L, 1570L,
  1613L, 1871L, 1805L, 1650L, 1557L, 1326L, 1205L, 1387L, 1003L,
  1266L, 1541L, 1425L, 1514L, 1056L, 1279L, 1115L, 1218L, 1346L,
  1295L, 1221L, 958L, 874L, 653L, 1118L, 1006L
), "Total Cases" = c(
  0L,
  0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1035L,
  1700L, 2667L, 4094L, 5546L, 7675L, 10565L, 13121L, 15623L, 18288L,
  21538L, 26105L, 30627L, 35299L, 39299L, 45498L, 49641L, 53529L,
  59394L, 64069L, 69775L, 75009L, 80297L, 84639L, 89891L, 94496L,
  99114L, 104713L, 110239L, 116089L, 120765L, 125066L, 129517L,
  134100L, 139486L, 144399L, 148862L, 153172L, 157168L, 161244L,
  167276L, 173477L, 178283L, 182622L, 186607L, 191013L, 197124L,
  202738L, 207387L, 211283L, 215206L, 219083L, 222486L, 225728L,
  229174L, 232734L, 236185L, 239747L, 242431L, 244843L, 247315L,
  249930L, 253217L, 256176L, 258585L, 260210L, 262214L, 264227L,
  266114L, 268209L, 270654L, 272590L, 274160L, 275773L, 277644L,
  279449L, 281099L, 282656L, 283982L, 285187L, 286574L, 287577L,
  288843L, 290384L, 291809L, 293323L, 294379L, 295658L, 296773L,
  297991L, 299337L, 300632L, 301853L, 302811L, 303685L, 304338L,
  305456L, 306462L
)), row.names = c(NA, -113L), class = c(
  "tbl_df",
  "tbl", "data.frame"
)) -> flu

with(flu, plot(Date, Cases, pch = 19, col = "red", ylim = c(0, 1)))
predictions <- sir_1(alpha = 1/997500000, beta = 1/1000, gamma = 1/14, eta = 1/280, S0 = 1000, E0 = 10, I0 = 1, R0 = 0, D0 = 0, times = flu$Date)
with(predictions, lines(time, I, col = "red"))

plot(flu$Total.Cases, pch = 19, col = "red")
flu[1:10,]
flu$Total.Cases

model_fit <- function(alpha, beta, data, N = 327200000, ...) {
  I0 <- data$Cases[1] # initial number of infected (from data)
  times <- data$Date   # time points (from data)
  # model's predictions:
  predictions <- sir_1 (alpha = alpha, beta  = beta, gamma = gamma, eta = eta,   # parameters
                       S0 = N - I0, E0 = E0, I0 = I0, R0 = 0, D = D0, # variables' intial values
                       times = time_values)              # time points
  # plotting the observed prevalences:
  with(data, plot(Date, Cases, ...))
  # adding the model-predicted prevalence:
  with(predictions, lines(time, I, col = "red"))
}

model_fit(alpha = 1/997500000, beta = 1/1000, gamma = 1/14, eta = 1/280, flu, pch = 19, col = "red", ylim = c(0, 100000))


predictions <- sir_1(alpha = 1/997500000, beta = 1/1000, gamma = 1/14, eta = 1/280, S0 = 1000, E0 = 10, I0 = 1, R0 = 0, D0 = 0, times = flu$Date)
predictions


sum((predictions$I - flu$Cases)^2)

# the observed prevalences:
with(flu, plot(Date, Cases, pch = 19, col = "red", ylim = c(0, 1000)))
# the model-predicted prevalences:
with(predictions, lines(time, I, col = "red", type = "o"))
# the "errors":
segments(flu$Date, flu$Cases, predictions$time, predictions$I)

ss <- function(alpha, beta, data = flu, N = 763) {
  I0 <- data$Cases[1]
  times <- data$Date
  predictions <- sir_1(alpha = alpha, beta = beta, gamma = gamma, eta = eta,   # parameters
                       S0 = N - I0, E0 = E0, I0 = I0, R0 = 0, D = D0, # variables' intial values
                       times = time_values)                # time points
  sum((predictions$I[-1] - data$Cases[-1])^2)
}

ss(alpha = 1/997500000, beta = 1/1000, gamma = 1/14, eta = 1/280)

alpha_val <- seq(from = 0.0016, to = 1/1000, le = 100)
ss_val <- sapply(alpha_val, ss, beta = 0.5)

min_ss_val <- min(ss_val)
min_ss_val

alpha_hat <- alpha_val[ss_val == min_ss_val]
alpha_hat

plot(alpha_val, ss_val, type = "l", lwd = 2,
     xlab = expression(paste("infectious contact rate ", alpha)),
     ylab = "sum of squares")
# adding the minimal value of the sum of squares:
abline(h = min_ss_val, lty = 2, col = "grey")
# adding the estimate of alpha:
abline(v = alpha_hat, lty = 2, col = "grey")


beta_val <- seq(from = 0.4, to = 0.575, le = 100)
ss_val <- sapply(beta_val, function(x) ss(alpha_hat, x))
(min_ss_val <- min(ss_val))

(beta_hat <- beta_val[ss_val == min_ss_val])

plot(beta_val, ss_val, type = "l", lwd = 2,
     xlab = expression(paste("recovery rate ", beta)),
     ylab = "sum of squares")
abline(h = min_ss_val, lty = 2, col = "grey")
abline(v = beta_hat, lty = 2, col = "grey")

n <- 10 # number of parameter values to try
alpha_val <- seq(from = 0.002, to = 0.0035, le = n)
beta_val <- seq(from = 0.3, to = 0.65, le = n)
param_val <- expand.grid(alpha_val, beta_val)
ss_val <- with(param_val, Map(ss, Var1, Var2))
ss_val <- matrix(unlist(ss_val), n)
persp(alpha_val, beta_val, -ss_val, theta = 40, phi = 30,
      xlab = "alpha", ylab = "beta", zlab =  = "-sum of squares")


n <- 30 # number of parameters values
alpha_val <- seq(from = 0.002, to = 0.0035, le = n)
beta_val <- seq(from = 0.3, to = 0.65, le = n)
# calculating the sum of squares:
param_val <- expand.grid(alpha_val, beta_val)
ss_val <- with(param_val, Map(ss, Var1, Var2))
ss_val <- unlist(ss_val)

# minimum sum of squares and parameters values:
(ss_min <- min(ss_val))

ind <- ss_val == ss_min
(alpha_hat <- param_val$Var1[ind])

(beta_hat <- param_val$Var2[ind])


# visualizing the sum of squares profile:
ss_val <- matrix(ss_val, n)
image(alpha_val, beta_val, ss_val,
      xlab = expression(paste("infectious contact rate ", alpha, " (/person/Date)")),
      ylab = expression(paste("recovery rate ", beta, " (/Date)")))
contour(alpha_val, beta_val,ss_val, add = TRUE)
points(alpha_hat, beta_hat, pch = 3)
box(bty = "o")


ss(alpha = 1/997500000, beta = 1/1000, gamma = 1/14, eta = 1/280)
ss2 <- function(x) {
  ss(alpha = x[1], beta = x[2],  gamma = x[3], eta = x[4])
}

ss2(c(1/997500000, 1/1000, 1/14, 1/128))

starting_param_val <- c(1/997500000, 1/1000, 1/14, 1/128)
ss_optim <- optim(starting_param_val, ss2)


ss_optim
ss_optim$value
ss_optim$par


mLL <- function(alpha, beta, sigma, Date, Cases, N = 763) {
  alpha <- exp(alpha) # to make sure that the parameters are positive
  beta <- exp(beta)
  sigma <- exp(sigma)
  I0 <- Cases[1] # initial number of infectious
  observations <- Cases[-1] # the fit is done on the other data points
  predictions <- sir_1(alpha = alpha, beta = beta, gamma = gamma, eta = eta,
                       S0 = N - I0, E0 = E0 I0 = I0, R0 = 0, D = D0, times = Date)
  predictions <- predictions$I[-1] # removing the first point too
  # returning minus log-likelihood:
  -sum(dnorm(x = observations, mean = predictions, sd = sigma, log = TRUE))
}

library(bbmle) # for "mle2", "coef", "confint", "vcov", "logLik", "profile", "summary", "plot.profile.mle2"

starting_param_val <- list(alpha = 1/997500000, beta = 1/1000, gamma = 1/14, eta = 1/280, sigma = 1)
estimates <- mle2(minuslogl = mLL, start = lapply(starting_param_val, log),
                  method = "Nelder-Mead", data = c(flu, N = 763))

summary(estimates)
exp(coef(estimates))
exp(confint(estimates))
vcov(estimates)
-logLik(estimates)


prof <- profile(estimates)
plot(prof, main = NA)


N <- 10000 # total population size
time_points <- seq(min(flu$Date), max(flu$Date), le = 100) # vector of time points
I0 <- flu$Cases[1] # initial number of infected
param_hat <- exp(coef(estimates)) # parameters estimates
# model's best predictions:
best_predictions <- sir_1(alpha = param_hat["alpha"], beta = param_hat["beta"], gamma = param_hat["gamma"], eta = param_hat["eta"],
                          S0 = N - I0, E0 = E0 I0 = I0, R0 = 0, D = D0, time_points)$I
# confidence interval of the best predictions:
cl <- 0.95 # confidence level
cl <- (1 - cl) / 2
lwr <- qnorm(p = cl, mean = best_predictions, sd = param_hat["sigma"])
upr <- qnorm(p = 1 - cl, mean = best_predictions, sd = param_hat["sigma"])
# layout of the plot:
plot(time_points, time_points, ylim = c(0, max(upr)), type = "n",
     xlab = "time (Dates)", ylab = "prevalence")
# adding the predictions' confidence interval:
sel <- time_points >= 1 # predictions start from the second data point
polygon(c(time_points[sel], rev(time_points[sel])), c(upr[sel], rev(lwr[sel])),
        border = NA, col = adjustcolor("red", alpha.f = 0.1))
# adding the model's best predictions:
lines(time_points, best_predictions, col = "red")
# adding the observed data:
with(flu, points(Date, Cases, pch = 19, col = "red"))


#Poisson Distribution of Errors

#mLL <- function(alpha, beta, sigma, Date, Cases, N = 10000) {
mLL_pois <- function(alpha, Date, Cases, N = 1000) {
  alpha <- exp(alpha) # to make sure that the parameters are positive
  beta <- 1/1000
  gamma <- 1/14
  eta <- 1/280
    
 
  #  sigma <- exp(sigma)
  I0 <- Cases[1] # initial number of infectious
  observations <- Cases[-1] # the fit is done on the other data points
  predictions <- sir_1(alpha = alpha, beta = 1/1000, gamma = 1/14, eta = 1/280,
                       S0 = N - I0, E0 = E0 I0 = I0, R0 = 0, D = D0, times = Date)
  predictions <- predictions$I[-1] # removing the first point too
  if (any(predictions < 0)) return(NA) # safety
  # returning minus log-likelihood:
  #  -sum(dnorm(x = observations, mean = predictions, sd = sigma, log = TRUE))
  -sum(dpois(x = observations, lambda = predictions, log = TRUE))
}

starting_param_val <- list(alpha = 1/997500000)
estimates_pois <- mle2(minuslogl = mLL_pois,
                       start = lapply(starting_param_val, log),
                       data = c(flu$Cases, N = 1000))

exp(coef(estimates))
exp(coef(estimates_pois))
exp(confint(estimates))
exp(confint(estimates_pois))
vcov(estimates_pois)

# points estimates:
param_hat <- exp(coef(estimates_pois))
# model's best predictions:
best_predictions <- sir_1(alpha = param_hat["alpha"], beta = param_hat["beta"],
                          S0 = N - I0, E0 = E0 I0 = I0, R0 = 0, D = D0, time_points)$I
# confidence interval of the best predictions:
cl <- 0.95 # confidence level
cl <- (1 - cl) / 2
lwr <- qpois(p = cl, lambda = best_predictions)
upr <- qpois(p = 1 - cl, lambda = best_predictions)
# layout of the plot:
plot(time_points, time_points, ylim = c(0, max(upr)), type = "n",
     xlab = "time (Dates)", ylab = "prevalence")
# adding the predictions' confidence interval:
sel <- time_points >= 1 # predictions start from the second data point
polygon(c(time_points[sel], rev(time_points[sel])), c(upr[sel], rev(lwr[sel])),
        border = NA, col = adjustcolor("red", alpha.f = 0.1))
# adding the model's best predictions:
lines(time_points, best_predictions, col = "red")
# adding the observed data:
with(flu, points(Date, Cases, pch = 19, col = "red"))

-logLik(estimates)
-logLik(estimates_pois)


This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.