# Fit Inverse Gamma Distribution to dataset in R

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
library(MCMCpack)

data=my.csv.data$V1 hist(data) # 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 mle = optim(c(5,2,2),ll) params = mle$par

# Fit
hist(data,probability=T,ylim=c(0,2.5))
points(seq(2,4.5,0.001),dinvgamma(seq(2,4.5,0.001)-params[3],params[1],params[2]),type="l",col="red")


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.

-
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