I need to simulate specific scenarios 1,000 times (to get the mean of an estimated value). Additionally, various sample sizes need to be specified.
My R Code so far includes (an simpler) scenario which is repeated 1,000 for the sample size n=100. Now I need to run the code for sample size 101, 102, 103, …
I’m quite sure that a for-loop is needed, but I do not know how to write down.
As a result I want a data frame, containing a column with the sample size and a column with the mean of the 1,000 runs.
I would be very thankful, if anyone could help 🙂
My R Code:
simulation <- function(n){
id <- 1:n
trt <- rbinom(n, 1, 0.2)
x <- rnorm(n, 0 , 1)
data <- data.frame(id, trt, x)
Mean.1 <- aggregate(data[, 3], list(data$trt), mean)[1,2]
Mean.2 <- aggregate(data[, 3], list(data$trt), mean)[2,2]
s.1 <- aggregate(data[, 3], list(data$trt), sd)[1,2]
s.2 <- aggregate(data[, 3], list(data$trt), sd)[2,2]
n.1 <- table(data$trt)[1]
n.2 <- table(data$trt)[2]
#SMD
d_con <- (Mean.1 - Mean.2)/(sqrt((s.1^2+s.2^2)/2))
# z-difference
z_con <- (Mean.1 - Mean.2)/(sqrt(s.1^2/n.1+s.2^2/n.2))
tibble(d_con, z_con)
}
# Run simulation
results <- tibble()
for(i in 1:1000){
results <- bind_rows(results, simulation(100))}
# a data frame containing means of d_con and z_con per sample size
# I only have sample size n=100
sample_size <- c(100)
mean_d <- mean(results$d_con)
mean_z <- mean(results$z_con)
final <- data.frame(sample_size, mean_d, mean_z)