Load the package needed.
library(ggplot2)
library(MASS)
Generate 10,000 numbers fitted to gamma distribution.
x <- round(rgamma(100000,shape = 2,rate = 0.2),1)
x <- x[which(x>0)]
Draw the probability density function, supposed we don't know which distribution x fitted to.
t1 <- as.data.frame(table(x))
names(t1) <- c("x","y")
t1 <- transform(t1,x=as.numeric(as.character(x)))
t1$y <- t1$y/sum(t1[,2])
ggplot() +
geom_point(data = t1,aes(x = x,y = y)) +
theme_classic()
From the graph, we can learn that the distribution of x is quite like gamma distribution, so we use fitdistr()
in package MASS
to get the parameters of shape and rate of gamma distribution.
fitdistr(x,"gamma")
## output
## shape rate
## 2.0108224880 0.2011198260
## (0.0083543575) (0.0009483429)
Draw the actual point(black dot) and fitted graph(red line) in the same plot, and here is the question, please look the plot first.
ggplot() +
geom_point(data = t1,aes(x = x,y = y)) +
geom_line(aes(x=t1[,1],y=dgamma(t1[,1],2,0.2)),color="red") +
theme_classic()
I have two questions:
-
The real parameters are
shape=2
,rate=0.2
, and the parameters I use the functionfitdistr()
to get areshape=2.01
,rate=0.20
. These two are nearly the same, but why the fitted graph don't fit the actual point well, there must be something wrong in the fitted graph, or the way I draw the fitted graph and actual points is totally wrong, what should I do? -
After I get the parameter of the model I establish, in which way I evaluate the model, something like RSS(residual square sum) for linear model, or the p-value of
shapiro.test()
,ks.test()
and other test?
I am poor in statistical knowledge, could you kindly help me out?
ps: I have search in the Google, stackoverflow and CV many times, but found nothing related to this problem
Best Answer
Question 1
The way you calculate the density by hand seems wrong. There's no need for rounding the random numbers from the gamma distribution. As @Pascal noted, you can use a histogram to plot the density of the points. In the example below, I use the function
density
to estimate the density and plot it as points. I present the fit both with the points and with the histogram:Here is the solution that @Pascal provided:
Question 2
To assess the goodness of fit I recommend the package
fitdistrplus
. Here is how it can be used to fit two distributions and compare their fits graphically and numerically. The commandgofstat
prints out several measures, such as the AIC, BIC and some gof-statistics such as the KS-Test etc. These are mainly used to compare fits of different distributions (in this case gamma versus Weibull). More information can be found in my answer here:@NickCox rightfully advises that the QQ-Plot (upper right panel) is the best single graph for judging and comparing fits. Fitted densities are hard to compare. I include the other graphics as well for the sake of completeness.