Skip to content
Prev 26187 / 398502 Next

Modified Bessel Function - 2nd kind

Dr Andrew Wilson <eia018 at comp.lancs.ac.uk> writes:
It appears that the value of q is being driven toward negative
values.  The numerical derivative routine is trying to evaluate the
predictions at a negative value of a and q.  This suggests that your
model may not fit the data or that you need better starting values for
the parameters.
One way of enforcing constraints on the parameters in a nonlinear
regression is to use transformed parameters.  In this case you can use
a logistic transformation for q and the logarithm of a.  I suggest
that you write a function for the model rather than an expression

modl <- function(x, loga, logisq) {
 xm5 <- x - 0.5
 a <- exp(loga)
 q <- 1/(1 + exp(-logisq))
 sqrt((2*a)/pi)*exp(a*sqrt(1-q))*((((a*q)/2)^x)/factorial(x))* ((pi/2) *
 (besselI(a,-xm5) - besselI(a,xm5))/sin(xm5 * pi)) 
}

and try to fit

 y2 <- nls(y ~ modl(x, loga, logisq), start=list(loga=log(0.1),
           logisq=log(0.1/0.9), trace = TRUE)