Fitting Models fitdistrplus

Author

Prof. Eric A. Suess

Fitting Models

Random sample from the normal distribution, estimate the parameters

simulated sample

mu <- 43; sigma <- 5.97   # parameters
n <- 300

x <- rnorm(n, mu, sigma)

hist(x, probability = TRUE)

Estimate the parameters using MLE

library(fitdistrplus)
Loading required package: MASS
Loading required package: survival
plotdist(x, histo = TRUE, demp = TRUE)

fit.norm <- fitdist(x, "norm", method = "mle")
summary(fit.norm)
Fitting of the distribution ' norm ' by maximum likelihood 
Parameters : 
      estimate Std. Error
mean 42.941226  0.3513940
sd    6.086323  0.2484731
Loglikelihood:  -967.4948   AIC:  1938.99   BIC:  1946.397 
Correlation matrix:
     mean sd
mean    1  0
sd      0  1
fit.gam <- fitdist(x, "gamma", method = "mle")
summary(fit.gam)
Fitting of the distribution ' gamma ' by maximum likelihood 
Parameters : 
      estimate Std. Error
shape 48.11161 3.91456164
rate   1.12040 0.09163608
Loglikelihood:  -970.5232   AIC:  1945.046   BIC:  1952.454 
Correlation matrix:
          shape      rate
shape 1.0000000 0.9948079
rate  0.9948079 1.0000000

Plot the fitted model on the histogram

denscomp(list(fit.norm, fit.gam), legendtext = c("Normal", "Gamma"))

cdfcomp(list(fit.norm, fit.gam), legendtext = c("Normal", "Gamma"))

qqcomp(list(fit.norm, fit.gam), legendtext = c("Normal", "Gamma"))

Random sample from the exponential distribution, estimate the parameters

simulated sample

lambda <- 11.34   # parameter
n <- 30

x <- rexp(n, rate = lambda)

hist(x, probability = TRUE)

Estimate the parameters using MLE

plotdist(x, histo = TRUE, demp = TRUE)

fit.exp <- fitdist(x, "exp", method = "mle")
summary(fit.exp)
Fitting of the distribution ' exp ' by maximum likelihood 
Parameters : 
     estimate Std. Error
rate 12.91004   2.357039
Loglikelihood:  46.74015   AIC:  -91.4803   BIC:  -90.07911 
fit.gam <- fitdist(x, "gamma", method = "mle")
summary(fit.gam)
Fitting of the distribution ' gamma ' by maximum likelihood 
Parameters : 
        estimate Std. Error
shape  0.9435025  0.2133874
rate  12.1788161  3.5814844
Loglikelihood:  46.77363   AIC:  -89.54725   BIC:  -86.74486 
Correlation matrix:
          shape      rate
shape 1.0000000 0.7690735
rate  0.7690735 1.0000000

Plot the fitted model on the histogram

denscomp(list(fit.exp, fit.gam), legendtext = c("Exponential", "Gamma"))

cdfcomp(list(fit.exp, fit.gam), legendtext = c("Exponential", "Gamma"))

qqcomp(list(fit.exp, fit.gam), legendtext = c("Exponential", "Gamma"))