Home > OS >  Randomization of vector and average mean
Randomization of vector and average mean

Time:08-17

I have a script that is calculating cumulative average and standard deviation:

library(tidyverse)
tibble(aa = c(2, 3, 4, 5, 6, 7, 8)) %>%
  mutate(
    running_mean = sapply(seq(n()), function(i) mean(aa[seq(i)])),
    running_sd = sapply(seq(n()), function(i) sd(aa[seq(i)])),
  )

I would like now to randomize my observations 'aa' 100 times and calculate just one average of 100 runs.

CodePudding user response:

Try the code below using replicate

replicate(
  5,
  tibble(aa = sample(c(2, 3, 4, 5, 6, 7, 8))) %>%
    mutate(
      running_mean = cumsum(aa) / seq_along(aa),
      running_sd = sqrt(1 / (1 - seq_along(aa)^(-1)) * (cumsum(aa^2) / seq_along(aa) - running_mean^2))
    ),
  simplify = FALSE
)

which gives, for example

[[1]]
# A tibble: 7 × 3
     aa running_mean running_sd
  <dbl>        <dbl>      <dbl>
1     6         6       NaN
2     5         5.5       0.707
3     3         4.67      1.53
4     7         5.25      1.71
5     8         5.8       1.92
6     2         5.17      2.32
7     4         5         2.16

[[2]]
# A tibble: 7 × 3
     aa running_mean running_sd
  <dbl>        <dbl>      <dbl>
1     8         8       NaN
2     7         7.5       0.707
3     5         6.67      1.53
4     6         6.5       1.29
5     4         6         1.58
6     2         5.33      2.16
7     3         5         2.16

[[3]]
# A tibble: 7 × 3
     aa running_mean running_sd
  <dbl>        <dbl>      <dbl>
1     3         3        NaN
2     6         4.5        2.12
3     7         5.33       2.08
4     2         4.5        2.38
5     8         5.2        2.59
6     5         5.17       2.32
7     4         5          2.16

[[4]]
# A tibble: 7 × 3
     aa running_mean running_sd
  <dbl>        <dbl>      <dbl>
1     8         8        NaN
2     3         5.5        3.54
3     5         5.33       2.52
4     6         5.5        2.08
5     2         4.8        2.39
6     7         5.17       2.32
7     4         5          2.16

[[5]]
# A tibble: 7 × 3
     aa running_mean running_sd
  <dbl>        <dbl>      <dbl>
1     8         8        NaN
2     5         6.5        2.12
3     6         6.33       1.53
4     2         5.25       2.5
5     3         4.8        2.39
6     4         4.67       2.16
7     7         5          2.16
  •  Tags:  
  • r
  • Related