Reputation: 21
I have want to estimate the parameters of the function which involves Bessel function and integration. However, when i tried to run it, i got a message that "Error in f(x, ...) : could not find function "BesselI" ". I don't know to fix it and would appreciate any related proposal.
library(Bessel)
library(maxLik)
library(miscTools)
K<-300
f <- function(theta,lambda,u) {exp(-u*theta)*Vectorize(BesselI(2*sqrt(t*u*theta*lambda),1))/u^0.5}
F <- function(theta,lambda){integrate(f,0,K,theta=theta,lambda=lambda)$value}
tt <- function(theta,lambda){(sqrt(lambda)*exp(-t*lambda)/(2*sqrt(t*theta)))*
(theta*(2*t*lambda-1)*F(theta,lambda))}
loglik <- function(param) {
theta <- param[1]
lambda <- param[2]
ll <-sum(log(tt(theta,lambda)))
}
t <- c(24,220,340,620,550,559,689,543)
res <- maxNR(loglik, start=c(0.001,0.0005),print.level=1,tol = 1e-08)
summary(res)
Newton-Raphson maximisation Number of iterations: 0 Return code: 100 Initial value out of range.
I got "There were 50 or more warnings (use warnings() to see the first 50)" and when I used warnings(), following is the warning.
In t * u : longer object length is not a multiple of shorter object length.
sessionInfo()
R version 2.14.2 (2012-02-29)
Platform: i386-pc-mingw32/i386 (32-bit)
locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] maxLik_1.1-2 miscTools_0.6-16 Bessel_0.5-4 Rmpfr_0.5-1
[5] gmp_0.5-4
loaded via a namespace (and not attached):
[1] sandwich_2.2-10
Upvotes: 2
Views: 815
Reputation: 96
Here's what I've got. Unfortunately I wasn't able to figure out how to run it across the t vector but I suppose you could just do a for loop.
library(base)
install.packages("maxLik")
library(maxLik)
#setting some initial values to test as I go
K <- 300
u <- 1
theta <- 1
T <- c(1,2,3,4)
lambda <- 1
#I got it to work with besselI instead of BesselI
#I also dropped Vectorize and instead do a for loop at the end
f <- function ( theta, lambda, u ) {
exp(-u * theta) * besselI(2 * sqrt(t * u * theta * lambda), 1) / u^0.5
}
#testing to see if everything is working
f(1,1,1)
#making a function with only one input so it can be integrated
F <- function ( u ) {
f(theta, lambda, u)
}
F(1)
#testing for integration
t <- T[1]
integrate(F, 0, K)
#entered the integration function inside here at the bottom
tt <- function ( theta, lambda ) {
(sqrt(lambda) * exp(-t * lambda) / (2 * sqrt(t * theta))) *
(theta * (2 * t * lambda - 1) * integrate(F, 0, K)$value)
}
tt(1,1)
#took the absolute value of theta and lambda just in case they turn out negative
loglik <- function(param) {
theta <- param[1]
lambda <- param[2]
ll <-sum(log(tt(abs(theta),abs(lambda))))
return(ll)
}
#example for using the for loop
for ( i in 1 : length(T) ) {
t <- T[i]
print(loglik(c(1,1)))
}
maxNR(loglik, start = c(1, 5), print.level = 1, tol = 1e-08)
I'm not sure what you want your parameters theta and lamba but my settings worked and with the values of t used. There may be some dependencies on the the magnitude of t and theta/lambda as far as singular matrices go. Not sure if maxNR uses generalized inverses but I'm sure it does.
Upvotes: 0
Reputation: 226577
Warning: incomplete answer/exploration.
Let's strip this down a little to try to see where the warnings are coming from, which may give further insight. At least we can eliminate that as a possibility.
K <- 300 ## global variable, maybe a bad idea
t <- c(24,220,340,620,550,559,689,543) ## global/same name as t(), ditto
library(Bessel)
f <- function(theta,lambda,u) {
exp(-u*theta)*BesselI(2*sqrt(t*u*theta*lambda),1)/u^0.5}
## Vectorize() isn't doing any good here anyway, take it out ...
F <- function(theta,lambda){
integrate(f,0,K,theta=theta,lambda=lambda)$value}
F()
works but still gives the vectorization warnings:
F(theta=1e-3,lambda=5e-4)
## [1] 3.406913
## There were 50 or more warnings (use warnings() to see the first 50)
f(theta=1e-3,lambda=5e-4,u=0:10)
## [1] NaN 0.010478182 0.013014566
## [4] 0.017562239 0.016526010 0.016646497
## [7] 0.018468755 0.016377872 0.003436664
## [10] 0.010399265 0.012919646
## Warning message:
## In t * u : longer object length is not a multiple of shorter object length
We still get a warning. We can also see that evaluating the integrand at 0 is likely to be a problem (we have a square-root of u
in the denominator ...)
It looks like we might need to evaluate f()
over the outer product (all combinations of t
and u
), and then perhaps sum f
over the values of t
? This really needs to be resolved, because t
has a fixed length (presumably it's a data sample of some sort) while the integration variable u
is going to have arbitrarily many values ...
Can you provide a link to the original derivation of the log-likelihood function?
Upvotes: 1