R: Kernel Density Plots (Bandwidth Must be Strictly Positive)

烂漫一生 提交于 2021-02-11 12:38:03

问题


I am using the R programming language. I am following this tutorial over here for making 3d kernel density plots in R: https://plotly.com/r/3d-surface-plots/:

library(MASS)
library(plotly)

kd <- with(MASS::geyser, MASS::kde2d(duration, waiting, n = 50))
fig <- plot_ly(x = kd$x, y = kd$y, z = kd$z) %>% add_surface()

fig

I decided to try this on my own data :

#generate data
a = rnorm(100,10,10)
b = rnorm(100,5,5)
c = rnorm(100,5,10)
d = data.frame(a,b,c)

#make 3d plot (I think n = 50 refers to selecting the first 50 points?)
kd <- with(d, MASS::kde2d(a,b,c, n = 50))
fig <- plot_ly(x = kd$x, y = kd$y, z = kd$z) %>% add_surface()

But this results in the following error:

Error in MASS::kde2d(a, b, c, n = 50) : 
  bandwidths must be strictly positive

This error prevents me from creating the "kd" object.

Can someone please tell me what am I doing wrong? Is there a problem with the specific data I am using? Or is this a syntax error?

Thanks


回答1:


You seem to be misunderstanding the purpose of kde2d. From help(kde2d):

Two-dimensional kernel density estimation with an axis-aligned bivariate normal kernel, evaluated on a square grid.

From the same help file regarding the h argument:

h
vector of bandwidths for x and y directions. Defaults to normal reference bandwidth (see bandwidth.nrd). A scalar value will be taken to apply to both directions.

You are passing c, a length 100 numeric vector as h. You appear to be trying to pass data to h, this does not make sense. Pass either one or two values for bandwidth or nothing and accept the default.

From lines 31 and 32 of the source, we can see why you got the error:

    if (any(h <= 0))
        stop("bandwidths must be strictly positive")

Thus, if either of the first two values of c are negative or zero, you will get this error.


The n argument, as described in the help file:

n
Number of grid points in each direction. Can be scalar or a length-2 integer vector.

This determines the grid that the density is provided. If you provide a single value, a square grid is produced.



来源:https://stackoverflow.com/questions/65994272/r-kernel-density-plots-bandwidth-must-be-strictly-positive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!