Vorarbeit

knitr::opts_chunk$set(include = FALSE)
#erstmal alles aufraeumen
#rm(list=ls())
#install.packages(ez)
library(ez)

#roundEZ
roundEZ=function(res,digits = 5){
  cols=names(res$ANOVA)
  cols=cols[!cols=="p<.05" &!cols=="Effect"]
  for (icol in cols){
    res$ANOVA[[icol]]=round(res$ANOVA[[icol]],digits = digits)
  }
  
  cols=names(res$"Mauchly's Test for Sphericity")
  cols=cols[!cols=="p<.05" &!cols=="Effect"]
  for (icol in cols){
    res$"Mauchly's Test for Sphericity"[[icol]]=round(res$"Mauchly's Test for Sphericity"[[icol]],digits = digits)
  }
  
  cols=names(res$"Sphericity Corrections")
  cols=cols[!cols=="p[GG]<.05" &!cols=="p[HF]<.05" &!cols=="Effect"]
  for (icol in cols){
    res$"Sphericity Corrections"[[icol]]=round(res$"Sphericity Corrections"[[icol]],digits = digits)
  }
  return(res)
}
require("knitr")
opts_knit$set(root.dir = "/Users/Lea/Documents/EEG/Daten")

Umwandlung der Daten

#Pfad festlegen
opts_knit$set(root.dir = "/Users/Lea/Documents/EEG")

#Die Daten einlesen
daten_rand = read.table("/Users/Lea/Documents/EEG/Lea_MMN_randmeanamps_neu.txt", 
                        header = TRUE)


#Im Long-Format; Format der Namen: Elektrode_Stimulustyp_Bedingung
daten2_rand = reshape(daten_rand, direction = "long", 
              varying = 1:10, v.names = "Voltage", 
              timevar = c("Elektrode","Stimulustyp","Bedingung"), sep = "_")

#Keine Automatische Erkennung der Variablenauspruegungen: 
#deswegen muss man selbst Hand anlegen
daten2_rand$Stimulustyp = rep(c(rep("STA",max(daten2_rand$id)),
                                rep("DEV",max(daten2_rand$id))),
                                ncol(daten_rand)/2)
daten2_rand$Elektrode = c(rep("Fz",2*max(daten2_rand$id)),
                          rep("Cz",2*max(daten2_rand$id)),
                          rep("Pz",2*max(daten2_rand$id)),
rep("M1",2*max(daten2_rand$id)),rep("M2",2*max(daten2_rand$id)))
daten2_rand$Bedingung = "rand"

#Bedeutungslose ID-Variable loeschen, damit man sie nicht 
#mehr mit der VP-Nummer verwechseln kann
daten2_rand=daten2_rand[,-c(ncol(daten2_rand))]

#Einlesen der Daten der predictable Bedingung
opts_knit$set(root.dir = "/Users/Lea/Documents/EEG")

#Die Daten einlesen
daten3_pred = read.table("/Users/Lea/Documents/EEG/Lea_MMN_predmeanamps_neu.txt", 
                         header = TRUE)

#Im Long-Format; Format der Namen: Elektrode_Stimulustyp_Bedingung
daten4_pred = reshape(daten3_pred, direction = "long", 
              varying = 1:10, v.names = "Voltage", 
              timevar = c("Elektrode","Stimulustyp","Bedingung"), sep = "_")

#Keine Automatische Erkennung der Variablenauspraegungen: 
#deswegen muss man selbst Hand anlegen
daten4_pred$Stimulustyp = rep(c(rep("STA",max(daten4_pred$id)),
                                rep("DEV",max(daten4_pred$id))),
                                ncol(daten3_pred)/2)
daten4_pred$Elektrode = c(rep("Fz",2*max(daten4_pred$id)),
                          rep("Cz",2*max(daten4_pred$id)),
                          rep("Pz",2*max(daten4_pred$id)), rep("M1",2*max(daten4_pred$id)),rep("M2",2*max(daten4_pred$id)))
daten4_pred$Bedingung = "pred"

