问题描述
我正在使用R编程语言.我在这里按照本教程制作R中的3d内核密度图: https://plotly.com/r/3d-surface-plots/:
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()
但这会导致以下错误:
Error in MASS::kde2d(a, b, c, n = 50) :
bandwidths must be strictly positive
此错误使我无法创建"kd"对象.
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?
谢谢
推荐答案
您似乎误解了 kde2d
的目的.从 help(kde2d)
:
You seem to be misunderstanding the purpose of kde2d
. From help(kde2d)
:
来自与 h
参数有关的同一帮助文件:
From the same help file regarding the h
argument:
您正在传递 c
,它是长度为100的数字矢量,为 h
.您似乎正在尝试将数据传递给 h
,但这没有任何意义.传递一个或两个带宽值或不传递任何值,并接受默认值.
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")
因此,如果 c
的前两个值之一为负或为零,则会出现此错误.
Thus, if either of the first two values of c
are negative or zero, you will get this error.
n
参数,如帮助文件中所述:
The n
argument, as described in the help file:
这确定提供密度的网格.如果您提供一个值,则会生成一个正方形网格.
This determines the grid that the density is provided. If you provide a single value, a square grid is produced.
这篇关于R:内核密度图(带宽必须严格为正)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!