Go back to Flu Mortality
library(bbmle)
library(deSolve)
dyn.load(“mymod.so”)
data9 = c(119, 125, 137, 142, 115, 125, 143, 151, 141, 138, 141, 132, 132, 160, 151, 191, 226, 210, 244, 239, 269, 314, 387, 422, 406, 347, 325, 279, 224, 178, 155, 169, 165, 126, 135, 135, 136)
t9 = c(0:36)
base9 = c(108.2354, 108.7691, 109.4303, 110.2099, 111.0971, 112.0794, 113.1430, 114.2729, 115.4532, 116.6672, 117.8978, 119.1274, 120.3388, 121.5148, 122.6388, 123.6950, 124.6684, 125.5454, 126.3136, 126.9625, 127.4830, 127.8679, 128.1122, 128.2127, 128.1684, 127.9803, 127.6515, 127.1874, 126.5949, 125.8831, 125.0628, 124.1461, 123.1469, 122.0800, 120.9613, 119.8074, 118.6354)
model_SIR = function(t, theta) {
with(as.list(theta), { state1 = c(S = N - I1, I = I1) pars1 = c(beta = beta1, alpha = alpha1, N = N) state2 = c(S = N - I2, I = I2) pars2 = c(beta = beta2, alpha = alpha1, N = N) out1 = ode(y = state1, times = t, func = "derivs", parms = pars1, jacfunc = "jac", dllname = "mymod", initfunc = "initmod", nout = 1, outnames = "S") out2 = ode(y = state2, times = t, func = "derivs", parms = pars2, jacfunc = "jac", dllname = "mymod", initfunc = "initmod", nout = 1, outnames = "S") return(cbind(-diff(out1[,"S"]), -diff(out2[,"S"]))) })
}
loglik_negbin = function(theta, data, mean) {
# for negative bimonial, mean = r p /(1-p) r = theta[["r"]]; # if r < 0, return a tiny likelihood if (r < 0) { return(-1e10) } sum(dnbinom(x=data, size=r, mu=mean, log=TRUE))
}
SIR_fit_given_logL = function(times, data, base, theta0, model, logL, is_first) { # construct a general negative log-likelyhood function
L <- function() { # reconstruct the vector of named pairs of # parameters from the list of arguments l=length(theta0) theta=c() vars=names(theta0) for (i in 1:l) { item=c(x=get(vars[i])) names(item) <- vars[i] theta <-c(theta, item) } N = theta[["N"]] if(N < sum(data)) { return(1e10) } for(i in 1:length(theta)) { if(theta[[i]] < 0) { return(1e10) } } l = model(times, theta) #"base" is the vector of baseline values mean = l[,1] + l[,2] + base # compute the negative log-likelihood L = -logL(theta, data, mean) # if not a number, return a very small likelihood if (is.na(L)) { return(1e10) } return(L) }
# replace the input arguments of L by the list of parameters formals(L)←as.list(theta0)
if(is_first == TRUE) { fit = mle2(L, method = "SANN", start = as.list(theta0)) return(fit) }
fit = mle2(L, method = “BFGS”, start = as.list(theta0), control = list(maxit = 1e6*length(theta0))) # fit = mle2(L, method = “Nelder-Mead”, start = as.list(theta0), control = list(maxit = 1e6*length(theta0), ndeps = 1e-4, reltol = 1e-10)) # if there is only one parameter to be fitted # the returned confidence interval is a vector # change it to a matrix return(fit) }
SIR_fit = function(times, data, base, theta0, model) {
# we start with negative binomial if (is.na(theta0['r'])) { theta0 = c(theta0, r = 693) } is_first = TRUE p = SIR_fit_given_logL(times, data, base, theta0, model, loglik_negbin, is_first) theta0 = coef(p) names(theta0) = names(coef(p)) is_first = FALSE while (TRUE) { p = SIR_fit_given_logL(times, data, base, theta0, model, loglik_negbin, is_first) conf = confint(p) if(is.vector(conf)) { conf = matrix(conf, nrow = 1) } if(is.matrix(conf)) { break } theta0 = coef(conf) names(theta0) = names(coef(p)) } return(list(fit=p, conf = conf, r=coef(p)["r"]))
}
c = SIR_fit(c(0:37), data9, base9, c(beta1 = 4.9, alpha1 = 4.8, N = 12183.5, I1 = 2.1, beta2 = 5.3, I2 = 0.002), model_SIR)
print©
aicc = AICc(c"fit", nobs = length(data9))
print(aicc)