Skip to content

R: numerical integration problems

2 messages · Allan Clark, Duncan Murdoch

#
On 02/01/2009 6:37 AM, Allan Clark wrote:
For large n, the density is highly peaked near its mode.   The 
integrate() function doesn't work well on functions like that, but you 
can improve it by breaking the range up into three parts:  the centre, 
to the left, and to the right.  If you define the centre so that almost 
all of the mass will be there, then it doesn't matter if the other two 
integrals are inaccurate.  I'd suggest using the MLE plus or minus 5 
standard errors.  This should cover the mode unless the prior is very 
informative.

You'll still have a problem of overflow when evaluating it.  To fix 
that, just rescale the density, so that its max is 1.  (Since the 
density occurs in both numerator and denominator, this won't affect the 
final answer).  In general it might be hard to find the max of the 
density, but it's probably good enough to find the max of the 
likelihood, or some other rough approximation, e.g. the density at the 
MLE.  The value 1 isn't important, all you need is an upper bound that 
doesn't cause overflow.

Duncan Murdoch