[R] Optim(...) estimate of stDev far too low

Peter Ehlers ehlers at ucalgary.ca
Wed Sep 30 16:48:10 CEST 2009


Luis,

I don't think that you want log() in fn. Try it with

   pdf=-sum(dnorm(x, media, st, TRUE))

(and note the x, rather than xx).

-Peter Ehlers

Luis Ridao Cruz wrote:
> R-help,
> 
> I'm just trying to find the ML (maximum likelihood) estimates
> of the mean and standard deviation of a set of observations:
> 
>> xx=c(2.5,3.5,4,6,6.5,7.5)
> 
> 
> fn<-function(params,x=xx)
> {
> media<-params[1]
> st   <-params[2]
> pdf=-sum(dnorm(log(xx),log(media),st,TRUE))
> return(pdf)
> }
> 
> optim(c(mu,stdev),fn,method="L-BFGS-B",lower=c(0.001, 0.001)
>         ,upper = rep(Inf, 2), hessian=TRUE, control=list(trace=1))
> 
> iter    0 value 3.011784
> final  value 2.802694 
> converged
> $par
> [1] 4.6597779 0.3860387
> 
> $value
> [1] 2.802694
> 
> $counts
> function gradient 
>       17       17 
> 
> $convergence
> [1] 0
> 
> $message
> [1] "CONVERGENCE: REL_REDUCTION_OF_F <= FACTR*EPSMCH"
> 
> WHich gives an estimate of stDev = 0.38
> while the empirical stDev = 1.94
> 
> Is there anything wrong above in the code?
> 
> Thanks in advance
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 
>




More information about the R-help mailing list