Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在给定一些条件数据的情况下,是否可以从R中的条件密度进行采样?_R_Sampling_Kernel Density - Fatal编程技术网

在给定一些条件数据的情况下,是否可以从R中的条件密度进行采样?

在给定一些条件数据的情况下,是否可以从R中的条件密度进行采样?,r,sampling,kernel-density,R,Sampling,Kernel Density,在R中,使用np包,我为条件密度创建了带宽。我想做的是,给定一些新的条件向量,从结果分布中抽取样本 当前代码: library('np') # Generate some test data. somedata = data.frame(replicate(10,runif(100, 0, 1))) # Conditional variables. X <- data.frame(somedata[, c('X1', 'X2', 'X3')]) # Dependent variables.

在R中,使用
np
包,我为条件密度创建了带宽。我想做的是,给定一些新的条件向量,从结果分布中抽取样本

当前代码:

library('np')
# Generate some test data.
somedata = data.frame(replicate(10,runif(100, 0, 1)))
# Conditional variables.
X <- data.frame(somedata[, c('X1', 'X2', 'X3')])
# Dependent variables.
Y <- data.frame(somedata[, c('X4', 'X5', 'X6')])
# Warning, this can be slow (but shouldn't be too bad).
bwsome = npcdensbw(xdat=X, ydat=Y)
# TODO: Given some vector t of conditional data, how can I sample from the resulting distribution?
library('np')
#生成一些测试数据。
somedata=data.frame(复制(10,runif(100,0,1)))
#条件变量。

X这里是示例2.49,来自:,它给出了以下内容 两个变量的解决方案:

###
library(np)
data(faithful)
n <- nrow(faithful)
x1 <- faithful$eruptions
x2 <- faithful$waiting
## First compute the bandwidth vector
bw <- npudensbw(~x1 + x2, ckertype = "gaussian")
plot(bw, view = "fixed", ylim = c(0, 3))
## Next generate draws from the kernel density (Gaussian)
n.boot <- 1000
i.boot <- sample(1:n, n.boot, replace = TRUE)
x1.boot <- rnorm(n.boot,x1[i.boot],bw$bw[1])
x2.boot <- rnorm(n.boot,x2[i.boot],bw$bw[2])
## Plot the density for the bootstrap sample using the original
## bandwidths
plot(npudens(~x1.boot+x2.boot,bws=bw$bw), view = "fixed")
## Generate some test data.
somedata = data.frame(replicate(10, runif(100, 0, 1)))
## Conditional variables.
X <- data.frame(somedata[, c('X1', 'X2', 'X3')])
## Dependent variables.
Y <- data.frame(somedata[, c('X4', 'X5', 'X6')])
## First compute the bandwidth vector
n <- nrow(somedata)
bw <- npudensbw(~X$X1 + X$X2 + X$X3 + Y$X4 + Y$X5 + Y$X6, ckertype = "gaussian")
plot(bw, view = "fixed", ylim = c(0, 3))
## Next generate draws from the kernel density (Gaussian)
n.boot <- 1000
i.boot <- sample(1:n, n.boot, replace=TRUE)
x1.boot <- rnorm(n.boot, X$X1[i.boot], bw$bw[1])
x2.boot <- rnorm(n.boot, X$X2[i.boot], bw$bw[2])
x3.boot <- rnorm(n.boot, X$X3[i.boot], bw$bw[3])
x4.boot <- rnorm(n.boot, Y$X4[i.boot], bw$bw[4])
x5.boot <- rnorm(n.boot, Y$X5[i.boot], bw$bw[5])
x6.boot <- rnorm(n.boot, Y$X6[i.boot], bw$bw[6])
## Plot the density for the bootstrap sample using the original
## bandwidths
ob1 <- npudens(~x1.boot + x2.boot + x3.boot + x4.boot + x5.boot + x6.boot, bws = bw$bw)
plot(ob1, view = "fixed", ylim = c(0, 3))
###
图书馆(np)
数据(忠实)

n我得到:
错误:找不到函数“npcedensbw”
。当我查看np包中的可用函数时,我没有看到任何同名函数。当我使用
npcdensbw
重新运行,然后
plot
结果时,我看到6个变量。现在。。。问题到底是什么?事实上,我正在研究多元数据,包括条件变量和因变量。我想做的是从确定的分布中抽取样本。给定一些条件/自变量的新向量,我想根据给定条件变量的分布进行采样。在一个简单的例子中,如果x和y都是一维的,我想修正x,使得y上有一个分布,然后在该分布中进行采样。我想在这里做同样的事情。这更清楚吗?只是为了确保我正确理解这个问题:您的案例与中的FAQ 2.49有何不同?因此,如果我理解正确。。你想计算像P(X4 | X1)这样的东西,或者更复杂的东西。。。P(X5 | X1,X2,X3)。。。甚至P(X1 | X4)。。。这是否正确?此示例从内核无条件密度估计中采样(使用
npudensbw
),但不用于内核条件密度估计(使用
npcdensbw
)。也许有一种简单的方法可以修改这段代码来尝试这一点,但我在
np
帮助文件中没有看到很好的文档记录,因此我希望能为这个问题的特定方面提供一个明确的答案。