# Using MLE in R stats4

I have been trying to estimate the MLE for my joint posterior. I'm using R and the package stats4. I have 14 parameters and two of them are $$\geq 0$$, which I did not know how to implement (and I was creating NaN due to the minus log posterior required in for the mle function) and I just made it return very high value (1000) if either of the parameters were negative. Is this the right way to solve this problem? As I was forced to change my prior each time (because MLE told me that my prior estimates were way to high) and I find these nonnegative parameters going down to were low numbers (0.001 and 0.01) which did not seem right and at each iteration way below my suggested prior.

Also, since I didn't have the exact posterior due to the structure of the model and I tried to scale it such that the point estimate from the mle function plugged in the log joint posterior had the value 0. Is this approximation okay for this function?

• Why are you doing MLE if you're dealing with a Bayesian problem? Commented May 12, 2014 at 2:50
• I'm using block updating and in order to use that approach I need point estimate and covariance matrix to sample from and Metropolis-Hastings step to accept/reject. Commented May 12, 2014 at 12:32
• What does the stats4 package do? Commented May 12, 2014 at 13:31
• @smilig it's part of the standard distribution of R. I believe stats4 is a library containing statistical functions based on S4 classes (as opposed to stats which contains a large collection of base stats functions using the older S3 classes). It (stats4) contains a number of highly generic workhorses like plot, summary, mle and so on. Commented May 12, 2014 at 21:28
• Raxel: why not reparameterize the parameters with a lower bound (say by taking logs)? Commented May 13, 2014 at 5:37

Why use maximum likelihood for a Bayesian problem?

Nevertheless, the problem with two nonnegative parameters can be solved in several ways:

• Using a solver which admits restrictions.

• Maybe more practical, reparametrize the parameter, if it is $$\theta_0 \ge 0$$, represent it in the model as $$\theta=\log \theta_0$$. That admittedly also avoids the value zero, but it seems that's ok with you.

I had to solve this kind of a problem recently for computing the MLE of a triangle distribution. I also wanted to use the stats4 package so that users of my triangle package could use the related functionality that comes with the stats4::mle object. If you are interested, look here and here.

If you look at the function call for stats4::mle, you notice a couple of things:

> stats4::mle
function (minuslogl, start, optim = stats::optim,
method = if (!useLim) "BFGS" else "L-BFGS-B",
fixed = list(), nobs, lower, upper, ...)


You are essentially calling the stats::optim function with starting parameters start, a function to optimize minuslogl, a function optim, a method method, and other parameters that are useful for control fixed, lower, upper, ...

So, being a little more specific than in @kjetil-b-halvorsen 's answer:

• You can use "L-BFGS-B" with lower and upper limits to accomplish your desired of some parameters being non-negative.
• You can switch our the stats::optim algorithm for something better like using methods in optimx
• You can re-parameterize your non-negative parameters in the negative log likelihood using exponentials: $$f(x_1, \dots,x_{14})$$ where $$x_1 \ge 0$$ changed to $$g(z_1, x_2, \cdots, x_{14}) = f(x_1=e^{z_1}, x_2, \dots, x_{14})$$
• You can just just run the optimization yourself instead of trying to fit it into stats4::mle. In fact, I found it easier to troubleshoot this way.