[R] Need Iteration and convergence in R
mithunum
mithunru at gmail.com
Thu Oct 18 06:28:49 CEST 2012
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)
--
View this message in context: http://r.789695.n4.nabble.com/Printing-the-loop-number-for-each-iteration-tp3683495p4646567.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list