Printing the loop number for each iteration

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Printing the loop number for each iteration

Bogaso
Hi all, I have a lengthy 'for' loop and for each loop I want to track the
iteration number that is currently going on. For this, I have tried
following:

 

> for (i in 1:10) {

+ DumDat <- rnorm(1000)

+ cat("iteration:", i, " \n")

+ }

iteration: 1  

iteration: 2  

iteration: 3  

iteration: 4  

iteration: 5  

iteration: 6  

iteration: 7  

iteration: 8  

iteration: 9  

iteration: 10  

 

However when I run this loop, all iteration numbers are showing only after
completion of all calculation, so could not achieve my goal. Can somebody
help me out, how can I do that?

 

Thanks,

 

 


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Printing the loop number for each iteration

jholtman
if using the GUI, turn off buffering, or use 'flush.console()' after 'cat'.

Sent from my iPad

On Jul 21, 2011, at 7:34, "Bogaso Christofer" <[hidden email]> wrote:

> Hi all, I have a lengthy 'for' loop and for each loop I want to track the
> iteration number that is currently going on. For this, I have tried
> following:
>
>
>
>> for (i in 1:10) {
>
> + DumDat <- rnorm(1000)
>
> + cat("iteration:", i, " \n")
>
> + }
>
> iteration: 1  
>
> iteration: 2  
>
> iteration: 3  
>
> iteration: 4  
>
> iteration: 5  
>
> iteration: 6  
>
> iteration: 7  
>
> iteration: 8  
>
> iteration: 9  
>
> iteration: 10  
>
>
>
> However when I run this loop, all iteration numbers are showing only after
> completion of all calculation, so could not achieve my goal. Can somebody
> help me out, how can I do that?
>
>
>
> Thanks,
>
>
>
>
>
>
>    [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Need Iteration and convergence in R

jack
In reply to this post by Bogaso
I want to iterate the program until it converge? can anybody help me? I just write it manual, but i need to generalize it
so that simulation can stop if the difference between two iteration is less than 0.0001.  
 The following are the program :

rm(list=ls(all=T))

x=rnorm(50,2,0.7)
y=rnorm(50,3,0.3)
beta=0.80
n=length(x)
alpha=mean(y)-beta*mean(x)
###initail value
indel=sum((x-mean(x))^2)/(n)
inep=sum((y-mean(y))^2)/(n)
indel
inep
inXhat=((x/indel)+(beta/inep)*(y-alpha))/((1/indel)+(beta^2/inep))
del1=sum((x-inXhat)^2)/(n)
ep1=sum((y-alpha-beta*inXhat)^2)/(n)
del1
ep1

## 1st iteration

Xhat1=((x/del1)+(beta/ep1)*(y-alpha))/((1/del1)+(beta^2/ep1))
del2=sum((x-Xhat1)^2)/(n)
ep2=sum((y-alpha-beta*Xhat1)^2)/(n)
del2
ep2

abs(del2-del1)
abs(ep2-ep1)

## 2nd Iteration

Xhat2=((x/del2)+(beta/ep2)*(y-alpha))/((1/del2)+(beta^2/ep2))
del3=sum((x-Xhat2)^2)/(n)
ep3=sum((y-alpha-beta*Xhat2)^2)/(n)
del3
ep3

abs(del3-del2)
abs(ep3-ep2)