【问题标题】:Sampling in the wrong direction R采样方向错误 R
【发布时间】:2017-10-16 08:04:57
【问题描述】:

背景

我有两个变量,分别称为 xy请查看图片下方的 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


    【解决方案1】:

    这足以说明原因。

    x <- 10:0; y <- 10:0
    

    你有没有注意到

    split(y, x)
    

    排序列表?要获得所需的排序,请控制因素水平:

    split(y, factor(x, levels = unique(x))
    

    在您的上下文中,您可以在没有unique 的情况下高效使用:

    split(y, factor(x, levels = predict.range))
    

    【讨论】:

    • 哲元,我的抽样问题和我昨天问的这个问题有关。我可以在这里使用:sampled &lt;- lapply(split(y, x), function(z) sample(z, n.sample, prob = dnorm(x, sub.pop.means, sub.pop.sd), replace = TRUE))
    • 我们在这里谈谈吧!看我上面的评论返回错误!
    • 除了这个抽样之外,还有更有趣的问题。显然,样本回归线从未如此偏离总体回归线!
    猜你喜欢
    • 1970-01-01
    • 2013-03-25
    • 2019-09-22
    • 2012-06-25
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多