# [ADMB Users] Problems with RE model, using FUNCTIONs for integration

dave fournier davef at otter-rsch.com
Wed Dec 5 12:23:18 PST 2012

```On 12-12-05 09:59 AM, Jeff Laake wrote:

It could be done with a bit of work. The main idea would be to make
then you need to pass all the necessary variables though it to trapz and
further through to h.
you could make trapz and h just dvariable type objects with the
corresponding df1b2variable types.
so for example h would be somethng like

dvariable h(const double x,const prevariable& sigma)
{
dvariable tmp;
tmp=exp(-.5*square(x/sigma))/(sqrt(2*PI)*sigma);    // Normal
(0,sigma) pdf
return tmp;
}
df1b2variable h(const double x,const df1b2variable& sigma)
{
df1b2variable tmp;
tmp=exp(-.5*square(x/sigma))/(sqrt(2*PI)*sigma);    // Normal
(0,sigma) pdf
return tmp;
}

and trapzd becomes somethng like

dvariable trapzd(double a,double b,int n,const dvariable& s,const
prevariable& sigma)
{
double x,num_interval,hn;^M
dvariable sum;^M

static int interval;^M
int j;^M
if (n == 1) {^M
interval=1;^M
return (s=0.5*(b-a)*(h(a,sigma)+h(b,sigma)));^M
} else {^M
num_interval=interval;^M

with a corresponding df1b2variable trapz as well.

etc.

> So I spent some time with this and I was able to get it to work with
> adromb although there are some minor differences probably related to
> Laplace approximation. It is not due to the integration because I
> swapped out the adromb code for cumd_norm and got the same answer.
> From what I understand this will not work with separable functions
> which is a bit of an issue because it doesn't scale up nicely like the
> separable function. If I'm wrong about that please let me know. It
> would be a big improvement if the integration could be used from a
> separable function.
>
> I've attached the tpl and dat files (they are identical) from existing
> example for random effect example (hnre_f2.tpl) and the adromb version
> of the same likelihood (inthnre_f2.tpl).  Both are based on eq 7 in
>
> Thanks for your effort here Dave. It does work as you stated and it
> worked great for say n<=200. If I were to take the integral into the
> loop for the case of covariates then the temp files get huge and
> become problematic.  The separable function approach is preferable and
> doesn't suffer from the temp file problem. So far the time being I'm
> left with functions like cumd_norm or internally coding an approximation.
>
> regards--jeff
>
>
> On Fri, Nov 30, 2012 at 1:34 PM, dave fournier <davef at otter-rsch.com
> <mailto:davef at otter-rsch.com>> wrote:
>
>     If the model is just crashing you should be able to run it with
>     a debugger and see at what line it happens.  Much faster than
>     thinking.
>
>

-------------- next part --------------
An HTML attachment was scrubbed...