Exemple #1
0
def mixedModelTrace(dm, model, winSize=1, effectIndex=1, **traceParams):

	"""
	desc:
		Perform a mixed model over a single trace. The dependent variable is
		specifed through the signal and phase keywords.

	arguments:
		dm:
			desc:	A DataMatrix.
			type:	DataMatrix
		model:
			desc:	An lmer-style model. This needs to be only the fixed and
					random effects part of the model, so everything after
					the `~` sign. For example `cond + (1|subject_nr)`.
			type:	str

	keywords:
		winSize:
			desc:	Indicates the number of samples that should be skipped
					each time. For a real analysis, this should be 1, but
					for a quick look, it can be increased (default=1)
			type:	int

	keyword-dict:
		*traceParams:	See getTrace().

	returns: |
		A DataMatrix with nr-of-effects*nr-of-samples rows and the following
		columns:

		- `i`: sample number
		- `effect`: name of effect
		- `est`: estimated effect (slope/ intercept)
		- `se`: standard error of effect
		- `t`: t-value of effect
	"""

	if not model.startswith('mmdv__ ~ '):
		model = 'mmdv__ ~ ' + model
	global R
	try:
		R
	except:
		R = RBridge()
	traceLen = traceParams['traceLen']
	l = [ ['i', 'effect', 'est', 'se', 't'] ]
	for i in range(0, traceLen, winSize):
		# First calculate the mean value for the current signal slice for each
		# trial and save that in a copy of the DataMatrix
		_dm = dm.addField('mmdv__', dtype=float)
		for trialId in range(len(_dm)):
			aTrace = getTrace(_dm[trialId], **traceParams)
			if i < len(aTrace):
				sliceMean = aTrace[i:i+winSize].mean()
			else:
				sliceMean = np.nan
			_dm['mmdv__'][trialId] = sliceMean
		# Do mixed effects
		R.load(_dm)
		_dm = R.lmer(model)
		_dm._print(sign=4, title='%d - %d' % (i, i+winSize))
		for k in range(winSize):
			if i+k >= traceLen:
				break
			for j in _dm.range():
					l.append([i+k, _dm['effect'][j], _dm['est'][j], _dm['se'][j],
						_dm['t'][j]])
	return DataMatrix(l)
def lme(dm, dv='iRt'):

	"""
	Runs the overall LME.

	Arguments:
	dm		--	A DataMatrix.

	Keyword arguments:
	dv		--	The dependent variable. (default='iRt')
	"""

	assert(dv in ['iRt', 'correct'])
	if dv == 'iRt':
		dm = dm.select('correct == 1')
	from exparser.RBridge import RBridge
	R = RBridge()
	R.load(dm)

	# Run all lmers
	lm = R.lmer('%s ~ postRotDelay*valid*cond + (1|subject_nr)' % dv, \
		lmerVar='lmerFull')
	lm._print(sign=5)
	lm.save('output/lme.%s.full.csv' % dv)

	lm = R.lmer('%s ~ postRotDelay*valid + (1|subject_nr)' % dv, \
		lmerVar='lmerNoCond')
	lm._print(sign=5)
	lm.save('output/lme.%s.noCond.csv' % dv)

	lm = R.lmer('%s ~ valid*cond + (1|subject_nr)' % dv, \
		lmerVar='lmerNoDelay')
	lm._print(sign=5)
	lm.save('output/lme.%s.noDelay.csv' % dv)

	lm = R.lmer('%s ~ postRotDelay*cond + (1|subject_nr)' % dv, \
		lmerVar='lmerNoValid')
	lm._print(sign=5)
	lm.save('output/lme.%s.noValid.csv' % dv)

	# Do the model comparisons
	am = R.anova('lmerNoCond', 'lmerFull')
	am._print(sign=5)
	am.save('output/anova.%s.noCond.csv' % dv)

	am = R.anova('lmerNoDelay', 'lmerFull')
	am._print(sign=5)
	am.save('output/anova.%s.noDelay.csv' % dv)

	am = R.anova('lmerNoValid', 'lmerFull')
	am._print(sign=5)
	am.save('output/anova.%s.noValid.csv' % dv)

	R.load(dm.select('postRotDelay == 0'))
	lm = R.lmer('%s ~ valid + (1|subject_nr)' % dv)
	lm._print(sign=5)
	lm.save('output/lme.%s.0.noCond.csv' % dv)

	R.load(dm.select('postRotDelay == 1000'))
	lm = R.lmer('%s ~ valid + (1|subject_nr)' % dv)
	lm._print(sign=5)
	lm.save('output/lme.%s.1000.noCond.csv' % dv)
