[英]Constructing derivative of a conditional density
我正在使用np包中的npcdens
在協變量x上構造y的條件密度。 但是,我需要此密度相對於y的對數的導數。 R中有某種方法可以做到這一點嗎?
bw <- npcdensbw(formula=y ~ x1+x2+x3)
fhat <- npcdens(bws=bw,gradients=TRUE)
grad.fhat <- gradients(npcdens(bws=bw,gradients=TRUE))
返回關於x1
, x2
和x3
的梯度
我們可以使用此示例數據集嗎?
dta = data.frame(expand.grid(x1=1:5,x2=2:6,x3=5:10))
dta$y = with(dta,x1+2*x2 + 3*x3^2)
head(dta)
x1 x2 x3 y
1 1 2 5 80
2 2 2 5 81
3 3 2 5 82
4 4 2 5 83
5 5 2 5 84
6 1 3 5 82
y是“密度”的值。 估計帶對象的條件帶
bw <- npcdensbw(formula = y ~ x1+x2+x3,data=dta)
看一下漸變
head(gradients(npcdens(bws=bw,gradients=TRUE)))
[,1] [,2] [,3]
[1,] -2.024422e-15 -2.048994e-50 -1.227563e-294
[2,] -1.444541e-15 -1.994174e-50 -1.604693e-294
[3,] -1.017979e-31 -1.201719e-50 -1.743784e-294
[4,] 1.444541e-15 -6.753912e-64 -1.604693e-294
[5,] 2.024422e-15 1.201719e-50 -1.227563e-294
[6,] -2.024422e-15 -3.250713e-50 -1.227563e-294
“關於y的導數”是什么意思? 這是一個函數g(x1,x2,x3),所以您只能對這3個維取導數。 關於您問題的“ y的對數”部分,可以嗎?
bw <- npcdensbw(formula = log(y) ~ x1 + x2 + x3,data=dta)
我從來沒有使用過這個軟件包,所以這些是非從業者的想法。 我猜您看了help(npcdensbw)
中的示例?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.