Simulation with reported differences between conditions

n_test_trials = 8 # Each child took part in 8 test trials
#n_exposure_trials = 7 # Each child took part in 7 exposure trials
n_children = 1:50 # There were 48 3-year-old, 49 4-year-old, and 43 5-year-old children

noisy_channel = expand.grid(
  
  n_test_trials = n_test_trials,
  N = n_children,
  Age = c("3years", "4years", "5years"),
  Condition = c("plausible", "implausible"),
  Simulations = 1:20
  )
noisy_channel = 
  noisy_channel %>%
  mutate(mean3 = ifelse(Age == "3years",
                 ifelse(Condition == "plausible", 0.51,0.49),
                 ifelse(Condition == "plausible", NA, NA)),
           sd3 = ifelse(Age == "3years",
                 ifelse(Condition == "plausible", 0.10,0.12),
                 ifelse(Condition == "plausible", NA, NA)))%>%
  mutate(mean4 = ifelse(Age == "4years",
                 ifelse(Condition == "plausible", 0.60,0.40),
                 ifelse(Condition == "plausible", NA, NA)),
          sd4 =  ifelse(Age == "4years",
                 ifelse(Condition == "plausible", 0.11,0.10),
                 ifelse(Condition == "plausible", NA, NA)))%>%
  mutate(mean5 = ifelse(Age == "5years",
                 ifelse(Condition == "plausible", 0.54, 0.25),
                 ifelse(Condition == "plausible", NA, NA)),
          sd5 =  ifelse(Age == "5years",
                 ifelse(Condition == "plausible", 0.12, 0.11),
                 ifelse(Condition == "plausible", NA, NA)))

noisy_channel$mean = rowMeans(noisy_channel[, c("mean3", "mean4", "mean5")], na.rm=TRUE)
noisy_channel$sd = rowMeans(noisy_channel[, c("sd3", "sd4", "sd5")], na.rm=TRUE)
noisy_channel[6:11] = list(NULL)

noisy_channel = 
  noisy_channel %>%
group_by(Age, Condition,n_test_trials,N,Simulations) %>% 
  do(subject_prob = rnorm(.$N,.$mean,.$sd)) %>%
  unnest(subject_prob)%>%
  mutate(subject_prob = replace(subject_prob ,subject_prob <0,0.01),
         subject_prob = replace(subject_prob ,subject_prob >1,0.99))%>%
group_by(Age,Condition,n_test_trials,N,Simulations,subject_prob) %>% 
  do(data = rbinom(n = 1, size = .$n_test_trials, prob = .$subject_prob)/.$n_test_trials) %>%
  unnest(data)

noisy_channel_means = noisy_channel %>%
  group_by(Condition, Age) %>%
  summarize(Mean = mean(data), SE = sd(data)/sqrt(n()))
print(noisy_channel)
## # A tibble: 153,000 x 7
##       Age Condition n_test_trials     N Simulations subject_prob  data
##    <fctr>    <fctr>         <dbl> <int>       <int>        <dbl> <dbl>
##  1 3years plausible             8     1           1    0.4752501 0.750
##  2 3years plausible             8     1           2    0.4192129 0.500
##  3 3years plausible             8     1           3    0.5105411 0.500
##  4 3years plausible             8     1           4    0.5521900 0.250
##  5 3years plausible             8     1           5    0.7215211 0.750
##  6 3years plausible             8     1           6    0.4583379 0.500
##  7 3years plausible             8     1           7    0.6592275 0.875
##  8 3years plausible             8     1           8    0.3947244 0.375
##  9 3years plausible             8     1           9    0.5461541 0.375
## 10 3years plausible             8     1          10    0.3964997 0.625
## # ... with 152,990 more rows
ggplot(noisy_channel_means, aes(Condition, Mean, fill=Condition))+
  geom_bar(stat="identity")+  
  ylim(0.0, 1.00)+
  facet_grid(.~Age)+
  geom_errorbar(aes(ymin=Mean-SE, ymax=Mean+SE))

#Simulation for Condition by Age
summary_noisy_channel = noisy_channel%>%
  select(Condition,n_test_trials, Simulations, N, data, Age) %>% 
  group_by(Simulations, N, Age) %>% 
    do(p_value = summary(lm(data ~ Condition, data = .))$coefficient[2,4]) %>%
  unnest(p_value) %>%
  mutate(significant = ifelse(p_value < 0.05, 1, 0))  %>%
  group_by(N, Age) %>%
  summarise(
    n_sims = length(p_value),
    proportion_significant = sum(significant)/n_sims)
## Warning in summary.lm(lm(data ~ Condition, data = .)): essentially perfect
## fit: summary may be unreliable

## Warning in summary.lm(lm(data ~ Condition, data = .)): essentially perfect
## fit: summary may be unreliable

## Warning in summary.lm(lm(data ~ Condition, data = .)): essentially perfect
## fit: summary may be unreliable
#Simulation for Interaction
summary_noisy_channel_interaction = noisy_channel%>%
  select(Condition,n_test_trials, Simulations, N, data, Age) %>% 
  group_by(Simulations, N) %>% 
    do(p_value = summary(lm(data ~ Condition * Age, data = .))$coefficient[4,4]) %>%
  unnest(p_value) %>%
  mutate(significant = ifelse(p_value < 0.05, 1, 0))  %>%
  group_by(N) %>%
  summarise(
    n_sims = length(p_value),
    proportion_significant = sum(significant)/n_sims)

Plot for Condition by Age

ggplot(summary_noisy_channel, aes(x= N, y=proportion_significant, colour=Age))+
  geom_line()
## Warning: Removed 3 rows containing missing values (geom_path).

Plot for interaction between Condition and Age

ggplot(summary_noisy_channel_interaction, aes(x= N, y=proportion_significant))+
 geom_line()
