[R] fitdistr() Error

Peter Maclean pmaclean2011 at yahoo.com
Mon Jul 11 23:21:20 CEST 2011

I am trying to estimate a gamma function using real data and I am getting the 
following error messages.
When I set a lower limit; the error message is "L-BFGS-B needs finite values of 
For other method the error message is:
Error in optim(x = c(0.105286666666667, 0.3472275, 2.057625, 0.329675,  : 
  non-finite finite-difference value [1]

The codes works fine for simulated data (see below). I am using the same codes

#Grouped vector
n <- c(1:100)
yr <-c(1:100)
ny <- list(yr=yr,n=n)
ny <- expand.grid(ny) 
y = rgamma(1000, shape=1.5, rate = 1, scale = 2)
Gdata <- cbind(ny,y)
#MLE Estimation of Gamma Distribution Parameters 
#Generate starting values
y <- as.numeric(Gdata2$y)
me  <- mean(y)
sde <- sd(y)
sh  <- sqrt(me/sde)
sc  <- sqrt(sde)/me
Gdata <-split(Gdata,Gdata$n)
parm <- lapply(Gdata, function(x){
          y <- as.numeric(x$y)
           fitdistr(y ,"gamma",list(shape=sh.mom, scale=sc),
#method = c("Nelder-Mead", "BFGS", "CG", "L-BFGS-B", "SANN"),
           lower=0, method = c("CG"),control = list(maxit=10000))
parmss<-lapply(parm, function(x) x$estimate) 
parmss<- t(as.data.frame(parmss))  #Estimates
parmsd<-lapply(parm, function(x) x$sd)     
parmsd<- t(as.data.frame(parmsd)) #Standard errors

More information about the R-help mailing list