【问题标题】:Weibull cumulative distribution function starting from "fitdistr" commandWeibull 累积分布函数从“fitdistr”命令开始
【发布时间】:2013-02-24 13:08:09
【问题描述】:

我使用了 R MASS 包中的 fitdistr 函数来调整 Weibull 2 参数概率密度函数 (pdf)。

这是我的代码:

require(MASS)

h = c(31.194, 31.424, 31.253, 25.349, 24.535, 25.562, 29.486, 25.680, 26.079, 30.556,      30.552, 30.412, 29.344, 26.072, 28.777, 30.204, 29.677, 29.853, 29.718, 27.860, 28.919, 30.226, 25.937, 30.594, 30.614, 29.106, 15.208, 30.993, 32.075, 31.097, 32.073, 29.600, 29.031, 31.033, 30.412, 30.839, 31.121, 24.802, 29.181, 30.136, 25.464, 28.302, 26.018, 26.263, 25.603, 30.857, 25.693, 31.504, 30.378, 31.403, 28.684, 30.655,  5.933, 31.099, 29.417, 29.444, 19.785, 29.416, 5.682, 28.707, 28.450, 28.961, 26.694, 26.625, 30.568, 28.910, 25.170, 25.816, 25.820)

weib = fitdistr(na.omit(h),densfun=dweibull,start=list(scale=1,shape=5))

hist(h, prob=TRUE, main = "", xlab = "x", ylab = "y", xlim = c(0,40), breaks = seq(0,40,5))
curve(dweibull(x, scale=weib$estimate[1], shape=weib$estimate[2]),from=0, to=40, add=TRUE)

现在,我想创建 Weibull 累积分布函数 (cdf) 并将其绘制为图表:

,其中 x > 0,b = 比例,a = 形状

我尝试使用上面的公式为 h 应用比例和形状参数,但不是这样。

【问题讨论】:

    标签: r plot statistics cdf weibull


    【解决方案1】:

    这是对累积密度函数的尝试。您只需要记住对采样点的间距进行调整(注意:它适用于均匀间距小于或等于 1 的采样点):

    cdweibull <- function(x, shape, scale, log = FALSE){
      dd <- dweibull(x, shape= shape, scale = scale, log = log)
      dd <- cumsum(dd) * c(0, diff(x))
      return(dd)
    }
    

    尽管上面讨论了比例差异,但您可以将其绘制在与dweibull 相同的图表上:

    require(MASS)
    
    h = c(31.194, 31.424, 31.253, 25.349, 24.535, 25.562, 29.486, 25.680,
          26.079, 30.556, 30.552, 30.412, 29.344, 26.072, 28.777, 30.204, 
          29.677, 29.853, 29.718, 27.860, 28.919, 30.226, 25.937, 30.594, 
          30.614, 29.106, 15.208, 30.993, 32.075, 31.097, 32.073, 29.600, 
          29.031, 31.033, 30.412, 30.839, 31.121, 24.802, 29.181, 30.136, 
          25.464, 28.302, 26.018, 26.263, 25.603, 30.857, 25.693, 31.504, 
          30.378, 31.403, 28.684, 30.655,  5.933, 31.099, 29.417, 29.444, 
          19.785, 29.416, 5.682, 28.707, 28.450,  28.961, 26.694, 26.625, 
          30.568, 28.910, 25.170, 25.816, 25.820)
    
    weib = fitdistr(na.omit(h),densfun=dweibull,start=list(scale=1,shape=5))
    
    hist(h, prob=TRUE, main = "", xlab = "x", 
         ylab = "y", xlim = c(0,40), breaks = seq(0,40,5), ylim = c(0,1))
    
    curve(cdweibull(x, scale=weib$estimate[1], shape=weib$estimate[2]),
      from=0, to=40, add=TRUE)
    

    【讨论】:

    • 它可能适用于均匀间距大于 1 的采样点,但可以理解的是,情节不太漂亮。
    • 太棒了!只是一个疑问。你谈到的调整是在diff(x) 内部的lag 参数中?例如:lag 的默认值为 1,但如果我的样本间距较大,我会设置 lag = 2、4、10 等......是这样吗?
    • Andre,我不这么认为,因为cumsum 正在直接处理dweibull 返回的结果。从某种意义上说,“滞后”校正是diff 返回的。你可以试试:运行上面的hist命令后,运行lines(seq(0, 40, by = 4), cdweibull(seq(0, 40, by = 4), scale=weib$estimate[1], shape=weib$estimate[2]))。您会看到添加了一条线,但它更粗糙,因为分辨率要低得多。
    • 也就是说,即使采样间距较大,只要间距均匀,也应该没问题。
    • 太棒了!非常感谢诺亚。
    【解决方案2】:

    这适用于我的数据,但您的数据可能会有所不同。它使用来自FAdist 包的rweibull3 函数。

    >h=rweibull3(1000,2,2,2)
    
    >#this gives some warnings...that I ignore.
    >weib = fitdistr(h,densfun=dweibull3,start=list(scale=1,shape=5,thres=0.5))
    
    There were 19 warnings (use warnings() to see them)    
    

    需要注意的是,起始值会影响拟合的进行方式。因此,如果起始值接近真实值,您将收到更少的警告。

    >curve(dweibull3(   x, 
                scale=weib$estimate[1], 
                shape=weib$estimate[2], 
                thres=weib$estimate[3]),
                add=TRUE)
    

    【讨论】:

    • 是否为您想要拟合的值范围定义了威布尔密度?它不适用于零或更小的值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-27
    • 2012-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多