Home > database >  How to write a loop in R to perform a statistical test multiple times on two sets of randomly genera
How to write a loop in R to perform a statistical test multiple times on two sets of randomly genera

Time:02-15

Using R, I have two vectors a and b:

a <- rnorm(3, mean = 6, sd = 2)
b <- rnorm(3, mean = 8, sd = 2)

I want to generate a and b a specified number of times and each time perform a t.test comparing the mean of a and the mean of b

t.test(a, b, paired = FALSE)

How do I code repeating the rnorm and t.test functions a specified number of times and retrieving the full output of the t.test() or just the p.value

CodePudding user response:

To expand on your comment:

pvals <- replicate(20, t.test(rnorm(3, mean = 6, sd = 2), rnorm(3, mean = 8, sd = 2))$p.value)
sum(pvals < .05)
# [1] 2
hist(pvals)

To get all of the results

results <- replicate(20, t.test(rnorm(3, mean = 6, sd = 2), rnorm(3, mean = 8, sd = 2)), simplify=FALSE)
results[[1]]
# 
#   Welch Two Sample t-test
# 
# data:  rnorm(3, mean = 6, sd = 2) and rnorm(3, mean = 8, sd = 2)
# t = 0.052882, df = 2.6225, p-value = 0.9616
# alternative hypothesis: true difference in means is not equal to 0
# 95 percent confidence interval:
#  -8.880021  9.155836
# sample estimates:
# mean of x mean of y 
#  7.156974  7.019067 

With the other results being results[[2]], . . . , results[[20]]. The simplify=FALSE argument is needed to preserve the list structure of each run.

  • Related