# What is the best way to perform a goodness-of-fit test of data to a continuous distribution with the chi-square method in R?

I'm trying to write a piece of code in R that

• finds the most-fitting distribution to a set of data, by
• performing goodness-of-fit tests to a list of distributions, and then
• finding the most fitting one.
• This program should be able to run in real-time, so analysis should be very light on computational load. What I mean by this is that it should be able to process, say, a fit every second or few seconds at the most, so the simpler the program, the better.

For instance, I've already written the following code:

for(i in 1:numfit) {
if(distrib[[i]] == "negative binomial"){
gf_shape = "negative binomial"
fd_nb <- tryCatch((fitdistr(data, "negative binomial", start=list(size=1,prob=0.5))),
error = function(fd_nb) fd_nb <- fitdistr(data, "negative binomial"))
est_size = fd_nb$estimate[] est_prob = fd_nb$estimate[]
gfn = goodfit(data,type="nbinomial",method="MinChisq",par = list(size = est_size))
tidied = tidy(summary(gfn))
results[i,] = c(gf_shape, est_lambda, "NA", tidied$X.2, tidied$P...X.2.)
}

else if(distrib[[i]] == "poisson"){
gf_shape = "poisson"
fd_p <- fitdistr(data, "poisson")
est_lambda = fd_p\$estimate[]
gf = goodfit(data,type="poisson",method="MinChisq",par = list(lambda = est_lambda))
tidied = tidy(summary(gf))
results[i,] = c(gf_shape, est_lambda, "NA", tidied[1,1], tidied[1,3])
}
results = rbind(c("distribution", "parameter 1", "parameter 2", "chi-squared test statistic", "P > X2"),   results)
return(results)
}


that performs a chi-square goodness-of-fit test of my data to a Poisson and negative binomial distribution, from which I can then find the distribution with the lowest chi-squared test statistic and infer the most suitable distribution to the data from there.

My question is how to do this with continuous data/continuous distributions. Using the goodfit package in R worked really well for me, but it only works with discrete distributions. I do, however, need to use the chi-square goodness-of-fit method (project requirement), so I'm not sure which package or method to turn to and how to implement this.

Can anyone help me with an easy idea of how to implement something similar for, say, a normal distribution? At the moment I think I'm just going to write a piece of code to bin the data into categories, the way you would do the chi-square test by hand, but any way to optimize this process would help. Help would be much appreciated.

• I should clarify that you'll need to reword your question for the other site, but with some work, it might be a good fit. – Richard Erickson Aug 10 '15 at 16:26
• I added "continuous" to the title so that this wouldn't sound like a boring introductory statistics question. – EdM Aug 10 '15 at 21:50

• For a parallel construction with your discrete-distribution case, you could use the Akaike Information Criterion to compare fits generated by the fitdistr function to continuous distributions. That's analogous to your use of chi-square for discrete distributions, and avoids the difficulties in K-S tests on distributions with parameters estimated from the data.. Again, see my second linked page for examples. I repeat my caution about thinking hard about what you are trying to accomplish in this process. – EdM Aug 11 '15 at 0:24