拟合 3 参数威布尔分布 [英] Fitting a 3 parameter Weibull distribution

查看:44
本文介绍了拟合 3 参数威布尔分布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在用 R 进行一些数据分析,我正在尝试弄清楚如何将我的数据拟合到 3 参数 Weibull 分布.我找到了如何使用 2 参数 Weibull 进行操作,但在找到如何使用 3 参数进行操作时遇到了问题.

I have been doing some data analysis in R and I am trying to figure out how to fit my data to a 3 parameter Weibull distribution. I found how to do it with a 2 parameter Weibull but have come up short in finding how to do it with a 3 parameter.

以下是我使用 MASS 包中的 fitdistr 函数拟合数据的方法:

Here is how I fit the data using the fitdistr function from the MASS package:

y <- fitdistr(x[[6]], 'weibull')

x[[6]] 是我的数据的一个子集,y 是我存储拟合结果的地方.

x[[6]] is a subset of my data and y is where I am storing the result of the fitting.

推荐答案

首先,您可能需要查看 FAdist 包.然而,从 rweibull3rweibull 并不难:

First, you might want to look at FAdist package. However, that is not so hard to go from rweibull3 to rweibull:

> rweibull3
function (n, shape, scale = 1, thres = 0) 
thres + rweibull(n, shape, scale)
<environment: namespace:FAdist>

类似地从 dweibull3dweibull

> dweibull3
function (x, shape, scale = 1, thres = 0, log = FALSE) 
dweibull(x - thres, shape, scale, log)
<environment: namespace:FAdist>

所以我们有这个

> x <- rweibull3(200, shape = 3, scale = 1, thres = 100)
> fitdistr(x, function(x, shape, scale, thres) 
       dweibull(x-thres, shape, scale), list(shape = 0.1, scale = 1, thres = 0))
      shape          scale          thres    
    2.42498383     0.85074556   100.12372297 
 (  0.26380861) (  0.07235804) (  0.06020083)

如评论中所述,尝试以这种方式拟合分布时会出现各种警告

As mentioned in the comment, there appears various warnings when trying to fit the distribution in this way

Error in optim(x = c(60.7075705026659, 60.6300379017397, 60.7669410153573,  : 
  non-finite finite-difference value [3]
There were 20 warnings (use warnings() to see them)
Error in optim(x = c(60.7075705026659, 60.6300379017397, 60.7669410153573,  : 
  L-BFGS-B needs finite values of 'fn'
In dweibull(x, shape, scale, log) : NaNs produced

对我来说,起初它只是 NaN 生成,这不是我第一次看到它,所以我认为它没有那么有意义,因为估计值很好.经过一番搜索,它似乎是一个非常流行的问题,我既找不到原因也找不到解决方案.一种替代方法是使用 stats4 包和 mle() 函数,但它似乎也有一些问题.但我可以让您使用 danielmedic 的代码的修改版本,我已经检查了几次:

For me at first it was only NaNs produced, and that is not the first time when I see it so I thought that it isn't so meaningful since estimates were good. After some searching it seemed to be quite popular problem and I couldn't find neither cause nor solution. One alternative could be using stats4 package and mle() function, but it seemed to have some problems too. But I can offer you to use a modified version of code by danielmedic which I have checked a few times:

thres <- 60
x <- rweibull(200, 3, 1) + thres

EPS = sqrt(.Machine$double.eps) # "epsilon" for very small numbers

llik.weibull <- function(shape, scale, thres, x)
{ 
  sum(dweibull(x - thres, shape, scale, log=T))
}

thetahat.weibull <- function(x)
{ 
  if(any(x <= 0)) stop("x values must be positive")

  toptim <- function(theta) -llik.weibull(theta[1], theta[2], theta[3], x)

  mu = mean(log(x))
  sigma2 = var(log(x))
  shape.guess = 1.2 / sqrt(sigma2)
  scale.guess = exp(mu + (0.572 / shape.guess))
  thres.guess = 1

  res = nlminb(c(shape.guess, scale.guess, thres.guess), toptim, lower=EPS)

  c(shape=res$par[1], scale=res$par[2], thres=res$par[3])
}

thetahat.weibull(x)
    shape     scale     thres 
 3.325556  1.021171 59.975470 

这篇关于拟合 3 参数威布尔分布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