Hi! I was trying to solve this exercise:
and the code below is what I believed would be the best fit:
N <- 10000
m <- 1000
lambda <- 0.005
p <- 0.01
S_function = function(m,lambda,p){
number.claims = rpois(m,lambda)
n = sum(number.claims)
x = matrix(NA, nrow = m, ncol = n)
for (i in 1:m){
for (j in 1:n){
x[i,j] = rgeom(1,p)
}
#compute s_k
s = apply(x,1,sum)
}
#total claim
S = sum(s)
return(S)
}
S=array(NA, dim = N)
for (i in 1:N){
S[i] = S_function(m,lambda,p)
}
However, when I run it, I get the error "Error in [<-
(*tmp*
, i, j, value = rgeom(1, p)) : subscript out of bounds," which I suppose is caused by the change in the size of x at each iteration. How can I solve this? It's frustrating to understand that the exercise is simple, but I can't solve it.