########################################### dat<-read.csv("commune.measles.csv",header=F) par(mfrow=c(1,1)) matplot(dat,type="b",pch=19,xlab="biweek",ylab="cases", col=c(1,2,4),lty=c(1,1,1),ylim=c(0,1200)) abline(v=12,lty=2,lwd=2,col=grey(.5)) text(12,1100,labels="MSF campaign",pos=2) ########################################### likelihood<-function(par,vecs){ S0<-par[1] beta<-par[2] I<-vecs$I S<-floor(S0-cumsum(I[1:(length(I)-1)])) p<-1-exp(-beta*(I[1:(length(I)-1)])) L<-dbinom(I[2:length(I)],S,p,log=TRUE) Lik<-sum(L,na.rm=TRUE) # note that this returns the log-likelihood } ########################################### #first read in the data dat<-read.csv("commune.measles.csv",header=F) #choose the time series for the Commune 1 I<-dat[,1] #make the data list to pass to the likelihood function (above) vecs<-list(I=I) #set the number of MCMC iterations iter<-100000 #set storage vectors S0<-numeric(iter) beta<-numeric(iter) #choose initial values S0[1]<-floor(1.9*sum(I)) beta[1]<-1e-4 #set step sizes for proposing new candidate values of parameters S0.step<-10 beta.step<-5e-6 for(i in 2:iter){ #propose a new value for S0 (note that I've restricted #proposals so that S0 must be greater than the sum of reported cases) S0.tmp<-0 while(S0.tmp