[R] Fitting distributions with fitdistr

Peter Ehlers ehlers at ucalgary.ca
Thu Feb 24 22:39:47 CET 2011


On 2011-02-24 08:18, Andrea Storto wrote:
> Dear all,
>
>    I am having troubles in using
> fitdistr() from MASS package
> to fit self-defined distributions.
>
> I try to use it in this simple example
> where I want to fit some data to
> a Gaussian+Flat distribution:
>
>
> gaussflat<-function(x,sd,k){
>      res<-x
>      res[ abs(x)<= k ]<- dnorm(x[ abs(x)<= k ],mean=0,sd=sd)
>      res[ abs(x)>  k ]<- dnorm(k,mean=0,sd=sd)
>      res
> }
>
> a<-fitdistr(omgn,densfun=gaussflat,start=list(sd=0.4,k=0.8))
>
> Starting values are very reasonable and gives me a good
> approximation of hist(omgn,probability=TRUE), but I get
> this message:
>
> Error in optim(x = c(0.195668995619466, 0.166501159354129,
> 0.259120988666992,  :
>    non-finite finite-difference value [1]
> Calls: fitdistr ->  eval.parent ->  eval ->  eval ->  optim
> In addition: There were 50 or more warnings (use warnings() to see the
> first 50)
> Execution halted
>
>
> Any hint? Perhaps should I carefully choose which minimizer
> to use in optim? Is a problem if the densfun has non-continuous derivative??
> Or something completely wrong in the call to fitdistr?

You might consider providing a properly normalized density
to fitdistr.

Peter Ehlers

>
> Thanks in advance
> Andrea
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list