简体   繁体   English

使用R中的核密度估计生成样本

[英]Generation sample using kernel density estimates in 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. 我需要使用R中的核密度估计从现有数据生成样本。在我的数据中缺少负值(并且不能),但是在生成样本负值时。

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)

How to limit the range of the KDE and generated sample? 如何限制KDE和生成的样本的范围?

rkde pas a positive argument: rkde pas是一个positive论点:

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

An alternative would be to transform the data (eg, 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)

If you can accept a density estimate that is not a KDE then look at the logspline package. 如果您可以接受不是KDE的密度估算值,请查看logspline软件包。 This is a different way to estimate density estimates and there are arguments to set lower (and/or upper) bounds so that the resulting estimate will not go beyond the bound and makes sense near the bound. 这是估计密度估计值的另一种方法,并且存在设置下限(和/或上限)的论据,以使所得的估计值不会超出边界并在边界附近有意义。

Here is a basic example: 这是一个基本示例:

set.seed(1)
x<-rlnorm(100)
hist(x, prob=TRUE)

lines(density(x), col='red')

library(ks)
tmp <- kde(x, hpi(x))
lines(tmp$eval.points, tmp$estimate, col='green')

library(logspline)
lsfit <- logspline(x, lbound=0)
curve( dlogspline(x,lsfit), add=TRUE, col='blue' )

curve( dlnorm, add=TRUE, col='orange' )

在此处输入图片说明

You can generate new data points from the fitted density using the rlogspline function and there are also plogspline and qlogspline functions. 您可以使用rlogspline函数根据拟合的密度生成新的数据点,并且还有plogsplineqlogspline函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM