Skip to content
Back to formatted view

Raw Message

Message-ID: <CAAmySGOKGcZ1c-ob5q2PNAfLd0R=KaNS6LeZoaVSgSbuVHKtYQ@mail.gmail.com>
Date: 2011-12-05T22:18:46Z
From: R. Michael Weylandt
Subject: Problem in while loop
In-Reply-To: <CAK=huh6LUBFFnQobbAWcE-BxpssxBi7+o2aQWuSk5Bo4+jks7Q@mail.gmail.com>

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.