Announcement

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

  • How do I code my own likelihood function for this kind of a problem.

    I am trying to fit a Poisson regression model using my own likelihood function. I have seen the basic example of how I would do it but the thing is that my function has several if statements that makes it really complicated. I managed to write the code for this in SAS but I can't seem to get the whole Idea to STATA.


    If SMD650 is not 20 and not 30 the log likelihood function should be something like what is written on line 14 (Normal likelihood function for a poisson distribution)

    Click image for larger version

Name:	smd1.PNG
Views:	1
Size:	5.7 KB
ID:	1429694

    This is coded in line 15 to 22


    Click image for larger version

Name:	smd2.PNG
Views:	1
Size:	5.6 KB
ID:	1429695


    The likelihood function therefore checks the value of SMD650 first, then depending on the value it does the summation appropriately. there after the likelihood function is then applied to the model with Age as the independent variable.

    below is the SAS code.....how can I come up with a STATA code?


    1. proc nlmixed data = Work.final ;
    2. array v[10];
    3. array n[10];
    4. do i = 1 to 10;
    5. v[i] = i + 4;
    6. n[i] = i + 14;
    7. end;
    8. parms b0=0 b1=0;
    9. mu = exp(b0 + b1*Age);
    10. if SMD650 ~= 10 and SMD650 ~=20 then
    11. ll = log(((mu**SMD650)*exp(-mu))/gamma(SMD650+1));
    12. else if SMD650= 20 then do;
    13. p = 0;
    14. do i = 1 to 6;
    15. p = p + ((n[i]-15)/5)*((mu**n[i])*exp(-mu))/gamma(n[i]+1);
    16. end;
    17. do i = 7 to 10;
    18. p = p + ((24-n[i])/4)*((mu**n[i])*exp(-mu))/gamma(n[i]+1);
    19. end;
    20. ll=log(p);
    21. end;
    22. else do;
    23. sum = 0;
    24. do i = 1 to 6;
    25. sum = sum + ((v[i]-5)/5)*((mu**v[i])*exp(-mu))/gamma(v[i]+1);
    26. end;
    27. do i = 7 to 10;
    28. sum = sum + ((14-v[i])/4)*((mu**v[i])*exp(-mu))/gamma(v[i]+1);
    29. end;
    30. ll=log(sum);
    31. end;
    32. model SMD650 ~ general(ll);
    33. run;
Working...
X