[R] Fitting distributions with fitdistr

Andrea Storto andrea.storto at cmcc.it
Thu Feb 24 17:18:01 CET 2011


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?

Thanks in advance
Andrea



More information about the R-help mailing list