def lmePlot(dm, dv='iRt'):

	"""
	Plots the graph for the overall LME.

	Arguments:
	dm		--	A DataMatrix.

	Keyword arguments:
	dv		--	The dependent variable. (default='iRt')
	"""

	assert(dv in ['iRt', 'correct'])
	if dv == 'iRt':
		dm = dm.select('correct == 1')
	from exparser.RBridge import RBridge
	R = RBridge()
	# Now plot!
	if dv == 'iRt':
		fig = plt.figure(figsize=(5,3))
	else:
		fig = plt.figure(figsize=(5,1.5))
	plt.subplots_adjust(wspace=0, bottom=.15)
	i = 1
	for postRotDelay in (0, 1000):
		_dm = dm.select('postRotDelay == %s' % postRotDelay)
		_dmObj = _dm.select('cond == "object-based"')
		_dmSpa = _dm.select('cond == "spatial"')
		_dmVal = _dm.select('valid == "{0}valid"')
		_dmInv = _dm.select('valid == "{1}invalid"')
		_dmObjVal = _dmObj.select('valid == "{0}valid"')
		_dmObjInv = _dmObj.select('valid == "{1}invalid"')
		_dmSpaVal = _dmSpa.select('valid == "{0}valid"')
		_dmSpaInv = _dmSpa.select('valid == "{1}invalid"')

		R.load(_dm)
		__dm = R.lmer('%s ~ valid + (1|subject_nr)' % dv)
		__dm._print(sign=5)
		if dv == 'iRt':
			mVal = 1 / _dmVal[dv].mean()
			mInv = 1 / _dmInv[dv].mean()
			mObjVal = 1 / _dmObjVal[dv].mean()
			mObjInv = 1 / _dmObjInv[dv].mean()
			mSpaVal = 1 / _dmSpaVal[dv].mean()
			mSpaInv = 1 / _dmSpaInv[dv].mean()
			# Get the errorbars!
			lo = ((__dm['est'][0]+__dm['ci95lo'][1])**-1 - \
				(__dm['est'][0]+__dm['est'][1])**-1) / 2
			up = ((__dm['est'][0]+__dm['ci95up'][1])**-1 - \
				(__dm['est'][0]+__dm['est'][1])**-1) / 2
		elif dv == 'correct':
			mVal = 100. - 100.*_dmVal[dv].mean()
			mInv = 100. - 100.*_dmInv[dv].mean()
			mObjVal = 100. - 100.*_dmObjVal[dv].mean()
			mObjInv = 100. - 100.*_dmObjInv[dv].mean()
			mSpaVal = 100. - 100.*_dmSpaVal[dv].mean()
			mSpaInv = 100. - 100.*_dmSpaInv[dv].mean()
			# Get the errorbars!
			lo = ((__dm['est'][0]+__dm['ci95lo'][1]) - \
				(__dm['est'][0]+__dm['est'][1])) / 2
			up = ((__dm['est'][0]+__dm['ci95up'][1]) - \
				(__dm['est'][0]+__dm['est'][1])) / 2
			lo *= 100.
			up *= 100.
		eVal = [lo, up]
		eInv = [lo, up]
		plt.subplot(1,2,i)
		plt.errorbar([0,1], [mVal, mInv], yerr=[eVal, eInv], fmt='o-', \
			label='Preferred model', color='black')
		plt.plot([0,1], [mObjVal, mObjInv], '--', label='Object-centered', \
			color='black')
		plt.plot([0,1], [mSpaVal, mSpaInv], ':', label='Retinotopic', \
			color='black')
		plt.xlim(-.2, 1.2)
		plt.xticks([0,1], ['Valid', 'Invalid'])
		if dv == 'correct':
			plt.ylim(9, 15)
			plt.yticks([10,12,14])
		else:
			plt.ylim(595, 665)
		plt.xlabel('Cue Validity')
		if i == 2:
			plt.title('Long SOA')
			plt.gca().yaxis.set_ticklabels([])
			if dv != 'correct':
				plt.legend(frameon=False)
		else:
			plt.title('Short SOA')
			if dv == 'correct':
				plt.ylabel('Error rate (%)')
			else:
				plt.ylabel('Response time (ms)')
		i += 1
	plt.savefig('plot/lme.%s.png' % dv)
	plt.savefig('plot/lme.%s.svg' % dv)
	plt.show()
