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

Dataset Upload

Demographics

vars <- c("age", "gender")
cat <- c("gender")
demotable <- CreateTableOne(vars = vars, data = demo, factorVars = cat)
demotable
##                  
##                   Overall      
##   n                  30        
##   age (mean (SD)) 24.37 (5.77) 
##   gender = m (%)     13 (43.3)

Parietal

#tidy data

GD_parietal <- grad_double |>
  mutate( #Mutate new value for alpha modulation proportion against neutral condition

# Left Hemisphere    
    Left_Parietal_ROI_Left_Attention_stim_Double = 
      (`Left-parietal_ROI_left_attention` - `Left-parietal_ROI_neutral_attention`)/(`Left-parietal_ROI_left_attention`+`Left-parietal_ROI_neutral_attention`),
    
    Left_Parietal_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 Hemisphere    
    Right_Parietal_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`),
    
    Right_Parietal_ROI_Right_Attention_stim_Double = (`Right-parietal_ROI_right_attention`-`Right-parietal_ROI_neutral_attention`)/(`Right-parietal_ROI_right_attention`+`Right-parietal_ROI_neutral_attention`)) |>
  select(1,
         "Left_Parietal_ROI_Left_Attention_stim_Double", 
         "Left_Parietal_ROI_Right_Attention_stim_Double",
         "Right_Parietal_ROI_Left_Attention_stim_Double",
         "Right_Parietal_ROI_Right_Attention_stim_Double") 


GN_parietal <- grad_nostim |>
  mutate( #Mutate new value for alpha modulation proportion against neutral condition
    Left_Parietal_ROI_Left_Attention_stim_NoStim = 
      (`Left-parietal_ROI_left_attention` - `Left-parietal_ROI_neutral_attention`)/(`Left-parietal_ROI_left_attention`+`Left-parietal_ROI_neutral_attention`),
    Left_Parietal_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_Parietal_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`),
    Right_Parietal_ROI_Right_Attention_stim_NoStim = (`Right-parietal_ROI_right_attention`-`Right-parietal_ROI_neutral_attention`)/(`Right-parietal_ROI_right_attention`+`Right-parietal_ROI_neutral_attention`)) |>
  select(1,
         "Left_Parietal_ROI_Left_Attention_stim_NoStim", 
         "Left_Parietal_ROI_Right_Attention_stim_NoStim",
         "Right_Parietal_ROI_Left_Attention_stim_NoStim",
         "Right_Parietal_ROI_Right_Attention_stim_NoStim")


all_stim_parietal <- 
  full_join(GD_parietal, GN_parietal, by = "...1") 
  
#Convert to long formata for analysis
grad_parietal <- pivot_longer(all_stim_parietal,
  cols = c(Left_Parietal_ROI_Left_Attention_stim_Double:Right_Parietal_ROI_Right_Attention_stim_NoStim),
  names_to = c("hemisphere", "attention", "stimulus"),
  names_pattern = "(.*)_ROI_(.*)_stim_(.*)",
  values_to = "alpha")

Summary Descriptive

#Mean / SD for alpha value
grad_parietal|>
  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_Parietal

Left_Attention

Double

alpha

30

0.008

0.039

Left_Parietal

Left_Attention

NoStim

alpha

30

0.011

0.038

Left_Parietal

Right_Attention

Double

alpha

30

-0.011

0.041

Left_Parietal

Right_Attention

NoStim

alpha

30

-0.015

0.037

Right_Parietal

Left_Attention

Double

alpha

30

-0.010

0.040

Right_Parietal

Left_Attention

NoStim

alpha

30

-0.010

0.038

Right_Parietal

Right_Attention

Double

alpha

30

0.017

0.040

Right_Parietal

Right_Attention

NoStim

alpha

30

0.022

0.039

Outliers

#To identify outliers
grad_parietal|>
  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_Parietal

Left_Attention

Double

S009_fha01

0.07892581

TRUE

FALSE

Left_Parietal

Left_Attention

Double

S015_csi07

-0.09326127

TRUE

FALSE

Left_Parietal

Left_Attention

Double

S018_ade02

-0.05267781

TRUE

