Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 每次复制的平均值_R - Fatal编程技术网

R 每次复制的平均值

R 每次复制的平均值,r,R,我有以下代码 set.seed(30) nsim <- 50 ## NUMBER OF REPLICATIONS demand <- c(12,13,24,12,13,12,14,10,11,10) res <- replicate(nsim, { load <- runif(10,11,14) diff <- load - demand ## DIFFERENCE BETWEEN DEMAND AND LOAD return

我有以下代码

set.seed(30)
nsim <- 50    ## NUMBER OF REPLICATIONS
demand <- c(12,13,24,12,13,12,14,10,11,10)

res <- replicate(nsim, {
    load <- runif(10,11,14)
    diff <- load - demand    ## DIFFERENCE BETWEEN DEMAND AND LOAD 
    return(sum(diff < 0))
})
res
[1] 6 5 7 4 4 5 4 3 6 4 5 5 5 4 2 5 3 3 3 5 3 2 4 6 5 4 4 3 5 6 4 4 3 6 5 3 5 5 4 3 3
[42] 6 4 4 4 6 6 5 4 5
set.seed(30)

nsim我们可以得到结果的累积和,然后除以“res”序列

cumsum(res)/seq_along(res)
#[1] 6.000000 5.500000 6.000000 5.500000 5.200000 5.166667 5.000000 4.750000 4.888889 4.800000 4.818182 4.833333 4.846154 4.785714 4.600000 4.625000 4.529412
#[18] 4.444444 4.368421 4.400000 4.333333 4.227273 4.217391 4.291667 4.320000 4.307692 4.296296 4.250000 4.275862 4.333333 4.322581 4.312500 4.272727 4.323529
#[35] 4.342857 4.305556 4.324324 4.342105 4.333333 4.300000 4.268293 4.309524 4.302326 4.295455 4.288889 4.326087 4.361702 4.375000 4.367347 4.380000

或者使用
cummean
from
dplyr

library(dplyr)
cummean(res)
这两种解决方案都是矢量化的,应该是快速的