[R] Fitdistr and mle

Ben Bolker bbolker at gmail.com
Tue Dec 24 17:00:41 CET 2013


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.



More information about the R-help mailing list