Maximum Likelihood Estimation
Description
Estimate parameters by the method of maximum likelihood.
Usage
mle(minuslogl, start,
optim = stats::optim,
method = if(!useLim) "BFGS" else "LBFGSB",
fixed = list(), nobs, lower, upper, ...)
Arguments
minuslogl 
Function to calculate negative loglikelihood.

start 
Named list of vectors or single vector. Initial values
for optimizer. By default taken from the default arguments of minuslogl

optim 
Optimizer function. (Experimental)

method 
Optimization method to use. See optim .

fixed 
Named list of vectors or single vector. Parameter values to keep fixed during
optimization.

nobs 
optional integer: the number of observations, to be used for
e.g. computing BIC .

lower , upper

Named lists of vectors or single vectors. Bounds for optim , if relevant.

... 
Further arguments to pass to optim .

Details
The optim
optimizer is used to find the minimum of the
negative loglikelihood. An approximate covariance matrix for the
parameters is obtained by inverting the Hessian matrix at the optimum.
By default, optim
from the stats
package is used; other
optimizers need to be plugcompatible, both with respect to arguments
and return values.
The function minuslogl
should take one or several arguments,
each of which can be a vector. The optimizer optimizes a function
which takes a single vector argument, containing the
concatenation of the arguments to minuslogl
, removing any
values that should be held fixed. This function internally unpacks the
argument vector, inserts the fixed values and calls minuslogl
.
The vector arguments start
, fixed
, upper
, and
lower
, can be given in both packed and unpacked form, either as
a single vector or as a list of vectors. In the latter case, you only
need to specify those list elements that are actually affected. For vector
arguments, including those inside lists, use a default marker for
those values that you don't want to set: NA
for fixed
and start
, and +Inf, Inf
for upper
, and
lower
.
Value
An object of class mleclass
.
Note
Notice that the mll
argument should calculate 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
mleclass
Examples
od < options(digits = 5)
x < 0:10
y < c(26, 17, 13, 12, 20, 5, 9, 8, 5, 4, 8)
nLL < function(lambda) sum(stats::dpois(y, lambda, log = TRUE))
fit0 < mle(nLL, start = list(lambda = 5), nobs = NROW(y))
stopifnot(nobs(fit0) == length(y))
fit1 < mle(nLL, start = list(lambda = 5), nobs = NROW(y),
method = "Brent", lower = 1, upper = 20)
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))
ll2 < function(ymax = 15, xhalf = 6)
sum(stats::dpois(y, lambda = ymax/(1+x/xhalf), log = TRUE))
mle(ll2, lower = rep(0, 2))
AIC(fit)
BIC(fit)
summary(fit)
logLik(fit)
vcov(fit)
plot(profile(fit), absVal = FALSE)
confint(fit)
(fit2 < mle(ll2, lower = c(0, 0)))
plot(profile(fit2), absVal = FALSE)
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))
fit4 < mle(ll, lower = c(0, 4))
confint(fit4)
mle(ll, lower = c(0, 4), fixed=list(ymax=23))
x < 1:10
y < c(0.48, 2.24, 2.22, 5.15, 4.64, 5.53, 7, 8.8, 7.67, 9.23)
LM_mll < function(formula, data = environment(formula))
{
y < model.response(model.frame(formula, data))
X < model.matrix(formula, data)
b0 < numeric(NCOL(X))
names(b0) < colnames(X)
function(b=b0, sigma=1)
sum(dnorm(y, X %*% b, sigma, log=TRUE))
}
mll < LM_mll(y ~ x)
summary(lm(y~x))
summary(mle(mll, lower=c(Inf,Inf, 0.01)))
summary(mle(mll, lower=list(sigma = 0.01)))
confint(mle(mll, lower=list(sigma = 0.01)))
plot(profile(mle(mll, lower=list(sigma = 0.01))))
Binom_mll < function(x, n)
{
force(x); force(n)
function(p=.5) dbinom(x, n, p, log=TRUE)
}
curve(Binom_mll(0, 10)(p), xname="p", ylim=c(0, 10))
mll_list < list(10)
for (x in 1:10)
mll_list[[x]] < Binom_mll(x, 10)
for (mll in mll_list)
curve(mll(p), xname="p", add=TRUE)
mll < Binom_mll(4,10)
mle(mll, lower = 1e16, upper = 11e16)
mll < Binom_mll(0, 10)
mle(mll, lower=.005, upper=.995)
mle(mll, lower = 1e16, upper = 11e16, optim=optimx::optimr)
options(od)