How do I fit the parameters of a t-distribution, i.e. the parameters corresponding to the ‘mean’ and ‘standard deviation’ of a normal distribution. I assume they are called ‘mean’ and ‘scaling/degrees of freedom’ for a t-distribution?
The following code often results in ‘optimization failed’ errors.
library(MASS) fitdistr(x, "t")
Do I have to scale x first or convert into probabilities? How best to do that?
fitdistr uses maximum-likelihood and optimization techniques to find parameters of a given distribution. Sometimes, especially for t-distribution, as @user12719 noticed, the optimization in the form:
fails with an error.
In this case you should give optimizer a hand by providing starting point and lower bound to start searching for optimal parameters:
fitdistr(x, "t", start = list(m=mean(x),s=sd(x), df=3), lower=c(-1, 0.001,1))
df=3 is your best guess at what an “optimal”
df could be. After providing this additional info your error will be gone.
Couple of excerpts to help you better understand the inner mechanics of
For the Normal, log-Normal, geometric, exponential and Poisson distributions the closed-form MLEs (and exact standard errors) are used, and
startshould not be supplied.
For the following named distributions, reasonable starting values will be computed if
startis omitted or only partially specified: “cauchy”, “gamma”, “logistic”, “negative binomial” (parametrized by mu and size), “t” and “weibull”. Note that these starting values may not be good enough if the fit is poor: in particular they are not resistant to outliers unless the fitted distribution is long-tailed.