## set up a lookup table for basis functions for the seasonality
tbasis <- seq(0,25,by=1/52)
basis <- periodic.bspline.basis(tbasis,nbasis=3)
colnames(basis) <- paste("seas",1:3,sep='')
## some parameters
params <- c(
gamma=26,mu=0.02,iota=0.01,
beta1=1200,beta2=1800,beta3=600,
beta.sd=1e-3,
pop=2.1e6,
rho=0.6,
S.0=26/1200,I.0=0.001,R.0=1-0.001-26/1200
)
## set up the pomp object
## the C codes "sir_euler_simulator", "sir_euler_density", and "sir_ODE" are included
## in the "examples" directory (file "sir.c")
po <- pomp(
time=seq(1/52,20,by=1/52),
data=rbind(measles=numeric(52*20)),
t0=0,
tcovar=tbasis,
covar=basis,
delta.t=1/52/20,
statenames=c("S","I","R","cases","W","B","dW"),
paramnames=c("gamma","mu","iota","beta1","beta.sd","pop"),
covarnames=c("seas1"),
zeronames=c("cases"),
step.fun="sir_euler_simulator",
dens.fun="sir_euler_density",
skeleton="sir_ODE",
rprocess=euler.simulate,
dprocess=euler.density,
measurement.model=measles~binom(size=cases,prob=exp(rho)),
initializer=function(params,t0,...){
p <- exp(params)
with(
as.list(p),
{
fracs <- c(S.0,I.0,R.0)
x0 <- c(
round(pop*fracs/sum(fracs)), # make sure the three compartments sum to 'pop' initially
rep(0,9) # zeros for 'cases', 'W', and the transition numbers
)
names(x0) <- c("S","I","R","cases","W","B","SI","SD","IR","ID","RD","dW")
x0
}
)
}
)
## simulate from the model
tic <- Sys.time()
x <- simulate(po,params=log(params),nsim=3)
toc <- Sys.time()
print(toc-tic)
plot(x[[1]])
t <- seq(0,4/52,1/52/20)
X <- simulate(po,params=log(params),nsim=10,states=TRUE,obs=TRUE,times=t)
f <- dprocess(
po,
x=X$states[,,31:40],
times=t[31:40],
params=matrix(
log(params),
nrow=length(params),
ncol=10,
dimnames=list(names(params),NULL)
),
log=TRUE
)
apply(f,1,sum)
g <- dmeasure(
po,
y=rbind(measles=X$obs[,7,]),
x=X$states,
times=t,
params=matrix(
log(params),
nrow=length(params),
ncol=10,
dimnames=list(names(params),NULL)
),
log=TRUE
)
apply(g,1,sum)
Run the code above in your browser using DataLab