简体   繁体   English

R:来自模型“ ks”包的样本数据

[英]R: Sample data from model, package `ks`

I'm sorry if this has an obvious answer someplace, I haven't been able to find one and I'm not too experienced with R. 很抱歉,如果在某个地方有一个明显的答案,我还没有找到答案,并且我对R不太有经验。

I'm using the kernel-smoothing package ks to produce a kernel-density estimation model using kde . 我正在使用内核平滑软件包ks使用kde生成内核密度估计模型。 I would like to then sample the model produced. 然后,我想对生成的模型进行采样。

library(ks)
data <- read.table("data_file.txt", header=FALSE)
model <- kde(data)

And then I'm not sure how to proceed. 然后我不确定如何继续。 I had read the help document but hadn't found the necessary function. 我已经阅读了帮助文档,但是没有找到必要的功能。

Univariate case 单变量情况

Please refer to the help document: https://cran.r-project.org/web/packages/ks/ks.pdf 请参阅帮助文档: https : //cran.r-project.org/web/packages/ks/ks.pdf

You can see dkde , pkde , qkde , rkde as same as any R distribution. 您可以看到dkdepkdeqkderkde与任何R发行版一样。

rkde(100, model)

It'll generate 100 random number from estimated distribution. 它将根据估计的分布生成100个随机数。

Multivariate case 多元案例

I'm surprised finding ks package whick don't prodive sample method directly. 我惊讶地发现ks包鞭子不直接生产样品方法。 Anyway it's easily in principle. 无论如何,原则上很容易。 You only need first to select randomly a sample point and then to apply a estimated kernel noise on it. 您只需要首先随机选择一个采样点,然后在其上应用估计的内核噪声即可。 For slow but basic code example with multivariate normal kernel: 对于带有多变量普通内核的缓慢但基本的代码示例:

library(mvtnorm)

rmkde <- function(size,flat){
  n <- nrow(flat$x)
  s <- sample(1:n,size,replace=TRUE)
  t(apply(flat$x[s,],1,function(mean)rmvnorm(1,mean=mean,sigma=flat$H)))
}

rmkde(100, model)

Since rmvnorm compute same matrix decomposition many times, it is slow, you can pick decomposition from rmvnorm source code to accelerate it. 由于rmvnorm计算相同的矩阵分解,所以速度很慢,因此可以从rmvnorm源代码中选择分解来加速分解。

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

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