Commit d7264228 by Walmes Marques Zeviani

### Adiciona script com dicas para estudos de simulação.

parent b5e21aec
 #======================================================================= # Formas eficientes para fazer estudos de simulação/reamostragem. #----------------------------------------------------------------------- # Cria a função com lista de todos os argumentos e use mapply(). library(microbenchmark) # Curva de poder. power_k_groups <- function(k = 3, r = 4, sigma_group = 0.01, simulations = 50, alpha = 0.05) { trt <- gl(n = k, k = r) X <- model.matrix(~0 + trt) results <- replicate(simulations, { b <- rnorm(k, mean = 0, sigma_group) y <- rnorm(k * r, mean = X %*% b, sd = 1) anova(lm(y ~ X))[1, 5] }) mean(results <= alpha) } # Criar o grid de condições (prototipar com um grid pequeno). grid <- expand.grid(k = 3:5, r = c(3, 5, 8), sigma_group = seq(0.01, 0.5, length.out = 10), KEEP.OUT.ATTRS = FALSE) nrow(grid) # Aplicar. grid\$taxa <- mapply(FUN = power_k_groups, k = grid\$k, r = grid\$r, sigma_group = grid\$sigma_group, MoreArgs = list(simulations = 10, alpha = 0.05), SIMPLIFY = TRUE) #----------------------------------------------------------------------- # Usando purrr. library(purrr) grid\$taxa <- pmap_dbl(.l = grid, .f = power_k_groups, simulations = 10, alpha = 0.05) #----------------------------------------------------------------------- library(microbenchmark) microbenchmark( mapply = { mapply(FUN = power_k_groups, k = grid\$k, r = grid\$r, sigma_group = grid\$sigma_group, MoreArgs = list(simulations = 10, alpha = 0.05), SIMPLIFY = TRUE) }, pmap = { pmap_dbl(.l = grid, .f = power_k_groups, simulations = 10, alpha = 0.05) }, replications = 20)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!