ERPsimulate_script
Upcoming SlideShare
Loading in...5
×
 

ERPsimulate_script

on

  • 449 views

R script to accompany blogpost ...

R script to accompany blogpost
http://deevybee.blogspot.co.uk/2013/06/interpreting-unexpected-significant.html
Generates random normal numbers to simulate ERP data. Used to demonstrate how false positive rate depends on N factors in ANOVA.

Statistics

Views

Total Views
449
Views on SlideShare
443
Embed Views
6

Actions

Likes
0
Downloads
2
Comments
0

1 Embed 6

https://twitter.com 6

Accessibility

Upload Details

Uploaded via as Microsoft Word

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

ERPsimulate_script ERPsimulate_script Document Transcript

  • #-------------------------------------------------------------------------------------# ERPsimulate# by D V M Bishop, 18thJune 2013# Script to accompany blogpost:# http://deevybee.blogspot.co.uk/2013/06/interpreting-unexpected-significant.html#-------------------------------------------------------------------------------------# Create datasets of simulated ERP averages by random number generation# Assign means to fictitious subjects/conditions/electrodes and use ezANOVA# to analyse# ForezANOVA, data are organised in a format where repeated# measures are stacked below one another#For each dataset, do ANOVA for 4 way, 3 way (elec as factor) and 2 way (difference score as factor)require(MASS) #used for multivariate random normal data generation, mvrnorm commandrequire(ez) #for ezanovaptm<- proc.time() #500 runs took about 5 minsnsims=8# n values to simulate for each subject; 2 locs x 2 sides x 2 conditionsmycorrel=0 #intercorrelation between dvs, will be in mysigma matrixmysigma=matrix(c(rep(mycorrel,nsims*nsims)),nsims,nsims)for (i in 1:nsims){mysigma[i,i]=1} #ones on diagonalmymu=rep(0,nsims) #mean is zeronsub=40 # here 2 groups of 20mydata=matrix(rep(0,nsub*nsims*8),nsub*nsims,8)#initialise data matrix; 8 cols to include allpossible factorsnrun=20 #N runs through the simulation; ANOVA done for each one; once working, try around 1000runsmypvalues=array(0,dim=c(25,nrun)) #initialise array for 4way, 3way and 2way stacked: used to saveANOVA pvalue outputs - 15 terms altogethermyeffect=0 #this effect will be added to cols 4-8 (one level of task factor); .2 will give a modesteffectfor (myrun in 1:nrun){ # do repeated runs of the simulationmyavg=mvrnorm(n = nsub, mymu, mysigma, empirical = FALSE) #make matrix of random values#for all subjects with intercorrelation#determined by mysigmamyavg[,4:8]=myavg[,4:8]+myeffect #constant effect added to cols corresponding to one level oftaskfor (mysub in 1:nsub){mygrp=1
  • if (mysub>20){mygrp=2}#2 groups each of 20 #can change this if desiredstartrow=(mysub-1)*(nsims)+1endrow=mysub*nsimsmydata[startrow:endrow,1]=mysub;#ID in col 1mydata[startrow:endrow,2]=t(myavg[mysub,]) #simulated avgs stacked in col 2mydata[startrow:endrow,3]=mygrp;#group in col 3mydata[startrow:endrow,4]=c(1,1,1,1,2,2,2,2);#task in col 4mydata[startrow:endrow,5]=c(1,1,2,2,1,1,2,2) #side in col 5mydata[startrow:endrow,6]=c(1,2,1,2,1,2,1,2) #loc in col 6mydata[startrow:endrow,7]=c(1,2,3,4,1,2,3,4) #electrode in col 7 (equiv to side x loc)mydata[startrow:endrow,8]=c(myavg[mysub,1]-myavg[mysub,5],myavg[mysub,2]-myavg[mysub,6],myavg[mysub,3]-myavg[mysub,7],myavg[mysub,4]-myavg[mysub,8])#difference between the two levels of task} #end of mysub loopmydf=data.frame(mydata)names(mydf)=c("id","amp","group" ,"task", "side", "loc","elec","diff")mydf$id<- as.factor(mydf$id)mydf$group<- as.factor(mydf$group)mydf$task<- as.factor(mydf$task)mydf$side<- as.factor(mydf$side)mydf$loc<- as.factor(mydf$loc)mydf$elec<- as.factor(mydf$elec)mydf$diff=as.numeric(mydf$diff)mydf2 <- mydf[which(mydf$task==1 ), ] #for 2 way Anova, just take rows where task=1 so theresjust one difference valueattach(mydf)attach(mydf2)myresult4=ezANOVA(data=mydf, dv=.(amp), wid=.(id), between=.(group),within=.(task, side,loc),type=3)myresult3=ezANOVA(data=mydf,dv=.(amp),wid=.(id),between=.(group),within=.(task,elec),type=3)myresult2=ezANOVA(data=mydf2,dv=.(diff),wid=.(id),between=.(group),within=.(elec),type=3)detach(mydf)detach(mydf2)mypvalues[,myrun]=c(myresult4$ANOVA[,5],myresult3$ANOVA[,5],myresult2$ANOVA[,5]) #5th colof ANOVA output has pvaluesrownames(mypvalues)=c(myresult4$ANOVA$Effect,myresult3$ANOVA$Effect,myresult2$ANOVA$Effect)}myfilename="mypvalues_all.TXT"#write.table(mydata, "mydata.txt", sep="t") #write data from last run only to a tab-sep text file; willbe saved in default directory
  • write.table(t(mypvalues), myfilename, sep="t") #write transposed pvalues to a tab-sep text file; willbe saved in default directory#First 15 cols will give output for 4-way, next 7 give output for 3-way, and last 3 for 2-way#NB for 2-way, the group term is equivalent to group x task in other ANOVAs, because dv is taskdifferenceproc.time()-ptm #check how long it takes to run