duxin123的个人博客分享 http://blog.sciencenet.cn/u/duxin123

博文

r语言,box核的概率密度函数估算

已有 843 次阅读 2023-5-24 15:52 |系统分类:科研笔记

R语言中density函数中kernel参数所对应的核函数视乎都具有拖尾,一下为box核的概率密度估算代码。


density_box=function(entity,bandwidth)

{

  if (missing(bandwidth)) {

    bandwidth=density(entity)$bw

  }

  min_x=min(entity)

  max_x=max(entity)

  x=seq(min_x, max_x,length.out=1000)

  y=rep(0, length(x))


 

  box=function(entity){

    for (i in 1:length(x)) {

      if (abs(x[i] - entity) <= bandwidth/2) {

        y[i] <<- y[i] + 1

      }

    }

  }

  for(i in 1:length(entity))

  {

    box(entity[i])

  }

  y=y/length(entity)

  result=cbind(x,y)

  colnames(result)=c("scale","density")

  result

}

y=density_box(entity)

par(mfrow = c(2, 1))

plot(y,type="l")

plot(density(entity))

以下为效果图,上图为box核,下图为高斯核

Rplot.png




https://wap.sciencenet.cn/blog-3511023-1389246.html

上一篇:双变量莫兰指数Moran’S I的R代码,有待修正验证
下一篇:基于R语言,计算加权距离矩阵笔记。
收藏 IP: 125.211.161.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-29 10:17

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部