Implementing Maximum Likelihood Estimation in R

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Implementing Maximum Likelihood Estimation in R

TrueTears91
This post has NOT been accepted by the mailing list yet.
I have a dataset here.

I wish to apply maximum likelihood estimation with the following likelihood function:



From the dataset file, I have a column vector of three variables, rho, (s_x*)^2, and T. There are in total N = 3295 observations. I would like to maximize the above likelihood function with respect to two variables: mu and sigma^2.

By logging the above likelihood, we have the following log likelihood:



How can I find the MLE of mu and sigma^2? I have tried the following, but I am not confident if it is right.

Assume I imported the data into R with names rho, s.sq and T, then the following is my code:

####################################

N = 3295

negloglike.f<-function(theta){
 
  mu<-theta[1]
  sigsq<-theta[2]
 
  negloglike<- 0
  negloglike<- -(-N/2*log(2*pi) - sum(1/2*log(sigsq+4/(s.sq*T))+1/(2*(sigsq+4/(s.sq*T)))*(rho-mu)^2))  
 
  negloglike
}

## optimisation  ##
MLE_obj<-optim(c(30,500),negloglike.f,method="BFGS", hessian=TRUE, control = list(maxit = 30000))

MLE_obj

#################################

I am worried that my code isn't right, or this isn't the right approach because sometimes I get warning messages such as:

Warning message:
In log(sigsq + 4/(s.sq * T)) : NaNs produced