Atualiza com conteúdo da aula.

parent d7264228
...@@ -4,8 +4,6 @@ ...@@ -4,8 +4,6 @@
#----------------------------------------------------------------------- #-----------------------------------------------------------------------
# Cria a função com lista de todos os argumentos e use mapply(). # Cria a função com lista de todos os argumentos e use mapply().
library(microbenchmark)
# Curva de poder. # Curva de poder.
power_k_groups <- function(k = 3, power_k_groups <- function(k = 3,
r = 4, r = 4,
...@@ -17,37 +15,59 @@ power_k_groups <- function(k = 3, ...@@ -17,37 +15,59 @@ power_k_groups <- function(k = 3,
results <- replicate(simulations, { results <- replicate(simulations, {
b <- rnorm(k, mean = 0, sigma_group) b <- rnorm(k, mean = 0, sigma_group)
y <- rnorm(k * r, mean = X %*% b, sd = 1) y <- rnorm(k * r, mean = X %*% b, sd = 1)
anova(lm(y ~ X))[1, 5] anova(lm(y ~ trt))[1, 5]
}) })
mean(results <= alpha) mean(results <= alpha)
} }
# Criar o grid de condições (prototipar com um grid pequeno). # Criar o grid de condições (prototipar com um grid pequeno).
grid <- expand.grid(k = 3:5, grid <- expand.grid(k = c(3, 5, 8),
r = c(3, 5, 8), r = c(3, 8),
sigma_group = seq(0.01, 0.5, length.out = 10), sigma_group = seq(0.01, 2, length.out = 20),
KEEP.OUT.ATTRS = FALSE) KEEP.OUT.ATTRS = FALSE)
nrow(grid) nrow(grid)
grid
# Aplicar. # Aplicar.
grid$taxa <- mapply(FUN = power_k_groups, grid$taxa <- mapply(FUN = power_k_groups,
k = grid$k, k = grid$k,
r = grid$r, r = grid$r,
sigma_group = grid$sigma_group, sigma_group = grid$sigma_group,
MoreArgs = list(simulations = 10, MoreArgs = list(simulations = 100,
alpha = 0.05), alpha = 0.05),
SIMPLIFY = TRUE) 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. # Usando purrr.
library(purrr) library(purrr)
ls("package:purrr")
grid$taxa <- NULL
grid$taxa <- pmap_dbl(.l = grid, grid$taxa <- pmap_dbl(.l = grid,
.f = power_k_groups, .f = power_k_groups,
simulations = 10, simulations = 100,
alpha = 0.05) alpha = 0.05)
xyplot(taxa ~ sigma_group | ordered(r),
groups = k,
auto.key = TRUE,
data = grid,
type = "o")
#----------------------------------------------------------------------- #-----------------------------------------------------------------------
library(microbenchmark) library(microbenchmark)
...@@ -69,3 +89,5 @@ microbenchmark( ...@@ -69,3 +89,5 @@ microbenchmark(
alpha = 0.05) alpha = 0.05)
}, },
replications = 20) 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!
Please register or to comment