2

We have not been able to pinpoint what is causing the error of Invalid Function Value in Optimize in our Optimizing code. If you could offer any insight, it would be appreciated.

H_fun <- function(c) 
{ 
val = -current_c_weight*c - X_counts%*%log( 
exp(rep(c,length(current_Theta))*current_Theta) - 
current_elongation_rates ) 
print('#########iteration display#############') 
print('c') 
print(c) 
print('val') 
print(val) 
print('current_c_weight') 
print(current_c_weight) 
print('current_Theta') 
print(current_Theta) 
print('current_elongation_rates') 
print(current_elongation_rates) 
} 

#...snip...

# minimize -H(c) without the non-negativity constraint 
#tmp = optim(c(0,1),H_fun,NULL, method = "BFGS", hessian = TRUE); 
tmp = optimize(H_fun,interval = c(0,1)); 

Here is a link to the code:

http://www.text-upload.com/read.php?id=102950&c=8605046

2
  • Unfortunately your example is not reproducible. Please post the values of current_c_weight, X_counts, current_Theta and current_elongation_rates. Commented Jul 12, 2011 at 7:59
  • @Andrie, the full source code is at the text-upload.com link. Commented Jul 12, 2011 at 15:49

1 Answer 1

5

Are you sure H_fun is returning a one-dimensional value?

Look at fcn1() in the R optimize() source code:

static double fcn1(double x, struct callinfo *info)
{
    SEXP s;
    REAL(CADR(info->R_fcall))[0] = x;
    s = eval(info->R_fcall, info->R_env);
    switch(TYPEOF(s)) {
    case INTSXP:
        if (length(s) != 1) goto badvalue;
        if (INTEGER(s)[0] == NA_INTEGER) {
            warning(_("NA replaced by maximum positive value"));
        return DBL_MAX;
        }
        else return INTEGER(s)[0];
        break;
    case REALSXP:
        if (length(s) != 1) goto badvalue;
        if (!R_FINITE(REAL(s)[0])) {
            warning(_("NA/Inf replaced by maximum positive value"));
            return DBL_MAX;
        }
        else return REAL(s)[0];
        break;
    default:
        goto badvalue;
    }
 badvalue:
    error(_("invalid function value in 'optimize'"));
    return 0;/* for -Wall */
}

goto badvalue occurs if length is not 1. Also, the package summary states that optimize() works on a one-dimensional unconstrained function.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.