2015-10-13 190 views
2

我想这个数据拟合到威布尔分布Weibull分布中的R拟合曲线利用NLS

y <- c(1, 1, 1, 4, 7, 20, 7, 14, 19, 15, 18, 3, 4, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1) 
x <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24) 

情节是这样的:

enter image description here

我期待这样的事情: fitted plot

我想对它适合一个威布尔曲线。我使用R中的NLS功能是这样的:

nls(y ~ ((a/b) * ((x/b)^(a-1)) * exp(- (x/b)^a))) 

此函数总是抛出了一个错误说:

Error in numericDeriv(form[[3L]], names(ind), env) : 
    Missing value or an infinity produced when evaluating the model 
In addition: Warning message: 
In nls(y ~ ((a/b) * ((x/b)^(a - 1)) * exp(-(x/b)^a))) : 
    No starting values specified for some parameters. 
Initializing ‘a’, ‘b’ to '1.'. 
Consider specifying 'start' or using a selfStart model 

所以首先我尝试了不同的初始值没有任何成功。我无法理解如何对初始值做出“好”的猜测。 然后我去了SSweibull(x, Asym, Drop, lrc, pwr)函数这是一个selfStart函数。现在SSWeibull函数需要Asym,Drop,lrc和pwr的值,我对这些值可能是什么都没有任何线索。

如果有人能帮我弄清楚如何着手,我将不胜感激。

数据背景:我从bugzilla获取了一些数据,我的“y”变量是特定月份中报告的错误数量,“x”变量是发布后的月份数量。

+0

也许这篇文章在交叉验证将帮助:http://stats.stackexchange.com/questions/19866/how-to-fit-a-weibull-distribution-to-input-data-containing-zeroes – MrFlick

+0

我没有在问这个问题之前先阅读这篇文章,但没有发现它有用,因为使用fitdistr函数没有问题。而fitdistr则提供了最佳的配戴合适性,而不是最佳的配合。 –

+1

_hint_:概率密度函数将积分为1.这是否适合您数据的曲线? –

回答

4

您可能会考虑修改公式以更好地适合数据。例如,你可以添加一个截距(因为你的数据在1处而不是0,这是模型想要做的)和一个标量乘数,因为你实际上并不适合密度。

总是值得花一些时间真正思考哪些参数是有意义的,因为模型拟合程序通常对初始估计非常敏感。你也可以进行网格搜索,找出可能的参数范围,并尝试使用错误捕获函数对各种组合进行拟合。 nls2有一个选项可以为你做到这一点。

因此,例如,

## Put the data in a data.frame 
dat <- data.frame(x=x, y=y) 

## Try some possible parameter combinations 
library(nls2) 
pars <- expand.grid(a=seq(0.1, 100, len=10), 
        b=seq(0.1, 100, len=10), 
        c=1, 
        d=seq(10, 100, len=10)) 

## brute-force them 
## note the model has changed slightly 
res <- nls2(y ~ d*((a/b) * ((x/b)^(a-1)) * exp(- (x/b)^a)) + c, data=dat, 
      start=pars, algorithm='brute-force') 

## use the results with another algorithm 
res1 <- nls(y ~ d*((a/b) * ((x/b)^(a-1)) * exp(- (x/b)^a)) + c, data=dat, 
      start=as.list(coef(res))) 

## See how it looks 
plot(dat, col="steelblue", pch=16) 
points(dat$x, predict(res), col="salmon", type="l", lwd=2) 

enter image description here

并不完美,但它是一个开始。

+0

这看起来非常好。谢谢!我对nls2没有任何线索。所以要改进你的方法..我可能会使用更多的参数?或者你有更好的建议吗? –

+0

我会玩实际的模型公式。你也可以看看最大似然估计。 – jenesaisquoi

+0

我看着fitdistr最大似然估计,但我找不到正确的方式来使用它。 –