[英]Fitting a 3 parameter Weibull distribution
我一直在用 R 做一些數據分析,我想弄清楚如何使我的數據適合 3 參數 Weibull 分布。 我找到了如何使用 2 個參數的 Weibull 來執行此操作,但在查找如何使用 3 個參數來執行此操作時卻遇到了困難。
以下是我如何使用MASS
包中的fitdistr
函數擬合數據:
y <- fitdistr(x[[6]], 'weibull')
x[[6]]
是我的數據的一個子集,y 是我存儲擬合結果的地方。
首先,您可能想查看FAdist 包。 然而,從rweibull3
到rweibull
並不難:
> rweibull3
function (n, shape, scale = 1, thres = 0)
thres + rweibull(n, shape, scale)
<environment: namespace:FAdist>
同樣從dweibull3
到dweibull
> 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
對我來說,一開始它只NaNs produced
,這不是我第一次看到它,所以我認為它沒有那么有意義,因為估計很好。 經過一番搜索,這似乎是一個非常普遍的問題,我既找不到原因也找不到解決方案。 一種替代方法是使用stats4
包和mle()
函數,但它似乎也有一些問題。 但我可以為您提供使用 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
另一種選擇:包“lmom”。 L矩技術的估計
library(lmom)
thres <- 60
x <- rweibull(200, 3, 1) + thres
moments = samlmu(x, sort.data = TRUE)
log.moments <- samlmu( log(x), sort.data = TRUE )
weibull_3parml <- pelwei(moments)
weibull_3parml
zeta beta delta
59.993075 1.015128 3.246453
但我不知道如何在這個包或上面的解決方案中做一些擬合優度統計。 其他包你可以很容易地做擬合優度統計。 無論如何,您可以使用以下替代方法:ks.test 或 chisq.test
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.