#Bedeutungslose ID-Variable loeschen, damit man sie nicht 
#mehr mit der VP-Nummer verwechseln kann
daten4_pred=daten4_pred[,-c(ncol(daten4_pred))]

#Daten in einer Datei
daten_all=rbind(daten2_rand, daten4_pred)

Statistische Analyse

ANOVA

#Erzeugung einer neuen Variable
daten_all$VP= rep(1:22,20)

library('ez')
ezANOVA (data=daten_all, dv = Voltage, wid = VP, 
         within_full = .(Elektrode, Stimulustyp, Bedingung), 
         within=.(Elektrode, Stimulustyp, Bedingung))
## $ANOVA
##                            Effect DFn DFd          F            p p<.05
## 2                       Elektrode   4  84 10.1352251 9.618648e-07     *
## 3                     Stimulustyp   1  21  2.3257183 1.421714e-01      
## 4                       Bedingung   1  21  0.3204668 5.773273e-01      
## 5           Elektrode:Stimulustyp   4  84  0.8845274 4.768910e-01      
## 6             Elektrode:Bedingung   4  84  0.2164807 9.285886e-01      
## 7           Stimulustyp:Bedingung   1  21  3.1810681 8.895619e-02      
## 8 Elektrode:Stimulustyp:Bedingung   4  84  2.6996360 3.600030e-02     *
##            ges
## 2 0.0560029143
## 3 0.0204510867
## 4 0.0004317956
## 5 0.0102981231
## 6 0.0005322160
## 7 0.0120015406
## 8 0.0110339608
## 
## $`Mauchly's Test for Sphericity`
##                            Effect           W            p p<.05
## 2                       Elektrode 0.082088291 2.325113e-07     *
## 5           Elektrode:Stimulustyp 0.001384991 6.729087e-23     *
## 6             Elektrode:Bedingung 0.091026169 5.449620e-07     *
## 8 Elektrode:Stimulustyp:Bedingung 0.052530598 5.559529e-09     *
## 
## $`Sphericity Corrections`
##                            Effect       GGe        p[GG] p[GG]<.05
## 2                       Elektrode 0.5764064 0.0001080591         *
## 5           Elektrode:Stimulustyp 0.3109219 0.3781916376          
## 6             Elektrode:Bedingung 0.5868177 0.8391389044          
## 8 Elektrode:Stimulustyp:Bedingung 0.5254914 0.0757745400          
##         HFe        p[HF] p[HF]<.05
## 2 0.6515832 4.642922e-05         *
## 5 0.3209244 3.811152e-01          
## 6 0.6653176 8.632331e-01          
## 8 0.5852889 6.892936e-02

Dreifachinteraktion (Elektrode, Stimulustyp, Bedingung)

res=ezANOVA(data = daten_all, dv = Voltage, wid = VP,
        type = 3, within = .(Elektrode, Stimulustyp, Bedingung)) 
roundEZ(res,digits = 5)
## $ANOVA
##                            Effect DFn DFd        F       p p<.05     ges
## 2                       Elektrode   4  84 10.13523 0.00000     * 0.05600
## 3                     Stimulustyp   1  21  2.32572 0.14217       0.02045
## 4                       Bedingung   1  21  0.32047 0.57733       0.00043
## 5           Elektrode:Stimulustyp   4  84  0.88453 0.47689       0.01030
## 6             Elektrode:Bedingung   4  84  0.21648 0.92859       0.00053
## 7           Stimulustyp:Bedingung   1  21  3.18107 0.08896       0.01200
## 8 Elektrode:Stimulustyp:Bedingung   4  84  2.69964 0.03600     * 0.01103
## 
## $`Mauchly's Test for Sphericity`
##                            Effect       W p p<.05
## 2                       Elektrode 0.08209 0     *
## 5           Elektrode:Stimulustyp 0.00138 0     *
## 6             Elektrode:Bedingung 0.09103 0     *
## 8 Elektrode:Stimulustyp:Bedingung 0.05253 0     *
## 
## $`Sphericity Corrections`
##                            Effect     GGe   p[GG] p[GG]<.05     HFe
## 2                       Elektrode 0.57641 0.00011         * 0.65158
## 5           Elektrode:Stimulustyp 0.31092 0.37819           0.32092
## 6             Elektrode:Bedingung 0.58682 0.83914           0.66532
## 8 Elektrode:Stimulustyp:Bedingung 0.52549 0.07577           0.58529
##     p[HF] p[HF]<.05
## 2 0.00005         *
## 5 0.38112          
## 6 0.86323          
## 8 0.06893

