【发布时间】:2017-10-16 08:04:57
【问题描述】:
背景
我有两个变量,分别称为 x 和 y(请查看图片下方的 R 代码)。当我plot(x, y) 时,我获得了顶行图(见下文)。 y 值堆叠在每个 x 值的顶部。然后我尝试从这些y 值中取样,并在母图下方制作第二个图。
问题
我想知道为什么当我使用predit.range(请参阅下面的 R 代码)作为 10:0(使用 0:10 时不会发生问题)我的抽样程序完全走错方向了?(请比较顶行图和底行图)
############# Input Values ################
each.sub.pop.n = 150;
sub.pop.means = 20:10;
predict.range = 10:0;
sub.pop.sd = .75;
n.sample = 2;
#############################################
par( mar = c(2, 4.1, 2.1, 2.1) )
m = matrix( c(1, 2), nrow = 2, ncol = 1 ); layout(m)
Vec.rnorm <- Vectorize(function(n, mean, sd) rnorm(n, mean, sd), 'mean')
y <- c( Vec.rnorm(each.sub.pop.n, sub.pop.means, sub.pop.sd) )
x <- rep(predict.range, each = each.sub.pop.n)
plot(x, y)
## Unsuccessful Sampling ## The problem must be lying in here:
sampled <- lapply(split(y, x), function(z) sample(z, n.sample, replace = TRUE))
sampled <- data.frame(y = unlist(sampled),
x = rep(predict.range, each = n.sample))
plot(sampled$x, sampled$y)
【问题讨论】:
标签: r random sampling resampling