Created
February 16, 2017 10:10
-
-
Save benwhalley/11d4beed85af5c9aad3e259935b4d413 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(lmerTest) | |
library(broom) | |
pwr <- function(N){ | |
simdata <- data_frame(person =1:N, c1=rnorm(N, 20, 25), c2 = rnorm(N, 77, 27)) %>% | |
reshape2::melt(id.var="person") | |
lmer(value~variable+(1|person), data=simdata) %>% summary tidy %>% | |
mutate(N=N) | |
} | |
pwrwarp <- function(){ | |
data_frame(i=2:10) %>% rowwise() %>% do(pwr(N=.$i)) %>% | |
filter(term=="variablec2") %>% | |
as_data_frame | |
} | |
sims <- data_frame(i=1:100) %>% rowwise() %>% do(pwrwarp()) | |
sims %>% | |
mutate(yes=statistic>1.96) %>% | |
group_by(N) %>% | |
summarise(pyes=mean(yes)) %>% | |
ggplot(aes(N, pyes)) + stat_smooth(se=F) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment