21

I am new to R, so forgive me if the question is a little silly. I am trying to write a simple while loop for a value function iteration. My function (optim.routine) uses the solver ipoptr. Here is my code:

d<-1
old1<-0
old2<-0
num.iter<-0
i.esp<-1e-05
i.T<-100
lb<-0
ub<-10

while (d>i.eps & num.iter<i.T){
new1 <- optim.routine(old1, old2, eval_f=eval_f, eval_grad_f=eval_grad_f, lb=lb, ub=ub, update=FALSE)
d<-dist(c(old1, new1), method="euclidean")
num.iter<-num.iter+1
old1<-new1
}

where optim.routine is the following function:

optim.routine<-function(old1, old2, eval_f=obj, eval_grad_f=obj.deriv, lb=lb, ub=ub, update){
  if (isTRUE(update)){
    var2<-old2
    var1<-old1
    var1.deriv<-deriv(var1)
    optimize <- ipoptr(x0 = old2, eval_f = eval_f, eval_grad_f = eval_grad_f, lb = lb,
                       ub = ub)

    new1<- optimize$objective
    new2<- optimize$solution
    old2<-new2
    old1<-new1
  }else{
    var2<-old2
    var1<-old1
    var1.deriv<-vf.deriv(var1)
    optimize <- ipoptr(x0 = old2, eval_f = eval_f, eval_grad_f = eval_grad_f, lb = lb,
                       ub = ub)

    new1<- optimize$objective
    new2<- optimize$solution
    old1<-new1
  }
}

and deriv is a function that computes derivatives.

I get the following error if i try to run the code:

source('/mnt/ide0/home/myname/Documents/optim.R')
Error in get(as.character(FUN), mode = "function", envir = envir) : 
  object 'fn' of mode 'function' was not found

and if I debug the function:

Browse[2]> n
Error in isTRUE(update) : argument "update" is missing, with no default

If I only source the function without the while loop no error is displayed. Honestly, I have no clue. Any help is greatly appreciated. Thanks!

Claudia

Claudia M.
  • 333
  • 1
  • 2
  • 8
  • +1 I have run in similar problem and still do not fully understand what's going on: http://stackoverflow.com/q/17808575/684229 (the answer there won't help you much though). Another similar problem here: http://stackoverflow.com/q/16723036/684229 – Tomas Jan 26 '14 at 19:17
  • 1
    Where do you define eval_f and eval_grad_f? Do you have a self-contained example that exhibits the behavior? – Jonathan Chang Jan 26 '14 at 19:57
  • They are the objective function and its gradient. I could post the whole code, if needed. The gradient is approximated by Chebychev polynomials, in my case. – Claudia M. Jan 26 '14 at 20:00
  • 1
    I had exactly the same error message when I named a variable with the same name of an existing function in R. I've found this tip here:http://notepad.patheticcockroach.com/2565/a-bad-idea-in-r-using-variables-with-the-same-name-as-existing-functions/ Hope it helps you too. [1]: http://notepad.patheticcockroach.com/2565/a-bad-idea-in-r-using-variables-with-the-same-name-as-existing-functions/ – FraNut Oct 12 '14 at 11:26

2 Answers2

24

I had exactly the same error message when I named a variable with the same name of an existing function in R. I've found this tip here: http://notepad.patheticcockroach.com/2565/a-bad-idea-in-r-using-variables-with-the-same-name-as-existing-functions/ Hope it helps you too. – FraNut Oct 12 at 11:26

He's right refrain from using variables that might be function names too.

e.g

z1<-aggregate(steps ~ interval, data_df, mean)
mean<-mean(z[,2],na.rm = TRUE)

mean is a variable and a function name passed as an argument to the aggregate function causing a conflict

user14764
  • 748
  • 1
  • 9
  • 19
Anirudh Ashok
  • 241
  • 2
  • 3
14

Many times that error will appear when you previously created an object called "mean" in the R environment. This creates a conflict when calling the function "mean". To stop this error use:

rm(mean)

This removes the object "mean" from the environment and allows R to call the function "mean".

Brett DeWoody
  • 59,771
  • 29
  • 135
  • 184
Nyine
  • 141
  • 1
  • 2