[R] NaNs produced as a returned value for a function

Maram SAlem marammagdysalem at gmail.com
Tue Feb 16 16:03:41 CET 2016


Thanks for helping Dunlap.

I just don't get why does the function bettarg() executes well if I
evaluate it (by hand) step-by step . nut for the same input values , if I
tried to execute bettarg() it will give me the error

Error in if (y <= accept.prob) { : missing value where TRUE/FALSE needed
 Although both y and accept.prob have values and are not missing.

Any help would be appreciated as this error is causing my entire(large and
complicated) code to stop execution.

Maram Salem

On 14 February 2016 at 22:08, William Dunlap <wdunlap at tibco.com> wrote:

> You can do things like
>    while ( !is.nan( r <- randomFunction(x) )) {}
>    # r will be a non-NaN value of randomFunction(x) now
> or
>    for(i in seq_len(1000)) {
>       if (!is.nan( r <- randomFunction(x))) {
>           break
>       }
>       if (i == 1000) {
>           stop("no good values of randomFunction(x) in 1000 tries")
>       }
>    }
> to avoid infinite loops when randomFunction [almost] always produces a NaN.
>
> You may  be able to avoid some NaNs by replacing log(b^n) with n*log(b) and
> log(prod(x^b)) with sum(b*log(x)).  That would avoid unneeded Inf values,
> which
> can lead to NaN down the line (Inf-Inf -> NaN, Inf/Inf -> NaN).
>
>
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
>
> On Sun, Feb 14, 2016 at 9:22 AM, Maram SAlem <marammagdysalem at gmail.com>
> wrote:
>
>> Hi all,
>>
>> I'm trying to write 2 functions(as a part of a larger code) to evaluate a
>> certain equation.  The function is :
>>
>> X= c (0.3893094  2.0962311  2.6007558  3.0761810  3.3246947  3.3917976
>>  4.1981546  6.8826140 12.3128013 15.5588470)
>> R=c (0 1 0 0 0 1 1 1 0 1)
>>
>> alpha.update=function(X, R, alpha.curr, beta.curr=1 ,m=10,
>> hyp=c(3,15,6,22.5))
>>
>>   {
>>
>>   o<-numeric(m)
>>
>>      for (i in 1:m) {
>>
>>        o[i]<- (1+R[i])*((X[i])^(beta.curr))
>>
>>         }
>>
>>    sh<-sum(o) + hyp[2] + (hyp[4]* beta.curr)
>>
>>    rg<-rgamma(1, shape= m+hyp[1]+hyp[3] , rate = sh )
>>
>>    return(rg)
>>
>>    }
>>
>>
>> alpha.curr<- alpha.update(X, R, alpha.curr=0.2, beta.curr=1 ,m, hyp)
>>
>>
>>     bettarg<- function(X, R, alpha.curr, beta.curr=1 ,m=10,
>> hyp=c(3,15,6,22.5))
>>
>>        {
>>
>>            o<-numeric(m)
>>
>>            for (i in 1:m) {
>>
>>        o[i]<- (1+R[i])*((X[i])^( beta.curr))
>>
>>         }
>>
>>       logbt<- log(beta.curr ^(m+hyp[3]-1)) + log(prod((X)^( beta.curr
>> -1)))
>> + (-1*alpha.curr *(sum(o) +  (hyp[4]* beta.curr)))
>>
>>
>>
>>       bt<- exp(logbt)
>>
>>       return(bt)
>>
>>        }
>>
>>
>> The problem is that the function bettarg() sometimes produces   NaN, and
>> this stops the evaluation of my equation, so how can I force it to ignore
>> the NaNs produced and repeat the evaluation again till it prroduces a
>> number?
>>
>>
>> Thanks in advance,
>>
>>
>> Maram Salem
>>
>>         [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list