knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(rstatix)
library(lsr)
library(ggpubr)
library(flextable)
library(emmeans)

Dataset Upload

grad_double_low <- read_csv("data/grad_data_double_5_10Hz_300_1000ms.csv")
grad_nostim_low <- read_csv("data/grad_data_nostim_5_10Hz_300_1000ms.csv")
grad_double_high <- read_csv("data/grad_data_double_10_15Hz_300_1000ms.csv")
grad_nostim_high <- read_csv("data/grad_data_nostim_10_15Hz_300_1000ms.csv")

Parietal Low + Occipital High

#Combining low alpha from parietal contralaterally and high alpha occipital ipsilaterally

#Double

GD_parietal_low <- grad_double_low |>
  mutate(
    Left_Hem_ROI_Right_Attention_stim_Double = (`Left-parietal_ROI_right_attention`-`Left-parietal_ROI_neutral_attention`)/(`Left-parietal_ROI_right_attention`+`Left-parietal_ROI_neutral_attention`),
    Right_Hem_ROI_Left_Attention_stim_Double = (`Right-parietal_ROI_left_attention`-`Right-parietal_ROI_neutral_attention`)/(`Right-parietal_ROI_left_attention`+`Right-parietal_ROI_neutral_attention`)) |>
  select(1,
         "Left_Hem_ROI_Right_Attention_stim_Double",
         "Right_Hem_ROI_Left_Attention_stim_Double",)


GD_occipital_high <- grad_double_high |>
  mutate(
    Left_Hem_ROI_Left_Attention_stim_Double = 
      (`Left-occipital_ROI_left_attention` - `Left-occipital_ROI_neutral_attention`)/(`Left-occipital_ROI_left_attention`+`Left-occipital_ROI_neutral_attention`),
    Right_Hem_ROI_Right_Attention_stim_Double = (`Right-occipital_ROI_right_attention`-`Right-occipital_ROI_neutral_attention`)/(`Right-occipital_ROI_right_attention`+`Right-occipital_ROI_neutral_attention`)) |>
  select(1,
         "Left_Hem_ROI_Left_Attention_stim_Double",
         "Right_Hem_ROI_Right_Attention_stim_Double",)


all_double <- 
  full_join(GD_parietal_low, GD_occipital_high, by = "...1") 


#NoStim

GN_parietal_low <- grad_nostim_low |>
  mutate(
    Left_Hem_ROI_Right_Attention_stim_NoStim = (`Left-parietal_ROI_right_attention`-`Left-parietal_ROI_neutral_attention`)/(`Left-parietal_ROI_right_attention`+`Left-parietal_ROI_neutral_attention`),
    Right_Hem_ROI_Left_Attention_stim_NoStim = (`Right-parietal_ROI_left_attention`-`Right-parietal_ROI_neutral_attention`)/(`Right-parietal_ROI_left_attention`+`Right-parietal_ROI_neutral_attention`)) |>
  select(1,
         "Left_Hem_ROI_Right_Attention_stim_NoStim",
         "Right_Hem_ROI_Left_Attention_stim_NoStim",)


GN_occipital_high <- grad_nostim_high |>
  mutate(
    Left_Hem_ROI_Left_Attention_stim_NoStim = 
      (`Left-occipital_ROI_left_attention` - `Left-occipital_ROI_neutral_attention`)/(`Left-occipital_ROI_left_attention`+`Left-occipital_ROI_neutral_attention`),
    Right_Hem_ROI_Right_Attention_stim_NoStim = (`Right-occipital_ROI_right_attention`-`Right-occipital_ROI_neutral_attention`)/(`Right-occipital_ROI_right_attention`+`Right-occipital_ROI_neutral_attention`)) |>
  select(1,
         "Left_Hem_ROI_Left_Attention_stim_NoStim",
         "Right_Hem_ROI_Right_Attention_stim_NoStim",)

all_nostim <- 
  full_join(GN_parietal_low, GN_occipital_high, by = "...1")


all_stim <- full_join(all_double, all_nostim, by = "...1")  

