It looks like
mu=max(diff(d$y))/(d[i+1,"t"]-d[i, "t"]), lambda=i)
is returning NA
, which would explain the result. No \mu
suppressPackageStartupMessages(library(dplyr))
dat <- structure(list(
confirmed =
c(2, 5, 18, 28, 43, 61, 95, 139, 245, 388, 593, 978, 1501, 2336, 2922, 3513, 4747, 5823, 6566, 7161, 8042, 9000, 10075, 11364, 12729, 13938, 14991, 16169, 17361, 18407, 19644, 20610, 21638, 23049, 24811, 27017, 29406, 32332, 35408, 38309),
deaths =
c(2, 2, 4, 5, 8, 12, 16, 19, 26, 34, 43, 54, 66, 77, 92, 107, 124, 145, 194, 237, 291, 354, 429, 514, 611, 724, 853, 988, 1135, 1284, 1433, 1556, 1685, 1812, 1934, 2077, 2234, 2378, 2517, 2640),
recovered =
c(0, 0, 0, 0, 0, 0, 0, 49, 49, 73, 123, 175, 291, 291, 552, 739, 913, 1669, 2134, 2394, 2731, 2959, 2959, 2959, 2959, 4590, 4590, 5389, 5389, 5710, 6745, 7635, 7931, 7931, 8913, 9625, 10457, 11133, 11679, 12391)),
class =c("spec_tbl_df", "tbl_df", "tbl", "data.frame"),
row.names =c(NA, -40L), spec = structure(list(cols = list(confirmed = structure(list(),
class =c("collector_double", "collector")), deaths = structure(list(),
class =c("collector_double", "collector")), recovered = structure(list(),class =c("collector_double", "collector"))), default = structure(list(),class =c("collector_guess", "collector")), skip = 1), class = "col_spec"))
tibble::rownames_to_column(dat) %>% rename(day = rowname) -> dat
dat
#> # A tibble: 40 x 4
#> day confirmed deaths recovered
#> <chr> <dbl> <dbl> <dbl>
#> 1 1 2 2 0
#> 2 2 5 2 0
#> 3 3 18 4 0
#> 4 4 28 5 0
#> 5 5 43 8 0
#> 6 6 61 12 0
#> 7 7 95 16 0
#> 8 8 139 19 49
#> 9 9 245 26 49
#> 10 10 388 34 73
#> # … with 30 more rows
fit.gompertz <- function(data, time){
d <- data.frame(y=data, t=time)
if (length(unique(d$t)) < 3) stop("too few data points to fit curve")
i <- which.max(diff(d$y))
starting.values <- c(a=max(d$y),
mu=max(diff(d$y))/(d[i+1,"t"]-d[i, "t"]),
lambda=i)
print("Starting Values for Optimization: ")
print(starting.values)
formula.gompertz <- "y~a*exp(-exp(mu*exp(1)/a*(lambda-t)+1))"
nls(formula.gompertz, d, starting.values)
}
model <- fit.gompertz(dat$confirmed, dat$day)
#> Warning in Ops.factor(d[i + 1, "t"], d[i, "t"]): '-' not meaningful for factors
#> [1] "Starting Values for Optimization: "
#> a mu lambda
#> 38309 NA 38
#> Warning in Ops.factor(lambda, t): '-' not meaningful for factors
#> Warning in Ops.factor(lambda, t): '-' not meaningful for factors
#> Error in numericDeriv(form[[3L]], names(ind), env): Missing value or an infinity produced when evaluating the model
Created on 2020-03-30 by the reprex package (v0.3.0)