简体   繁体   English

使用 R 中的矩法拟合 Weibull

[英]Fitting Weibull using method of moments in R

I am trying to fit a Weibull distribution using method of moments to my data in RStudio.我正在尝试使用矩量法将 Weibull 分布拟合到 RStudio 中的数据。 I don't know about the necessary commands and packages one needs to fit distributions such as Weibull or Pareto.我不知道适合 Weibull 或 Pareto 等发行版所需的必要命令和包。 Specifically I am trying to estimate the shape parameter k and the scale λ.具体来说,我试图估计形状参数 k 和尺度 λ。

I use this code to generate my data:我使用此代码生成我的数据:

a <- rweibull(100, 10, 1)

Here is a function to estimate the Weibull distribution parameters with the method of moments.这里有一个 function 用矩量法估计威布尔分布参数。

weibull_mom <- function(x, interval){
  mom <- function(shape, x, xbar){
    s2 <- var(x, na.rm = TRUE)
    lgamma(1 + 2/shape) - 2*lgamma(1 + 1/shape) - log(xbar^2 + s2) + 2*log(xbar)
  }
  xbar <- mean(x, na.rm = TRUE)
  shape <- uniroot(mom, interval = interval, x = x, xbar = xbar)$root
  scale <- xbar/gamma(1 + 1/shape)
  list(shape = shape, scale = scale)
}


set.seed(2021)    # Make the results reproducible
a <- rweibull(100, 10, 1)
weibull_mom(a, interval = c(1, 1e6))
#$shape
#[1] 9.006623
#
#$scale
#[1] 0.9818155

The maximum likelihood estimates are最大似然估计是

MASS::fitdistr(a, "weibull")
#     shape        scale   
#  8.89326148   0.98265852 
# (0.69944224) (0.01165359)
#Warning messages:
#1: In densfun(x, parm[1], parm[2], ...) : NaNs produced
#2: In densfun(x, parm[1], parm[2], ...) : NaNs produced

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM