简体   繁体   English

如何为 R 中的不同 theta 绘制柯西分布对数似然的一阶导数图

[英]How can I make a plot of first derivative of loglikelihood of Cauchy Distribution for different thetas in R

I have a set of observations from a Cauchy (theta,1) and I have a plot for the log-likelihood against different x values我有一组来自柯西 (theta,1) 的观察结果,并且我有一个针对不同 x 值的对数似然图

obs=c(1.77, -0.23, 2.76, 3.80, 3.47, 56.75, -1.34, 4.24, -2.44, 3.29, 3.71, -2.40, 4.53, -0.07, -1.05, -13.87, -2.53, -1.75, 0.27, 43.21)
ll_c=function(theta, obs){ #define Loglikelihood function for Cauchy(θ,1) distribution
  logl= sum(dcauchy(obs, location = theta, scale = 1, log = T)) 
  return(logl)
}
x  = seq(from=-10,to=10,by=0.1) #create test values 
ll = NULL
for (i in x){
  ll = c(ll, ll_c(i, obs)) #perform ll_c for all test values and store
}
plot(x, ll)

I also need to make a plot of the first derivative of the log-likelihood function against the same x values and I can not figure out how to do so.我还需要针对相同的 x 值绘制对数似然函数的一阶导数的图,但我不知道该怎么做。

fdll_c=function(theta,obs){
  Dlogl=D(sum(dcauchy(obs,location=theta,scale=1,log=T)),'theta')
  return(Dlogl)
}
fdll = NULL
for (j in x){
  fdll = c(fdll, fdll_c(j,obs))
}
plot(x,fdll)

I have tried different variations on this code, but every time it has come back with an error or with a derivative of 0 at all points.我尝试了此代码的不同变体,但每次返回时都会出现错误或导数为 0。

Maybe the following answers the question.也许以下回答了这个问题。
It uses an explicit log-likelihood partial derivative function and then applies it to a vector around 0.它使用显式对数似然偏导函数,然后将其应用于 0 左右的向量。

obs <- c(1.77, -0.23, 2.76, 3.80, 3.47, 56.75, -1.34, 4.24, -2.44, 3.29, 3.71, -2.40, 4.53, -0.07, -1.05, -13.87, -2.53, -1.75, 0.27, 43.21)

dll_theta <- function(x, theta, scale){
  cc <- (x - theta)/scale
  -2*sum(1/cc)/scale
}

x <- seq(from = -10, to = 10, by = 0.001)
y <- sapply(x, function(.x) dll_theta(obs, theta = .x, scale = 1))

i <- which(abs(y) > 1e15)
plot(x[-i], y[-i], pch = ".")

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

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