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))
}
}