How to fill Wednesday in R

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.

+4
source share
2 answers

You can put environment(IMIS) <- environment() at the top of imisWrap to make it work. This only changes the behavior of IMIS in imisWrap . The version of the function in the package namespace does not change.

+2
source

You can make it work attach with enviromnent (and detach ing after use for cleaning):

 imisWrap() <- function() { imisList <- list( 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 <- as.environment(imisList) attach(imisEnv) result = IMIS(500, 3000, 100, 10) detach(imisEnv) return(result) } imisWrap() [1] "5000 likelihoods are evaluated in 0 minutes" [1] "Stage MargLike UniquePoint MaxWeight ESS" [1] 1.000 -0.806 1796.246 0.001 2434.921 [1] "maximum posterior= -1.96 , likelihood= 0.61 , prior= -2.57 , time used= 0 minutes, convergence= 0" ... 

However, the echo of @BenBolker is that it really is a problem with a function that the package author should solve more cleanly.

+2
source

Source: https://habr.com/ru/post/1439279/


All Articles