Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# RE: st: ml - could not calculate numerical derivatives missing values encountered

 From Alan Marshall To "statalist@hsphsun2.harvard.edu" Subject RE: st: ml - could not calculate numerical derivatives missing values encountered Date Thu, 15 Dec 2011 15:29:00 +0000

```Thank you very much to Stas Kolenikov and Martin Buis for their excellent advice (and patience with my blunders).

The points they raised were indeed what was causing the problems with my analysis.

Changing my syntax (see below) led to the program running without hitch and returned the estimates of 'a' and 'b' I expected:

program define Brass
args lnf a b
replace `lnf'=2*`a'*dx_Dis+`b'*Yxl*dx_Dis-(Nx_Dis*ln(1+(exp(2*(`a'+`b'*Yxl)))))
end

set more off
ml model lf Brass (D_OBS_RT = ) (D_OBS_RT = )
ml maximize

Best wishes

Alan Marshall
________________________________________
From: owner-statalist@hsphsun2.harvard.edu [owner-statalist@hsphsun2.harvard.edu] On Behalf Of Stas Kolenikov [skolenik@gmail.com]
Sent: 15 December 2011 14:37
To: statalist@hsphsun2.harvard.edu
Subject: Re: st: ml - could not calculate numerical derivatives missing values encountered

On top of what Maarten said about the dangers of using the global
macros, the way Stata interprets the call

ml model lf Brass (D_OBS_RT=Yxl Nx_Dis dx_Dis) (D_OBS_RT=Yxl Nx_Dis dx_Dis)

is that the first parameter is a linear function of variables Yxl
Nx_Dis dx_Dis (_cons), and the second parameters is a linear function
of variables Yxl Nx_Dis dx_Dis (and _cons). There are several ways to
avoid it:

1. specify it as

ml model lf Brass (eq1:D_OBS_RT=) (eq2:)

with no explanatory variables, and then substitute these variables
explicitly in your code by their names. This is non-transparent and
not re-usable.

2. Utilizing Stata's convention regarding the linear forms, you can
specify it as

generate YxDx = Yx*Dx

constraint 31: _b[nx:Nx] = 1
constraint 41: _b[ln1ex:_cons] = _b[a:Dx]
constraint 42: _b[ln1ex:Lx] = _b[b:YxDx]
ml model lf BrassAlt (a: Dx, nocons) (b:YxDx, nocons) (nx: Nx, nocons)
(ln1ex: Lx), constraints(31 41 42)

whereas your ml evaluator will include only the local macros:

program define BrassAlt
args lnf theta1 theta2 theta3 theta4
replace `lnf' = 2*`theta1' + `theta2' - `theta3' *ln(1+exp(2*(`theta4')))
end

I have not tried this code, but that could be a starting point for you.

2011/12/15 Alan Marshall <A.D.Marshall@leeds.ac.uk>:
> Dear Statalist,
>
> I having problems fitting a model to predict  rates of specific types of disability for single years of age (0,1,2,......88) using maximum likelihood estimation in stata. I would be very grateful for any advice that can be offered.
>
> I am using a  technique from the demography literature, known as relational models, to estimate age specific rates of disability (which are unreliable due to small samples) using age specific rates of limiting long term illness (which are reliable). The model I am fitting involves two parameters α and β that adjust the level and shape of the curve of age specific limiting long term illness rates to represent the curve of age specific disability rates.
>
> I have fitted the model using weighted least squares regession but have been advised that a maximum likelihood model is more appropriate.
>
> I have worked out the log-likelihood function as below. Dx is the number of people with a disability at age x, Nx is the total population at age x and Lx is the logit of the  rate of limiting long term illness at age x. I’m fairly confident in this function because if I experiment in Excel with different values of α and β the log likelihood function does appear to be maximised by values of α and β that are very close to the Weighted Least Squares solutions.
>
> Log Likelihood=(2*α*Dx) + (β*Yx*Dx )- (Nx*ln(1+exp(2*(α+βLx)))
>
> (Note this likelihood is summed over all ages x)
>
> I am fitting this model in stata as below:
> *line 1
> program define Brass
>
> *line 2
> args lnf a b
>
> *line 3
> Quietly replace `lnf'=2*`a'*\${dx_Dis}1+`b'*\${Yxl}1*\${dx_Dis}1-(\${Nx_Dis}1*ln(1+(exp(2*(`a'+`b'*\${Yxl}1)))))
>
> *line 4
> end
>
> *Line 5
> ml model lf Brass (D_OBS_RT=Yxl Nx_Dis dx_Dis) (D_OBS_RT=Yxl Nx_Dis dx_Dis)
>
> *Line 6
> ml maximize
>
>
> Line 3 defines the log likelihood function to be maximised.
> Yxl are the log odds of the LLTI rates
> dx_Dis is the age specific number of people with a disability, Nx_Dis is the  age-specific number of people at each age
>
> Line 5 - I have included two brackets which as I need to estimate two parameters α and β. In each case I think I am predicting the rate of disability (D_OBS_RT) given three variables of dx_Dis (age specific number of people with a disability), Yxl (log odds of limiting long term illness) and Nx_Dis (age specific total population).
>
> The error that is returned from this is:
> "could not calculate numerical derivatives
> missing values encountered"
>
>
> Best wishes
>
> Alan Marshall
>
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/

--
Stas Kolenikov, also found at http://stas.kolenikov.name
Small print: I use this email account for mailing lists only.

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```