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
h <- hist(x, 1000, plot = FALSE); t1 <- data.frame(x = h$mids, y = h$density)
. $\endgroup$density
function is a useful one. $\endgroup$