Skip to content

Instantly share code, notes, and snippets.

@debruine
Created May 15, 2019 13:57
Show Gist options
  • Save debruine/4f891a6f864ddeed161d678dfe8910cb to your computer and use it in GitHub Desktop.
Save debruine/4f891a6f864ddeed161d678dfe8910cb to your computer and use it in GitHub Desktop.
Power calculation for inattentive raters
library(faux)
library(tidyverse)
inatt <- function(within, between, n, mu, sd, r, prop_bad = 0) {
good_n <- n * (1-prop_bad)
bad_n <- n * prop_bad
good_dat <- sim_design(within, between, n = good_n, mu, sd, r)
if (bad_n > 0) {
# should r be 0 for inattentive responders?
bad_dat <- sim_design(within, between, n = bad_n, unlist(mu)%>% mean(), sd, r) %>%
dplyr::mutate(sub_id = make_id(nrow(.), "B"))
} else {
bad_dat <- good_dat[FALSE,]
}
all_dat <- dplyr::bind_rows(good_dat, bad_dat)
}
within <- list(
condition = c("pre", "post")
)
between <- list(
pet = c("dog", "cat")
)
mu <- list(
dog = c(pre = 100, post = 110),
cat = c(pre = 100, post = 120)
)
sd <- 30
r <- 0.5
sim_dat <- purrr::map_df(1:100, ~{
dat <- inatt(within, between, 150, mu, sd, r, .10) %>%
tidyr::gather(condition, val, pre, post)
suppressMessages(
analysis <- afex::aov_4(val ~ pet + (condition | sub_id), data = dat)
)
analysis$aov %>% broom::tidy()
})
alpha <- 0.05
sim_dat %>%
dplyr::filter(term != "Residuals") %>%
dplyr::group_by(term) %>%
dplyr::summarise(power = mean(p.value < alpha))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment