mle
Maximum Likelihood Estimation
Estimate parameters by the method of maximum likelihood.
- Keywords
- models
Usage
mle(minuslogl, start = formals(minuslogl), method = "BFGS",
fixed = list(), nobs, …)
Arguments
- minuslogl
Function to calculate negative log-likelihood.
- start
Named list. Initial values for optimizer.
- method
Optimization method to use. See
optim
.- fixed
Named list. Parameter values to keep fixed during optimization.
- nobs
optional integer: the number of observations, to be used for e.g.computing
BIC
.- …
Further arguments to pass to
optim
.
Details
The optim
optimizer is used to find the minimum of the
negative log-likelihood. An approximate covariance matrix for the
parameters is obtained by inverting the Hessian matrix at the optimum.
Value
An object of class mle-class
.
Note
Be careful to note that the argument is -log L (not -2 log L). It is for the user to ensure that the likelihood is correct, and that asymptotic likelihood inference is valid.
See Also
Examples
library(stats4)
# NOT RUN {
## Avoid printing to unwarranted accuracy
od <- options(digits = 5)
x <- 0:10
y <- c(26, 17, 13, 12, 20, 5, 9, 8, 5, 4, 8)
## Easy one-dimensional MLE:
nLL <- function(lambda) -sum(stats::dpois(y, lambda, log = TRUE))
fit0 <- mle(nLL, start = list(lambda = 5), nobs = NROW(y))
# For 1D, this is preferable:
fit1 <- mle(nLL, start = list(lambda = 5), nobs = NROW(y),
method = "Brent", lower = 1, upper = 20)
stopifnot(nobs(fit0) == length(y))
## This needs a constrained parameter space: most methods will accept NA
ll <- function(ymax = 15, xhalf = 6) {
if(ymax > 0 && xhalf > 0)
-sum(stats::dpois(y, lambda = ymax/(1+x/xhalf), log = TRUE))
else NA
}
(fit <- mle(ll, nobs = length(y)))
mle(ll, fixed = list(xhalf = 6))
## alternative using bounds on optimization
ll2 <- function(ymax = 15, xhalf = 6)
-sum(stats::dpois(y, lambda = ymax/(1+x/xhalf), log = TRUE))
mle(ll2, method = "L-BFGS-B", lower = rep(0, 2))
AIC(fit)
BIC(fit)
summary(fit)
logLik(fit)
vcov(fit)
plot(profile(fit), absVal = FALSE)
confint(fit)
## Use bounded optimization
## The lower bounds are really > 0,
## but we use >=0 to stress-test profiling
(fit2 <- mle(ll, method = "L-BFGS-B", lower = c(0, 0)))
plot(profile(fit2), absVal = FALSE)
## a better parametrization:
ll3 <- function(lymax = log(15), lxhalf = log(6))
-sum(stats::dpois(y, lambda = exp(lymax)/(1+x/exp(lxhalf)), log = TRUE))
(fit3 <- mle(ll3))
plot(profile(fit3), absVal = FALSE)
exp(confint(fit3))
options(od)
# }
Community examples
I don't understand why the example that accompanied this function continues to proliferate even though the NLL function gives the impression that it solves the Poisson prolem for the x and y data wheen it does not. x <- 0:10 y <- c(26, 17, 13, 12, 20, 5, 9, 8, 5, 4, 8) ## Easy one-dimensional MLE: nLL <- function(lambda) -sum(stats::dpois(y, lambda, log = TRUE)) The proper function was given at this link http://r.789695.n4.nabble.com/Maximum-Likelihood-Estimation-Poisson-distribution-mle-stats4-td4635464.html and reproduced below for the convience of the reader. > x <- 0:10 > nLL <- function(lambda) -sum(y*stats::dpois(x, lambda, log=TRUE)) > mle(nLL, start = list(lambda = 5), nobs = NROW(y)) Call: mle(minuslogl = nLL, start = list(lambda = 5), nobs = NROW(y))