Reproducable example which will give the mentioned error code every time is: (Note that even without set.seed, the error comes up every time)
library(MASS)
set.seed(seed = 1)
data<-rnorm(n = 10000,mean = 0.0002,sd = 0.001)
fitdistr(x = data,densfun = "t")
The error message is:
Error in stats::optim(x = c(-0.000426453810742332, 0.000383643324222082, :
non-finite finite-difference value [2]
In addition: Warning message:
In log(s) : NaNs produced
The problem is the "non-finite finite-difference value". Fitdistr does not give me a result.
My knowledge: I researched and apparently this could mean that a parameter is negative during the iteration. And that the solution could be to provide a better or at least different starting value. But I could not figure out how to do this and I am not sure if this is the issue.
MY QUESTION: a) Why do I get this error message and b)how can I fix it in R, so that I can fit the student-t distribution to my normally distributed data?