title: How do I fit distributions to sample data in R?
body: <p>I've been struggling with fitting a distribution to sample data I have in R. I've looked at using the fitdist as well as fitdistr functions, but I seem to be running into problems with both.</p>
  
  <p>A quick background; the output of my code should be the most fitting distribution (from a list of distributions) to the data provided, with parameters. This needs to happen without human interaction, so comparing graphs is not an option. I was thinking that I could fit each distribution to the data, draw the p-value from the chi-squared test and find the distribution with the highest p-value. I've gotten some success in a normal distribution to the sample data, but as soon as I try to fit something more complex (a gamma distribution, as seen in the code), I get all kinds of errors. What am I doing wrong? </p>
  
  <pre><code>library(fitdistrplus) 
  require(MASS) 
  set.seed(1) 
  testData &lt;- rnorm(1000) 
  distlist &lt;- c("norm","unif","exp")
  
  (z &lt;- fitdist(testData,"gamma",start=list(rate=0.1),fix.arg=list(shape=4)))
  </code></pre>
  
  <p>Examples of errors I get are:</p>
  
  <blockquote>
    <p>[1] "Error in optim(par = vstart, fn = fnobj, fix.arg = fix.arg, obs =
    data,  : \n  initial value in 'vmmin' is not finite\n" attr(,"class")</p>
  </blockquote>
  
  <p>and</p>
  
  <blockquote>
    <p>Error in fitdist(testData, "gamma", start = list(rate = 0.1), fix.arg
    = list(shape = 4)) :    the function mle failed to estimate the parameters, 
                    with the error code 100</p>
  </blockquote>
  
  <p>I know I'm probably implementing the fitdist function incorrectly, but I can't seem to find simple examples I can adapt to achieve my code objectives. Can anyone help?</p>