19.3: Simulación de potencia estadística
- Page ID
- 150713
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)
Simulemos esto para ver si el análisis de potencia realmente da la respuesta correcta. Tomaremos muestras de datos para dos grupos, con una diferencia de 0.5 desviaciones estándar entre sus distribuciones subyacentes, y veremos con qué frecuencia rechazamos la hipótesis nula.
nRuns <- 5000
effectSize <- 0.5
# perform power analysis to get sample size
pwr.result <- pwr.t.test(d=effectSize, power=.8)
# round up from estimated sample size
sampleSize <- ceiling(pwr.result$n)
# create a function that will generate samples and test for
# a difference between groups using a two-sample t-test
get_t_result <- function(sampleSize, effectSize){
# take sample for the first group from N(0, 1)
group1 <- rnorm(sampleSize)
group2 <- rnorm(sampleSize, mean=effectSize)
ttest.result <- t.test(group1, group2)
return(tibble(pvalue=ttest.result$p.value))
}
index_df <- tibble(id=seq(nRuns)) %>%
group_by(id)
power_sim_results <- index_df %>%
do(get_t_result(sampleSize, effectSize))
p_reject <-
power_sim_results %>%
ungroup() %>%
summarize(pvalue = mean(pvalue<.05)) %>%
pull()
p_reject
## [1] 0.8
Esto debería devolver un número muy cercano a 0.8.