grad_all <- pivot_longer(all_stim,
  cols = c(2:9),
  names_to = c("hemisphere", "attention", "stimulus"),
  names_pattern = "(.*)_ROI_(.*)_stim_(.*)",
  values_to = "alpha")

Summary Descriptive

grad_all|>
  group_by(hemisphere, attention, stimulus) |>
  get_summary_stats(alpha, type = "mean_sd") |>
  flextable(cwidth = .95) |>
  fontsize(size = 8) |>
  fontsize(size = 10, part = "header") |>
  set_header_labels(hemisphere = "Hemisphere",
                    attention = "Attention",
                    stimulus = "Stimulus", 
                    variable = "Variable")

Hemisphere

Attention

Stimulus

Variable

n

mean

sd

Left_Hem

Left_Attention

Double

alpha

30

0.054

0.031

Left_Hem

Left_Attention

NoStim

alpha

30

0.055

0.041

Left_Hem

Right_Attention

Double

alpha

30

-0.011

0.035

Left_Hem

Right_Attention

NoStim

alpha

30

-0.016

0.028

Right_Hem

Left_Attention

Double

alpha

30

-0.012

0.037

Right_Hem

Left_Attention

NoStim

alpha

30

-0.012

0.036

Right_Hem

Right_Attention

Double

alpha

30

0.047

0.043

Right_Hem

Right_Attention

NoStim

alpha

30

0.058

0.059

Outliers

grad_all|>
  group_by(hemisphere, attention, stimulus) |>
  identify_outliers(alpha) |>
  flextable(cwidth = .95) |>
  fontsize(size = 8) |>
  fontsize(size = 10, part = "header") |>
  set_header_labels(hemisphere = "Hemisphere",
                    attention = "Attention",
                    stimulus = "Stimulus",
                    ...1 = "ID",
                    alpha = "Alpha")

Hemisphere

Attention

Stimulus

ID

Alpha

is.outlier

is.extreme

Left_Hem

Left_Attention

Double

S021_jmn22

0.13046326

TRUE

FALSE

Left_Hem

Left_Attention

NoStim

S022_dss19

0.15716029

TRUE

FALSE

Left_Hem

Right_Attention

Double

S008_mtr19

0.07403136

TRUE

FALSE

Right_Hem

Right_Attention

NoStim

S022_dss19

0.20387509

TRUE

FALSE

QQ

#Residual QQ
qq_p  <- lm(alpha ~ hemisphere*attention*stimulus, data=grad_all) 
ggqqplot(residuals(qq_p))

Normality

grad_all|>
  group_by(hemisphere, attention, stimulus) |>
  shapiro_test(alpha) |> #must be P >.05
  flextable(cwidth = .95) |>
  fontsize(size = 8) |>
  fontsize(size = 10, part = "header") |>
  set_header_labels(hemisphere = "Hemisphere",
                    attention = "Attention",
                    stimulus = "Stimulus", 
                    variable = "Variable",
                    statistic = "Statistics")

Hemisphere

Attention

Stimulus

Variable

Statistics

p

Left_Hem

Left_Attention

Double

alpha

0.9852727

0.9417364

Left_Hem

Left_Attention

NoStim

alpha

0.9831715

0.9021231

Left_Hem

Right_Attention

Double

alpha

0.9815029

0.8639572

Left_Hem

Right_Attention

NoStim

alpha

0.9843194

0.9250434

Right_Hem

Left_Attention

Double

alpha

0.9929952

0.9990520

Right_Hem

Left_Attention

NoStim

alpha

0.9682042

0.4912829

Right_Hem

Right_Attention

Double

alpha

0.9743878

0.6646983

Right_Hem

Right_Attention

NoStim

alpha

0.9887965

0.9836196

ANOVA Hemisphere X Attention X Stimulus

anova_test(grad_all, 
           dv = alpha, 
           wid = ...1,
          within = c(hemisphere, attention, stimulus),
          effect.size = "pes") |>
  flextable(cwidth = .95) |>
  fontsize(size = 8) |>
 fontsize(size = 10, part = "header") |>
  set_header_labels(pes = "Effect Size (ƞ2p)")

Effect

DFn

DFd

F

p

p<.05

Effect Size (ƞ2p)

hemisphere

1

29

0.0000089

0.9980000000000

0.000000307

attention

1

29

0.1140000

0.7380000000000

0.004000000

stimulus

1

29

0.1260000

0.7250000000000

0.004000000

hemisphere:attention

1

29

116.6290000

0.0000000000112

*

0.801000000

hemisphere:stimulus

1

29

2.6550000

0.1140000000000

0.084000000

attention:stimulus

1

29

0.1710000

0.6820000000000

0.006000000

hemisphere:attention:stimulus

1

29

0.7340000

0.3990000000000

0.025000000

#.01: Small effect size
#.06: Medium effect size
#.14 or higher: Large effect size

Mean Compare

#Tidy Data

G_all <- all_stim |>
  mutate(
    Left_Hem_ROI_Left_Attention = 
      ((`Left_Hem_ROI_Left_Attention_stim_Double` + `Left_Hem_ROI_Left_Attention_stim_NoStim`)/2),
  
    Left_Hem_ROI_Right_Attention = 
      ((`Left_Hem_ROI_Right_Attention_stim_Double` + `Left_Hem_ROI_Right_Attention_stim_NoStim`)/2),
    
    Right_Hem_ROI_Left_Attention = 
      ((`Right_Hem_ROI_Left_Attention_stim_Double` + `Right_Hem_ROI_Left_Attention_stim_NoStim`)/2),
   
    Right_Hem_ROI_Right_Attention = 
      ((`Right_Hem_ROI_Right_Attention_stim_Double` + `Right_Hem_ROI_Right_Attention_stim_NoStim`)/2)) |>
      
  select(1,
         "Left_Hem_ROI_Left_Attention", 
         "Left_Hem_ROI_Right_Attention",
         "Right_Hem_ROI_Left_Attention",
         "Right_Hem_ROI_Right_Attention")

One Sample t-test

t1 <- t.test(G_all$"Left_Hem_ROI_Left_Attention",
           mu = 0,
           alternative = "greater")
t1_mean <- mean(G_all$Left_Hem_ROI_Left_Attention)
t1_sd <- sd(G_all$Left_Hem_ROI_Left_Attention)
t1_es <- cohensD(G_all$Left_Hem_ROI_Left_Attention, mu = 0)

t2 <- t.test(G_all$"Left_Hem_ROI_Right_Attention",
           mu = 0,
           alternative = "less")
t2_mean <- mean(G_all$Left_Hem_ROI_Right_Attention)
t2_sd <- sd(G_all$Left_Hem_ROI_Right_Attention)
t2_es <- cohensD(G_all$Left_Hem_ROI_Right_Attention, mu = 0)


t3 <- t.test(G_all$"Right_Hem_ROI_Left_Attention",
           mu = 0,
           alternative = "less")
t3_mean <- mean(G_all$Right_Hem_ROI_Left_Attention)
t3_sd <- sd(G_all$Right_Hem_ROI_Left_Attention)
t3_es <- cohensD(G_all$Right_Hem_ROI_Left_Attention, mu = 0)


t4 <- t.test(G_all$"Right_Hem_ROI_Right_Attention",
           mu = 0,
           alternative = "greater")
t4_mean <- mean(G_all$Right_Hem_ROI_Right_Attention)
t4_sd <- sd(G_all$Right_Hem_ROI_Right_Attention)
t4_es <- cohensD(G_all$Right_Hem_ROI_Right_Attention, mu = 0)

t_table <- map_df(list(t1, t2, t3, t4), tidy) 

