An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20131223/dffe6861/attachment.pl>
Fitdistr and mle
4 messages · Tia Borrelli, Ben Bolker
Tia Borrelli <tiaborrelli <at> yahoo.it> writes:
Hello, i'm using R for the exploration of a time series and i'm stuck in a
problem with the fitting of the distribution.
What's the difference between "fitdistr" and "mle"?
Hard to say without a reproducible example. In the example below
the answers are not identical (different starting values etc.) but
they're closer than in your example.
(I assume that what you're really doing is more complicated than
the trivial example shown here, since the MLEs of the Normal distribution
parameters are very easy ...)
set.seed(101)
ret <- rnorm(10000,mean=-1.5e-5,sd=1.69e-2)
MASS::fitdistr(ret,densfun="normal")
## mean sd
## 7.419639e-05 1.678380e-02
## (1.678380e-04) (1.186794e-04)
library(stats4)
loglink <- function(media=0, devstd=1){
-sum(dnorm(ret, mean=media, sd=devstd, log=TRUE))
}
mle(loglink)
## media devstd
## 7.402637e-05 1.680457e-02
library(MASS)
fitting <- fitdistr(ret,densfun="normal")
print(c(mean(ret),sd(ret)))
-------------------------------------------------------------
The output of fitdistr is:?
? mean ? ? ? ? ? ? sd ? ? ?
? -1.526547e-05 ? ?1.692554e-02?
?( 5.105564e-04) ( 3.610179e-04)
-------------------------------------------------------------
library(stats4)
loglink <- function(media=0, devstd=1){
? -sum(dnorm(ret, mean=media, sd=devstd, log=TRUE))
}
mle(loglink)
-------------------------------------------------------------
The output of mle is:
Call:
mle(minuslogl = loglink)
Coefficients:
? ? ? ? media ? ? ? ?devstd?
-1.593559e-05 ?1.695075e-02?
Thank you for the help.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20131224/836ef727/attachment.pl>
Tia Borrelli <tiaborrelli <at> yahoo.it> writes:
Thanks for answering, in ret i've the returns of FTSE MIB (the benchmark stock market index in Italy) and i'm estimating the parametres of the distribution of the returns of the index using different methods.?
OK, but this still isn't a *reproducible* example (see e.g. http://tinyurl.com/reproducible-000 )
I need the mle and i found this two function and i could not understand why the result were different: it's possibile that i obtain different result because in the mle() i don't need to know the original distribution and in the fitdistr() i don't need to know the function i had to maximize?
In your example fitdistr() and mle() are doing the same thing under the hood, i.e. using the built-in optim() function to minimize a negative log-likelihood function based on the built-in dnorm(). fitdistr() picks the distribution for you based on your specification of which distribution to use; mle() requires you to specify the negative log-likelihood function (the mle2() function in the bbmle package is an extension of stats4::mle that offers a middle ground, e.g. you can say y ~ dnorm(mu,sigma) to specify the fit of a Normal distribution). The differences between the results you get will be based on small numerical differences, e.g. the starting values of the parameters, or differences in the control parameters for optimization. In general you should get very similar, but not necessarily identical, answers from these two functions; big differences would probably indicate some kind of wonky data or numerical problem. Again, we would need a reproducible example to see precisely what is going on.