【发布时间】:2020-02-26 08:02:27
【问题描述】:
我需要绘制柯西分布的对数似然函数。这是柯西分布的对数似然的代码:
CauchyLL <- function(theta,x){
#CauchyLL is the log-likelihood function for the Cauch Distribution
#x is the data vector and theta is the unknown parameter
n <- length(x)
#f0 is the log likelihood function
#f1 is the first derivative of the log likelihood
#f2 is the second derivative of the log likelihood
f0 <- -n*log(pi)-sum(log((x-theta)^2+1),na.rm=TRUE)
f1 <- sum((2*(x-theta))/((x-theta)^2+1),na.rm=TRUE)
f2 <- 2*sum(((x-theta)^2-1)/((x-theta)^2+1),na.rm=TRUE)
return(c(f0,f1,f2))
}
我的数据 x 由以下给出:
x <- 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)
我的 theta 由以下给出:
xgrid<-seq(-9,10,by=1)
我想使用 for 循环为每个 theta 值绘制柯西分布的对数似然函数。这是我的尝试:
for(j in 1:20){
print(xgrid[j])
print(CauchyLL(xgrid[j],x)[1])
plot(xgrid[j],CauchyLL(xgrid[j],x)[1])
}
这个for循环似乎只为theta的最后一个值绘制,但它没有为theta的前19个值绘制。如何更改此设置,以便获得所有 20 个 theta 值的图?
【问题讨论】:
标签: r plot statistics distribution