This script contains pre-processing of the Joint Action Task related to participants who took part in our TMS study on race, requesting to cooperate with either a Black or White avatar.

First, we load packages we need for analyses

library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.4.0      ✔ purrr   0.3.5 
## ✔ tibble  3.1.8      ✔ dplyr   1.0.10
## ✔ tidyr   1.2.1      ✔ stringr 1.4.1 
## ✔ readr   2.1.3      ✔ forcats 0.5.2 
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(psych)
## 
## Caricamento pacchetto: 'psych'
## 
## I seguenti oggetti sono mascherati da 'package:ggplot2':
## 
##     %+%, alpha
library(Hmisc)
## Caricamento del pacchetto richiesto: lattice
## Caricamento del pacchetto richiesto: survival
## Caricamento del pacchetto richiesto: Formula
## 
## Caricamento pacchetto: 'Hmisc'
## 
## Il seguente oggetto è mascherato da 'package:psych':
## 
##     describe
## 
## I seguenti oggetti sono mascherati da 'package:dplyr':
## 
##     src, summarize
## 
## I seguenti oggetti sono mascherati da 'package:base':
## 
##     format.pval, units
library(reshape2)
## 
## Caricamento pacchetto: 'reshape2'
## 
## Il seguente oggetto è mascherato da 'package:tidyr':
## 
##     smiths
library(here)
## here() starts at C:/Users/Giovy/Downloads/Progetti Attivi/TMS-race/EXP_3/Analisi Joint Task
library(readr)
library(utils)

PRE-PROCESSING First, we set the directory

here::i_am("data_cleaning_TMS_BW.Rmd")
## here() starts at C:/Users/Giovy/Downloads/Progetti Attivi/TMS-race/EXP_3/Analisi Joint Task
setwd("C:/Users/Giovy/Downloads/Progetti Attivi/TMS-race/EXP_3/Analisi Joint Task") 
bala =  read.table("balan_bw.csv",sep = ";",header=T) #upload balancing file

Then, we specify file names and the data directory

file_names <- dir("C:/Users/Giovy/Downloads/Progetti Attivi/TMS-race/EXP_3/Script analisi in R_Valerio/dati")
setwd("C:/Users/Giovy/Downloads/Progetti Attivi/TMS-race/EXP_3/Analisi Joint Task/dati") 

We create a dataframe

BW =read_delim(file_names, delim = ";", 
           escape_double = FALSE, trim_ws = TRUE) #adding a column with the filenames, header=F if columns have no title

BW_raw = merge(bala, BW, all=T) #merging with the balancing file

names <- c("Subject","Session", "bw","sito")
BW_raw[,names] <- lapply(BW_raw[,names] , factor) #declaring factors

We now clean the dataframe removing Catch Trials (CatchTrial_CORR =1) and Inaccurate trials (StartSoggetto1 = 1; StopSoggetto1 = 1)

BW_Clean = subset(BW_raw, BW_raw$CatchTrial_CORR != 1 &
                               BW_raw$StartSoggetto1 != 1 & BW_raw$StopSoggetto1 != 1)

We create a new dataframe in which Inaccurate trials are = NA (AccSogg1 = 0)

#BW_Clean$Hmax_ok = ifelse(BW_Clean$AccSogg1 == 1, BW_Clean$HmaxPolso1y,NA)     
#BW_Clean$MaxAp_ok = ifelse(BW_Clean$AccSogg1 == 1, BW_Clean$MaxAp1,NA)   
BW_Clean$Asynchrony_ok = ifelse(BW_Clean$AccSogg1 == 1, BW_Clean$RTNetto,NA)
BW_Clean$Start_ok = ifelse(BW_Clean$AccSogg1 == 1, BW_Clean$StartSoggetto1,NA)   
BW_Clean$Stop_ok = ifelse(BW_Clean$AccSogg1 == 1, BW_Clean$StopSoggetto1,NA)
BW_Clean$TM_ok = ifelse(BW_Clean$AccSogg1 == 1, BW_Clean$TMSoggetto1,NA)

Then, we add the independent variables and rename the factors level

BW_Clean$Grasp = ifelse(BW_Clean$CorrectResponse == 3, "Precision","Power")                                                         
BW_Clean$Movement = ifelse(BW_Clean$OP_UG == 0, "Complementary","Imitative")  

BW_Clean$Grasp = as.factor(BW_Clean$Grasp)                                                       
BW_Clean$Movement = as.factor(BW_Clean$Movement) 

OUTLIER REMOVAL We proceed to remove each trial that is 2.5 STDV above or below the Mean in each Condition in each Subject

First, we create a dataframe of means and stdv for each subj and cond

