如何在R中更快地使数据随机化?

时间:2014-05-08 03:48:21

标签: r

我有非常大的数据集,我为它计算了数千种模型。对于每个模型,我需要将我的数据随机化100次。这个随机化部分使我的脚本非常慢。 有人会帮助我加快这一步吗?

这是我的代码:

    for (l in seq(repeat.times)) {


    y <- as.matrix(dfr[1])
    x <- as.matrix(dfr[2:ncol(dfr)])
        # Random Generation

                x.random.name = sample(colnames(x),1,replace=FALSE)
                x.random.1 <- sample(x[,x.random.name],nrow(y),replace=FALSE)
                x <- cbind(x,x.random.1)

.
.
.

例如:

> x
     A B  C  D  E
[1,] 1 5  9 13 17
[2,] 2 6 10 14 18
[3,] 3 7 11 15 19
[4,] 4 8 12 16 20

> y
     [,1]
[1,]   10
[2,]   20
[3,]   30
[4,]   40

随机化后:

> x
     A B  C  D  E x.random.1
[1,] 1 5  9 13 17         10
[2,] 2 6 10 14 18         12
[3,] 3 7 11 15 19          9
[4,] 4 8 12 16 20         11
> 

3 个答案:

答案 0 :(得分:2)

如果我正确理解OP的要求,这种方式会更快

x
##      A B  C  D  E
## [1,] 1 5  9 13 17
## [2,] 2 6 10 14 18
## [3,] 3 7 11 15 19
## [4,] 4 8 12 16 20

y
##      [,1]
## [1,]   10
## [2,]   20
## [3,]   30
## [4,]   40

xncol <- ncol(x)
ynrow <- nrow(y)

require(microbenchmark)
microbenchmark(xrand <- sapply(1:100, FUN = function(iter) {
    sample(x[, sample(1:xncol, 1)], ynrow)
}), times = 1L)
## Unit: milliseconds
##                                                                                         expr      min
##  xrand <- sapply(1:100, FUN = function(iter) {     sample(x[, sample(1:xncol, 1)], ynrow) }) 1.083906
##        lq   median       uq      max neval
##  1.083906 1.083906 1.083906 1.083906     1



x <- cbind(x, xrand)

x
##      A B  C  D  E                                                                                           
## [1,] 1 5  9 13 17 8 16 2 18 5 3 10 10 14  9 19 6 6 15 18 2 13 13 15 18 7 20 17 11 13 1 16 1 20 1  9 19 14 20
## [2,] 2 6 10 14 18 7 14 3 20 8 4 12  9 13 10 20 8 8 13 20 1 14 15 16 20 6 19 19 10 16 2 15 4 17 4 12 20 15 19
## [3,] 3 7 11 15 19 5 15 1 19 7 2 11 12 15 11 18 7 7 14 17 4 15 16 14 19 8 17 18  9 14 4 14 2 18 3 11 18 16 17
## [4,] 4 8 12 16 20 6 13 4 17 6 1  9 11 16 12 17 5 5 16 19 3 16 14 13 17 5 18 20 12 15 3 13 3 19 2 10 17 13 18
##                                                                                                             
## [1,] 5 13 2 3 5 2 5 8 4 6 19 3 7 19 4 7 6 4 17  9 18  9 5 3 1 15 8 19 19 3 19 15 15 1 1 10 15 19 11 6 5 17 7
## [2,] 7 15 1 1 7 1 6 6 3 8 18 2 6 17 2 6 5 3 18 10 17 11 8 1 3 13 6 17 18 4 17 16 13 4 3 11 16 18  9 8 8 18 6
## [3,] 8 14 3 2 8 3 8 7 2 7 20 1 8 18 3 8 8 1 20 12 19 10 6 2 2 16 5 20 17 2 18 13 16 3 4 12 13 20 12 7 7 20 8
## [4,] 6 16 4 4 6 4 7 5 1 5 17 4 5 20 1 5 7 2 19 11 20 12 7 4 4 14 7 18 20 1 20 14 14 2 2  9 14 17 10 5 6 19 5
##                                                                  
## [1,] 3 3 15 19 2 12 16 11 18 7 10 11 5 12 12 10 1 2 19 2 16 17 11
## [2,] 4 2 13 20 1 11 15 12 17 5 11 12 6 10  9 11 4 3 18 3 14 19  9
## [3,] 1 4 16 18 4 10 14  9 19 8 12  9 8 11 11  9 3 4 20 4 13 20 12
## [4,] 2 1 14 17 3  9 13 10 20 6  9 10 7  9 10 12 2 1 17 1 15 18 10

关键步骤是当然,我已经用microbenchmark包装纯粹用于基准测试目的。

xrand <- sapply(1:100, FUN = function(iter) {     sample(x[, sample(1:xncol, 1)], ynrow) })

答案 1 :(得分:2)

这是一个单行:

# Data
x<-matrix(1:10^4,nrow=10)
# Generate 2000 replicates.
replicate(2000,x[order(runif(nrow(x))),sample(ncol(x),1)])

甚至只是:

replicate(2000,sample(x[,sample(ncol(x),1)]))

答案 2 :(得分:1)

我发现你可以通过在循环外移动x和y来大大减少运行时间。然后你可以在循环中创建一个新的变换矩阵

y <- as.matrix(dfr[1])
XX <- as.matrix(dfr[2:ncol(dfr)])

for (l in seq(repeat.times)) {
    # Random Generation
    x.random.name = sample(colnames(x),1,replace=FALSE)
    x.random.1 <- sample(XX[,x.random.name],nrow(y),replace=FALSE)
    x <- cbind(XX,x.random.1)
}

所以我搬出了x并将其重命名。然后,当您进行分析时,您将继续使用新制作的x。我发现my benchmark这个速度提高了近两个数量级。