在R

时间:2018-10-12 10:59:19

标签: r

即使我认为我遇到的问题可能很简单,但我仍然无法弄清楚。这是东西:

我有以下列表和载体。该列表用于填充矢量:

probabilities = list(c(0.2, 0.3, 0.5), c(0.1, 0.1, 0.8), c(0.3,0.4,0.3))
nextState = c()

for(counter in 1:3){
nextState[counter] = sample(1:3, size = 1, prob = probabilities[[counter]])
}

代码工作正常。但是,当扩展到更大的列表(> 10,000个元素)时,循环会变得越来越慢。由于上面的循环在较大的代码中多次使用,因此所花费的时间太多了。有没有一种方法可以达到相同的结果而无需循环?

其他问题:

谢谢大家,您的帮助很大。另一个问题:如果概率和nextState是相互依存的,将如何处理相同的问题含义,如何避免for循环?也许需要澄清一些代码:

M <- list(matrix(c(0.1, 0.2, 0.7, 0.2, 0.2, 0.6, 0.3, 0.3, 0.4), nrow = 3, ncol = 3), 
         matrix(c(0.3, 0.3, 0.4, 0.5, 0.5, 0, 0.1, 0.1, 0.8), nrow = 3, ncol = 3))

probabilities <- list()
nextState <- c(2, NA, NA)

for(i in 1:2){
probabilities[[i]] <- M[[i]][nextState[i], ]
nextState[i + 1] <- sample(1:3, size = 1, prob = probabilities[[i]])
}

如果您有任何想法,那么您确实是奇迹工作者!

2 个答案:

答案 0 :(得分:1)

尝试尝试

nextstate <- sapply( probabilities, function(x) {sample(1:3, size = 1, prob = x)})

基准

# Unit: microseconds
#   expr      min       lq      mean    median       uq      max neval
#    for 2115.170 2223.475 2436.0797 2283.2755 2371.546 10048.64   100
# sapply   24.704   29.524  164.0261   37.3565   41.123 12763.03   100


microbenchmark::microbenchmark(
  `for` = { 
    nextState = c()
    for(counter in 1:3){
      nextState[counter] = sample(1:3, size = 1, prob = probabilities[[counter]])
    }
  },
  sapply = sapply( probabilities, function(x) {sample(1:3, size = 1, prob = x)}),
  times = 100)

答案 1 :(得分:0)

使用purrr软件包的另一种可能性:

library(purrr)

nexstate <- map_int(probabilities, function(x) {sample(1:3, size = 1, prob = x)})

数据:

probabilities = list(c(0.2, 0.3, 0.5), c(0.1, 0.1, 0.8), c(0.3,0.4,0.3))