ordinaper <- BW_Clean %>% group_by(Subject,bw, sito, Movement, Grasp)
head(ordinaper)
## # A tibble: 0 × 10
## # Groups:   Subject, bw, sito, Movement, Grasp [0]
## # … with 10 variables: Subject <fct>, Session <fct>, bw <fct>, sito <fct>,
## #   Asynchrony_ok <lgl>, Start_ok <lgl>, Stop_ok <lgl>, TM_ok <lgl>,
## #   Grasp <fct>, Movement <fct>
tabella1 = ordinaper %>% summarise(
  av_Asy = mean(Asynchrony_ok,na.rm = TRUE),
  sd_Asy = sd(Asynchrony_ok,na.rm = TRUE),
  av_TM = mean(TM_ok,na.rm = TRUE),
  sd_TM = sd(TM_ok,na.rm = TRUE))
## `summarise()` has grouped output by 'Subject', 'bw', 'sito', 'Movement'. You
## can override using the `.groups` argument.

Ee now merge all our tables

df2 = merge(BW_Clean, tabella1, all=T)

We create a new dataset without the grasp level in wich we merge the “start” RT

ordinaper2 <- BW_Clean %>% group_by(Subject,bw, sito, Movement)
head(ordinaper2)
## # A tibble: 0 × 10
## # Groups:   Subject, bw, sito, Movement [0]
## # … with 10 variables: Subject <fct>, Session <fct>, bw <fct>, sito <fct>,
## #   Asynchrony_ok <lgl>, Start_ok <lgl>, Stop_ok <lgl>, TM_ok <lgl>,
## #   Grasp <fct>, Movement <fct>
tabella2 = ordinaper2 %>% summarise(
  av_Start = mean(Start_ok,na.rm = TRUE),
  sd_Start = sd(Start_ok,na.rm = TRUE))
## `summarise()` has grouped output by 'Subject', 'bw', 'sito'. You can override
## using the `.groups` argument.

We merge the last two dataset we created

df3 = merge(df2, tabella2, all=T)
str(df3)
## 'data.frame':    0 obs. of  16 variables:
##  $ Subject      : Factor w/ 0 levels: 
##  $ bw           : Factor w/ 0 levels: 
##  $ sito         : Factor w/ 0 levels: 
##  $ Movement     : Factor w/ 0 levels: 
##  $ Grasp        : Factor w/ 0 levels: 
##  $ Session      : Factor w/ 0 levels: 
##  $ Asynchrony_ok: logi 
##  $ Start_ok     : logi 
##  $ Stop_ok      : logi 
##  $ TM_ok        : logi 
##  $ av_Asy       : num 
##  $ sd_Asy       : num 
##  $ av_TM        : num 
##  $ sd_TM        : num 
##  $ av_Start     : num 
##  $ sd_Start     : num

Finally, we remove the outliers

df3$Asynchrony = ifelse(df3$Asynchrony_ok > df3$av_Asy + 2.5 * df3$sd_Asy | 
                          df3$Asynchrony_ok < df3$av_Asy - 2.5 * df3$sd_Asy, NA,df3$Asynchrony_ok )

df3$MovTime = ifelse(df3$TM_ok > df3$av_TM + 2.5 * df3$sd_TM | 
                         df3$TM_ok < df3$av_TM - 2.5 * df3$sd_TM, NA, df3$TM_ok )

df3$Start = ifelse(df3$Start_ok > df3$av_Start + 2.5 * df3$sd_Start | 
                     df3$Start_ok < df3$av_Start - 2.5 * df3$sd_Start, NA, df3$Start_ok)

Then, the final Dataset is create in Excel, with only the columns of interest

str(df3) 
## 'data.frame':    0 obs. of  19 variables:
##  $ Subject      : Factor w/ 0 levels: 
##  $ bw           : Factor w/ 0 levels: 
##  $ sito         : Factor w/ 0 levels: 
##  $ Movement     : Factor w/ 0 levels: 
##  $ Grasp        : Factor w/ 0 levels: 
##  $ Session      : Factor w/ 0 levels: 
##  $ Asynchrony_ok: logi 
##  $ Start_ok     : logi 
##  $ Stop_ok      : logi 
##  $ TM_ok        : logi 
##  $ av_Asy       : num 
##  $ sd_Asy       : num 
##  $ av_TM        : num 
##  $ sd_TM        : num 
##  $ av_Start     : num 
##  $ sd_Start     : num 
##  $ Asynchrony   : logi 
##  $ MovTime      : logi 
##  $ Start        : logi
keeps <- c("Subject","Session", "bw","sito","Movement","Grasp","Session","Asynchrony",
           "MovTime","Start")
df = df3[keeps]

str(df)
## 'data.frame':    0 obs. of  10 variables:
##  $ Subject   : Factor w/ 0 levels: 
##  $ Session   : Factor w/ 0 levels: 
##  $ bw        : Factor w/ 0 levels: 
##  $ sito      : Factor w/ 0 levels: 
##  $ Movement  : Factor w/ 0 levels: 
##  $ Grasp     : Factor w/ 0 levels: 
##  $ Session.1 : Factor w/ 0 levels: 
##  $ Asynchrony: logi 
##  $ MovTime   : logi 
##  $ Start     : logi
write.table(df, file = "DatiTMSBW.csv", sep = ",", row.names = F) #export in xlsx