在 r 中使用核密度估计生成样本 [英] Generation sample using kernel density estimates in r

查看:45
本文介绍了在 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 中使用核密度估计生成样本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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