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

From: Berend Hasselman <>
Date: Wed, 13 Apr 2011 06:56:18 -0700 (PDT)


  1. why are you defining Bo within a loop?
  2. Why are you doing library(nleqslv) within the loop?

Doing both those statements outside the loop once is more efficient.

In your transdens function you are not using the function argument parameters, why?
Shouldn't there be a with(parameters) since otherwise where is for example K_vv supposed to come from?

I can't believe that the code "worked": in the call of nleqslv you have ... control=list(matxit=100000) ...
It should be maxit and nleqslv will issue an error message and stop (at least in the latest versions).
And why 100000? If that is required, something is not ok with starting
values and/or functions.

Finally the likelihood function at the end of your code

#Maximum likelihood estimation using mle package
#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?
As written now with argument x and x2 not being used in the body of the function, there is nothing to optimize.
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?


View this message in context:
Sent from the R help mailing list archive at

______________________________________________ mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.
Received on Wed 13 Apr 2011 - 13:58:27 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 14 Apr 2011 - 08:10:30 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive