在 r 中使用核密度估计生成样本

编程入门 行业动态 更新时间:2024-10-09 06:29:05
本文介绍了在 r 中使用核密度估计生成样本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要使用 R 中的核密度估计从现有数据中生成样本.在我的数据中缺少负值(并且不能),但在生成样本中存在负值.

I need generate sample from existing data using kernel density estimates in R. In my data missing negative values (and can not be), but in generate sample negative values present.

library(ks) set.seed(1) par(mfrow=c(2,1)) x<-rlnorm(100) hist(x, col="red", freq=F) y <- rkde(fhat=kde(x=x, h=hpi(x)), n=100) hist(y, col="green", freq=F)

如何限制KDE和生成样本的范围?

How to limit the range of the KDE and generated sample?

推荐答案

rkde 传递一个 positive 参数:

y <- rkde( fhat = kde(x=x, h=hpi(x)), n = 100, positive = TRUE )

另一种方法是在估计之前转换数据(例如,使用对数),使其不受约束,并在生成随机数后将其转换回来.

An alternative would be to transform the data (e.g., with a logarithm) before the estimation, to make it unconstrained, and transform it back after the random number generation.

x2 <- log(x) y2 <- rkde(fhat=kde(x=x2, h=hpi(x2)), n=100) y <- exp(y2) hist(y, col="green", freq=F)

更多推荐

在 r 中使用核密度估计生成样本

本文发布于:2023-10-31 06:50:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545340.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:样本   密度

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!