Sign up ×
Cross Validated is a question and answer site for people interested in statistics, machine learning, data analysis, data mining, and data visualization. It's 100% free, no registration required.

I am having some problems in modifying Procrastinator's code that assesses the fit of the Inverse Gamma distribution to some randomly generated data. I am refering to the code appearing in the following link:

'Fixing' PearsonFitML to fit to a Pearson V distribution

In my case, I have the dataset save in a .csv file which I directly import in R for further processing. MY modification of this code is as follows:

# Required packages

# log-likelihood
ll = function(par){
if(par[1]>0 & par[2]>0 & par[3]<min(data)) return( -sum(log(dinvgamma(data-    par[3],par[1],par[2]))) )
else return(Inf)

mle = optim(c(5,2,2),ll)
params = mle$par

# Fit

It seems that I cannot attach the .csv file in here, but I can send it to anyone who is familiar and willing to help. I get the following error:

Error in optim(c(5, 2, 2), ll) : 
  function cannot be evaluated at initial parameters

and I cannot understand why due to my lack of knowledge in R.

share|improve this question
You have to be a bit careful because this code is an ad hoc implementation of a modified Inverse Gamma (for including a location parameter, see the comments in the answer for more details). Have a look at the answer in this question for the implementation of the original Inverse Gamma. – user10525 Oct 4 '12 at 13:21
Strange piece of code: dinvgamma(data- par[3] – Stéphane Laurent Oct 4 '12 at 16:15
@StéphaneLaurent Have a look at the comments of the OP in the referred question. The code passed through a couple of modifications. The idea is to produce a modified Inverse Gamma with support on $(par[3],\infty)$. – user10525 Oct 4 '12 at 16:17
By the way perhaps the ML estimates for an inverse-Gamma can be computed with the distrMod R package ? – Stéphane Laurent Oct 4 '12 at 16:19
@StéphaneLaurent In the link I posted, the responder uses fitdistr. – user10525 Oct 4 '12 at 16:21

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.