来自序列的R样本逐步与复制,结果不同

时间:2016-03-08 14:39:35

标签: r replication sample

编辑(根据要求完全修改了问题)

从序列中逐步采样一个索引与对整个序列进行采样时,我会遇到一些意外行为。如果我设置一次种子

set.seed(123)

并执行

sample(c(0.9,0.95,1,1.01,1.02,1.03,1.04,1.05))

我得到了例如。

[1] 1.03 0.90 1.02 1.00 0.95 1.04 1.05 1.01  
[1] 1.05 0.95 1.01 1.04 0.90 1.00 1.03 1.02   
[1] 0.90 1.04 1.01 1.05 1.00 0.95 1.03 1.02   

但是,如果我反复执行(通常,例如100次)

sample(c(0.9,0.95,1,1.01,1.02,1.03,1.04,1.05))[3]

R永远不会采样任何东西,只有0.9,0.95,1或1.0。我也改变了种子,但行为是一样的。我错过了什么?

R版本3.1.3(2015-03-09)
平台:x86_64-w64-mingw32 / x64(64位)

2 个答案:

答案 0 :(得分:1)

没有重复:

> set.seed(123)
> sample(seq(from = 0.9, to = 1.1, by = 0.01), size=1, replace=T)[1]
[1] 0.96
> sample(seq(from = 0.9, to = 1.1, by = 0.01), size=1, replace=T)[1]
[1] 1.06
> sample(seq(from = 0.9, to = 1.1, by = 0.01), size=1, replace=T)[1]
[1] 0.98
> sample(seq(from = 0.9, to = 1.1, by = 0.01), size=1, replace=T)[1]
[1] 1.08
> sample(seq(from = 0.9, to = 1.1, by = 0.01), size=1, replace=T)[1]
[1] 1.09
> sample(seq(from = 0.9, to = 1.1, by = 0.01), size=1, replace=T)[1]
[1] 0.9
> sample(seq(from = 0.9, to = 1.1, by = 0.01), size=1, replace=T)[1]
[1] 1.01
> sample(seq(from = 0.9, to = 1.1, by = 0.01), size=1, replace=T)[1]
[1] 1.08
> sample(seq(from = 0.9, to = 1.1, by = 0.01), size=1, replace=T)[1]
[1] 1.01
> sample(seq(from = 0.9, to = 1.1, by = 0.01), size=1, replace=T)[1]
[1] 0.99

> set.seed(123)
> replicate(10,sample(seq(from = 0.9, to = 1.1, by = 0.01), size=1, replace=T))
 [1] 0.96 1.06 0.98 1.08 1.09 0.90 1.01 1.08 1.01 0.99

replicate完全相同的值列表(如预期的那样)只是sapply的包装:

> replicate
function (n, expr, simplify = "array") 
sapply(integer(n), eval.parent(substitute(function(...) expr)), 
    simplify = simplify)

通过一个小测试,我可以找到一个种子来复制你的问题(我认为):

for(i in 1000:2000) { 
  set.seed(i)
  if( all(replicate(10,sample(seq(from = 0.9, to = 1.1, by = 0.01), size=1, replace=T)) < 1 )) { 
    print(i)
    break
  }
}

给我1887年左右:

> set.seed(1887)
> sample(seq(from = 0.9, to = 1.1, by = 0.01), size=1, replace=T)[1]
[1] 0.99
> sample(seq(from = 0.9, to = 1.1, by = 0.01), size=1, replace=T)[1]
[1] 0.92
> sample(seq(from = 0.9, to = 1.1, by = 0.01), size=1, replace=T)[1]
[1] 0.96
> sample(seq(from = 0.9, to = 1.1, by = 0.01), size=1, replace=T)[1]
[1] 0.99
> sample(seq(from = 0.9, to = 1.1, by = 0.01), size=1, replace=T)[1]
[1] 0.95
> sample(seq(from = 0.9, to = 1.1, by = 0.01), size=1, replace=T)[1]
[1] 0.99
> sample(seq(from = 0.9, to = 1.1, by = 0.01), size=1, replace=T)[1]
[1] 0.96
> sample(seq(from = 0.9, to = 1.1, by = 0.01), size=1, replace=T)[1]
[1] 0.93
> sample(seq(from = 0.9, to = 1.1, by = 0.01), size=1, replace=T)[1]
[1] 0.94
> sample(seq(from = 0.9, to = 1.1, by = 0.01), size=1, replace=T)[1]
[1] 0.99
> replicate(10,sample(seq(from = 0.9, to = 1.1, by = 0.01), size=1, replace=T))
 [1] 1.07 1.06 0.97 1.07 1.00 0.99 0.91 1.01 1.05 0.97

答案 1 :(得分:0)

问题是在数字约束下发生的序列创建(选项(&#34;数字&#34; = 2))。请在此处查看答案"R seq function produces wrong result"