当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


R mle 最大似然估计


R语言 mle 位于 stats4 包(package)。

说明

通过最大似然法估计参数。

用法

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

参数

minuslogl

计算负对数似然的函数。

start

向量或单个向量的命名列表。优化器的初始值。默认情况下取自minuslogl的默认参数

optim

优化器函数。 (实验性)

method

使用的优化方法。请参阅optim

fixed

向量或单个向量的命名列表。优化期间保持固定的参数值。

nobs

可选整数:观察值的数量,用于例如计算BIC

lower, upper

向量或单个向量的命名列表。 optim 的边界(如果相关)。

...

要传递给 optim 的更多参数。

细节

optim 优化器用于查找负对数似然的最小值。通过在最优处对 Hessian 矩阵求逆获得参数的近似协方差矩阵。默认情况下,使用stats包中的optim;其他优化器需要是plug-compatible,无论是参数还是返回值。

函数 minuslogl 应采用一个或多个参数,每个参数都可以是一个向量。优化器优化采用单个向量参数的函数,其中包含 minuslogl 参数的串联,删除任何应保持固定的值。该函数在内部解包参数向量,插入固定值并调用 minuslogl

向量参数 startfixedupperlower 可以以打包和解包形式给出,可以作为单个向量或向量列表。在后一种情况下,您只需要指定那些实际受到影响的列表元素。对于向量参数(包括列表内的参数),请对不想设置的值使用默认标记: NA 表示 fixedstart+Inf, -Inf 表示 upperlower

mle-class 的对象。

注意

请注意,mll 参数应计算 -log L(而不是 -2 log L)。用户需要确保似然是正确的,并且渐近似然推断是有效的。

例子

## Avoid printing to unwarranted accuracy
od <- options(digits = 5)

## Simulated EC50 experiment with count data
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))

## sanity check --- notice that "nobs" must be input
## (not guaranteed to be meaningful for any likelihood)
stopifnot(nobs(fit0) == length(y))


# For 1D, this is preferable:
fit1 <- mle(nLL, start = list(lambda = 5), nobs = NROW(y),
            method = "Brent", lower = 1, upper = 20)

## 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, 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(ll2, 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))

# Regression tests for bounded cases (this was broken in R 3.x)
fit4 <- mle(ll, lower = c(0, 4)) # has max on boundary
confint(fit4)

## direct check that fixed= and constraints work together
mle(ll, lower = c(0, 4), fixed=list(ymax=23)) # has max on boundary

## Linear regression using MLE
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)) # for comparison -- notice variance bias in MLE
summary(mle(mll, lower=c(-Inf,-Inf, 0.01)))
summary(mle(mll, lower=list(sigma = 0.01))) # alternative specification

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) ## beware lazy evaluation
    function(p=.5) -dbinom(x, n, p, log=TRUE)
}

## Likelihood functions for different x.
## This code goes wrong, if force(x) is not used in Binom_mll:

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 = 1e-16, upper = 1-1e-16) # limits must be inside (0,1)

## Boundary case: This works, but fails if limits are set closer to 0 and 1  
mll <- Binom_mll(0, 10)
mle(mll, lower=.005, upper=.995)

## Not run: 
## We can use limits closer to the boundaries if we use the
## drop-in replacement optimr() from the optimx package.

mle(mll, lower = 1e-16, upper = 1-1e-16, optim=optimx::optimr)

## End(Not run)


options(od)

也可以看看

mle-class

相关用法


注:本文由纯净天空筛选整理自R-devel大神的英文原创作品 Maximum Likelihood Estimation。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。