## Warning: Removed 1 rows containing missing values (geom_path).

Simulation with 75% smaller differences between conditions

n_test_trials = 8 # Each child took part in 8 test trials
#n_exposure_trials = 7 # Each child took part in 7 exposure trials
n_children = 1:50 # There were 48 3-year-old, 49 4-year-old, and 43 5-year-old children

noisy_channel_75 = expand.grid(
  
  n_test_trials = n_test_trials,
  N = n_children,
  Age = c("3years", "4years", "5years"),
  Condition = c("plausible", "implausible"),
  Simulations = 1:20
  )
noisy_channel_75 = 
  noisy_channel_75 %>%
  mutate(mean3 = ifelse(Age == "3years",
                 ifelse(Condition == "plausible", 0.51,0.505),
                 ifelse(Condition == "plausible", NA, NA)),
           sd3 = ifelse(Age == "3years",
                 ifelse(Condition == "plausible", 0.10,0.105),
                 ifelse(Condition == "plausible", NA, NA)))%>%
  mutate(mean4 = ifelse(Age == "4years",
                 ifelse(Condition == "plausible", 0.60,0.55),
                 ifelse(Condition == "plausible", NA, NA)),
          sd4 =  ifelse(Age == "4years",
                 ifelse(Condition == "plausible", 0.11,0.1075),
                 ifelse(Condition == "plausible", NA, NA)))%>%
  mutate(mean5 = ifelse(Age == "5years",
                 ifelse(Condition == "plausible", 0.54, 0.4675),
                 ifelse(Condition == "plausible", NA, NA)),
          sd5 =  ifelse(Age == "5years",
                 ifelse(Condition == "plausible", 0.12, 0.1175),
                 ifelse(Condition == "plausible", NA, NA)))

noisy_channel_75$mean = rowMeans(noisy_channel_75[, c("mean3", "mean4", "mean5")], na.rm=TRUE)
noisy_channel_75$sd = rowMeans(noisy_channel_75[, c("sd3", "sd4", "sd5")], na.rm=TRUE)
noisy_channel_75[6:11] = list(NULL)

noisy_channel_75 = 
  noisy_channel_75 %>%
group_by(Age, Condition,n_test_trials,N,Simulations) %>% 
  do(subject_prob = rnorm(.$N,.$mean,.$sd)) %>%
  unnest(subject_prob)%>%
  mutate(subject_prob = replace(subject_prob ,subject_prob <0,0.01),
         subject_prob = replace(subject_prob ,subject_prob >1,0.99))%>%
group_by(Age,Condition,n_test_trials,N,Simulations,subject_prob) %>% 
  do(data = rbinom(n = 1, size = .$n_test_trials, prob = .$subject_prob)/.$n_test_trials) %>%
  unnest(data)

noisy_channel_75_means = noisy_channel_75 %>%
  group_by(Condition, Age) %>%
  summarize(Mean = mean(data), SE = sd(data)/sqrt(n()))
print(noisy_channel_75)
## # A tibble: 153,000 x 7
##       Age Condition n_test_trials     N Simulations subject_prob  data
##    <fctr>    <fctr>         <dbl> <int>       <int>        <dbl> <dbl>
##  1 3years plausible             8     1           1    0.3663766 0.625
##  2 3years plausible             8     1           2    0.4835545 0.500
##  3 3years plausible             8     1           3    0.5578258 0.500
##  4 3years plausible             8     1           4    0.5370978 0.500
##  5 3years plausible             8     1           5    0.3227277 0.500
##  6 3years plausible             8     1           6    0.3873584 0.375
##  7 3years plausible             8     1           7    0.6622434 0.625
##  8 3years plausible             8     1           8    0.3116956 0.375
##  9 3years plausible             8     1           9    0.4786630 0.500
## 10 3years plausible             8     1          10    0.6086412 0.125
## # ... with 152,990 more rows
ggplot(noisy_channel_75_means, aes(Condition, Mean, fill=Condition))+
  geom_bar(stat="identity")+  
  ylim(0.0, 1.00)+
  facet_grid(.~Age)+
  geom_errorbar(aes(ymin=Mean-SE, ymax=Mean+SE))

#Simulation for Condition by Age
summary_noisy_channel_75 = noisy_channel_75%>%
  select(Condition,n_test_trials, Simulations, N, data, Age) %>% 
  group_by(Simulations, N, Age) %>% 
    do(p_value = summary(lm(data ~ Condition, data = .))$coefficient[2,4]) %>%
  unnest(p_value) %>%
  mutate(significant = ifelse(p_value < 0.05, 1, 0))  %>%
  group_by(N, Age) %>%
  summarise(
    n_sims = length(p_value),
    proportion_significant = sum(significant)/n_sims)

#Simulation for Interaction
summary_noisy_channel_interaction_75 = noisy_channel_75%>%
  select(Condition,n_test_trials, Simulations, N, data, Age) %>% 
  group_by(Simulations, N) %>% 
    do(p_value = summary(lm(data ~ Condition * Age, data = .))$coefficient[4,4]) %>%
  unnest(p_value) %>%
  mutate(significant = ifelse(p_value < 0.05, 1, 0))  %>%
  group_by(N) %>%
  summarise(
    n_sims = length(p_value),
    proportion_significant = sum(significant)/n_sims)

Plot for Condition by Age

ggplot(summary_noisy_channel_75, aes(x= N, y=proportion_significant, colour=Age))+
  geom_line()
## Warning: Removed 3 rows containing missing values (geom_path).

Plot for interaction between Condition and Age

ggplot(summary_noisy_channel_interaction_75, aes(x= N, y=proportion_significant))+
 geom_line()
## Warning: Removed 1 rows containing missing values (geom_path).