1

我想将伽玛和正态分布的混合拟合到我在 R 中的数据。

数据:

dput(A)
0.0838, 0.081, 0.0816, 0.0838, 0.0824, 0.0871, 0.0899, 0.0938, 0.099, 0.1018, 0.0998, 0.1, 0.0955, 0.0972

根据我认为(通过查看直方图)的数据,伽马分布和正态分布的混合是最佳候选者。

我使用以下代码使用 fitdistrplus 包中的 fitdist 函数来拟合混合分布:

# Define the quantile
qgm_normal <- function(p, 
                   w_gm=0.5,
                   par_shape=2,
                   par_rate=1,
                   mean=0,
                   sd=1) {
  w_normal=1-w_gm
  qgm=qgamma(p,shape=shape, rate=rate)
  qnormal=qnorm(p,mean =mean,sd = sd)
  return(w_gm*qgm+w_normal*qnormal)
}

# Define the distribution function
pgm_normal <- function(q, 
                   w_gm=0.5,
                   par_shape=2,
                   par_rate=1,
                   mean=0,
                   sd=1) {
  w_normal=1-w_gm
  pgm=pgamma(q,shape=shape, rate=rate)
  pnormal=pnorm(q,mean =mean,sd = sd)
  return(w_gm*pgm+w_norm*pnormal)
}

# Define the density
dgm_normal <-function(x,
                  w_gm=0.5,
                  par_shape=2,
                  par_rate=1,
                  mean=0,
                  sd=1) {
  w_normal=1-w_gm
  dgm=dgamma(x,shape=par_shape, rate=par_rate)
  dnormal=dnorm(x,mean =mean,sd = sd)
  return(w_gm*dgm+w_normal*dnormal)
}

fit_A <- fitdist(A, "gm_normal",start=list(w_gm=0.5, par_shape=2,par_rate=1, mean=0, sd=1))

我已阅读此处发布的问题R - 使用 fitdistrplus 拟合混合分布,但出现以下错误:

Error in fitdist(A, "gm_normal", start = list(w_gm = 0.5, par_shape = 100,  : the function mle failed to estimate the parameters, 
            with the error code 1

我可能不完全理解那里发布的解决方案。如果有人可以提供一些帮助将不胜感激。提前致谢!

4

1 回答 1

0

对于任何想要做类似事情的人来说,如果你分别用 和 替换 和 ,这个代码示例就可以par_shape工作。par_rateshaperate

问题是函数调用中的rateandshape参数没有定义,因为他将它们表示为par_rateand par_shape

还要添加lower=c(0,0,0,0,0), upper=c(1,1000,1000,1000,1000)fitdist调用以确保混合参数保持在 [0,1] 中。祝你好运。

于 2021-05-05T12:05:34.823 回答