For a given vector of observations x and a given vector of parameters(of length 5) ,theta0, the code below produces a vector of better estimates of these parameters, theta1.
part1=(1-theta0[5])*dnorm(x,theta0[1],theta0[3])
part2=theta0[5]*dnorm(x,theta0[2],theta0[4])
gam=part2/([part1+part2)
denom1=sum(1-gam)
denom2=sum(gam)
mu1=sum((1-gam)*x)/denom1
sig1=sqrt(sum((1-gam)*((x-mu1)^2))/denom1)
mu2=sum(gam*x)/denom2
sig2=sqrt(sum(gam*((x-mu2)^2))/denom2
p=mean(gam)
theta[1] <- c(mu1,mu2,sig1,sig2,p)
My question is how can I iterate the procedure without having to write the code anew? I would like theta1 to replace my vector of original estimates, theta0, theta2 to replace theta1 etc etc. The rate of convergence is rather slow, so I suppose 10000 iterations will have to be performed. Is there a convenient shortcut?
PS As you can probably tell, I am a beginner in R so please keep it as simple as possible. Thank you.