title: Reproducing PROC NLIN output with nls
body: <p>I'd like to implement the following <em>SAS</em> code in <em>R</em>. </p>
  
  <p>$$N_e = N_o{1-exp[\frac{(d+bN_o)(T_h N_e - T)}{(1+c N_o)}]}$$</p>
  
  <p>where $b>0$, $c\geq 0$, $T_h>0$, and $T=72$. </p>
  
  <p>The SAS code is</p>
  
  <pre><code> DATA NOTONECT;
    INPUT N0  REP FATE    NE  N02 N03 PROPEAT;
    DATALINES;
  5   1   0   0   25  125 0
  5   2   0   1   25  125 0.2
  5   3   0   1   25  125 0.2
  5   4   0   2   25  125 0.4
  5   5   0   2   25  125 0.4
  5   6   0   2   25  125 0.4
  5   7   0   2   25  125 0.4
  5   8   0   3   25  125 0.6
  7   1   0   0   49  343 0
  7   2   0   0   49  343 0
  7   3   0   1   49  343 0.14286
  7   4   0   1   49  343 0.14286
  7   5   0   2   49  343 0.28571
  7   6   0   2   49  343 0.28571
  7   7   0   2   49  343 0.28571
  7   8   0   3   49  343 0.42857
  10  1   0   1   100 1000    0.1
  10  2   0   1   100 1000    0.1
  10  3   0   2   100 1000    0.2
  10  4   0   2   100 1000    0.2
  10  5   0   3   100 1000    0.3
  10  6   0   3   100 1000    0.3
  10  7   0   3   100 1000    0.3
  10  8   0   4   100 1000    0.4
  10  9   0   7   100 1000    0.7
  15  1   0   1   225 3375    0.06667
  15  2   0   1   225 3375    0.06667
  15  3   0   3   225 3375    0.2
  15  4   0   3   225 3375    0.2
  15  5   0   4   225 3375    0.26667
  15  6   0   5   225 3375    0.33333
  15  7   0   5   225 3375    0.33333
  15  8   0   5   225 3375    0.33333
  20  1   0   3   400 8000    0.15
  20  2   0   4   400 8000    0.2
  20  3   0   7   400 8000    0.35
  20  4   0   7   400 8000    0.35
  20  5   0   8   400 8000    0.4
  20  6   0   8   400 8000    0.4
  20  7   0   9   400 8000    0.45
  20  8   0   11  400 8000    0.55
  25  1   0   4   625 15625   0.16
  25  2   0   5   625 15625   0.2
  25  3   0   6   625 15625   0.24
  25  4   0   7   625 15625   0.28
  25  5   0   9   625 15625   0.36
  25  6   0   9   625 15625   0.36
  25  7   0   13  625 15625   0.52
  25  8   0   14  625 15625   0.56
  30  1   0   5   900 27000   0.16667
  30  2   0   8   900 27000   0.26667
  30  3   0   10  900 27000   0.33333
  30  4   0   11  900 27000   0.36667
  30  5   0   11  900 27000   0.36667
  30  6   0   12  900 27000   0.4
  30  7   0   14  900 27000   0.46667
  30  8   0   20  900 27000   0.66667
  45  1   0   4   2025    91125   0.08889
  45  2   0   7   2025    91125   0.15556
  45  3   0   8   2025    91125   0.17778
  45  4   0   10  2025    91125   0.22222
  45  5   0   11  2025    91125   0.24444
  45  6   0   14  2025    91125   0.31111
  45  7   0   15  2025    91125   0.33333
  45  8   0   19  2025    91125   0.42222
  60  1   0   9   3600    216000  0.15
  60  2   0   14  3600    216000  0.23333
  60  3   0   14  3600    216000  0.23333
  60  4   0   16  3600    216000  0.26667
  60  5   0   18  3600    216000  0.3
  60  6   0   21  3600    216000  0.35
  60  7   0   24  3600    216000  0.4
  60  8   0   26  3600    216000  0.43333
  80  1   0   7   6400    512000  0.0875
  80  2   0   11  6400    512000  0.1375
  80  3   0   12  6400    512000  0.15
  80  4   0   15  6400    512000  0.1875
  80  5   0   17  6400    512000  0.2125
  80  6   0   12  6400    512000  0.15
  80  7   0   21  6400    512000  0.2625
  80  8   0   23  6400    512000  0.2875
  100 1   0   7   10000   1000000 0.07
  100 2   0   8   10000   1000000 0.08
  100 3   0   10  10000   1000000 0.1
  100 4   0   11  10000   1000000 0.11
  100 5   0   15  10000   1000000 0.15
  100 6   0   24  10000   1000000 0.24
  100 7   0   26  10000   1000000 0.26
  100 8   0   33  10000   1000000 0.33
  ;
  
  PROC NLIN DATA=NOTONEC2;
   PARMS BHAT= 0.001 0.01 0.1 /* initial parameter estimates */
   CHAT= 0.001 0.01 0.1 
   DHAT= 0 THHAT=3.0; 
   BOUNDS BHAT&gt;0,CHAT&gt;=0,
   THHAT&gt;0; /* parameter bounds */
   T=72; /* experimental period in H */
   X=NE; /* initial predicted value */ 
   A=(DHAT+BHAT*N0)/(1+CHAT*N0); /* expression for A */
   /* define the implicit function */
   C1=EXP(-A*T); /* components of the implicit function */
   C2=A*THHAT;
   H=N0*C1*EXP(C2*X)+X-N0; /* the implicit function */
   ITER=0; /* iterations for Newton's method */
   /* Newton's method employed to find predicted number eaten */
   DO WHILE(ABS(H)&gt;0.0001 AND ITER&lt;50); /* stop criteria for Newton's method */
        X=X-H/(N0*C1*C2*EXP(C2*X)+1); /* new predicted value */
        H=N0*C1*EXP(C2*X)+X-N0; /* new value of implicit function */
        ITER=ITER+1; /* iteration counter */
   END;
  MODEL NE=X; /* model for nonlinear least squares */
  Run;
  </code></pre>
  
  <p>The SAS output is</p>
  
  <pre><code>                                       The NLIN Procedure
                                    Dependent Variable NE
  
                                         Grid Search
                                                                    Sum of
                      BHAT        CHAT        DHAT       THHAT     Squares
  
                   0.00100     0.00100           0      3.0000      3714.9
                    0.0100     0.00100           0      3.0000     47602.6
                    0.1000     0.00100           0      3.0000      100896
                   0.00100      0.0100           0      3.0000      3118.3
                    0.0100      0.0100           0      3.0000     29395.6
                    0.1000      0.0100           0      3.0000     97841.3
                   0.00100      0.1000           0      3.0000      1804.1
                    0.0100      0.1000           0      3.0000      6625.5
                    0.1000      0.1000           0      3.0000     53940.3
  
  
  
                                     The NLIN Procedure
                                    Dependent Variable NE
                                    Method: Gauss-Newton
  
                                       Iterative Phase
                                                                         Sum of
               Iter        BHAT        CHAT        DHAT       THHAT     Squares
  
                  0     0.00100      0.1000           0      3.0000      1804.1
                  1    0.000396      0.0176     0.00190      3.2240      1774.5
                  2    0.000327     0.00578     0.00206      3.5044      1762.4
                  3    0.000307     0.00193     0.00208      3.6507      1754.8
                  4    0.000302    0.000793     0.00208      3.7006      1752.3
                  5    0.000299    0.000265     0.00208      3.7246      1751.1
                  6    0.000299    0.000123     0.00208      3.7312      1750.7
                  7    0.000298    0.000039     0.00208      3.7351      1750.6
                  8    0.000298    0.000013     0.00208      3.7363      1750.5
                  9    0.000298    5.224E-6     0.00208      3.7367      1750.5
                 10    0.000298    2.547E-7     0.00208      3.7369      1750.5
                 11    0.000298    6.092E-8     0.00208      3.7369      1750.5
                 12    0.000298    3.59E-10     0.00208      3.7369      1750.5
                 13    0.000298    6.37E-11     0.00208      3.7369      1750.5
                 14    0.000298    -287E-13     0.00208      3.7369      1750.5
                 15    0.000298    -864E-13     0.00208      3.7369      1750.5
                 16    0.000298    -955E-13     0.00208      3.7369      1750.5
                 17    0.000298    -983E-13     0.00208      3.7369      1750.5
                 18    0.000298    -992E-13     0.00208      3.7369      1750.5
                 19    0.000298    -997E-13     0.00208      3.7369      1750.5
                 20    0.000298    -999E-13     0.00208      3.7369      1750.5
                 21    0.000298      -1E-10     0.00208      3.7369      1750.5
                 22    0.000298      -1E-10     0.00208      3.7369      1750.5
                 23    0.000298      -1E-10     0.00208      3.7369      1750.5
                 24    0.000298      -1E-10     0.00208      3.7369      1750.5
                 25    0.000298      -1E-10     0.00208      3.7369      1750.5
                 26    0.000298      -1E-10     0.00208      3.7369      1750.5
                 27    0.000298      -1E-10     0.00208      3.7369      1750.5
                 28    0.000407           0    0.000884      4.0885      1733.8
                 29    0.000416           0    0.000825      4.0645      1733.2
                 30    0.000415           0    0.000832      4.0640      1733.2
                 31    0.000416           0    0.000832      4.0640      1733.2
  
  
              NOTE: Convergence criterion met.
  
  
                                     Estimation Summary
  
                            Method                   Gauss-Newton
                            Iterations                         31
                            Subiterations                     108
                            Average Subiterations        3.483871
                            R                             1.51E-6
  
                                     The NLIN Procedure
  
                                     Estimation Summary
  
                            PPC(DHAT)                    0.000057
                            RPC(DHAT)                    0.000663
                            Object                       2.84E-10
                            Objective                    1733.203
                            Observations Read                  89
                            Observations Used                  89
                            Observations Missing                0
  
  
                    NOTE: An intercept was not specified for this model.
  
                                            Sum of        Mean               Approx
          Source                    DF     Squares      Square    F Value    Pr &gt; F
  
          Model                      3      9526.8      3175.6     157.57    &lt;.0001
          Error                     86      1733.2     20.1535
          Uncorrected Total         89     11260.0
  
  
                                     Approx       Approximate 95%
       Parameter      Estimate    Std Error      Confidence Limits     Label
  
       BHAT           0.000416     0.000228    -0.00004    0.000869
       CHAT                  0            0           0           0
       DHAT           0.000832      0.00382    -0.00676     0.00842
       THHAT            4.0640       0.3575      3.3534      4.7747
       Bound0            107.9        315.6      -509.1       725.0    0 &lt;= CHAT
  
  
                               Approximate Correlation Matrix
                             BHAT            CHAT            DHAT           THHAT
  
            BHAT        1.0000000               .      -0.8928900       0.7190236
            CHAT         .                      .        .               .
            DHAT       -0.8928900               .       1.0000000      -0.5474038
            THHAT       0.7190236               .      -0.5474038       1.0000000
  </code></pre>
  
  <p>I don't know how to use Newton method in <code>nls</code>. I'd highly appreciate if someone help me to figure out this problem. Thanks in advance.</p>