Home > front end >  Simulations of Poisson Distributions with different sample sizes and lambdas
Simulations of Poisson Distributions with different sample sizes and lambdas

Time:10-10

Let's start with a statistical model, X where X is a random Poisson distribution with parameter
k - lambda with k being a constant

X ~ Pois(k - lambda)

Now,assume that k = 20. How do I create a function or make use of for loops to run a simulation where we have different values of lambda <- c(2, 4, 8, 16) and each lambda has different sample sizes, n = [1,25] (from n = 1 to n = 25 ).

n <- 1:10
k <- 20 
lambda <- c(2, 4, 8, 16)
result <- rpois(n, k - lambda)
result

The output:

28 12 13 1 13 16 16 3 12 15

Now obviously, my code is wrong because it is not giving me the right output. For any lambda values there should be output for each sample size from n=1, n=2, n=3 and up to n = 25. My idea is to use a double for loop in order to create this. A for loop for the changing sample size,n and another for loop for the changing lambda values but I'm not too sure how to implement this.

The expected output should be something like this. For lambda = 8,

11
12,11
13,11,14
11,14,14,14
......
10 9 13 13 13 11 8 17 10 11 13 11 17 13 9 8 13 15 10 10 15 14 14 15 9

CodePudding user response:

You can Vectorize rpois and put it in outer.

n <- 1:10
k <- 20 
lambdas <- c(2, 4, 8, 16)

set.seed(42)
res <- outer(n, k - lambdas, Vectorize(rpois)) |> apply(2, as.list)

Gives

res |> setNames(paste0('lambda_', lambdas))
# $lambda_2
# $lambda_2[[1]]
# [1] 11
# 
# $lambda_2[[2]]
# [1] 16 16
# 
# $lambda_2[[3]]
# [1] 16 23 17
# 
# $lambda_2[[4]]
# [1] 19 14 18 13
# 
# $lambda_2[[5]]
# [1] 23 12 17 17 14
# 
# $lambda_2[[6]]
# [1] 13 14 12 13 13 15
# 
# $lambda_2[[7]]
# [1] 13 18 24 13 10 15 21
# 
# $lambda_2[[8]]
# [1] 17 33 14 19 16 23 19 12
# 
# $lambda_2[[9]]
# [1] 15 21 10 16 15 19 28 23 17
# 
# $lambda_2[[10]]
# [1] 28 20 22 29 17 16 17 15 18 21
# 
# 
# $lambda_4
# $lambda_4[[1]]
# [1] 15
# 
# $lambda_4[[2]]
# [1] 15 17
# 
# $lambda_4[[3]]
# [1] 19 11 14
# 
# $lambda_4[[4]]
# [1] 16 18 18 15
# 
# $lambda_4[[5]]
# [1] 15 13 16 11 18
# 
# $lambda_4[[6]]
# [1] 11 16 15 23 12 18
# 
# $lambda_4[[7]]
# [1] 15 10 18 14 12 15 13
# 
# $lambda_4[[8]]
# [1] 20 14 20 22 19 11 17 20
# 
# $lambda_4[[9]]
# [1]  9 22 15 16 18 18 13 20 14
# 
# $lambda_4[[10]]
# [1] 19 14 22 14 19 15 17 22 21 15
# 
# 
# $lambda_8
# $lambda_8[[1]]
# [1] 13
# 
# $lambda_8[[2]]
# [1] 15 12
# 
# $lambda_8[[3]]
# [1] 17 11 14
# 
# $lambda_8[[4]]
# [1] 10  7  8  8
# 
# $lambda_8[[5]]
# [1] 20  8 13 11 12
# 
# $lambda_8[[6]]
# [1]  7 14 16 14 13 10
# 
# $lambda_8[[7]]
# [1] 13 10 11 15 13 12 11
# 
# $lambda_8[[8]]
# [1] 15 16  8  8  9 16 13 13
# 
# $lambda_8[[9]]
# [1]  7  9  6  9  6  4 12 13 26
# 
# $lambda_8[[10]]
# [1] 12  9  8 10 13 12 11 18 10 10
# 
# 
# $lambda_16
# $lambda_16[[1]]
# [1] 1
# 
# $lambda_16[[2]]
# [1] 2 4
# 
# $lambda_16[[3]]
# [1] 3 6 6
# 
# $lambda_16[[4]]
# [1] 1 6 3 5
# 
# $lambda_16[[5]]
# [1] 2 4 7 4 7
# 
# $lambda_16[[6]]
# [1] 5 5 6 7 2 2
# 
# $lambda_16[[7]]
# [1] 2 6 6 3 4 4 3
# 
# $lambda_16[[8]]
# [1] 3 7 3 1 5 5 2 1
# 
# $lambda_16[[9]]
# [1] 3 0 4 7 3 3 4 2 3
# 
# $lambda_16[[10]]
# [1]  3  7  7  5  5 11  4  2  2  6
  • Related