Calculating the mean of every replication

前端 未结 2 1308
旧巷少年郎
旧巷少年郎 2021-01-29 15:26

I have the following code

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

res <- replicate(nsim, {
             


        
相关标签:
2条回答
  • 2021-01-29 15:46

    To illustrate my comment, you can generate a matrix where columns (or rows, if you prefer) represent replications, after which you can use R's matrix operations capabilities:

    set.seed(47)    # make reproducible
    
    nsim <- 50    ## NUMBER OF REPLICATIONS
    demand <- c(12,13,24,12,13,12,14,10,11,10)
    
    loads <- matrix(runif(10 * nsim, 11, 14), ncol = nsim)
    
    diffs <- loads - demand    # with vector recycling
    # or: diffs <- apply(loads, 2, `-`, demand)    
    # or: diffs <- apply(loads, 2, function(x){x - demand})
    
    res <- colSums(diffs > 0)
    LOLE <- sum(res) / nsim
    
    LOLE
    #> [1] 5.7
    
    0 讨论(0)
  • 2021-01-29 16:03

    In the edited version of the question (edit of Feb 11 at 5:53), the OP has specified the expected result. These indicate that the OP might be looking for a cumulative mean of the result vector 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
    #[10] 4.800000 4.818182 4.833333 4.846154 4.785714 4.600000 4.625000 4.529412 4.444444
    #[19] 4.368421 4.400000 4.333333 4.227273 4.217391 4.291667 4.320000 4.307692 4.296296
    #[28] 4.250000 4.275862 4.333333 4.322581 4.312500 4.272727 4.323529 4.342857 4.305556
    #[37] 4.324324 4.342105 4.333333 4.300000 4.268293 4.309524 4.302326 4.295455 4.288889
    #[46] 4.326087 4.361702 4.375000 4.367347 4.380000
    

    Alternatively, dplyr::cummean(res) can be used.

    0 讨论(0)
提交回复
热议问题