FALSE

Left_Parietal

Left_Attention

Double

S028_omr03

-0.08087781

TRUE

FALSE

Left_Parietal

Right_Attention

NoStim

S027_fmn28

-0.13610540

TRUE

TRUE

Right_Parietal

Left_Attention

Double

S028_omr03

-0.11255864

TRUE

FALSE

Right_Parietal

Right_Attention

Double

S026_dja01

0.12442160

TRUE

FALSE

Right_Parietal

Right_Attention

NoStim

S019_dtl05

-0.09752286

TRUE

FALSE

QQ

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

Normality With Stimulus

#Normality testing
grad_parietal|>
  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_Parietal

Left_Attention

Double

alpha

0.9402059

0.09212739

Left_Parietal

Left_Attention

NoStim

alpha

0.9585659

0.28456791

Left_Parietal

Right_Attention

Double

alpha

0.9907699

0.99438757

Left_Parietal

Right_Attention

NoStim

alpha

0.9266838

0.04011766

Right_Parietal

Left_Attention

Double

alpha

0.9341299

0.06327180

Right_Parietal

Left_Attention

NoStim

alpha

0.9757527

0.70490248

Right_Parietal

Right_Attention

Double

alpha

0.9583086

0.28024089

Right_Parietal

Right_Attention

NoStim

alpha

0.9412470

0.09827434

Normality Without Stimulus

#Normality test above speculates stimulus affected normality, this is to test nromality without taking into account stimulus
grad_parietal|>
  select(-c("stimulus")) |>
  group_by(hemisphere, attention) |>
  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",
                    variable = "Variables",
                    Sstatistics = "Statistics")

Hemisphere

Attention

Variables

statistic

p

Left_Parietal

Left_Attention

alpha

0.9792159

0.3962772

Left_Parietal

Right_Attention

alpha

0.9859400

0.7188646

Right_Parietal

Left_Attention

alpha

0.9749632

0.2532652

Right_Parietal

Right_Attention

alpha

0.9765110

0.2991998

ANOVA Hemisphere X Attention X Stimulus

