Created
February 16, 2017 10:35
-
-
Save benwhalley/43e8f18b10500fb12efddd5b93ee28cb 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) | |
mu_type1 <- 20 | |
mu_type2 <- 70 | |
sd_both <- 20 | |
pwr <- function(N, mu_type1, mu_type2, sd_both){ | |
simdata <- data_frame( | |
person = 1:N, c1=rnorm(N, mu_type1, sd_both), | |
c2 = rnorm(N, mu_type2, sd_both) | |
) %>% | |
reshape2::melt(id.var="person") | |
lmer(value~variable+(1|person), data=simdata) %>% | |
tidy %>% | |
mutate(N=N, mu_type1=mu_type1, mu_type2=mu_type2, sd_both=sd_both ) | |
} | |
pwrwrapper <- function(parms){ | |
parms %>% | |
as.data.frame %>% | |
rowwise() %>% | |
do(pwr(N =.$N, mu_type1=.$mu_type1, mu_type2=.$mu_type2, sd_both=.$sd_both)) %>% | |
filter(term=="variablec2") %>% | |
mutate(sigresult=statistic>1.96) | |
} | |
simparms <- expand.grid(N=5:15, mu_type1=25, mu_type2=seq(30,70,5), sd_both=20) | |
sims <- data_frame(i=1:50) %>% rowwise() %>% do(pwrwrapper(simparms)) | |
sims %>% | |
group_by(N, mu_type1, mu_type2) %>% | |
summarise(psigresults=mean(sigresult)) %>% | |
ggplot(aes(mu_type2, psigresults)) + geom_smooth(se=F)+ facet_grid(N~mu_type1) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment