R中的MLE错误:'vmmin'中的初始值不是有限的 [英] MLE error in R: initial value in 'vmmin' is not finite
本文介绍了R中的MLE错误:'vmmin'中的初始值不是有限的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有2个data.frame
对象:
df1 <- data.frame(x = 1:100)
df1$y <- 20 + 0.3 * df1$x + rnorm(100)
df2 <- data.frame(x = 1:200000)
df2$y <- 20 + 0.3 * df2$x + rnorm(200000)
我想做MLE.使用df1
,一切正常:
I want to do MLE. With df1
everything is ok:
LL1 <- function(a, b, mu, sigma) {
R = dnorm(df1$y - a- b * df1$x, mu, sigma)
-sum(log(R))
}
library(stats4)
mle1 <- mle(LL1, start = list(a = 20, b = 0.3, sigma=0.5),
fixed = list(mu = 0))
> mle1
Call:
mle(minuslogl = LL1, start = list(a = 20, b = 0.3, sigma = 0.5),
fixed = list(mu = 0))
Coefficients:
a b mu sigma
23.89704180 0.07408898 0.00000000 3.91681382
但是如果我要对df2
执行相同的任务,则会收到错误消息:
But if I would do the same task with df2
I would receive an error:
LL2 <- function(a, b, mu, sigma) {
R = dnorm(df2$y - a- b * df2$x, mu, sigma)
-sum(log(R))
}
mle2 <- mle(LL2, start = list(a = 20, b = 0.3, sigma=0.5),
fixed = list(mu = 0))
Error in optim(start, f, method = method, hessian = TRUE, ...) :
initial value in 'vmmin' is not finite
我该如何克服?
推荐答案
R
的值在某些时候变为零;导致函数的非限定值最小化并返回错误.
The value of R
becomes zero at some point; it leads to a non-finite value of the function to be minimized and returns an error.
使用参数log=TRUE
可以更好地解决此问题,请参见下面的功能LL3
.以下给出了一些警告,但返回了结果,其参数估计值接近真实参数.
Using the argument log=TRUE
handles better this issue, see function LL3
below. The following gives some warnings but a result is returned, with parameter estimates close to the true parameters.
require(stats4)
set.seed(123)
e <- rnorm(200000)
x <- 1:200000
df3 <- data.frame(x)
df3$y <- 20 + 0.3 * df3$x + e
LL3 <- function(a, b, mu, sigma) {
-sum(dnorm(df3$y - a- b * df3$x, mu, sigma, log=TRUE))
}
mle3 <- mle(LL3, start = list(a = 20, b = 0.3, sigma=0.5),
fixed = list(mu = 0))
Warning messages:
1: In dnorm(df3$y - a - b * df3$x, mu, sigma, log = TRUE) : NaNs produced
2: In dnorm(df3$y - a - b * df3$x, mu, sigma, log = TRUE) : NaNs produced
3: In dnorm(df3$y - a - b * df3$x, mu, sigma, log = TRUE) : NaNs produced
4: In dnorm(df3$y - a - b * df3$x, mu, sigma, log = TRUE) : NaNs produced
5: In dnorm(df3$y - a - b * df3$x, mu, sigma, log = TRUE) : NaNs produced
6: In dnorm(df3$y - a - b * df3$x, mu, sigma, log = TRUE) : NaNs produced
7: In dnorm(df3$y - a - b * df3$x, mu, sigma, log = TRUE) : NaNs produced
8: In dnorm(df3$y - a - b * df3$x, mu, sigma, log = TRUE) : NaNs produced
> mle3
Call:
mle(minuslogl = LL3, start = list(a = 20, b = 0.3, sigma = 0.5),
fixed = list(mu = 0))
Coefficients:
a b mu sigma
19.999166 0.300000 0.000000 1.001803
这篇关于R中的MLE错误:'vmmin'中的初始值不是有限的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文