In this tutorial we are exploring the the basics of fitting a distribution to a simulated dataset. In our case we are using functions such as ‘rnorm’, ‘rgamma’, and ‘rexp’ to generate random variables with an arbitrary set of parameters. After that we use `fitdistr’ to estimate those parameters for the simulated data and graph the results.
library(MASS)
op <- options(digits = 3)
set.seed(123)
x <- rnorm(10000, mean = 0, sd=1)
fit = fitdistr(x, "normal")
hist(x, pch=20, breaks=50, prob=TRUE, main="")
curve(dnorm(x, fit$estimate[1], fit$estimate[2]), col="red", lwd=2, add=T)
op <- options(digits = 3)
set.seed(123)
x <- rgamma(10000, shape = 5, rate = 0.1)
fit = fitdistr(x, "gamma")
hist(x, pch=20, breaks=50, prob=TRUE, main="")
curve(dgamma(x, fit$estimate[1], fit$estimate[2]), col="red", lwd=2, add=T)
op <- options(digits = 3)
set.seed(123)
x <- rexp(10000, rate=1)
fit = fitdistr(x, "exponential")
hist(x, pch=20, breaks=50, prob=TRUE, main="")
curve(dexp(x, fit$estimate[1]), col="red", lwd=2, add=T)