# Re: st: How can I prevent -simulate- from stopping afternon-convergence?

 From "Jamie Griffin" <[email protected]> To <[email protected]> Subject Re: st: How can I prevent -simulate- from stopping afternon-convergence? Date Fri, 15 Jul 2005 13:46:53 +0100

```You could use a -capture- loop within your program simPTM1. You could
also use the iterate() option of nl to limit the number of iterations,
if the default of 16000 iterations takes too long.

program simPTM1, eclass
drop _all
set more off
set obs 20
gen time=_n-1
/* set parameters to be estimated */
gen m=1000
gen type1Sh=.4
gen q=.5
gen p=.1
/* generate dependent variable according to nonlinear model */
gen
CumSales=m*(1-type1Sh*(exp(-p*time)-(exp(-q*time)*exp(q/p*(1-exp(-p*time)))))-(exp(-q*time)*exp(q/p*(1-exp(-p*time)))))
gen Sales=CumSales[_n]-CumSales[_n-1]
replace Sales=CumSales if time==1
/* generate shock and dependent variable which enters estimation*/
gen shock1=1+0.1*invnorm(uniform())
gen RealSales=Sales*shock1
/* estimate model*/

capture{
nl PTM1SalR RealSales
/* or e.g.
nl PTM1SalR RealSales,iterate(200)
*/
}
if e(converge)==1{
scalar define bM=_b[EM]
scalar define bP=_b[EP]
scalar define bQ=_b[EQ]
scalar define bTH=_b[ETH]
scalar define rmse=e(rmse)
scalar define rsq=e(r2)
scalar define iter=e(ic)
scalar define Dconv=e(converge)
}
else if e(converge)==0{
/* return something else */
}

end

>>> [email protected] 07/15/05 11:02 am >>>
Dear Listeners,

using Stata 8.2 (just updated) on Win XP, I attempt to

. simulate "simPTM1" bM=_b[M], reps(20)

The program simPTM1 contains an -nl- estimation and I get the following

error whenever the estimation lacks convergence:

---snip
convergence not achieved

command -> simPTM1
an error occurred when command was executed on original dataset
please rerun and specify the trace option to see a trace of the
commands
executed
r(430);
---snip

I would rather like that -simulate- posts a missing value for bM in
that
round and continues with the next round of replication. Actually, that
is what Jeff Pitblado promised in a previous Statalist response:
http://www.hsph.harvard.edu/cgi-bin/lwgate/STATALIST/archives/statalist.0401/date/article-461.html

Any hints?

The complete do-file is appended below.
(Any advise on the -nl- program is also appreciated.)

Many thanks
Jonathan

----begin do-file
version 8.2
clear
set mem 100m
set matsize 800
set more off

program drop _all

program nlPTM1SalR
version 8.2
if "`1'" == "?" {
global S_1 "NLS estimation of PTM1 for period sales /*
*/ (constrained parameters)"
global S_1 "EM ETH EP EQ"
local exp "[`e(wtype)' `e(wexp)']"
global EM=6.8
global ETH=-.4
global EP=-1.7
global EQ=-3
exit
}
replace
`1'=exp(\$EM)*((exp(\$ETH)/(1+exp(\$ETH)))*(exp(-exp(\$EP)*(time-1))-exp(-exp(\$EP)*time)+(exp(-exp(\$EQ)*time)*exp(exp(\$EQ)/exp(\$EP)*(1-exp(-exp(\$EP)*time))))-(exp(-exp(\$EQ)*(time-1))*exp(exp(\$EQ)/exp(\$EP)*(1-exp(-exp(\$EP)*(time-1))))))+(exp(-exp(\$EQ)*(time-1))*exp(exp(\$EQ)/exp(\$EP)*(1-exp(-exp(\$EP)*(time-1)))))-(exp(-exp(\$EQ)*time)*exp(exp(\$EQ)/exp(\$EP)*(1-exp(-exp(\$EP)*time)))))
end

program simPTM1, eclass
drop _all
set more off
set obs 20
gen time=_n-1
/* set parameters to be estimated */
gen m=1000
gen type1Sh=.4
gen q=.5
gen p=.1
/* generate dependent variable according to nonlinear model */
gen
CumSales=m*(1-type1Sh*(exp(-p*time)-(exp(-q*time)*exp(q/p*(1-exp(-p*time)))))-(exp(-q*time)*exp(q/p*(1-exp(-p*time)))))
gen Sales=CumSales[_n]-CumSales[_n-1]
replace Sales=CumSales if time==1
/* generate shock and dependent variable which enters estimation*/
gen shock1=1+0.1*invnorm(uniform())
gen RealSales=Sales*shock1
/* estimate model*/
nl PTM1SalR RealSales
scalar define bM=_b[EM]
scalar define bP=_b[EP]
scalar define bQ=_b[EQ]
scalar define bTH=_b[ETH]
scalar define rmse=e(rmse)
scalar define rsq=e(r2)
scalar define iter=e(ic)
scalar define Dconv=e(converge)
end

set seed 8

simulate "simPTM1" bM=_b[EM] bTH=_b[ETH] bP=_b[EP] bQ=_b[EQ] /*
*/ rmse=e(rmse) rsq=e(r2) iter=e(ic) Dconv=e(converge),
reps(20)
descr
summarize, detail
----end do-file

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```