Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Fitting a logistic curve to data - minimizing Chi-Squared

    Hi,

    I would like to fit a logistic curve to my data. (I have data for the number of internet users in the US and want to fit a logistic curve to forecast the data.)

    The underlying logistic function is: X(t) = M / (1+ exp^(-alpha*(t-t0)))


    Literature recommends I should minimize Chi-Squared: χ^2=(qi-(M/(1+e^(-α(ti-t0) ) )))^2/(M/((1+e^(-α(ti-t0) ) )*(1+e^α(ti-t0) ) ))


    Is there a way to to this in Stata?

    Any comment & advice would be very much appreciated!
    Thanks,
    Isabella May
    Last edited by ifmm; 18 Sep 2014, 06:15.

  • #2
    Hi Isabella
    I believe one possible option is to use MLE, so that you can set the Chi2 as your objective function, and simply let Stata do its job finding the values (alfa?) that minimize your function.
    Technically, tho, it should be maximizing the negative of the X2 function.
    Hope this helps
    Fernando

    Comment


    • #3
      I'd try non-linear least squares. Indeed this function is a previously prepared example for the nl command. Fitting some kind of sigmoid curve is one of the most common applications of nl. The help file explains fully.

      Comment


      • #4
        Thank you both very much for your help & recommendations!

        Nick, I used the non-linear least squares & got a fit that is slightly different from my calculations in excel (which are based on minimizing Chi squared).
        Can I somehow set a function (i.e. Chi squared) for the regression to account for the uncertainties in the reported data?

        Thank you!
        Isabella

        Comment


        • #5
          I don't know what you mean by "set a function" here or what Excel does here (I gather that's a spreadsheet program).

          But your graphs seem to imply that the fits are about as good in each case. You could try also looking at the pattern of residuals. Fitting these curves is a very inexact science, especially as you don't have totally clear-cut indications that the system reached its upper limit.

          Please see also advice on photos at http://www.statalist.org/forums/help Section 12.

          Comment


          • #6
            My excel calculations are based on minimizing Chi squared with SOLVER given the original data to obtain values for M, t0 and alpha. The reason I chose this approach is to incorporate the uncertainty/variance of each data point (since the actual data might differ from the natural growth pattern).
            Is there a way in Stata to weight the data point according to the variance; var = M/((1+e^(-α(ti-t0)))*(1+e^α(ti-t0))) ?

            Comment


            • #7
              Well, I would say that the only way to set an objective function is by directly programming using MLE capabilities in Stata.
              The -nl- as far as i know, will fit your model still assuming linearity on the error:
              y=f(x1,x2,x3)+e
              but if you work with mle, and set up your objective function, you can specify an specific functional form for the model.
              FYI, i like to point out that is one reason why poisson and simple ols with log dependent variables yield slighlty different results.


              Comment


              • #8
                The help for nl does explain that you can use weights. That's explicit in the syntax. However, the variance formula you cite depend on the parameter values that nl is designed to obtain.... I suppose you might try iteration.

                As said, the pattern of residuals might give you a clue to what is sensible.

                Comment

                Working...
                X