简体   繁体   English

在R中编写适当的正常对数似然

[英]Writing a proper normal log-likelihood in R

I have a problem regarding the following model, 我对以下型号有疑问,

在此处输入图片说明

where I want to make inference on μ and tau, u is a known vector and x is the data vector. 在这里我想对μ和tau进行推论,u是一个已知向量,x是数据向量。 The log-likelihood is 对数似然是

在此处输入图片说明

I have a problem writing a log-likelihood in R. 我在用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. 我希望能够为mu插入向量,并将其在mu上绘制为tau的固定值(例如2)。我还希望使用optim函数找出tau和mu的MLE。 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? 但这是行不通的。关于如何写可能性的任何建议?

First, as has been mentioned in the comments to your question, there is no need to use sapply() . 首先,正如您对问题的评论中提到的那样,不需要使用sapply() You can simply use sum() – just as in the formula of the logLikelihood. 您可以简单地使用sum() –就像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. 我在normal.lik1()更改了此部分, normal.lik1()分配给logl的表达式乘以减号1,以便该函数计算出减号logLikelihood。 You want to search for the minimum over theta since the function returns positive values. 由于函数返回正值,因此您要搜索theta上的最小值

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()完成此操作

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

where c(0,1) are the starting values for the algorithm. 其中c(0,1)是算法的起始值。

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. 要绘制一系列mu和一些固定tau值的logLikelihood,可以调整函数,使mutau是单独的数字参数。

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() . 然后为mu定义一个范围,计算对数似然并使用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")

在此处输入图片说明

I'm sure there are more elegant ways to do this but this does the trick. 我敢肯定,有更优雅的方法可以做到这一点,但这可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM