---
title: "Script A: literature review and power analysis"
format: html
---

## Libraries

```{r libraries}
#| echo: false
#| warning: false
#| error: false
#| message: false
library(osfr)
library(tidyverse)
library(knitr)
library(gt)
library(gtExtras)
library(rstatix)
library(ggpubr)
```

## Literature review: summary of results

```{r}
#| warning: false
#| error: false
#| label: tbl-litReview
#| tbl-cap: "Summary of the masked repetition priming effects as a function of word frequency reported in the literature. The power range estimates were calculated by simulating 10,000 datasets with the corresponding sample size (N) and FAE = 15 ms and 30 ms."

lit_effects_tbl_filename <- "../supplemental-data/MaskedPrimingDatabase-IDPriming-InteractionFrequency.csv"

lit_effects <- read_csv(lit_effects_tbl_filename)

power_estimates_15 <- read_csv("../supplemental-data/power_estimate2.dataset.range.csv") %>%
  select(nsubj, minPower, maxPower) %>%
  mutate(across(minPower:maxPower, ~round(., 2))) %>%
  mutate(minPower = paste0("[", minPower), maxPower = paste0(maxPower, "]")) %>%
  unite("Power range", minPower:maxPower, sep = " ")

power_estimates_30 <- read_csv("../supplemental-data/power_estimate3.dataset.range.csv") %>%
  select(nsubj, minPower, maxPower) %>%
  mutate(across(minPower:maxPower, ~round(., 2))) %>%
  mutate(minPower = paste0("[", minPower), maxPower = paste0(maxPower, "]")) %>%
  unite("Power range", minPower:maxPower, sep = " ")

lit_effects %>%
  left_join(., power_estimates_15, by=join_by("N" == "nsubj"), multiple="first") %>%
  left_join(., power_estimates_30, by=join_by("N" == "nsubj"), multiple="first", suffix = c("_15", "_30")) %>%
  mutate(across(MOP_HF:MOP_Interaction, as.numeric)) %>%
  #group_by(N) %>%
  gt(rowname_col = "PAPER") %>%
  #tab_options(row.striping.include_table_body = FALSE) %>% 
  tab_stubhead(label = "Study") %>%
  tab_spanner(
    label = "MOP (ms)", columns = c(MOP_HF, MOP_LF)
  ) %>% 
  tab_spanner(
    label = "FAE (ms)", columns = c(MOP_Interaction, `p<.05?`)
  ) %>%
  tab_spanner(
    label = "Power range [min max]", columns = c(`Power range_15`, `Power range_30`)
  ) %>%
  cols_label( LANGUAGE = "Language", 
              PRIME_DURATION = "SOA",
              MOP_HF = "HF", 
              MOP_LF = "LF", 
              MOP_Interaction = "ES",
              `p<.05?` = md("_p_<.05?"),
              `Power range_15` = "FAE=15ms",
              `Power range_30` = "FAE=30ms"
  ) %>%
  tab_footnote( 
    footnote = "SOA for each subject determined by pre-test", 
    locations = cells_body(column = "PRIME_DURATION", rows = 14)
  ) %>%
  tab_footnote(
    footnote = "Reported in Masson & Bodner (2003)",
    locations = cells_stub(rows = 16) 
  ) %>% 
  grand_summary_rows(
    columns = c(MOP_HF, MOP_LF, MOP_Interaction),
    fns = list(Mean ~ round(mean(.)),
               SD ~ round(sd(.))), 
    missing_text = " " 
  ) %>%
  grand_summary_rows(
    columns = c(MOP_Interaction),
    fns = list(Correlation ~ round(cor(MOP_HF, MOP_LF), 2)), 
    missing_text = " " 
  ) %>%
   sub_missing(
    missing_text = " "
  )

litReview.stats <- lit_effects %>%
    select(PAPER, MOP_HF, MOP_LF) %>%
    pivot_longer(MOP_HF:MOP_LF, names_to = "frequency.condition", values_to="mean.priming") %>%
    mutate(frequency.condition = factor(frequency.condition, levels=c("MOP_LF", "MOP_HF"))) %>%
    t_test(mean.priming ~ frequency.condition, paired=T, detailed=T)
```

### Power analysis 

```{r}
#| label: fig-power
#| fig-cap: "Power simulations for a FAE = 10 ms, for all combinations of standard deviation (sd), correlation (cor), and sample size. The red line identifies the threshold of 80% power."

load("../supplemental-data/freq_atten_sim.10ms.RData")

freq_atten_exp2.10ms_df.sub <- freq_atten_exp2.10ms_df %>% 
  filter(ES == 10) %>%
  filter(std_dev >= 80 & std_dev <= 120) %>%
  filter(rho >= 0.7 & rho <= 0.9)

freq_atten_exp2.10ms_df.sub |>
  ggplot(aes(y = power_unadjusted, x = nsubj)) + 
    geom_line() + 
    geom_point() +
    geom_hline(yintercept = 0.8, color = "red2") + 
    facet_grid(rho ~ std_dev) +
  scale_x_continuous(sec.axis = sec_axis(~ . , name = "sd", breaks = NULL, labels = NULL)) +
  scale_y_continuous(sec.axis = sec_axis(~ . , name = "cor", breaks = NULL, labels = NULL)) +
  theme_bw()+
    theme(#axis.line = element_line(colour = "black"),
      panel.grid.major = element_blank(),
      panel.grid.minor = element_blank(), 
      panel.border = element_blank(),
      legend.position="none"
      #panel.background = element_blank() 
     )+
  labs(title="FAE = 10 ms", y = "power (adjusted)", x = "sample size")

``` 
