我想在R中使用“矩估计方法”(MME)来拟合weibull参数。我知道我们可以用包中的fitdisr()
函数估计这些值MASS
,但我想知道是否有函数或包可以用MME计算参数。例如,我想用蒙特卡罗方法来近似 MME。当我从均匀分布中生成 1000 个值时,我为这个问题编写的函数(用于估计积分)给我 0 值。我该如何解决这个问题?
问问题
2636 次
1 回答
3
这是使用 MME 查找 Weibull 分布参数的一种方法。
# load packages
require(rootSolve)
# generate data
N <- 1000
shape <- 2
scale <- 6
X <- rweibull(n=N, shape=shape, scale=scale)
# range of plausible shapes (for solver)
min_shape <- 0.1
max_shape <- 100
# bootstraping
Nboot <- 1000
sim <- replicate(Nboot, {
Xboot <- sample(X, replace=TRUE)
# find shape
rt <- 1+(sd(Xboot)/mean(Xboot))^2
rootFct <- function(k) {
gamma(1+2/k)/gamma(1+1/k)^2 - rt
}
shape_est <- uniroot.all(rootFct, c(min_shape, max_shape))
if (length(shape_est)!=1) stop("The shape may be outside min_shape and max_shape")
scale_est <- mean(Xboot)/gamma(1+1/shape_est)
c(shape=shape_est, scale=scale_est)
})
apply(sim, 1, function(x)
c(est=mean(x), se=sd(x), quantile(x, c(.025, .5, .975))))
于 2013-11-11T16:13:42.387 回答