[英]Find the second derivative of a log likelihood function
I'm interested in finding the values of the second derivatives of the log-likelihood function for logistic regression with respect to all of my m predictor variables. 我感兴趣的是找到关于我的所有m个预测变量的对数回归函数的对数似然函数的二阶导数的值。
Essentially I want to make a vector of m ∂ 2 L/ ∂ β j 2 values where j goes from 1 to m. 基本上我想使米∂的矢量2 L /∂βĴ2个值,其中j变为从1到m。
I believe the second derivative should be -Σ i=1 n x ij 2 (e x i β )/((1+e x i β ) 2 ) and I am trying to code it in R. I did something dumb when trying to code it and was wondering if there was some sort of sapply function I could use to do it more easily. 相信所述第二导数应该-ΣI = 1分 的N×IJ 图2(e X Iβ)/((1 + E X Iβ)2)和我想它在R.我做了哑代码当试图对其进行编码,并且想知道是否可以使用某种类型的sapply函数来更轻松地实现它。
Here's the code I tried (I know the sum in the for loop doesn't really do anything, so I wasn't sure how to sum those values). 这是我尝试过的代码(我知道for循环中的总和实际上并没有做任何事情,因此我不确定如何对这些值求和)。
for (j in 1:m)
{
for (i in 1:n)
{
d2.l[j] <- -1*(sum((x.center[i,j]^2)*(exp(logit[i])/((1 + exp(logit[i])^2)))))
}
}
And logit is just a vector consisting of Xβ if that's not clear. 如果不清楚,logit只是由Xβ组成的向量。
I'm hazy on the maths (and it's hard to read latex) but purely on the programming side, if logit
is a vector with indices i
=1,...,n and x.center
is a nxm
matrix: 我在数学上很朦胧(很难阅读乳胶),但纯粹在编程方面,如果
logit
是一个索引为i
= 1,...,n和x.center
则nxm
矩阵:
for (j in 1:m)
dt.l[j] <- -sum( x.center[,j]^2 * exp(logit)/(1+exp(logit))^2 )
where the sum
sums over i
. sum
超过i
。
If you want to do it "vector-ish", you can take advantage of the fact that if you do matrix * vector
(your x.center * exp(logit)/...
) this happens column-wise in R which suits your equation: 如果要“向量化”,则可以利用以下事实:如果您进行
matrix * vector
(您的x.center * exp(logit)/...
),这在R中按列进行,这很适合你的方程式:
-colSums(x.center^2 * exp(logit)/(1+exp(logit))^2)
For what it's worth, although the latter is "slicker", I will often use the explicit loop (as with the first example), purely for readability. 值得一提的是,尽管后者是“ slicker”,但我经常会使用显式循环(与第一个示例一样),纯粹是为了提高可读性。 Or else when I come back in a month's time I get very confused about my
i
s and j
s and what is being summed over when. 要不然我回来的时候在一个月的时间里,我得到了我非常困惑
i
S和j
S和什么是被求和时。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.