Home > Mobile >  R - Creating Multiple Confidence Intervals for a Simulation Study
R - Creating Multiple Confidence Intervals for a Simulation Study

Time:10-08

So, I am trying to create a simulation study (random data, so it can be replicated by anyone by just copying and pasting the code). Here is the code I wrote to create a 95% confidence interval:

set.seed(10)
ransamp <- rnorm(30, 100, 5)
x.bar <- mean(ransamp)
n <- length(ransamp)
me.95 <- qt(0.05/2, df = (n-1), lower.tail = FALSE) * sd(ransamp) / sqrt(n)
ci.95 <- c(x.bar - me.95, x.bar   me.95)
ci.95

This works perfectly fine. However, I am trying to figure out a way to have this run 10 times (creating 10 different confidence intervals) using a for loop. Does anyone have any ideas as to how to go about this? I tried a few things, including:

matrix1 <- rep(NA, 10)
ransamp <- rnorm(30, 100, 5)
x.bar <- mean(ransamp)
n <- length(ransamp)
me.95 <- qt(0.05/2, df = (n-1), lower.tail = FALSE) * sd(ransamp) / sqrt(n)
ci.95 <- c(x.bar - me.95, x.bar   me.95)
loop1 <- for(i in 1:10){
 matrix1[i] <- ci.95
}
loop1

However, this code does not work. I am not entirely sure of the proper syntax here. I keep scrapping the code entirely a starting over, but none of my ideas so far have worked.

Any help here is appreciated.

Edit: Instead of posting an answer, I am putting the code that worked here. Thank you, everyone, for your help!

matrix1 <- matrix(NA, nrow = 10, ncol = 2)
set.seed(10)
loop1 <- for(i in 1:10){
  ransamp <- rnorm(30, 100, 5)
  x.bar <- mean(ransamp)
  n <- length(ransamp)
  me.95 <- qt(0.05/2, df = (n-1), lower.tail = FALSE) * sd(ransamp) / sqrt(n)
  ci.95 <- c(x.bar - me.95, x.bar   me.95)
  matrix1[i, 1] <- x.bar - me.95
  matrix1[i, 2] <- x.bar   me.95
}
matrix1

CodePudding user response:

You may use replicate to repeat the same function any number of times.

set.seed(10)
return_ci <- function() {
  ransamp <- rnorm(30, 100, 5)
  x.bar <- mean(ransamp)
  n <- length(ransamp)
  me.95 <- qt(0.05/2, df = (n-1), lower.tail = FALSE) * sd(ransamp) / sqrt(n)
  ci.95 <- c(x.bar - me.95, x.bar   me.95)
  ci.95
}
t(replicate(10, return_ci()))

#          [,1]      [,2]
# [1,] 96.65816  99.89507
# [2,] 97.66593 101.20540
# [3,] 98.07096 101.80158
# [4,] 99.09544 102.54808
# [5,] 97.58089 101.32755
# [6,] 97.27095 100.82177
# [7,] 97.52241 101.32308
# [8,] 97.37558 100.84174
# [9,] 97.81874 101.59072
#[10,] 99.65918 103.16812

CodePudding user response:

Those code to make ci.95 need to be inside of for loop. You may try this way.

res <- c()
for(i in 1:10){
  ransamp <- rnorm(30, 100, 5)
  x.bar <- mean(ransamp)
  n <- length(ransamp)
  me.95 <- qt(0.05/2, df = (n-1), lower.tail = FALSE) * sd(ransamp) / sqrt(n)
  ci.95 <- c(x.bar - me.95, x.bar   me.95)
  res <- c(res,ci.95)
} 
res %>% matrix(.,ncol = 2, byrow = T) %>% as.data.frame() %>% rename(lower = V1, upper = V2)

      lower    upper
1  99.29483 102.7386
2  96.80608 100.2073
3  97.31107 101.4798
4  98.26812 102.2788
5  99.02372 102.1647
6  98.73495 102.2814
7  98.49130 101.8332
8  99.08703 102.7505
9  97.06707 101.3432
10 97.96623 101.8470

CodePudding user response:

We may use rerun

library(purrr)
library(dplyr)
f1 <- function() {
 ransamp <- rnorm(30, 100, 5)
  x.bar <- mean(ransamp)
  n <- length(ransamp)
  me.95 <- qt(0.05/2, df = (n-1), lower.tail = FALSE) * sd(ransamp) / sqrt(n)
  ci.95 <- tibble(lower = x.bar - me.95, upper = x.bar   me.95)
  ci.95
}

-testing

rerun(10, f1()) %>%
    bind_rows
# A tibble: 10 × 2
   lower upper
   <dbl> <dbl>
 1  98.1  102.
 2  99.2  102.
 3  98.1  102.
 4  98.0  102.
 5  99.0  103.
 6  98.7  102.
 7  99.0  102.
 8  98.1  101.
 9  97.7  101.
10 101.   104.
  •  Tags:  
  • r
  • Related