#ANOVA Test
anova_test(grad_parietal, 
           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

2.144

0.15400000000

0.069

attention

1

29

0.473

0.49700000000

0.016

stimulus

1

29

0.039

0.84500000000

0.001

hemisphere:attention

1

29

67.704

0.00000000451

*

0.700

hemisphere:stimulus

1

29

0.258

0.61500000000

0.009

attention:stimulus

1

29

0.055

0.81700000000

0.002

hemisphere:attention:stimulus

1

29

2.327

0.13800000000

0.074

#Cohen's D Benchmarks
#.01: Small effect size
#.06: Medium effect size
#.14 or higher: Large effect size

ANOVA Without Outliers

#ANOVA for removed extreme outliers
grad_parietal_rm <- grad_parietal |>
  filter(!...1 == "S027_fmn28") #enter extreme outliers here

anova_test(grad_parietal_rm, 
           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

28

1.099

0.30300000000

0.038

attention

1

28

0.630

0.43400000000

0.022

stimulus

1

28

0.210

0.65000000000

0.007

hemisphere:attention

1

28

74.886

0.00000000212

*

0.728

hemisphere:stimulus

1

28

0.093

0.76300000000

0.003

attention:stimulus

1

28

0.132

0.71900000000

0.005

hemisphere:attention:stimulus

1

28

1.372

0.25100000000

0.047

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

Mean Compare

#Tidy data for averaged stimulus types

G_all_p <- all_stim_parietal |>
  mutate(
    Left_Parietal_ROI_Left_Attention = 
      ((`Left_Parietal_ROI_Left_Attention_stim_Double` + `Left_Parietal_ROI_Left_Attention_stim_NoStim`)/2),
  
    Left_Parietal_ROI_Right_Attention = 
      ((`Left_Parietal_ROI_Right_Attention_stim_Double` + `Left_Parietal_ROI_Right_Attention_stim_NoStim`)/2),
    
    Right_Parietal_ROI_Left_Attention = 
      ((`Right_Parietal_ROI_Left_Attention_stim_Double` + `Right_Parietal_ROI_Left_Attention_stim_NoStim`)/2),
   
    Right_Parietal_ROI_Right_Attention = 
      ((`Right_Parietal_ROI_Right_Attention_stim_Double` + `Right_Parietal_ROI_Right_Attention_stim_NoStim`)/2)) |>
      
  select(1,
         "Left_Parietal_ROI_Left_Attention", 
         "Left_Parietal_ROI_Right_Attention",
         "Right_Parietal_ROI_Left_Attention",
         "Right_Parietal_ROI_Right_Attention")

One Sample t-test

#T-tests for each interaction modulation from neutral baseline
t1_p <- t.test(G_all_p$"Left_Parietal_ROI_Left_Attention",
           mu = 0,
           alternative = "greater")
t1_p_mean <- mean(G_all_p$Left_Parietal_ROI_Left_Attention)
t1_p_sd <- sd(G_all_p$Left_Parietal_ROI_Left_Attention)
t1_p_es <- cohensD(G_all_p$Left_Parietal_ROI_Left_Attention, mu = 0)

t2_p <- t.test(G_all_p$"Left_Parietal_ROI_Right_Attention",
           mu = 0,
           alternative = "less")
t2_p_mean <- mean(G_all_p$Left_Parietal_ROI_Right_Attention)
t2_p_sd <- sd(G_all_p$Left_Parietal_ROI_Right_Attention)
t2_p_es <- cohensD(G_all_p$Left_Parietal_ROI_Right_Attention, mu = 0)


t3_p <- t.test(G_all_p$"Right_Parietal_ROI_Left_Attention",
           mu = 0,
           alternative = "less")
t3_p_mean <- mean(G_all_p$Right_Parietal_ROI_Left_Attention)
t3_p_sd <- sd(G_all_p$Right_Parietal_ROI_Left_Attention)
t3_p_es <- cohensD(G_all_p$Right_Parietal_ROI_Left_Attention, mu = 0)


t4_p <- t.test(G_all_p$"Right_Parietal_ROI_Right_Attention",
           mu = 0,
           alternative = "greater")
t4_p_mean <- mean(G_all_p$Right_Parietal_ROI_Right_Attention)
t4_p_sd <- sd(G_all_p$Right_Parietal_ROI_Right_Attention)
t4_p_es <- cohensD(G_all_p$Right_Parietal_ROI_Right_Attention, mu = 0)

t_p_table <- map_df(list(t1_p, t2_p, t3_p, t4_p), tidy) 

mutate(t_p_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_p_sd,
                                t2_p_sd,
                                t3_p_sd,
                                t4_p_sd),
       "Effect Size (d)" = c(t1_p_es,
                         t2_p_es,
                         t3_p_es,
                         t4_p_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.009179950

0.03280938

1.532509

0.068117370

29

-0.000998068

Inf

One Sample t-test

greater

0.2797965

Left Hemisphere - Right Attention

-0.012647025

0.03277551

-2.113487

0.021640931

29

-Inf

-0.0024795123

One Sample t-test

less

0.3858681

Right Hemisphere - Left Attention

-0.009816692

0.03364550

-1.598081

0.060432651

29

-Inf

0.0006207052

One Sample t-test

less

0.2917683

Right Hemisphere - Right Attention

0.019661449

0.03346623

3.217877

0.001585087

29

0.009279665

Inf

One Sample t-test

greater

0.5875012

Post Hoc

hemisphere:attention significancy in D/N

# any significance towards stimulus? (is hemisphere:attention significant in both D/N)
PH_1_parietal <- grad_parietal |>
  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

1.043

0.3160000000

0.035

Double

attention

1

29

0.727

0.4010000000

0.024

Double

hemisphere:attention

1

29

45.188

0.0000002240

*

0.609

NoStim

hemisphere

1

29

1.959

0.1720000000

0.063

NoStim

attention

1

29

0.198

0.6600000000

0.007

NoStim

hemisphere:attention

1

29

52.831

0.0000000527

*

0.646

Effects of 2 Levels of Hemisphere

# Effects of 2 Levels of Hemisphere
PH_2_parietal <- grad_parietal |>
  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_Parietal

Double

attention

1

29

11.026

0.002000

*

0.275

Right_Parietal

Double

attention

1

29

15.322

0.000505

*

0.346

Left_Parietal

NoStim

attention

1

29

10.580

0.003000

*

0.267

Right_Parietal

NoStim

attention

1

29

14.271

0.000729

*

0.330

Effects of 2 Levels of Attention

# Effects of 2 Levels of Attention
PH_3_parietal <- grad_parietal |>
  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

13.946

0.000819

*

0.325

Right_Attention

Double

hemisphere

1

29

15.167

0.000532

*

0.343

Left_Attention

NoStim

hemisphere

1

29

13.997

0.000804

*

0.326

Right_Attention

NoStim

hemisphere

1

29

19.445

0.000130

*

0.401

Effects of 2 Levels of Stimulus

# Effects of 2 Levels of Stimulus
PH_4_parietal <- grad_parietal |>
  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_Parietal

Left_Attention

stimulus

1

29

0.206

0.653

0.007000

Right_Parietal

Left_Attention

stimulus

1

29

0.003

0.957

0.000102

Left_Parietal

Right_Attention

stimulus

1

29

0.282

0.600

0.010000

Right_Parietal

Right_Attention

stimulus

1

29

0.423

0.520

0.014000

Pairwise Comparison 1

#Pairwise comparisons of 2 levels of attention within hemisphere
pw_comp_1 <- grad_parietal |>
  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_Parietal

Double

alpha

Left_Attention

Right_Attention

30

30

3.320548

29

0.002000

0.002000

**

Right_Parietal

Double

alpha

Left_Attention

Right_Attention

30

30

-3.914373

29

0.000505

0.000505

***

Left_Parietal

NoStim

alpha

Left_Attention

Right_Attention

30

30

3.252641

29

0.003000

0.003000

**

Right_Parietal

NoStim

alpha

Left_Attention

Right_Attention

30

30

-3.777759

29

0.000729

0.000729

***

Pairwise Comparison 2

#Pairwise comparisons of 2 levels of hemisphere within attention
pw_comp_2 <- grad_parietal |>
  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_Parietal

Right_Parietal

30

30

3.734408

29

0.000819

0.000819

***

Right_Attention

Double

alpha

Left_Parietal

Right_Parietal

30

30

-3.894424

29

0.000532

0.000532

***

Left_Attention

NoStim

alpha

Left_Parietal

Right_Parietal

30

30

3.741255

29

0.000804

0.000804

***

Right_Attention

NoStim

alpha

Left_Parietal

Right_Parietal

30

30

-4.409685

29

0.000130

0.000130

***

Occipital

GD_occipital <- grad_double |>
  mutate(
    Left_Occipital_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`),
    Left_Occipital_ROI_Right_Attention_stim_Double = (`Left-occipital_ROI_right_attention`-`Left-occipital_ROI_neutral_attention`)/(`Left-occipital_ROI_right_attention`+`Left-occipital_ROI_neutral_attention`),
    Right_Occipital_ROI_Left_Attention_stim_Double = (`Right-occipital_ROI_left_attention`-`Right-occipital_ROI_neutral_attention`)/(`Right-occipital_ROI_left_attention`+`Right-occipital_ROI_neutral_attention`),
    Right_Occipital_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_Occipital_ROI_Left_Attention_stim_Double", 
         "Left_Occipital_ROI_Right_Attention_stim_Double",
         "Right_Occipital_ROI_Left_Attention_stim_Double",
         "Right_Occipital_ROI_Right_Attention_stim_Double")


GN_occipital <- grad_nostim |>
  mutate(
    Left_Occipital_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`),
    Left_Occipital_ROI_Right_Attention_stim_NoStim = (`Left-occipital_ROI_right_attention`-`Left-occipital_ROI_neutral_attention`)/(`Left-occipital_ROI_right_attention`+`Left-occipital_ROI_neutral_attention`),
    Right_Occipital_ROI_Left_Attention_stim_NoStim = (`Right-occipital_ROI_left_attention`-`Right-occipital_ROI_neutral_attention`)/(`Right-occipital_ROI_left_attention`+`Right-occipital_ROI_neutral_attention`),
    Right_Occipital_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_Occipital_ROI_Left_Attention_stim_NoStim", 
         "Left_Occipital_ROI_Right_Attention_stim_NoStim",
         "Right_Occipital_ROI_Left_Attention_stim_NoStim",
         "Right_Occipital_ROI_Right_Attention_stim_NoStim")


all_stim_occipital <- 
  full_join(GD_occipital, GN_occipital, by = "...1") 
  

grad_occipital <- pivot_longer(all_stim_occipital,
  cols = c(Left_Occipital_ROI_Left_Attention_stim_Double:Right_Occipital_ROI_Right_Attention_stim_NoStim),
  names_to = c("hemisphere", "attention", "stimulus"),
  names_pattern = "(.*)_ROI_(.*)_stim_(.*)",
  values_to = "alpha")

Summary Descriptive

grad_occipital|>
  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_Occipital

Left_Attention

Double

alpha

30

0.047

0.032

Left_Occipital

Left_Attention

NoStim

alpha

30

0.053

0.046

Left_Occipital

Right_Attention

Double

alpha

30

0.006

0.039

Left_Occipital

Right_Attention

NoStim

alpha

30

0.004

0.045

Right_Occipital

Left_Attention

Double

alpha

30

0.014

0.037

Right_Occipital

Left_Attention

NoStim

alpha

30

0.009

0.037

Right_Occipital

Right_Attention

Double

alpha

30

0.040

0.043

Right_Occipital

Right_Attention

NoStim

alpha

30

0.059

0.060

Outliers

grad_occipital|>
  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 = "Stimulis",
                    ...1 = "ID",
                    alpha = "Alpha")

Hemisphere

Attention

Stimulis

ID

Alpha

is.outlier

is.extreme

Left_Occipital

Right_Attention

NoStim

S009_fha01

-0.08771973

TRUE

FALSE

Right_Occipital

Left_Attention

Double

S021_jmn22

0.09280188

TRUE

FALSE

Right_Occipital

Right_Attention

NoStim

S022_dss19

0.20147397

TRUE

FALSE

Right_Occipital

Right_Attention

NoStim

S028_omr03

0.20064041

TRUE

FALSE

QQ

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

Normality

grad_occipital|>
  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_Occipital

Left_Attention

Double

alpha

0.9585876

0.2849354

Left_Occipital

Left_Attention

NoStim

alpha

0.9630065

0.3688407

Left_Occipital

Right_Attention

Double

alpha

0.9742250

0.6599117

Left_Occipital

Right_Attention

NoStim

alpha

0.9621563

0.3512658

Right_Occipital

Left_Attention

Double

alpha

0.9734545

0.6373238

Right_Occipital

Left_Attention

NoStim

alpha

0.9503438

0.1726817

Right_Occipital

Right_Attention

Double

alpha

0.9684023

0.4964377

Right_Occipital

Right_Attention

NoStim

alpha

0.9532372

0.2062559

ANOVA

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

Effect

DFn

DFd

F

p

p<.05

Effect Size (ƞ2p)

hemisphere

1.000

29.000

0.876

0.357

0.029

attention

1.000

29.000

0.489

0.490

0.017

stimulus

1.000

29.000

0.353

0.557

0.012

hemisphere:attention

1.000

29.000

113.547

0.000

*

0.797

hemisphere:stimulus

1.000

29.000

0.898

0.351

0.030

attention:stimulus

1.000

29.000

1.588

0.218

0.052

hemisphere:attention:stimulus

1.000

29.000

16.993

0.000

*

0.369

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

Post Hoc

Simple Two way Interaction

# Simple Two way Interaction
PH_1_occipital <- grad_occipital%>%
  group_by(attention) %>%
  anova_test(dv = alpha, 
             wid = ...1, 
             within = c(stimulus, hemisphere),
             effect.size = "pes")
flextable(PH_1_occipital,
          cwidth = .95) |>
  fontsize(size = 8) |>
  fontsize(size = 10, part = "header") |>
  colformat_double(digits = 3) |>
  set_header_labels(attention = "Attention",
                    pes = "Effect Size (ƞ2p)")

Attention

Effect

DFn

DFd

F

p

p<.05

Effect Size (ƞ2p)

Left_Attention

stimulus

1.000

29.000

0.003

0.955

0.000

Left_Attention

hemisphere

1.000

29.000

66.696

0.000

*

0.697

Left_Attention

stimulus:hemisphere

1.000

29.000

2.796

0.105

0.088

Right_Attention

stimulus

1.000

29.000

1.088

0.306

0.036

Right_Attention

hemisphere

1.000

29.000

63.481

0.000

*

0.686

Right_Attention

stimulus:hemisphere

1.000

29.000

7.719

0.009

*

0.210

Simple Simple Main Effect

#effect of attention
PH_2_occipital <- grad_occipital%>%
  group_by(stimulus, hemisphere) %>%
  anova_test(dv = alpha, 
             wid = ...1, 
             within = c(attention),
             effect.size = "pes")
flextable(PH_2_occipital,
          cwidth = .95) |>
  fontsize(size = 8) |>
  fontsize(size = 10, part = "header") |>
  colformat_double(digits = 3) |>
  set_header_labels(attention = "Attention",
                    pes = "Effect Size (ƞ2p)")

hemisphere

stimulus

Effect

DFn

DFd

F

p

p<.05

Effect Size (ƞ2p)

Left_Occipital

Double

attention

1.000

29.000

36.712

0.000

*

0.559

Right_Occipital

Double

attention

1.000

29.000

13.598

0.001

*

0.319

Left_Occipital

NoStim

attention

1.000

29.000

40.592

0.000

*

0.583

Right_Occipital

NoStim

attention

1.000

29.000

26.085

0.000

*

0.474

Pairwise Comparison

#Pairwise comparisons
pw_occipital <- grad_occipital |>
  group_by(stimulus, hemisphere) |>
  pairwise_t_test(alpha ~ attention, #group1 vs group2
                  paired = TRUE, 
                  p.adjust.method = "bonferroni")
flextable(pw_occipital,
          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_Occipital

Double

alpha

Left_Attention

Right_Attention

30

30

6.059023

29

0.000001350

0.000001350

****

Right_Occipital

Double

alpha

Left_Attention

Right_Attention

30

30

-3.687508

29

0.000928000

0.000928000

***

Left_Occipital

NoStim

alpha

Left_Attention

Right_Attention

30

30

6.371193

29

0.000000578

0.000000578

****

Right_Occipital

NoStim

alpha

Left_Attention

Right_Attention

30

30

-5.107319

29

0.000018800

0.000018800

****

One Sample t-test

#Tidy Data for averaged stimulus

G_all_o <- all_stim_occipital |>
  mutate(
    Left_Occipital_ROI_Left_Attention = 
      ((`Left_Occipital_ROI_Left_Attention_stim_Double` + `Left_Occipital_ROI_Left_Attention_stim_NoStim`)/2),
  
    Left_Occipital_ROI_Right_Attention = 
      ((`Left_Occipital_ROI_Right_Attention_stim_Double` + `Left_Occipital_ROI_Right_Attention_stim_NoStim`)/2),
    
    Right_Occipital_ROI_Left_Attention = 
      ((`Right_Occipital_ROI_Left_Attention_stim_Double` + `Right_Occipital_ROI_Left_Attention_stim_NoStim`)/2),
   
    Right_Occipital_ROI_Right_Attention = 
      ((`Right_Occipital_ROI_Right_Attention_stim_Double` + `Right_Occipital_ROI_Right_Attention_stim_NoStim`)/2)) |>
      
  select(1,
         "Left_Occipital_ROI_Left_Attention", 
         "Left_Occipital_ROI_Right_Attention",
         "Right_Occipital_ROI_Left_Attention",
         "Right_Occipital_ROI_Right_Attention")

Double

t1_oD <- t.test(GD_occipital$"Left_Occipital_ROI_Left_Attention_stim_Double",
           mu = 0,
           alternative = "greater")
t1_oD_mean <- mean(GD_occipital$Left_Occipital_ROI_Left_Attention_stim_Double)
t1_oD_sd <- sd(GD_occipital$Left_Occipital_ROI_Left_Attention_stim_Double)
t1_oD_es <- cohensD(GD_occipital$Left_Occipital_ROI_Left_Attention_stim_Double, mu = 0)

t2_oD <- t.test(GD_occipital$"Left_Occipital_ROI_Right_Attention_stim_Double",
           mu = 0,
           alternative = "less")
t2_oD_mean <- mean(GD_occipital$Left_Occipital_ROI_Right_Attention_stim_Double)
t2_oD_sd <- sd(GD_occipital$Left_Occipital_ROI_Right_Attention_stim_Double)
t2_oD_es <- cohensD(GD_occipital$Left_Occipital_ROI_Right_Attention_stim_Double, mu = 0)


t3_oD <- t.test(GD_occipital$"Right_Occipital_ROI_Left_Attention_stim_Double",
           mu = 0,
           alternative = "less")
t3_oD_mean <- mean(GD_occipital$Right_Occipital_ROI_Left_Attention_stim_Double)
t3_oD_sd <- sd(GD_occipital$Right_Occipital_ROI_Left_Attention_stim_Double)
t3_oD_es <- cohensD(GD_occipital$Right_Occipital_ROI_Left_Attention_stim_Double, mu = 0)


t4_oD <- t.test(GD_occipital$"Right_Occipital_ROI_Right_Attention_stim_Double",
           mu = 0,
           alternative = "greater")
t4_oD_mean <- mean(GD_occipital$Right_Occipital_ROI_Right_Attention_stim_Double)
t4_oD_sd <- sd(GD_occipital$Right_Occipital_ROI_Right_Attention_stim_Double)
t4_oD_es <- cohensD(GD_occipital$Right_Occipital_ROI_Right_Attention_stim_Double, mu = 0)

t_oD_table <- map_df(list(t1_oD, t2_oD, t3_oD, t4_oD), tidy) 

mutate(t_oD_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_oD_sd,
                                t2_oD_sd,
                                t3_oD_sd,
                                t4_oD_sd),
       "Effect Size (d)" = c(t1_oD_es,
                         t2_oD_es,
                         t3_oD_es,
                         t4_oD_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.04737487

0.03150716

8.2356796

0.000000002214707

29

0.03760082

Inf

One Sample t-test

greater

1.5036225

Left Hemisphere - Right Attention

0.00564111

0.03912083

0.7897999

0.781972354616179

29

-Inf

0.01777705

One Sample t-test

less

0.1441971

Right Hemisphere - Left Attention

0.01418297

0.03669295

2.1171189

0.978526026664741

29

-Inf

0.02556574

One Sample t-test

less

0.3865313

Right Hemisphere - Right Attention

0.03955525

0.04273697

5.0694516

0.000010456360396

29

0.02629752

Inf

One Sample t-test

greater

0.9255510

NoStim

t1_oN <- t.test(GN_occipital$"Left_Occipital_ROI_Left_Attention_stim_NoStim",
           mu = 0,
           alternative = "greater")
t1_oN_mean <- mean(GN_occipital$Left_Occipital_ROI_Left_Attention_stim_NoStim)
t1_oN_sd <- sd(GN_occipital$Left_Occipital_ROI_Left_Attention_stim_NoStim)
t1_oN_es <- cohensD(GN_occipital$Left_Occipital_ROI_Left_Attention_stim_NoStim, mu = 0)

t2_oN <- t.test(GN_occipital$"Left_Occipital_ROI_Right_Attention_stim_NoStim",
           mu = 0,
           alternative = "less")
t2_oN_mean <- mean(GN_occipital$Left_Occipital_ROI_Right_Attention_stim_NoStim)
t2_oN_sd <- sd(GN_occipital$Left_Occipital_ROI_Right_Attention_stim_NoStim)
t2_oN_es <- cohensD(GN_occipital$Left_Occipital_ROI_Right_Attention_stim_NoStim, mu = 0)


t3_oN <- t.test(GN_occipital$"Right_Occipital_ROI_Left_Attention_stim_NoStim",
           mu = 0,
           alternative = "less")
t3_oN_mean <- mean(GN_occipital$Right_Occipital_ROI_Left_Attention_stim_NoStim)
t3_oN_sd <- sd(GN_occipital$Right_Occipital_ROI_Left_Attention_stim_NoStim)
t3_oN_es <- cohensD(GN_occipital$Right_Occipital_ROI_Left_Attention_stim_NoStim, mu = 0)


t4_oN <- t.test(GN_occipital$"Right_Occipital_ROI_Right_Attention_stim_NoStim",
           mu = 0,
           alternative = "greater")
t4_oN_mean <- mean(GN_occipital$Right_Occipital_ROI_Right_Attention_stim_NoStim)
t4_oN_sd <- sd(GN_occipital$Right_Occipital_ROI_Right_Attention_stim_NoStim)
t4_oN_es <- cohensD(GN_occipital$Right_Occipital_ROI_Right_Attention_stim_NoStim, mu = 0)

t_oN_table <- map_df(list(t1_oN, t2_oN, t3_oN, t4_oN), tidy) 

mutate(t_oN_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_oN_sd,
                                t2_oN_sd,
                                t3_oN_sd,
                                t4_oN_sd),
       "Effect Size (d)" = c(t1_oN_es,
                         t2_oN_es,
                         t3_oN_es,
                         t4_oN_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.053178412

0.04629933

6.2910225

0.0000003595577

29

0.03881558

Inf

One Sample t-test

greater

1.14857832

Left Hemisphere - Right Attention

0.003500427

0.04457691

0.4301022

0.6648496702868

29

-Inf

0.01732893

One Sample t-test

less

0.07852556

Right Hemisphere - Left Attention

0.009350656

0.03706369

1.3818282

0.9112112877044

29

-Inf

0.02084843

One Sample t-test

less

0.25228616

Right Hemisphere - Right Attention

0.059405114

0.06008796

5.4149815

0.0000040043818

29

0.04076482

Inf

One Sample t-test

greater

0.98863584

Neutral vs No Attention

GD_parietal_NN <- grad_double |>
  mutate(Left_parietal_ROI_neutral_attention_stim_Double = `Left-parietal_ROI_neutral_attention`,
         Right_parietal_ROI_neutral_attention_stim_Double = `Right-parietal_ROI_neutral_attention`,
         Left_parietal_ROI_no_attention_stim_Double = `Left-parietal_ROI_no_attention`,
         Right_parietal_ROI_no_attention_stim_Double = `Right-parietal_ROI_no_attention`) |>
  select(1,
         "Left_parietal_ROI_neutral_attention_stim_Double",
         "Right_parietal_ROI_neutral_attention_stim_Double",
         "Left_parietal_ROI_no_attention_stim_Double",
         "Right_parietal_ROI_no_attention_stim_Double")
GN_parietal_NN <- grad_nostim |>
  mutate(Left_parietal_ROI_neutral_attention_stim_NoStim = `Left-parietal_ROI_neutral_attention`,
         Right_parietal_ROI_neutral_attention_stim_NoStim = `Right-parietal_ROI_neutral_attention`,
         Left_parietal_ROI_no_attention_stim_NoStim = `Left-parietal_ROI_no_attention`,
         Right_parietal_ROI_no_attention_stim_NoStim = `Right-parietal_ROI_no_attention`) |>
  select(1,
         "Left_parietal_ROI_neutral_attention_stim_NoStim",
         "Right_parietal_ROI_neutral_attention_stim_NoStim",
         "Left_parietal_ROI_no_attention_stim_NoStim",
         "Right_parietal_ROI_no_attention_stim_NoStim")

all_stim_parietal_NN <- 
  full_join(GD_parietal_NN, GN_parietal_NN, by = "...1") 
  

grad_parietal_NN <- pivot_longer(all_stim_parietal_NN,
  cols = c(Left_parietal_ROI_neutral_attention_stim_Double:Right_parietal_ROI_no_attention_stim_NoStim),
  names_to = c("hemisphere", "attention", "stimulus"),
  names_pattern = "(.*)_ROI_(.*)_stim_(.*)",
  values_to = "alpha")