Skip to content
Prev 278922 / 398502 Next

Problem in log

You still haven't provided anything reproducible since we can't get to
your data file (also you used
the-you-really-shouldn't-use-this-function function attach) but here's
what I'd guess:

Your say the problem occurs when

exp(-alpha*d^(-beta)) > 1

Of course, this happens when

alpha*d^(-beta) < 0.

It seems like alpha , beta > 0, so you need to ask yourself why d < 0
: I can't immediately see why this would happen in your code. The
easiest way to get at this it just insert a print(d) statement or,
even better, a stopifnot(d > 0) with options(error = recover). This
will open an interactive debugger and you can do a post-mortem to see
exactly what happened.

This is really all I can see without minimal reproducible code, which
you aren't providing.

You may also want to work on vectorizing your code to make it faster
for long simulations (and arguably easier to read, though it's a
matter of taste)

E.g.,

for(i in 1:100){
        if(i!=k){
            if(inftime[i]==0){
                loglh<-loglh +log(1-p[i])
            }
            if(inftime[i]==2){
                loglh<-loglh + log(p[i])
            }
        }
    }

could become something like (very , very untested):

loglh <- loglh + sum((ifelse(inftime == 0, log(1-p), 0) +
ifelse(inftime == 2, log(p), 0))[-k])

which will be much faster. This takes all the elements and vectorizes
the two if statements, throws out the k case that you don't want, then
sums them and adds to loglh. It should be much faster than your loop
since it's all vectorized.

Finally, within the loglikelihood() function, you don't need to
pre-define d,p,k: it suffices to initialize them at the values you
calculate.

Hope something in here helps, but my bet is that your problem is
coming from the unprovided data and/or something with attach.

Michael

On Wed, Nov 30, 2011 at 7:43 AM, Gyanendra Pokharel
<gyanendra.pokharel at gmail.com> wrote: