简体   繁体   English

构造条件密度的导数

[英]Constructing derivative of a conditional density

I am using npcdens from np package to construct a conditional density of y on covariates x. 我正在使用np包中的npcdens在协变量x上构造y的条件密度。 However, I need the derivative of the log of this density with respect to y. 但是,我需要此密度相对于y的对数的导数。 Is there some way in R to get this? R中有某种方法可以做到这一点吗?

  bw   <- npcdensbw(formula=y ~ x1+x2+x3)
  fhat <- npcdens(bws=bw,gradients=TRUE)
  grad.fhat <- gradients(npcdens(bws=bw,gradients=TRUE)) 

which returns the gradient with respect to x1 , x2 and x3 返回关于x1x2x3的梯度

Can we use this example dataset? 我们可以使用此示例数据集吗?

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 is the value of the "density". y是“密度”的值。 estimate a conditional bandwith object 估计带对象的条件带

bw <- npcdensbw(formula = y ~ x1+x2+x3,data=dta)

and look at the gradients 看一下渐变

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

What do you mean with "derivative with respect to y"? “关于y的导数”是什么意思? this is a function g(x1,x2,x3), so you can only take derivatives wrt to those 3 dimensions. 这是一个函数g(x1,x2,x3),所以您只能对这3个维取导数。 Concerning the "log of y" part of your question, could this be it? 关于您问题的“ y的对数”部分,可以吗?

bw <- npcdensbw(formula = log(y) ~ x1 + x2 + x3,data=dta)

I've never used this package, so these are the thoughts of a non-practitioner. 我从来没有使用过这个软件包,所以这些是非从业者的想法。 I guess you looked at the examples in help(npcdensbw) ? 我猜您看了help(npcdensbw)中的示例?

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

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