Exemple #4
0
Plots landing position as a function of sacc latency (and other predictors, if
wanted), using LMM.
"""


from exparser.CsvReader import CsvReader
from exparser.RBridge import RBridge

src = "/home/lotje/Documents/PhD Marseille/Studies/004 - Single-object experiment - Handle-orientation effect/analysis 004/selected_dm_004B_WITH_drift_corr_onlyControl_True.csv"
dm = CsvReader(src).dataMatrix()
dm = dm.select("endX1NormToHandle == ''")
 dvRaw = "endX1Norm"
dvNorm = "endX1NormToHandle"
nsim = 10

R = RBridge()
R.load(dm)
print "Condition in DV"
# Run a full LME
lmerDm = R.lmer(\
	'%s ~ (1|file) + (1|object)'\
	% (dvNorm), nsim=nsim, printLmer=True)
lmerDm._print(sign=5)
lmerDm.save('lme_condition_in_dv.csv')

print "Condition as factor"
lmerDm = R.lmer(\
	'%s ~ handle_side + (1|file) + (1|object)'\
	% (dvRaw), nsim=nsim, printLmer=True)
lmerDm._print(sign=5)
lmerDm.save('lme_condition_as_factor.csv')
def lmeBehaviorBrightness(dm):
	
	"""
	Analyzes the behavioral data with mixed effects, separately for the bright
	and dark cue.

	Arguments:
	dm		--	A DataMatrix.
	"""	
	
	global R
	try:
		R
	except:
		R = RBridge()
	dm = dm.addField('targetLum', dtype=str, default='bright')
	dm['targetLum'][(dm['cueLum'] == "dark") \
		& (dm['cueValidity'] == "valid")] = 'dark'
	dm['targetLum'][(dm['cueLum'] == "bright") \
		& (dm['cueValidity'] == "invalid")] = 'dark'
	print '*** Target luminance\n'
	R.load(dm)
	lm = R.lmer('irt ~ targetLum * cueValidity * soa + (1|subject_nr)')
	lm._print('iRt')
	lm = R.lmer('correct ~ targetLum * cueValidity * soa + (1|subject_nr)')
	lm._print('Accuracy')
	for soa in dm.unique('soa'):
		R.load(dm.select('soa == %d' % soa, verbose=False))
		lm = R.lmer('irt ~ targetLum * cueValidity + (1|subject_nr)')
		lm._print('iRt (%d)' % soa)
		lm = R.lmer('correct ~ targetLum * cueValidity + (1|subject_nr)')
		lm._print('Accuracy (%d)' % soa)
	print '*** Cue luminance\n'
	R.load(dm)
	lm = R.lmer('irt ~ cueLum * cueValidity * soa + (1|subject_nr)')
	lm._print('iRt')
	lm = R.lmer('correct ~ cueLum * cueValidity * soa + (1|subject_nr)')
	lm._print('Accuracy')
	for soa in dm.unique('soa'):
		R.load(dm.select('soa == %d' % soa, verbose=False))
		lm = R.lmer('irt ~ cueLum * cueValidity + (1|subject_nr)')
		lm._print('iRt (%d)' % soa)
		lm = R.lmer('correct ~ cueLum * cueValidity + (1|subject_nr)')
		lm._print('Accuracy (%d)' % soa)
def lmeBehavior(dm, suffix=''):

	"""
	Analyzes the behavioral data with mixed effects.

	Arguments:
	dm		--	A DataMatrix.
	
	keywords:
	suffix	--	A filename suffix.
	"""

	global R
	try:
		R
	except:
		R = RBridge()

	i = 1
	Plot.new(size=Plot.ws)
	for dv in ['correct', 'irt']:
		# Also analyze the grouped 945 and 2445 SOAs, which gives more power
		# than analyzing them separately.
		R.load(dm.select('soa != 45'))
		lm = R.lmer('%s ~ cueValidity * soa + (1|subject_nr)' % dv)
		lm.save('output/lmeBehavior.longInteract.%s.csv' % dv)
		lm._print(title='Long: %s' % dv, sign=10)
		lm = R.lmer('%s ~ cueValidity + (1|subject_nr)' % dv)
		lm.save('output/lmeBehavior.longNoInteract.%s.csv' % dv)
		lm._print(title='Long: %s' % dv, sign=10)
		# Loop over SOAs
		lSoa = []
		lVal = []
		lInv = []
		for soa in [45, 945, 2445]:
			_dm = dm.select('soa == %d' % soa, verbose=False)
			R.load(_dm)
			lm = R.lmer('%s ~ cueValidity + (1|subject_nr)' % dv)
			lm.save('output/lmeBehavior.%s.%s.csv' % (dv, soa))
			lm._print(title='%s (%s)' % (dv, soa), sign=10)
			mInv = lm['est'][0] # Invalid is reference
			mVal = mInv + lm['est'][1] #4Add slope for validity effect
			d = lm['est'][1]
			m = mInv + .5*d
			minD = d - lm['se'][1]
			maxD = d + lm['se'][1]
			# Determine error bars based on the slope CIs
			cInv = [m-minD/2, m-maxD/2]
			cVal = [m+minD/2, m+maxD/2]
			if dv == 'irt':
				mVal = 1./mVal
				mInv = 1./mInv
				cInv = [1./cInv[0], 1./cInv[1]]
				cVal = [1./cVal[0], 1./cVal[1]]
			else:
				mVal = 100.*mVal
				mInv = 100.*mInv
				cInv = [100.*cInv[0], 100.*cInv[1]]
				cVal = [100.*cVal[0], 100.*cVal[1]]
			# We plot the errorbars separately, because it's a bit easier than
			# passing them onto `plt.errorbar()`.
			plt.plot([soa+55, soa+55], cVal, '-', color=valColor)
			plt.plot([soa+55, soa+55], cInv, '-', color=invColor)
			lSoa.append(soa+55)
			lVal.append(mVal)
			lInv.append(mInv)
		plt.xlim(-100, 2750)
		plt.xticks([100, 1000, 2500])
		plt.xlabel('Cue-target interval (ms)')
		if dv == 'irt':
			plt.ylabel('Respone time (ms)')
			plt.ylim(420, 630)
			plt.yticks([450, 500, 550, 600])
		else:
			plt.ylabel('Accuracy (%)')
			plt.ylim(80, 95)
			plt.yticks([82.5, 85, 87.5, 90, 92.5])
		i += 1
		nVal = len(dm.select('cueValidity == "valid"', verbose=False))
		nInv = len(dm.select('cueValidity == "invalid"', verbose=False))
		plt.plot(lSoa, lVal, 'o-', color=valColor, label='Valid (N=%d)' % nVal)
		plt.plot(lSoa, lInv, 'o-', color=invColor, label='Invalid (N=%d)' % \
			nInv)
		plt.legend(frameon=False)
	Plot.save('behavior%s' % suffix, 'behavior', show=show)