【问题标题】:kernel density bandwidth in RR中的核密度带宽
【发布时间】:2014-05-26 06:11:47
【问题描述】:

我有两个向量: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

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

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

【问题讨论】:

    标签: r bandwidth kernel-density


    【解决方案1】:

    看起来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))
    

    【讨论】:

      猜你喜欢
      • 2011-10-30
      • 2016-07-19
      • 1970-01-01
      • 1970-01-01
      • 2015-10-07
      • 2021-07-11
      • 2015-08-11
      • 2015-09-12
      • 2021-01-25
      相关资源
      最近更新 更多