Search code examples
rstatisticsnormal-distributionmlelog-likelihood

Writing a proper normal log-likelihood in R


I have a problem regarding the following model,

enter image description here

where I want to make inference on μ and tau, u is a known vector and x is the data vector. The log-likelihood is

enter image description here

I have a problem writing a log-likelihood in R.

x <- c(3.3569,1.9247,3.6156,1.8446,2.2196,6.8194,2.0820,4.1293,0.3609,2.6197)
mu <- seq(0,10,length=1000)

normal.lik1<-function(theta,x){ 
  u <- c(1,3,0.5,0.2,2,1.7,0.4,1.2,1.1,0.7)  
  mu<-theta[1] 
  tau<-theta[2] 
  n<-length(x) 

logl <-  sapply(c(mu,tau),function(mu,tau){logl<- -0.5*n*log(2*pi) -0.5*n*log(tau^2+u^2)- (1/(2*tau^2+u^2))*sum((x-mu)^2) } )

  return(logl) 
  } 

#test if it works for mu=1, tau=2
head(normal.lik1(c(1,2),x))
#Does not work..

I want to be able to plug in the vector for mu and plot it over mu for a fixed value of tau, say 2. I also want to find out the MLE's of tau and mu using the optim function. I tried:

theta.hat<-optim(c(1,1),loglike2,control=list(fnscale=-1),x=x,,method="BFGS")$par

But it does not work.. Any suggestions to how I can write the likelihood?


Solution

  • First, as has been mentioned in the comments to your question, there is no need to use sapply(). You can simply use sum() – just as in the formula of the logLikelihood.

    I changed this part in normal.lik1() and multiplied the expression that is assigned to logl by minus 1 such that the function computes the minus logLikelihood. You want to search for the minimum over theta since the function returns positive values.

    x < c(3.3569,1.9247,3.6156,1.8446,2.2196,6.8194,2.0820,4.1293,0.3609,2.6197)
    u <- c(1,3,0.5,0.2,2,1.7,0.4,1.2,1.1,0.7) 
    
    normal.lik1 <- function(theta,x,u){ 
      mu <- theta[1] 
      tau <- theta[2] 
      n <- length(x) 
      logl <- - n/2 * log(2*pi) - 1/2 * sum(log(tau^2+u^2)) - 1/2 * sum((x-mu)^2/(tau^2+u^2))
      return(-logl) 
    }
    

    This can be done using nlm(), for example

    nlm(normal.lik1, c(0,1), hessian=TRUE, x=x,u=u)$estimate
    

    where c(0,1) are the starting values for the algorithm.

    To plot the logLikelihood for a range of values of mu and some fixed tau you can adjust the function such that mu and tau are separate numeric arguments.

    normal.lik2 <- function(mu,tau,x,u){ 
      n <- length(x) 
      logl <- - n/2 * log(2*pi) - 1/2 * sum(log(tau^2+u^2)) - 1/2 * sum((x-mu)^2/(tau^2+u^2))
      return(logl) 
    }
    

    Then define some range for mu, compute the loglikelihood and use plot().

    range.mu <- seq(-10,20,0.1)
    
    loglik <- sapply(range.mu, function(m) normal.lik2(mu=m,tau=2,x=x,u=u))
    
    plot(range.mu, loglik, type = "l")
    

    enter image description here

    I'm sure there are more elegant ways to do this but this does the trick.