mutate(t_table,
       "t-test Comparison" = c("Left Hemisphere - Left Attention", 
                          "Left Hemisphere - Right Attention",
                          "Right Hemisphere - Left Attention",
                          "Right Hemisphere - Right Attention"),
       "Standard Deviation" = c(t1_sd,
                                t2_sd,
                                t3_sd,
                                t4_sd),
       "Effect Size (d)" = c(t1_es,
                         t2_es,
                         t3_es,
                         t4_es))|>
  subset(select=c(9, 1, 10, 2, 3, 4, 5, 6, 7, 8, 11)) |>
  flextable (cwidth = .95) |>
  fontsize(size = 8) |>
  fontsize(size = 10, part = "header") |>
  set_header_labels(estimate = "Estimate",
                    statistic = "Statistic",
                    p.value = "P Value",
                    parameter = "Parameter")

t-test Comparison

Estimate

Standard Deviation

Statistic

P Value

Parameter

conf.low

conf.high

method

alternative

Effect Size (d)

Left Hemisphere - Left Attention

0.05424683

0.02711054

10.959652

0.000000000003966089

29

0.04583668

Inf

One Sample t-test

greater

2.0009495

Left Hemisphere - Right Attention

-0.01358353

0.02764753

-2.691020

0.005849182444401403

29

-Inf

-0.005006802

One Sample t-test

less

0.4913107

Right Hemisphere - Left Attention

-0.01198284

0.03152342

-2.082031

0.023135716604195531

29

-Inf

-0.002203749

One Sample t-test

less

0.3801251

Right Hemisphere - Right Attention

0.05266743

0.04447409

6.486280

0.000000211727630807

29

0.03887082

Inf

One Sample t-test

greater

1.1842272

Post Hoc

hemisphere:attention significancy in D/N

# any significance towards stimulus? (is hemisphere:attention significant in both D/N)
PH_1_parietal <- grad_all |>
  group_by(stimulus) |>
  anova_test(dv = alpha, 
             wid = ...1, 
             within = c(hemisphere, attention),
             effect.size = "pes")
flextable(PH_1_parietal,
          cwidth = .95) |>
  fontsize(size = 8) |>
  fontsize(size = 10, part = "header") |>
  set_header_labels(stimulus = "Stimulus",
                    pes = "Effect Size (ƞ2p)")

Stimulus

Effect

DFn

DFd

F

p

p<.05

Effect Size (ƞ2p)

Double

hemisphere

1

29

0.854

0.363000000000

0.029000

Double

attention

1

29

0.433

0.516000000000

0.015000

Double

hemisphere:attention

1

29

59.731

0.000000016000

*

0.673000

NoStim

hemisphere

1

29

0.662

0.422000000000

0.022000

NoStim

attention

1

29

0.004

0.952000000000

0.000127

NoStim

hemisphere:attention

1

29

89.278

0.000000000235

*

0.755000

Effects of 2 Levels of Hemisphere

#effect of 2 levels of attention
PH_2_parietal <- grad_all |>
  group_by(stimulus, hemisphere) |>
  anova_test(dv = alpha, 
             wid = ...1, 
             within = attention,
             effect.size = "pes")

flextable(PH_2_parietal,
          cwidth = .95) |>
  fontsize(size = 8) |>
  fontsize(size = 10, part = "header") |>
  set_header_labels(hemisphere = "Hemisphere",
                    stimulus = "Stimulus",
                    pes = "Effect Size (ƞ2p)")

Hemisphere

Stimulus

Effect

DFn

DFd

F

p

p<.05

Effect Size (ƞ2p)

Left_Hem

Double

attention

1

29

63.077

0.0000000092800

*

0.685

Right_Hem

Double

attention

1

29

35.900

0.0000016300000

*

0.553

Left_Hem

NoStim

attention

1

29

98.909

0.0000000000747

*

0.773

Right_Hem

NoStim

attention

1

29

33.406

0.0000029200000

*

0.535

Effects of 2 Levels of Attention

#effect of 2 levels of hemisphere
PH_3_parietal <- grad_all |>
  group_by(stimulus, attention) |>
  anova_test(dv = alpha, 
             wid = ...1, 
             within = hemisphere,
             effect.size = "pes")

flextable(PH_3_parietal,
          cwidth = .95) |>
  fontsize(size = 8) |>
  fontsize(size = 10, part = "header") |>
  set_header_labels(attention = "Attention",
                    stimulus = "Stimulus", 
                    pes = "Effect Size (ƞ2p)")

Attention

Stimulus

Effect

DFn

DFd

F

p

p<.05

Effect Size (ƞ2p)

Left_Attention

Double

hemisphere

1

29

65.001

0.00000000685

*

0.691

Right_Attention

Double

hemisphere

1

29

36.642

0.00000138000

*

0.558

Left_Attention

NoStim

hemisphere

1

29

65.748

0.00000000609

*

0.694

Right_Attention

NoStim

hemisphere

1

29

65.667

0.00000000617

*

0.694

Effects of 2 Levels of Stimulus

#effect of 2 levels of stimulud
PH_4_parietal <- grad_all |>
  group_by(attention, hemisphere) |>
  anova_test(dv = alpha, 
             wid = ...1, 
             within = stimulus,
             effect.size = "pes")

flextable(PH_4_parietal,
          cwidth = .95) |>
  fontsize(size = 8) |>
  fontsize(size = 10, part = "header") |>
  set_header_labels(hemisphere = "Hemisphere",
                    attention = "Attention",
                    pes = "Effect Size (ƞ2p)")

Hemisphere

Attention

Effect

DFn

DFd

F

p

p<.05

Effect Size (ƞ2p)

Left_Hem

Left_Attention

stimulus

1

29

0.015

0.903

0.0005260

Right_Hem

Left_Attention

stimulus

1

29

0.001

0.971

0.0000454

Left_Hem

Right_Attention

stimulus

1

29

0.634

0.432

0.0210000

Right_Hem

Right_Attention

stimulus

1

29

1.227

0.277

0.0410000

Pairwise Comparison 1

# Pairwise comparisons
pw_comp_1 <- grad_all |>
  group_by(stimulus, hemisphere) |>
  pairwise_t_test(alpha ~ attention, 
                  paired = TRUE, 
                  p.adjust.method = "bonferroni")
flextable(pw_comp_1,
          cwidth = .95) |>
  fontsize(size = 8) |>
  fontsize(size = 10, part = "header") |>
  set_header_labels(hemisphere = "Hemisphere",
                    stimulus = "Stimulus",
                    .y. = "Variable")

Hemisphere

Stimulus

Variable

group1

group2

n1

n2

statistic

df

p

p.adj

p.adj.signif

Left_Hem

Double

alpha

Left_Attention

Right_Attention

30

30

7.942074

29

0.0000000092800

0.0000000092800

****

Right_Hem

Double

alpha

Left_Attention

Right_Attention

30

30

-5.991669

29

0.0000016300000

0.0000016300000

****

Left_Hem

NoStim

alpha

Left_Attention

Right_Attention

30

30

9.945282

29

0.0000000000747

0.0000000000747

****

Right_Hem

NoStim

alpha

Left_Attention

Right_Attention

30

30

-5.779823

29

0.0000029200000

0.0000029200000

****

Pairwise Comparison 2

# Pairwise comparisons
pw_comp_2 <- grad_all |>
  group_by(stimulus, attention) |>
  pairwise_t_test(alpha ~ hemisphere, 
                  paired = TRUE, 
                  p.adjust.method = "bonferroni")
flextable(pw_comp_2,
          cwidth = .95) |>
  fontsize(size = 8) |>
  fontsize(size = 10, part = "header") |>
  set_header_labels(attention = "Attention",
                    stimulus = "Stimulus",
                    .y. = "Variable")

Attention

Stimulus

Variable

group1

group2

n1

n2

statistic

df

p

p.adj

p.adj.signif

Left_Attention

Double

alpha

Left_Hem

Right_Hem

30

30

8.062341

29

0.00000000685

0.00000000685

****

Right_Attention

Double

alpha

Left_Hem

Right_Hem

30

30

-6.053252

29

0.00000138000

0.00000138000

****

Left_Attention

NoStim

alpha

Left_Hem

Right_Hem

30

30

8.108537

29

0.00000000609

0.00000000609

****

Right_Attention

NoStim

alpha

Left_Hem

Right_Hem

30

30

-8.103532

29

0.00000000617

0.00000000617

****