eeg_motivated_contagion_analysis_eegdata.Rmd 3.88 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
---
title: "EEG_analysis"
author: "Amit"
date: "October 21, 2016"
output: html_document
---


```{r packages, include=FALSE}

11 12
library (ggplot2) ; library ('lme4');  library ('lmerTest');    library ('plyr'); library ('dplyr'); library ('psych'); library ('car'); library("tidyr"); 

13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
library('lmerTest'); library('coefplot2'); library('Rmisc'); library('ez')

pl1way <- function(dat,y,x) {

	pld <- summarySEwithin(dat,y,withinvars = x, idvar = "subject")

	tmp <- dat

	colnames(tmp) <- gsub(as.name(y),"y",colnames(tmp))
	colnames(tmp) <- gsub(as.name(x),"x",colnames(tmp))

	res <- ezANOVA(data=tmp,dv=y, wid=subject, within=.(x), type=3)$ANOVA                    						# ANOVA

	result <- paste0("F(",res$DFn,",",res$DFd,")=",round(res$F,2), ", GES=", round(res$ges,2), ", p=", round(res$p,3))

	ggplot(pld,aes(x=pld[,x],y=pld[,y], ymin=pld[,y]-ci,ymax=pld[,y]+ci)) + geom_pointrange(fatten = 8) +
		labs(x=x,y=y, title = result)
}

32 33 34 35 36 37 38 39 40 41 42 43 44
```

```{r get file, include=FALSE}
setwd("C:/Users/Amit/Dropbox/Research/emotional conformity-non conformity/motivated_contation_EEG_2017/Results")

d <- read.csv("eeg_data_motivated_contagion.csv")

head(d)

#d = subset (d, condition != "none")
#ds = subset (d, phase == "social")
```

45 46 47 48 49 50 51 52 53 54 55
### Andero's stuff ####

# separate phases
a <- d[d$phase=="indiv",]
b <- d[d$phase=="social",]

# make a wide format with each picture in a separate row and the phases in separate columns
D3 <- merge(a[,c("subject", "photo", "order", "rt", "P3", "LPP", "SLW")],b,by=c("subject", "photo"))
colnames(D3) <- gsub(".x", ".indiv", colnames(D3), fixed =T)
colnames(D3) <- gsub(".y", ".social", colnames(D3), fixed =T)

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

# DESCRIPTIVES
table(D3$subject, D3$condition)
ggplot(d, aes(P3)) + geom_density()
ggplot(d, aes(LPP)) + geom_density()
ggplot(d, aes(SLW)) + geom_density()


# AFFECTIVE MAIN EFFECT
summary(fit <- lmer(P3.indiv ~ photoType + (1|subject) + (1|photo), data = D3))

pl1way(D3,"P3.indiv","photoType")
pl1way(D3,"LPP.indiv","photoType")
pl1way(D3,"LPP.indiv","photoType")


72
# continuous model
73
summary(fit <- lmer(SLW.social ~ SLW.indiv + rate*grate + (1|subject) + (1|photo), data = D3))
74 75

coefplot2(fit)
76
visreg(fit, xvar = "grate", by = "rate")
77 78 79 80 81 82 83 84 85 86 87 88 89

# categorical model
summary(fit <- lmer(SLW.social ~ SLW.social + photoType*condition + (1|subject) + (1|photo), data = D3))

coefplot2(fit)

# model without neutral pictures
summary(fit <- lmer(SLW.social ~ SLW.indiv + condition + (1|subject) + (1|photo), data = D3[D3$photoType=="emo",]))

coefplot2(fit)



90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
### my plan is to create two wide formats
####1. will allow me to create a difference score from the first phase to the second
####2. Will allow me to create a difference scroe from the emotional and the neutral pictures

```{r }

id = which( colnames(d)=="id" )
condition = which( colnames(d)=="condition" )
urevent = which( colnames(d)=="urevent" )
photo = which( colnames(d)=="photo" )
photoType = which( colnames(d)=="photoType" )
phase =  which( colnames(d)=="phase" )
rate  = which( colnames(d)=="rate" )
rt  = which( colnames(d)=="rt" )
grate  = which( colnames(d)=="grate" )
P3  = which( colnames(d)=="P3" )
LPP  = which( colnames(d)=="LPP" )
SLW  = which( colnames(d)=="SLW" )


d= d[,c(id,condition,urevent, photoType, photo, phase, rate, rt, grate, P3, LPP, SLW)]
head(d)
rm(id,urevent, photoType, photo, phase, rate, rt, grate, P3, LPP, SLW)


d1 = subset (d, phase == "indiv")
d2 = subset (d, phase =="social")
d2 = d2[,c(9:11)]

colnames(d2)= c("P3s", "LPPs", "SLWs")
head(d2)


length (d1$LPP)
length (d2$LPPs)

```

## as you can see the length of the two datasets is not the same

Amit Goldenberg's avatar
Amit Goldenberg committed
130
```{r Look just at the second, include=FALSE}
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151


d %>%
  group_by(photoType) %>% 
  do(r = print(summary(lmer(P3 ~ condition +urevent+(1|id), data = .))))



d %>%
  group_by(photoType) %>% 
  do(r = print(summary(lmer(LPP ~ condition+urevent +(1|id), data = .))))


d %>%
  group_by(photoType) %>% 
  do(r = print(summary(lmer(SLW ~ condition+urevent +(1|id), data = .))))



```