Zweifachinteraktion, ANOVA fuer Bedingung PRED

res = ezANOVA(data = daten_all[daten_all$Bedingung=='pred',], 
            dv = Voltage, wid = VP, 
            type = 3, within = .(Elektrode, Stimulustyp)) 
roundEZ(res,digits = 5)
## $ANOVA
##                  Effect DFn DFd       F       p p<.05     ges
## 2             Elektrode   4  84 4.68504 0.00184     * 0.04051
## 3           Stimulustyp   1  21 0.07332 0.78920       0.00096
## 4 Elektrode:Stimulustyp   4  84 0.14962 0.96268       0.00235
## 
## $`Mauchly's Test for Sphericity`
##                  Effect       W p p<.05
## 2             Elektrode 0.04933 0     *
## 4 Elektrode:Stimulustyp 0.00628 0     *
## 
## $`Sphericity Corrections`
##                  Effect     GGe   p[GG] p[GG]<.05     HFe   p[HF]
## 2             Elektrode 0.56852 0.01091         * 0.64122 0.00805
## 4 Elektrode:Stimulustyp 0.33014 0.77090           0.34367 0.78035
##   p[HF]<.05
## 2         *
## 4

Zweifachinteraktion, ANOVA fuer Bedingung RAND

res = ezANOVA(data = daten_all[daten_all$Bedingung=='rand',], 
            dv = Voltage, wid = VP, 
            type = 3, detailed = TRUE, within = .(Elektrode, Stimulustyp)) 
roundEZ(res, digits = 5)
## $ANOVA
##                  Effect DFn DFd     SSn      SSd        F       p p<.05
## 1           (Intercept)   1  21 0.00695 18.29704  0.00798 0.92967      
## 2             Elektrode   4  84 6.46263 11.11217 12.21321 0.00000     *
## 3           Stimulustyp   1  21 6.17963 19.13661  6.78136 0.01657     *
## 4 Elektrode:Stimulustyp   4  84 3.83232 24.93524  3.22751 0.01627     *
##       ges
## 1 0.00009
## 2 0.08084
## 3 0.07757
## 4 0.04957
## 
## $`Mauchly's Test for Sphericity`
##                  Effect       W     p p<.05
## 2             Elektrode 0.14809 3e-05     *
## 4 Elektrode:Stimulustyp 0.00878 0e+00     *
## 
## $`Sphericity Corrections`
##                  Effect     GGe   p[GG] p[GG]<.05     HFe   p[HF]
## 2             Elektrode 0.55392 0.00003         * 0.62213 0.00001
## 4 Elektrode:Stimulustyp 0.38004 0.06518           0.40355 0.06181
##   p[HF]<.05
## 2         *
## 4

Post-Hoc t-tests für RAND-Bedingung

#Fz
res = ezANOVA (data=daten_all[daten_all$Bedingung == 'rand' & daten_all$Elektrode == 'Fz',], 
               dv = Voltage, wid = VP, within = Stimulustyp)
roundEZ(res, digits = 4)
## $ANOVA
##        Effect DFn DFd      F      p p<.05    ges
## 2 Stimulustyp   1  21 6.0962 0.0222     * 0.1591
#Cz
res = ezANOVA (data=daten_all[daten_all$Bedingung == 'rand' & daten_all$Elektrode == 'Cz',], 
               dv = Voltage, wid = VP, within = Stimulustyp)
