[R] Non-finite finite difference error

Ravi Varadhan rvaradhan at jhmi.edu
Wed Dec 24 16:57:51 CET 2008

Hi Jason,

The error message indicates that there was problem in estimating the gradient of objective function.  It has nothing to do with your second data point.  This could happen for a variety of reasons, but the most proximate cause of the problem seems due to a parameter being negative during the iteration.  The best solution, most often, is to provide a better (or atleast a different) starting value.  Look at the example in the help page for "fitdistr":

fitdistr(x, dgamma, list(shape = 1, rate = 0.1), lower = 0.01)

Note that the above command specifies lower bounds on both the shape and the rate parameter (hence a different optimziation algorithm will be used in "optim").


Ravi Varadhan, Ph.D.
Assistant Professor,
Division of Geriatric Medicine and Gerontology
School of Medicine
Johns Hopkins University

Ph. (410) 502-2619
email: rvaradhan at jhmi.edu

----- Original Message -----
From: JS.Augustyn at gmail.com
Date: Tuesday, December 23, 2008 11:27 pm
Subject: [R] Non-finite finite difference error
To: r-help at r-project.org

> Hello, I'm trying to use fitdistr() from the MASS package to fit a 
> gamma  
>  distribution to a set of data. The data set is too large (1167 
> values) to  
>  reproduce in an email, but the summary statistics are:
>  Min. 1st Qu. Median Mean 3rd Qu. Max.
>  116.7 266.7 666.7 1348.0 1642.0 16720.0
>  The call I'm trying to make is:
>  fitdistr(x,"gamma")
>  and the error is:
>  Error in optim(x = c(3466.676842, 1666.749002, 2500.067852, 
> 1200.053892, :
>  non-finite finite-difference value [2]
>  In addition: Warning message:
>  In dgamma(x, shape, scale, log) : NaNs produced
>  I found a couple of other posts from folks who were getting the same 
> error  
>  from optim(), but did not find any useful tips for my situation. The 
> error  
>  seems to indicate a problem with value 2 in my data set 
> (1666.749002), but  
>  nothing seems odd about that value.
>  I'm willing to pass along the full data set as an attachment if it 
> would  
>  help.
>  Thank you in advance!
>  Jason S. Augustyn, Ph.D.
>  	[[alternative HTML version deleted]]
>  ______________________________________________
>  R-help at r-project.org mailing list
>  PLEASE do read the posting guide 
>  and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list