-1

我正在运行一个循环运行大约 100 万次的抛硬币模拟。

每次运行循环时,我都希望保留 RLE 命令的表输出。不幸的是,一个简单的附加似乎并不合适。每次我运行循环时,我得到的数据量都略有不同,这似乎是症结之一。

这段代码让我知道我在做什么:

N <- 5 #Number of times to run
rlex <-NULL
#begin loop#############################
for (i in 1:N) { #tells R to repeat N number
x <-sample(0:1, 100000, 1/2)
rlex <-append(rlex, rle(x))
}
table(rlex) #doesn't work
table(rle(x)) #only 1

因此,我想要一个合并的 rle 表,而不是有五个单独的 rle 结果(在这个模拟中,完整版有 100 万个)。希望这很清楚。显然我的实际代码要复杂一些,因此任何解决方案都应该尽可能接近我指定的内容。

更新:循环是绝对要求。没有如果或但是。也许我可以提取 table(rle(x)) 数据并将其放入矩阵中。然而,同样的绊脚石是一些不太频繁的运行长度并不总是在每个循环中出现。因此,我想我希望根据运行长度有条件地填充矩阵?

我放弃之前的最后一次更新:保留 rle$values 将意味着保留了太多数据。我的模拟是大规模的,我真的只希望保留 rle 的表输出。要么我为每个循环保留每个表(rle(x))并手动组合(会有数千个),要么我找到一种编程方式来保留数据(对于零和一来说是)并有一个由以下组成的表在我进行的过程中合并每个单独的循环。

按照规定,这很容易做到,或者我不会这样做。这似乎是一个愚蠢的想法/要求,但这应该与是否可以完成无关。

上次是认真的。这是一个动画 gif,显示了我期望发生的事情。在此处输入图像描述

在循环数据的每次迭代之后,将数据添加到表中。这一点很清楚,我将能够传达它。

4

3 回答 3

7

Following up @CarlWitthoft's answer, you probably want:

N <- 5
rlex <-NULL
for (i in 1:N) {
    x <-sample(0:1, 100000, 1/2)
    rlex <-append(rlex, rle(x)$lengths)
}

since I think you don't care about the $values component (i.e. whether each run is a run of zeros or ones).

Result: one long vector of run lengths.

But this would probably be a lot more efficient:

maxlen <- 30
rlemat <- matrix(nrow=N,ncol=maxlen)
for (i in 1:N) { 
    x <-sample(0:1, 100000, 1/2)
    rlemat[i,] <- table(factor(rle(x)$lengths,levels=1:maxlen))
}

Result: an N by maxlen table of run lengths from each iteration.

If you only want to save the total number of runs of each length you could try:

rlecumsum <- rep(0,maxlen)
for (i in 1:N) { 
    x <-sample(0:1, 100000, 1/2)
    rlecumsum <- rlecumsum + table(factor(rle(x)$lengths,levels=1:maxlen))
}

Result: an vector of length maxlen of the total numbers of run lengths across all iterations.

And here's my final answer:

rlecumtab <- matrix(0,ncol=2,nrow=maxlen)
for (i in 1:N) { 
   x <- sample(0:1, 100000, 1/2)
   r1 <- rle(x)
   rtab <- table(factor(r1$lengths,levels=1:maxlen),r1$values)
   rlecumtab <- rlecumtab + rtab
}

Result: a maxlen by 2 table of the total numbers of run lengths across all iterations, divided by type (0-run vs 1-run).

于 2012-10-15T13:11:37.197 回答
7

好的,尝试第 4 次:

N <- 5
set.seed(1)
x <- NULL
for (i in 1:N){
  x <- rbind(x, table(rle(sample(0:1, 100000, replace=TRUE))))
}

x <- as.data.frame(x)
x$length <- as.numeric(rownames(x))
aggregate(x[, 1:2], list(x[[3]]), sum)

产生:

   Group.1     0     1
1        1 62634 62531
2        2 31410 31577
3        3 15748 15488
4        4  7604  7876
5        5  3912  3845
6        6  1968  1951
7        7   979   971
8        8   498   477
9        9   227   246
10      10   109   128
11      11    65    59
12      12    24    30
13      13    21    11
14      14     7    10
15      15     0     4
16      16     4     2
17      17     0     1
18      18     0     1

如果要在循环内进行聚合,请执行以下操作:

N <- 5
set.seed(1)
x <- NULL
for (i in 1:N){
  x <- rbind(x, table(rle(sample(0:1, 100000, replace=TRUE))))
  y <- aggregate(x, list(as.numeric(rownames(x))), sum)
  print(y)
}
于 2012-10-15T11:42:33.723 回答
6

您需要阅读帮助页面rle。考虑:

names(rlex)  #"lengths"  "values"  "lengths"  "values" .... and so on

同时,我强烈建议您花一些时间阅读统计方法。运行一百万次二项式模拟的机会为零(+/- epsilon),它会告诉您经过数百次尝试后您不会学到的任何东西,除非您的硬币有 p=1e-5 :-)。

于 2012-10-15T11:39:34.367 回答