Simulation and parallelization

 

Provide the code that parallelizes the following:
library(MKinfer) # Load package used for permutation t-test

# Create a function for running the simulation:

simulate_type_I <- function(n1, n2, distr, level = 0.05, B = 999,alternative = “two.sided”, …)

{

# Create a data frame to store the results in:

p_values <- data.frame(p_t_test = rep(NA, B),p_perm_t_test = rep(NA, B),p_wilcoxon = rep(NA, B))

for(i in 1:B)

{

# Generate data:

x <- distr(n1, …)

y <- distr(n2, …)

# Compute p-values:

p_values[i, 1] <- t.test(x, y,

alternative = alternative)$p.value

p_values[i, 2] <- perm.t.test(x, y,alternative = alternative,R = 999)$perm.p.value

p_values[i, 3] <- wilcox.test(x, y,alternative = alternative)$p.value

}

# Return the type I error rates:

return(colMeans(p_values < level))

}

2. Provide the code that runs the following code in parallel with 4 workers (with mclapply):

lapply(airquality, function(x) { (x-mean(x))/sd(x) })

This question has been answered.

Get Answer