[英]Fitting a curve to weibull distribution in R using nls
我正在嘗試將此數據擬合為weibull分布:
我的x
和y
變量是:
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)
情節看起來像這樣:
我正在尋找這樣的東西: 擬合圖
我想為其擬合一條威布爾曲線。 我在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”變量是發布后的月份數。
您可以考慮修改公式以更好地擬合數據。 例如,您可以添加一個截距(因為數據平直度是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)
不完美,但這是一個開始。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.