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.
fitdistr. – user10525 Oct 4 '12 at 16:21