roundEZ(res, digits = 4)
## $ANOVA
##        Effect DFn DFd      F      p p<.05    ges
## 2 Stimulustyp   1  21 5.2901 0.0318     * 0.1266
#Pz
res = ezANOVA (data=daten_all[daten_all$Bedingung == 'rand' & daten_all$Elektrode == 'Pz',], 
               dv = Voltage, wid = VP, within = Stimulustyp)
roundEZ(res, digits = 4)
## $ANOVA
##        Effect DFn DFd      F      p p<.05    ges
## 2 Stimulustyp   1  21 4.6055 0.0437     * 0.1217
#M1
res = ezANOVA (data=daten_all[daten_all$Bedingung == 'rand' & daten_all$Elektrode == 'M1',], 
               dv = Voltage, wid = VP, within = Stimulustyp)
roundEZ(res, digits = 4)
## $ANOVA
##        Effect DFn DFd      F      p p<.05    ges
## 2 Stimulustyp   1  21 0.3567 0.5567       0.0072
#M2
res = ezANOVA (data=daten_all[daten_all$Bedingung == 'rand' & daten_all$Elektrode == 'M2',], 
               dv = Voltage, wid = VP,  within = Stimulustyp)
roundEZ(res, digits = 4)
## $ANOVA
##        Effect DFn DFd      F      p p<.05    ges
## 2 Stimulustyp   1  21 0.0962 0.7595       0.0026

Deskriptive Statistik

ezStats(data=daten_all, dv = Voltage, wid = VP, 
               within=.(Elektrode))
##   Elektrode  N        Mean        SD      FLSD
## 1        Cz 22 -0.20076896 0.4855416 0.1582892
## 2        Fz 22 -0.20045253 0.3573031 0.1582892
## 3        M1 22  0.08186593 0.2350155 0.1582892
## 4        M2 22  0.17573022 0.1788832 0.1582892
## 5        Pz 22  0.10335384 0.5190624 0.1582892

Deskriptive Statistik: Dreifachinteraktion (Elektrode, Stimulustyp, Bedingung)

ezStats(data=daten_all, dv = Voltage, wid = VP, 
               within=.(Elektrode, Stimulustyp, Bedingung))
##    Elektrode Stimulustyp Bedingung  N        Mean        SD      FLSD
## 1         Cz         DEV      pred 22 -0.24245781 1.0075720 0.2660116
## 2         Cz         DEV      rand 22 -0.48155621 0.9001619 0.2660116
## 3         Cz         STA      pred 22 -0.17904177 0.8854458 0.2660116
## 4         Cz         STA      rand 22  0.09997994 0.6418857 0.2660116
## 5         Fz         DEV      pred 22 -0.18826831 0.9562445 0.2660116
## 6         Fz         DEV      rand 22 -0.46707550 0.6954176 0.2660116
## 7         Fz         STA      pred 22 -0.19112121 0.8468214 0.2660116
## 8         Fz         STA      rand 22  0.04465491 0.4912674 0.2660116
## 9         M1         DEV      pred 22 -0.01783650 0.4859942 0.2660116
## 10        M1         DEV      rand 22  0.06965490 0.4230190 0.2660116
## 11        M1         STA      pred 22  0.14843876 0.4117468 0.2660116
## 12        M1         STA      rand 22  0.12720654 0.2428641 0.2660116
## 13        M2         DEV      pred 22  0.18115793 0.4454431 0.2660116
## 14        M2         DEV      rand 22  0.20566808 0.3487959 0.2660116
## 15        M2         STA      pred 22  0.13845748 0.3937429 0.2660116
## 16        M2         STA      rand 22  0.17763738 0.1889058 0.2660116
## 17        Pz         DEV      pred 22  0.04597905 0.8334133 0.2660116
## 18        Pz         DEV      rand 22 -0.13657477 0.8610132 0.2660116
## 19        Pz         STA      pred 22  0.08738863 0.8295528 0.2660116
## 20        Pz         STA      rand 22  0.41662247 0.6443794 0.2660116

