# st: Re: nonlinearleastsquare

 From "Steichen" To Subject st: Re: nonlinearleastsquare Date Thu, 13 Nov 2003 09:36:34 -0500

```Andreas Aschbacher asked about an -nl- error...

I captured his example and ran it through Stata 8.2
...it worked the first try:

. clear
. set obs 100
obs was 0, now 100

. gen x=uniform()
. gen y=3 +.75/(1+exp(-2*x-0.1))

. capture program drop nlexample
. program nlexample
1. version 8
2. if "`1'" == "?"{ // alt+0145 1 alt+0146 <=> for `1'
3. global S_1 "b c k A"
4. global k = 1
5. global A = 1
6. global b = 1
7. global c = 1
8. exit
9. }
10. replace `1'=\$b +\$c/(1+exp(-\$A*x-\$k))  // alt+0145 1 alt+0146 <=> for
`1'
11. end

. nl example y
(obs = 100)

Iteration 0:  residual SS =  302.5628
Iteration 1:  residual SS =  21.36867
Iteration 2:  residual SS =   5.00826
Iteration 3:  residual SS =  .0934839
Iteration 4:  residual SS =  .0852592
Iteration 5:  residual SS =  .0519648
Iteration 6:  residual SS =    .03807
Iteration 7:  residual SS =  .0003895
Iteration 8:  residual SS =  8.82e-08
Iteration 9:  residual SS =  5.27e-13

Source |       SS       df       MS            Number of obs =
100
-------------+------------------------------         F(  3,    96) =
4.64e+13
Model |   .63290202     3   .21096734         Prob > F      =
0.0000
Residual |  4.3634e-13    96  4.5452e-15         R-squared     =
1.0000
-------------+------------------------------         Adj R-squared =
1.0000
Total |   .63290202    99   .00639295         Root MSE      =
6.74e-08
Res. dev.
= -3022.766
(example)
----------------------------------------------------------------------------
--
y |      Coef.   Std. Err.      t    P>|t|     [95% Conf.
Interval]
-------------+--------------------------------------------------------------
--
b |   3.000001   2.32e-06        .   0.000     2.999996
3.000005
c |   .7499993   2.61e-06        .   0.000     .7499941
.7500044
k |   .0999987   4.97e-06        .   0.000     .0999888
.1000086
A |   2.000002   4.82e-06        .   0.000     1.999992
2.000011
----------------------------------------------------------------------------
--
* Parameter b taken as constant term in model & ANOVA table
(SEs, P values, CIs, and correlations are asymptotic approximations)

.
end of do-file

I would suggest that Andreas shut down and restart Stata, as nothing appears
to be wrong in his example.

Tom Steichen

----- Original Message -----
From: "Andreas Aschbacher" <aa_surf@gmx.at>
To: <statalist@hsphsun2.harvard.edu>
Sent: Thursday, November 13, 2003 8:56 AM
Subject: st: nonlinearleastsquare

> Here is a brief description of my problem:
>
> I put all the following code into nlexample.do
> ~~~~~~~~~~~~~~~~
> clear
> set obs 100
> gen x=uniform()
> gen y=3 +.75/(1+exp(-2*x-0.1))
>
>
> capture program drop nlexample
> program nlexample
> version 8
> if "`1'" == "?"{                //      alt+0145 1 alt+0146    <=>     for
> `1'
>
>  global S_1 "b c k A"
>  global k = 1
>  global A = 1
>  global b = 1
>  global c = 1
>  exit
> }
> replace `1'=\$b +\$c/(1+exp(-\$A*x-\$k))    //     alt+0145 1 alt+0146    <=>
> for   `1'
>
> end
>
>
> nl example y
> ~~~~~~~~~~~~~~~~~~~~~~~
> at last I give :do nlexample: in command line- and I always get the
> error-message
> ::: invalid name -  and I don't know why,
> my ambition is to make nonlinear-fit for z(d) = a1*exp(-((d-a2)/a3)^2) +
> a4*exp(-((d-a5)/a6)^2)
> with LevenbergMarquardt therefore I need this example above.
> Thank you for any help.  andreas
>
> --
> NEU FÜR ALLE - GMX MediaCenter - für Fotos, Musik, Dateien...
> Fotoalbum, File Sharing, MMS, Multimedia-Gruß, GMX FotoService
>
> Jetzt kostenlos anmelden unter http://www.gmx.net
>
> +++ GMX - die erste Adresse für Mail, Message, More! +++
>
> *
> *   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/
```