Hi all, I wrote the following lines of codes try to do some iterations to find the global optimal values, but the function does not execute properly. Every time codes stop after one iteration right after executing the optim() function. Does anyone could have me to take a look? Thanks. if (count>0){ k=k+0.05; mu0=c(83+k,0,0) Sigma0= diag(0.4,3) initpar=c(.1+10*k,10*k,10*k,10*k) # initial parameters for Phi[1,1], the 2 Q?s and R est=optim(initpar,Linn,NULL,method="BFGS",hessian=TRUE,control=list(trace=1,REPORT=1,maxit=300)) stderr=sqrt(diag(solve(est$hessian))) estimate=est$par u=cbind(estimate,stderr) if (u[1]>0 & u[2]>0 & u[3]>0 & u[4]>0){ print("mu0=");print(mu0); print("initpar=");print(initpar) #break } else {count=-1; } } -- View this message in context: http://www.nabble.com/Optim-function-in-the-loop-tp23352044p23352044.html Sent from the R help mailing list archive at Nabble.com.
Hi, It is not clear to me what you are trying to do, but you should try `while' instead of `if': count <- 1 while (count > 0) { . . # yuor code here . } Ravi. ____________________________________________________________________ Ravi Varadhan, Ph.D. Assistant Professor, Division of Geriatric Medicine and Gerontology School of Medicine Johns Hopkins University Ph. (410) 502-2619 email: rvaradhan at jhmi.edu ----- Original Message ----- From: Rstarer <bozhao86 at hotmail.com> Date: Sunday, May 3, 2009 5:01 am Subject: [R] Optim function in the loop To: r-help at r-project.org> Hi all, > I wrote the following lines of codes try to do some iterations to > find the > global optimal values, but the function does not execute properly. Every > time codes stop after one iteration right after executing the optim() > function. Does anyone could have me to take a look? Thanks. > if (count>0){ > > k=k+0.05; > > mu0=c(83+k,0,0) > > Sigma0= diag(0.4,3) > > initpar=c(.1+10*k,10*k,10*k,10*k) # initial parameters for Phi[1,1], > the 2 > Q?s and R > > est=optim(initpar,Linn,NULL,method="BFGS",hessian=TRUE,control=list(trace=1,REPORT=1,maxit=300)) > > stderr=sqrt(diag(solve(est$hessian))) > > estimate=est$par > > u=cbind(estimate,stderr) > > if (u[1]>0 & u[2]>0 & u[3]>0 & u[4]>0){ > > print("mu0=");print(mu0); > > print("initpar=");print(initpar) > > #break > > } else {count=-1; > > } > > } > > -- > View this message in context: > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > > PLEASE do read the posting guide > and provide commented, minimal, self-contained, reproducible code.
Thanks for the reply. optim(initpar,Linn,NULL,method="BFGS",hessian=TRUE,control=list(trace=1,REPORT=1,maxit=300)) The optim is to minimize the negative likelihood function which is performed in function "Linn", Every time the code stops right after optim, for example, the output looks like: " iter 16 value 42.818394 iter 16 value 42.818394 final value 42.818394 converged>" It seems from the code that the loop should continue to run, at least calculate "u" values... but it does not... Thank you. Hi all, I wrote the following lines of codes try to do some iterations to find the global optimal values, but the function does not execute properly. Every time codes stop after one iteration right after executing the optim() function. Does anyone could have me to take a look? Thanks. if (count>0){ k=k+0.05; mu0=c(83+k,0,0) Sigma0= diag(0.4,3) initpar=c(.1+10*k,10*k,10*k,10*k) # initial parameters for Phi[1,1], the 2 Q?s and R est=optim(initpar,Linn,NULL,method="BFGS",hessian=TRUE,control=list(trace=1,REPORT=1,maxit=300)) stderr=sqrt(diag(solve(est$hessian))) estimate=est$par u=cbind(estimate,stderr) if (u[1]>0 & u[2]>0 & u[3]>0 & u[4]>0){ print("mu0=");print(mu0); print("initpar=");print(initpar) #break } else {count=-1; } } -- View this message in context: http://www.nabble.com/Optim-function-in-the-loop-tp23352044p23374525.html Sent from the R help mailing list archive at Nabble.com.
Hard to say without seeing Linn() and maybe other relevant data. Uwe Ligges Rstarer wrote:> Thanks for the reply. > > optim(initpar,Linn,NULL,method="BFGS",hessian=TRUE,control=list(trace=1,REPORT=1,maxit=300)) > > The optim is to minimize the negative likelihood function which is performed > in function "Linn", > Every time the code stops right after optim, for example, the output looks > like: > > " > iter 16 value 42.818394 > iter 16 value 42.818394 > final value 42.818394 > converged > " > It seems from the code that the loop should continue to run, at least > calculate "u" values... but it does not... > > Thank you. > > > Hi all, > I wrote the following lines of codes try to do some iterations to find the > global optimal values, but the function does not execute properly. Every > time codes stop after one iteration right after executing the optim() > function. Does anyone could have me to take a look? Thanks. > if (count>0){ > > k=k+0.05; > > mu0=c(83+k,0,0) > > Sigma0= diag(0.4,3) > > initpar=c(.1+10*k,10*k,10*k,10*k) # initial parameters for Phi[1,1], the 2 > Q?s and R > > est=optim(initpar,Linn,NULL,method="BFGS",hessian=TRUE,control=list(trace=1,REPORT=1,maxit=300)) > > stderr=sqrt(diag(solve(est$hessian))) > > estimate=est$par > > u=cbind(estimate,stderr) > > if (u[1]>0 & u[2]>0 & u[3]>0 & u[4]>0){ > > print("mu0=");print(mu0); > > print("initpar=");print(initpar) > > #break > > } else {count=-1; > > } > > } > > >