# [R] integrate with large parameters

Andreas Wittmann andreas_wittmann at gmx.de
Fri May 1 11:46:30 CEST 2009

```Dear R-users,

i have to integrate the following function

`fun1` <- function (a, l1, l2)
{
exp(log(l1) * (a - 1) - l2 * lgamma(a))
}

but if l1 is large, i get the "non-finite function value" error, so my
idea is to rescale with exp(-l1)

`fun2` <- function (a, l1, l2)
{
exp(log(l1) * (a - 1) - l2 * lgamma(a) - l1)
}

but it seems this doesn't solve the problem, when l1=1.0e80. maybe i
have to iterate between large values in integrate and large values in
exp(l1) and then use the normal or the rescaled version?

> l1<-10; l2<-10;
> integrate(Vectorize(function(a) {fun1(a, l1=l1, l2=l2)}), 1, Inf)\$val
 11.65311
> integrate(Vectorize(function(a) {fun2(a, l1=l1, l2=l2)}), 1, Inf)\$val
* exp(l1)
 11.65312
>
> l1<-1.0e12; l2=50;
> integrate(Vectorize(function(a) {fun1(a, l1=l1, l2=l2)}), 1, Inf)\$val
 929558588152
> integrate(Vectorize(function(a) {fun2(a, l1=l1, l2=l2)}), 1, Inf)\$val
* exp(l1)
 NaN
>
> l1<-1.0e20; l2=50;
> integrate(Vectorize(function(a) {fun1(a, l1=l1, l2=l2)}), 1, Inf)\$val
 5.005192e+24
> integrate(Vectorize(function(a) {fun2(a, l1=l1, l2=l2)}), 1, Inf)\$val
* exp(l1)
 NaN
>
> l1<-1.0e80; l2=50;
> integrate(Vectorize(function(a) {fun1(a, l1=l1, l2=l2)}), 1, Inf)\$val
Fehler in integrate(Vectorize(function(a) { : non-finite function value
> integrate(Vectorize(function(a) {fun2(a, l1=l1, l2=l2)}), 1, Inf)\$val
* exp(l1)
 NaN

Many thanks if you have any advice for me!

best regards

Andreas

```