R: Question about Optimizing - Invalid Function Value in Optimize

后端 未结 1 1899
轻奢々
轻奢々 2021-01-28 02:51

R Programming Question - Hello, we are two summer research students participating in a research program. After trial and error, we have not been able to pinpoint what is causing

相关标签:
1条回答
  • 2021-01-28 03:10

    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.

    0 讨论(0)
提交回复
热议问题