Search code examples
rreplication

R: Replicate Vector of Sample Instead of Just a Sample


I want to use R to do what I do to sample size of 10 to do the same to set of samples

What I have I got the below R code of replicating a Shapiro test for normality here

sim = replicate(1000,shapiro.test(rnorm(10)))
## rejections go like, assuming an alpha of 0.05 :
table(sim["p.value",] < 0.05)

Result of rejections go like, assuming an alpha of 0.05 :

FALSE TRUE
948 52

What I want

Instead of carrying the count test on just a sample size of 10 I want it carried out on a vector of samples say (10, 20,50, 100) with a result with something like this:

. FALSE TRUE
10 948 52
20 970 30
50 956 44
100 944 36

Solution

  • Use a loop - sapply and pass those values in rnorm

    out <- t(sapply(c(10, 20, 50, 100), function(x) 
        table(replicate(1000, shapiro.test(rnorm(x)))["p.value",] < 0.05)))
    row.names(out) <- c(10, 20, 50, 100)
    

    -output

    out
        FALSE TRUE
    10    953   47
    20    942   58
    50    960   40
    100   951   49