Tom Flowerdew
Tom Flowerdew

Reputation: 23

Passing parameters through nested functions

I am passing the same parameters through multiple functions, part of the code looking like

inner.integral <-function(f,n,i,x,odds,alpha,beta)
{ 
 beta.func(x,alpha,beta) * ( beta.prob(alpha,beta) * eval(call(paste("func",as.character(i),as.character(alpha+1),sep=".")),(x*(1+f*odds)))+(1-beta.prob(alpha,beta))*eval(call(paste("func",as.character(i),as.character(alpha),sep=".")),(x*(1-f))))
}

inner.integral.int <- function(f,n,i,x,odds,alpha,beta) {
  integrate(inner.integral,lower=0,upper=1,n=n,i=i,
             x=x,odds=odds,alpha=alpha,beta=beta)
}

inner.integral.int.2 <- function(f,n,i,x,odds,alpha,beta) {
  y <- inner.integral.int(f,n,i,x,odds,alpha,beta)$value
  return(y)
}

When passing parameters into inner.integral.int.2, via the optimise function, something like:

optimise(inner.integral.int.2,lower=min.f,
          upper=max.f,n=n,i=i,x=x,odds=odds,alpha=a,beta=b)

for defined min.f, max.f, n, i, x etc I receive the error:

Error in paste("func", as.character(i), as.character(alpha + 1), sep = ".") : 
  argument "i" is missing, with no default

What have I done wrong?

Upvotes: 2

Views: 1835

Answers (2)

mrip
mrip

Reputation: 15163

The first thing you should do is clean up your code. You don't want to have a single line of code that is 245 characters long.

You also should avoid eval(call(paste( unless it is absolutely necessary, and here it is not. At the very least, you could switch to using do.call which can be passed a character string and a list of inputs, and doesn't require creating a call object. But the better thing to do would be to avoid manipulating function names as strings, and either define func to take i and alpha as inputs (as well as the current input), or else create a list of functions func[[i]] each of which takes alpha as an input.

Most likely, if you do this, the problem will either go away or become apparent.

Upvotes: 3

csgillespie
csgillespie

Reputation: 60472

I think you may be running into problems with R's lazy evaluation. Your example is pretty complicated (look at all the parameters you have included), but as a first step, try adding the line:

force(i)

in your functions, i.e.

inner.integral.int <- function(f,n,i,x,odds,alpha,beta) {
    force(i)
    integrate(inner.integral,lower=0,upper=1,n=n,i=i,
             x=x,odds=odds,alpha=alpha,beta=beta)
}

See ?force for details.

Upvotes: 0

Related Questions