R:内核密度图(带宽必须严格为正) [英] R: Kernel Density Plots (Bandwidth Must be Strictly Positive)

查看:80
本文介绍了R:内核密度图(带宽必须严格为正)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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):

使用轴对齐的二元正态核进行二维核密度估计,并在正方形网格上进行评估.

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

来自与 h 参数有关的同一帮助文件:

From the same help file regarding the h argument:

h
x和y方向的带宽向量.默认为正常参考带宽(请参阅bandwidth.nrd).标量值将应用于两个方向.

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.

您正在传递 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:

n
每个方向上的网格点数.可以是标量或长度为2的整数矢量.

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.

这篇关于R:内核密度图(带宽必须严格为正)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