R coding: Function to write Approximate Bayesian Computation with Population Monte Carlo method -
i trying write function can calculate approximate bayesian computation using population monte carlo method. however, ran troubles r code following error.
head(abc_pmc(2,5000,1,observeddata,observedsummary))
error in while (prior == 0) { : missing value true/false needed in addition: warning message: in log(importance.sample[2]) : nans produced
the code doesn't have problem when run 1 iteration, t=1. issue lies second iteration , think assignment of importance.sammpling step. can't figure out error tried run code manually , works.
any , appreciated.
thanks!
initialise variables:
set.seed(12345) n=5000 weight = rep(1,n) theta = matrix(na, nrow = n, ncol = 2) (i in 1:n) { fit = c(runif(1,-2,2), runif(1,0,4)) theta[i,] = fit } #calculate importance density current t use in next iteration old.prop.weight = weight / sum(weight) old.theta = theta kernel.mean = cbind(old.theta[,1],old.theta[,2]) kernel.cov = cov(old.theta)
main loop:
(i in 1:n) { prior=0 while (prior==0) { #loop test prior=0 choice = sample(1:n, size = 1, prob = old.prop.weight) importance.sample = rmvnorm(1,mean = kernel.mean[choice,], sigma = kernel.cov) #logsigma ensure theta2 nonnegative if (importance.sample[2] != 0) importance.sample[2] = log(importance.sample[2]) fit = c(importance.sample[1],importance.sample[2]) } prior = as.numeric(dunif(fit[1],min = -2,max = 2) * dunif(exp(fit[2]),min = 0,max = 4)) } theta[i,] = fit ##end of step 2 loop }
edit: tried make code reproducible example
Comments
Post a Comment