--- title: "Fitting Models fitdistrplus" author: "Prof. Eric A. Suess" format: html: self-contained: true --- ## Fitting Models Random sample from the normal distribution, estimate the parameters simulated sample ```{r} mu <- 43; sigma <- 5.97 # parameters n <- 300 x <- rnorm(n, mu, sigma) hist(x, probability = TRUE) ``` Estimate the parameters using MLE ```{r} library(fitdistrplus) plotdist(x, histo = TRUE, demp = TRUE) fit.norm <- fitdist(x, "norm", method = "mle") summary(fit.norm) fit.gam <- fitdist(x, "gamma", method = "mle") summary(fit.gam) ``` Plot the fitted model on the histogram ```{r} denscomp(list(fit.norm, fit.gam), legendtext = c("Normal", "Gamma")) ``` ```{r} 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 ```{r} lambda <- 11.34 # parameter n <- 30 x <- rexp(n, rate = lambda) hist(x, probability = TRUE) ``` Estimate the parameters using MLE ```{r} plotdist(x, histo = TRUE, demp = TRUE) fit.exp <- fitdist(x, "exp", method = "mle") summary(fit.exp) fit.gam <- fitdist(x, "gamma", method = "mle") summary(fit.gam) ``` Plot the fitted model on the histogram ```{r} denscomp(list(fit.exp, fit.gam), legendtext = c("Exponential", "Gamma")) ``` ```{r} cdfcomp(list(fit.exp, fit.gam), legendtext = c("Exponential", "Gamma")) qqcomp(list(fit.exp, fit.gam), legendtext = c("Exponential", "Gamma")) ```