Deskriptive Statistik: Post-Hoc t-Tests: Stimulustyp x Elektrode pro Bedingung (RAND/PRED)

res = ezStats (data=daten_all[daten_all$Bedingung == 'rand',], 
               dv = Voltage, wid = VP, 
               within=.(Elektrode, Stimulustyp))
roundEZ(res, digits = 4)
##    Elektrode Stimulustyp  N        Mean        SD      FLSD
## 1         Cz         DEV 22 -0.48155621 0.9001619 0.3266783
## 2         Cz         STA 22  0.09997994 0.6418857 0.3266783
## 3         Fz         DEV 22 -0.46707550 0.6954176 0.3266783
## 4         Fz         STA 22  0.04465491 0.4912674 0.3266783
## 5         M1         DEV 22  0.06965490 0.4230190 0.3266783
## 6         M1         STA 22  0.12720654 0.2428641 0.3266783
## 7         M2         DEV 22  0.20566808 0.3487959 0.3266783
## 8         M2         STA 22  0.17763738 0.1889058 0.3266783
## 9         Pz         DEV 22 -0.13657477 0.8610132 0.3266783
## 10        Pz         STA 22  0.41662247 0.6443794 0.3266783
res = ezStats (data=daten_all[daten_all$Bedingung == 'pred',], 
               dv = Voltage, wid = VP, 
               within=.(Elektrode, Stimulustyp))
roundEZ(res, digits = 4)
##    Elektrode Stimulustyp  N        Mean        SD      FLSD
## 1         Cz         DEV 22 -0.24245781 1.0075720 0.4067697
## 2         Cz         STA 22 -0.17904177 0.8854458 0.4067697
## 3         Fz         DEV 22 -0.18826831 0.9562445 0.4067697
## 4         Fz         STA 22 -0.19112121 0.8468214 0.4067697
## 5         M1         DEV 22 -0.01783650 0.4859942 0.4067697
## 6         M1         STA 22  0.14843876 0.4117468 0.4067697
## 7         M2         DEV 22  0.18115793 0.4454431 0.4067697
## 8         M2         STA 22  0.13845748 0.3937429 0.4067697
## 9         Pz         DEV 22  0.04597905 0.8334133 0.4067697
## 10        Pz         STA 22  0.08738863 0.8295528 0.4067697

Deskriptive Statistik für Haupteffekte in RAND-Bedingung

res = ezStats (data=daten_all[daten_all$Bedingung == 'rand',], 
               dv = Voltage, wid = VP, 
               within=.(Elektrode))
roundEZ(res, digits = 4)
##   Elektrode  N        Mean        SD      FLSD
## 1        Cz 22 -0.19078813 0.5094646 0.1542048
## 2        Fz 22 -0.21121030 0.3552727 0.1542048
## 3        M1 22  0.09843072 0.2605632 0.1542048
## 4        M2 22  0.19165273 0.1836745 0.1542048
## 5        Pz 22  0.14002385 0.4613210 0.1542048
res = ezStats (data=daten_all[daten_all$Bedingung == 'rand',], 
               dv = Voltage, wid = VP, 
               within=.(Stimulustyp))
roundEZ(res, digits = 4)
##   Stimulustyp  N       Mean        SD      FLSD
## 1         DEV 22 -0.1619767 0.4909869 0.2676851
## 2         STA 22  0.1732202 0.3397689 0.2676851

Deskriptive Statistik für Haupteffekt in PRED-Bedingung

res = ezStats (data=daten_all[daten_all$Bedingung == 'pred',], 
               dv = Voltage, wid = VP, 
               within=.(Elektrode))
roundEZ(res, digits = 4)
##   Elektrode  N        Mean        SD      FLSD
## 1        Cz 22 -0.21074979 0.5737546 0.2176931
## 2        Fz 22 -0.18969476 0.4373629 0.2176931
## 3        M1 22  0.06530113 0.2475777 0.2176931
## 4        M2 22  0.15980770 0.2100556 0.2176931
## 5        Pz 22  0.06668384 0.6904578 0.2176931