I'm using the BayesianTools package to run some MCMCs on data, but my specific project requires me to take some outputs of one MCMC and convert it into a prior for a new MCMC. Priors need to be defined as single input functions as far as I'm aware in BayesianTools, so what I'm trying to do is write a function that takes the posteriors from one round of MCMCs and generates a probability density function as a result. I tried doing this using the code below, but it seems that R doesn't save dens inside of the function probabilityDensityFunction as I had hoped it would. I need to generate 16 of these objects and my life would be so much easier if I could automatically generate them in a manner like this instead of manually coding for each one, so any help is apreciated.
Thanks!
probabilityDensityFunction_generator <- function(dens) {
# This function is to generate a separate function called the probability density function.
#It takes objects of type density() as an imput and returns a function which estimates the probability density.
probabilityDensityFunction <- function(x){
if(x < dens$x[1]){
out = 0
} else if(x>dens$x[length(dens$x)]){
out = 0
} else{
for(i in 1:length(dens$x)){
if(x<dens$x){
out = dens$y[i]
} else{
break
}
}
}
return(out)
}
return(probabilityDensityFunction)
}