I'm trying to write a piece of code in R that identifies a set of sample data as belonging to a specific distribution and pull the specific distribution parameters, by performing the K-S test and comparing the resulting p-values.
However, I've run into a bit of a logical problem. I can successfully generate gamma, weibull, logistic, normal and poisson distributions and correctly identify them, but as soon as I try to identify an exponential distribution, the resulting p-values are always lower than the p-values from trying to fit a weibull or gamma distribution. So, to put it another way, I generate a random set of values from an exponential distribtion using
rexp(1000)
but when I pass it through my code, the resulting p-values are, for instance:
[1,] "distribution" "ks pvalue"
[2,] gamma "0.850558314129566"
[3,] weibull "0.833929454438442"
[4,] logistic "0"
[5,] normal "0"
[6,] exponential "0.704115316673917"
[7,] poisson "0"
The section of code that performs the test on the exponential distribution, for instance, is (credited with GREAT thanks to @TinaW from StackOverflow here):
gf_shape = "exponential"
fd_e = fitdistr(data, "exponential")
est_rate = fd_e$estimate[[1]]
ks = ks.test(data, "pexp", rate=est_rate)
results[i,] = c(gf_shape, est_rate, "NA", ks$statistic, ks$p.value)
Do you guys have any idea what I'm doing wrong? I've tried increasing and decreasing the sample size, toying with the parameters, but the gamma and weibull p-values are always larger than that of the exponential fit. Any ideas?