I use the IMIS package (sampling for measuring the additive mixture) to evaluate the parameters. Unfortunately, he wrote to search for likelihood , sample.prior and prior functions in the environment in which he called, so I cannot wrap it in a function (my final goal). The one-dimensional example from ?IMIS works just fine,
require(IMIS) likelihood <- function(theta) exp(-1*sin(3*theta)*sin(theta^2) - 0.1*theta^2) prior <- function(theta) dnorm(theta, 0, 5) sample.prior <- function(n) rnorm(n, 0, 5) result = IMIS(500, 3000, 100, 10) ## also fine using do.call (pertinent below) result <- do.call(IMIS, args = list(B = 500, B.re = 3000, number_k = 100, D = 10))
but it is not surprising that wrapping it in a function fails:
rm(likelihood, prior, sample.prior, result) imisWrap <- function() { likelihood <- function(theta) exp(-1*sin(3*theta)*sin(theta^2) - 0.1*theta^2) prior <- function(theta) dnorm(theta, 0, 5) sample.prior <- function(n) rnorm(n, 0, 5) result = IMIS(500, 3000, 100, 10) return(result) } imisWrap() ## can't find sample.prior
I think there is a way to create an environment in my shell (or use its environment) and then use do.call to start IMIS in this environment, but I donβt know how to create a new environment that has likelihood , prior , sample.prior and result in him.
Edit: Using @BenBolker's excellent comments, I have an improved but still not working attempt:
imisWrap2 <- function() { likelihood <- function(theta) exp(-1*sin(3*theta)*sin(theta^2) - 0.1*theta^2) prior <- function(theta) dnorm(theta, 0, 5) sample.prior <- function(n) rnorm(n, 0, 5) imisEnv <- new.env() assign("likelihood", likelihood, envir = imisEnv) assign("sample.prior", sample.prior, envir = imisEnv) assign("prior", prior, envir = imisEnv) result = do.call(IMIS, args = list(B = 500, B.re = 3000, number_k = 100, D = 10), envir = imisEnv) return(result) }
But it still cannot find the function.