[R] Maximum Likelihood Estimation in R

Abhishek Pratap abhishek.vit at gmail.com
Thu Apr 22 00:45:57 CEST 2010

```Hey Henrik

I dont do MLE myself but this recent blog might be helpful.

http://www.johnmyleswhite.com/notebook/2010/04/21/doing-maximum-likelihood-estimation-by-hand-in-r/

-A

On Wed, Apr 21, 2010 at 10:02 AM, Thomas Stewart <tgstewart at gmail.com> wrote:
> Two possible problems:
>
> (a) If you're working with a normal likelihood---and it seems that you
> are---the exponent should be squared.  As in:
> ... + (1/(2*stdev*stdev))*sum(  ( y-(rev/12)-lag(y)*exp(-lap/12) )^2  )
>
> (b) lag may not be working like you think it should.  Consider this silly
> example:
> y<-c(1,2,3,5,34,2,3,5,2)
> lag(y)-y
> lag(ts(y))-ts(y)
>
> Good luck,
> -tgs
>
>
>
> On Wed, Apr 21, 2010 at 8:14 AM, Henkep <flyerhenke at hotmail.com> wrote:
>
>>
>> Dear R-Help,
>>
>> I also send the following post by e-mail to you, however I try to post it
>> here aswell. My name is Henrik and I am currently trying to solve a Maximum
>> Likelihood optimization problem in R. Below you can find the output from R,
>> when I use the "BFGS" method:
>>
>> The problem is that the parameters that I get are very unreasonable, I
>> would
>> expect the absolute value of each parameter to be bounded by say 5.
>> (furthermore the variable stdev should be greater than zero).
>>
>> One of the problems seems to be that I need to bound the stdev-variable
>> from
>> below by zero to avoid the  NaN:s produced. I unfortunately do not know how
>> to do that.
>>
>> Below "y" is the dataset, to which, I want to fit the parameters. I.e. y is
>> the vector (or R equivalent) of observations.
>> I have also multiplied the log-likelihood function by -1, since I know that
>> R by default minimizes the objective function.
>>
>> I would be very happy if you can come up with some Ideas on what is going
>> wrong in the code below.
>>
>> Thank you for your time!
>>
>> Henrik
>>
>>
>> > y<-data\$V1
>> > loglik<-function(estimate,y)
>> + {
>> + lap<-estimate[1]
>> + stdev<-estimate[2]
>> + rev<-estimate[3]
>> + n<-length(y)
>> +
>> + 0.5*n*log(2*pi)+ 0.5*n*log(stdev) +
>> + (1/(2*stdev*stdev))*sum(y-(rev/12)-lag(y)*exp(-lap/12))
>> + }
>> >
>> > optim(c(4,4,4),loglik,y=y,method="BFGS")
>> \$par
>> [1] -4884.34155   445.52350    88.53777
>> \$value
>> [1] -1.910321e+174
>> \$counts
>>     290        7
>> \$convergence
>> [1] 0
>> \$message
>> NULL
>> There were 50 or more warnings (use warnings() to see the first 50)
>>
>> --
>> View this message in context:
>> http://n4.nabble.com/Maximum-Likelihood-Estimation-in-R-tp2018822p2018822.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help