質問

I am using npcdens from np package to construct a conditional density of y on covariates x. However, I need the derivative of the log of this density with respect to y. Is there some way in R to get this?

  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

役に立ちましたか?

解決

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". 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"? this is a function g(x1,x2,x3), so you can only take derivatives w.r.t. to those 3 dimensions. Concerning the "log of y" part of your question, could this be it?

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)?

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top