r - 拟合 3 参数威 bool 分布

标签 r statistics distribution model-fitting weibull

我一直在 R 中进行一些数据分析,我正在尝试找出如何将我的数据拟合到 3 参数威 bool 分布。我找到了如何使用 2 参数 Weibull 来做到这一点,但在找到如何使用 3 参数来做到这一点方面却很欠缺。

以下是我如何使用 MASS 包中的 fitdistr 函数来拟合数据:

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

x[[6]] 是我的数据的子集,y 是我存储拟合结果的位置。

最佳答案

首先,您可能想查看 FAdist package 。然而,从 rweibull3rweibull 并不难:

> 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)

编辑:正如评论中提到的,尝试以这种方式适应分布时会出现各种警告

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() 函数,但它似乎也存在一些问题。但我可以建议您使用 code 的修改版本作者:danielmedic,我已经检查过几次了:

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 

关于r - 拟合 3 参数威 bool 分布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11817883/

相关文章:

algorithm - 吸引点集合中分配点优化问题的需要算法

python - 为什么我不能使用 distutils 在 Python 发行版中包含这些数据文件?

R markdown 赋值 js 变量

如果列中的 NA 数大于 3,则删除数据框的行

python - 确定数据集 "wiggliness"- Python

r - R 中参差不齐的数据框中按年份加权平均

r - 如果可能,在 R - 基 R 中着色置信区间

distribution - 在 Mathematica 中创建分布

r - 故意用不起作用的 R 代码编写 Markdown 文档

r - 如何整理一个固定宽度的文件,每 n (可变) 行带有标题?