[R] Problem in while loop

R. Michael Weylandt michael.weylandt at gmail.com
Mon Dec 5 23:18:46 CET 2011


Your code is not reproducible nor minimal, but why don't you put a
command print(acceptprob) in and see if you are getting reasonable
values. If these values are extremely low it shouldn't surprise you
that your loop takes a long time to run.

More generally, read up on the use of print() and browser() as debugging tools.

Michael

On Mon, Dec 5, 2011 at 3:47 PM, Gyanendra Pokharel
<gyanendra.pokharel at gmail.com> wrote:
> I forgot to upload the R-code in last email, so heare is one
>
> epiann <- function(T0 = 1, N=1000, ainit=1, binit=1,rho = 0.99, amean = 3,
> bmean=1.6, avar =.1, bvar=.1, f){
>
>        moving <- 1
>        count <- 0
>        Temp <- T0
>        aout <- ainit
>        bout <- binit
>        while(moving > 0){
>                moving <- 0
>                for (i in 1:N) {
>                aprop <- rnorm (1,amean, avar)
>                bprop <- rnorm (1,bmean, bvar)
>                if (aprop > 0 & bprop > 0){
>                acceptprob <- min(1,exp((f(aout, bout) -
> f(aprop,bprop))/Temp))
>                u <- runif(1)
>                if(u<acceptprob){
>                    moving <- moving +1
>                    aout <- aprop
>                    bout <- bprop
>                    }
>                    else{aprob <- aout
>                        bprob <- bout}
>                }
>            }
>        Temp <- Temp*rho
>            count <- count +1
>
>    }
>    fmin <- f(aout,bout)
>    return(c(aout, bout,fmin, count) )
>
> }
> out<- epiann(f = loglikelihood)
>
> On Mon, Dec 5, 2011 at 3:46 PM, Gyanendra Pokharel <
> gyanendra.pokharel at gmail.com> wrote:
>
>> Hi all,
>> I have the following code,
>> When I run the code, it never terminate this is because of the while loop
>> i am using. In general, if you need a loop for which you don't know in
>> advance how many iterations there will be, you can use the `while'
>> statement so here too i don't know the number how many iterations are
>> there. So Can some one suggest me whats going on?
>> I am using the Metropolis simulated annealing algorithm
>> Best
>>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.



More information about the R-help mailing list