HI Berend, Thank you for your reply.
2011/4/13 Berend Hasselman <b...@xs4all.nl> > Questions: > > 1. why are you defining Bo within a loop? > 2. Why are you doing library(nleqslv) within the loop? > > Yes, I see what you mean. There's no reason for defining that 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. > > I get the error message 6 that the Jacobian is singular, which I think stems from the parameter values. The parameter values are from a similar study and serve only as a starting point. The 100000 was just me playing around. 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. 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? 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. > Berend > > -- > View this message in context: > http://r.789695.n4.nabble.com/MLE-where-loglikelihood-function-is-a-function-of-numerical-solutions-tp3439436p3447224.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > Kristian [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.