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

Maram SAlem marammagdysalem at gmail.com
Sun Feb 14 18:22:03 CET 2016


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]]



More information about the R-help mailing list