[R] MLE where loglikelihood function is a function of numerical solutions

Berend Hasselman bhh at xs4all.nl
Thu Apr 14 16:34:19 CEST 2011


On 14-04-2011, at 09:00, Kristian Lind wrote:

> HI Berend, 
> 
> Thank you for your reply. 
> 
> ......
> Finally the likelihood function at the end of your code
> 
> #Maximum likelihood estimation using mle package
> library(stats4)
> #defining loglikelighood function
> #T <- length(v)
> #minuslogLik <- function(x,x2)
> #{    f <- rep(NA, length(x))
> #        for(i in 1:T)
> #        {
> #            f[1] <- -1/T*sum(log(transdens(parameters = parameters, x =
> c(v[i],v[i+1])))-log(Jac(outmat=outmat, x2=c(v[i],r[i])))
> #        }
> #    f
> # }
> 
> How do the arguments of your function x and x2 influence the calculations in
> the likelihood function?
>  
> What I was thinking was that the x and x2 would be the input for the transdens() and Jac() functions, but I guess that is already taken care of when defining them.  
> 

Huh?
If you define zz <- function(x) {...} x is an argument which must be specified when using the function.

> As written now with argument x and x2 not being used in the body of the
> function, there is nothing to optimize.
>  
> That is correct and that is the problem. The likelihood need to be stated as a function of the parameters, here the vector "parameters". Because in the maximum likelihood estimation we want to maximize the value of the likelihood function by changing the parameter values. The likelihood function is a function of the parameters but only through the functions, for example Kristian() is a function of "parameters" which feeds into Bo(), transdens() and Jac(). Do you have any suggestions how to get around this issue?  
> 

What kind of problem?. Why don't you then do (parameters and outmat already known globally)

          f[i] <- -1/T*sum(log(transdens(parameters = parameters, x = x))-log(Jac(outmat=outmat, x2=x2)))

You should pass the arguments used by the optimizer in calling your likelihood function to the functions you defined. That way f[] will depend on x and x2 and so will the likelihood.

> Shouldn't f[1] be f[i] because otherwise the question is why are looping
> for( i in 1:T)?
> But then returning f as a vector seems wrong here. Shouldn't a likelihood
> function return a scalar?
> 
> The likelihood function should return a scalar. I think the fix could be to make the function calculate the function value at each "i" and then make it return the mean of all the f[i]s. 
> 

Is the mean  a likelihood?

Berend



More information about the R-help mailing list