[datatable-help] Fitting a Mixture of Gamma and Normal distribution to real data

Izzy isaudin at gmail.com
Thu Feb 1 20:19:07 CET 2018


Dear R experts,

I would like to fit a mixture of gamma and normal distribution to my data in
R.

The data:
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

Based on the data I believe (by looking at the histogram) a mixture of gamma
and normal distribution is the best candidate.

I used the following codes to fit the mixture distribution using fitdist
function from fitdistrplus package:

# 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))

I've read the question posted here 
https://stackoverflow.com/questions/33942202/r-fitting-a-mixture-distribution-with-fitdistrplus
<https://stackoverflow.com/questions/33942202/r-fitting-a-mixture-distribution-with-fitdistrplus>  
but I've got the following error:

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

I could probably didn't fully understand the solution posted there. If
someone could provide some help would be very grateful. 

Thanks in advance!



--
Sent from: http://r.789695.n4.nabble.com/datatable-help-f2315188.html


More information about the datatable-help mailing list