diff --git a/scripts/ce089-09.R b/scripts/ce089-09.R index 6e680d54b980bfe3d67bfadbfe87780eaf3d484e..535ddf227976c718e5b776e88310f5f7ffe95206 100644 --- a/scripts/ce089-09.R +++ b/scripts/ce089-09.R @@ -4,8 +4,6 @@ #----------------------------------------------------------------------- # 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, @@ -17,37 +15,59 @@ power_k_groups <- function(k = 3, 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] + anova(lm(y ~ trt))[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), +grid <- expand.grid(k = c(3, 5, 8), + r = c(3, 8), + sigma_group = seq(0.01, 2, length.out = 20), KEEP.OUT.ATTRS = FALSE) nrow(grid) +grid # Aplicar. grid\$taxa <- mapply(FUN = power_k_groups, k = grid\$k, r = grid\$r, sigma_group = grid\$sigma_group, - MoreArgs = list(simulations = 10, + MoreArgs = list(simulations = 100, alpha = 0.05), SIMPLIFY = TRUE) +#----------------------------------------------------------------------- + +library(latticeExtra) + +xyplot(taxa ~ sigma_group | ordered(r), + groups = k, + auto.key = TRUE, + data = grid, + type = "o") + +apropos("power") +apropos("write") + #----------------------------------------------------------------------- # Usando purrr. library(purrr) +ls("package:purrr") +grid\$taxa <- NULL grid\$taxa <- pmap_dbl(.l = grid, .f = power_k_groups, - simulations = 10, + simulations = 100, alpha = 0.05) +xyplot(taxa ~ sigma_group | ordered(r), + groups = k, + auto.key = TRUE, + data = grid, + type = "o") + #----------------------------------------------------------------------- library(microbenchmark) @@ -69,3 +89,5 @@ microbenchmark( alpha = 0.05) }, replications = 20) + +#-----------------------------------------------------------------------