1

I have the following problem, I am generating random values and when I run the syntax I get the following error "NA / NaN / Inf in a call to an external function (arg 5)" but this error appears when there is text in the base (at least that I read), in this case it shouldn't happen.

It should be noted that the Ars command specifies the limits of the variable x, which is in the interval 0 <x <1. The problem occurs when executing the ARS command and the simulated values of "f" are only 0

library(ars)

f<-function(x){ 
               125*log(.5+(.25*x))+34*log(x)+38*log(1-x) +72*log(.25)
}


fprima<-function(x){ 
                    (125/(.2+.25*x))+(34/x)-(38/(1-x)) 
                    }


theta<-ars(n=10000,f,fprima,lb=TRUE,ub=TRUE,xlb=0,xub=1)


theta
  • Your `x` appears in denominators. Have you ensured that these denominators can't become 0? Your `x` appears in a logarithm. Have you ensured that the argument of the logarithm can become <= 0? – Roland Apr 28 '21 at 07:47
  • E.g., if `x=0` specifies some kind of starting value then `34/x` is `Inf` immediately. – Roland Apr 28 '21 at 07:49
  • @Roland the variable x if it is bounded – Edgar Valderrama Del Rosario Apr 28 '21 at 07:58
  • But you say "the simulated values are only zero". Are the "simulated values" in `x`? If so, @Roland has identified the problem. Does `ars` calculate the range of `x`? if so, that may be an issue, becase if `x` is "all zero", then so is its range. We need clarity, which we don't have at the moment. – Limey Apr 28 '21 at 08:16
  • @Limey Hi, The simulated values of the function of x, x is bounded between 0 and 1 (0 – Edgar Valderrama Del Rosario Apr 28 '21 at 08:25

1 Answers1

0

Add message(x) to f to see the x-values it gets passed. In your original code, you specified a starting value x = 0 and that is passed to f regardless of specified boundaries. The same if you use the default starting values (as in the current iteration of your code).

The code runs if you specify a sane starting value:

theta<-ars(n=10000,f,fprima,lb=TRUE,ub=TRUE,xlb=0,xub=1, m = 1, x = 0.5)

However, you then get these internal errors:

Error in sobroutine sample_...
ifault= 5 

According to the documentation this means "non-concavity detected".

It appears your fprima function is not correct. According to the documentation, it is supposed to be d/du log(f(u,...)). Based on D(quote(125*log(.5+(.25*x))+34*log(x)+38*log(1-x) +72*log(.25)), "x") I've corrected this:

fprima<-function(x){ 
  125 * (0.25/(0.5 + (0.25 * x))) + 34 * (1/x) - 38 * (1/(1 - x))
}

With that, the code runs fine.

Roland
  • 127,288
  • 10
  • 191
  • 288