1

我有两个向量:1)~1000 个样本均值和 2)这些均值对应的~1000 个标准差。我想创建这些数据的核密度图,使用样本均值作为估计密度的观测值,并将每个均值的标准差作为每个观测值的带宽。问题是,密度只允许将长度为 1 的向量用作带宽。例如:

plot(density(means,bw=error)) 

返回以下警告:

1: In if (!is.finite(bw)) stop("non-finite 'bw'") :
  the condition has length > 1 and only the first element will be used
2: In if (bw <= 0) stop("'bw' is not positive.") :
  the condition has length > 1 and only the first element will be used
3: In if (!is.finite(from)) stop("non-finite 'from'") :
  the condition has length > 1 and only the first element will be used
4: In if (!is.finite(to)) stop("non-finite 'to'") :
  the condition has length > 1 and only the first element will be used

...我得到一个使用列表中第一项的错误作为我所有观察的带宽的图。

关于如何为用于生成内核密度图的每个观察实现单独的用户定义带宽的任何想法?

4

1 回答 1

0

它看起来不density支持这种带宽规范。我想你可以自己动手

mydensity <- function(means, sds) {
  x <- seq(min(means - 3*sds), max(means + 3*sds), length.out=512)
  y <- sapply(x, function(v) mean(dnorm(v, means, sds)))
  cbind(x, y)
}

这将比实际函数(在计算中似乎使用 fft)慢很多。它在这里起作用,左边的带宽小,右边的带宽大:

set.seed(144)
means <- runif(1000)
sds <- ifelse(means < 0.5, 0.001, 0.05)
plot(mydensity(means, sds))

在此处输入图像描述

于 2014-04-12T02:53:59.113 回答