0

I am creating a simulation where n2 represents the sample size in the original dataset (a total of 800 observations), and probability represents the calculated proportion for each row (there are 19 rows in total). I would like to generate a value from 1 to 19 based on the probability for each row and calculate the average value obtained from all simulations. However, my code is not working. The probability within the loop does not take all the values from the column. Please help me. Thank you.

ss$pr<-(ss$def/s$n)*100

n2<-ss$n
def<-ss$def
sample_size <- 19  # 
num_trials <- 100000
probability <- ss$pr/100

for (i in 1:sample_size) { 
for (j in 1:length(ss$pr)) { 
probability <- ss$pr[j] / 100
 monte_carlo_sample <- rbinom(n = num_trials, size = n2, prob = probability) 
df2 <- data.table(n1 = c(1:length(monte_carlo_sample)), Class= c(19:1), a =monte_carlo_sample) 
 group <- df2 %>% group_by(Class) %>% summarise(gr = mean(a)) 

}
 }
Olesia
  • 1
  • 1
  • 3
    without a minimal reproducible example it's hard to say (we don't have your `ss` or `s` objects, for example), but one thing that stands out is that you reassign the "group" object each loop, overwriting the previous one. I would either preallocate `group` as a list then store the results at location `group[i]` (for example) or look at using `lapply()` instead of a `for` loop. – Paul Stafford Allen Jul 10 '23 at 09:48

0 Answers0