Randomizing a 2x2x2 factorial design

Hi @Mardock,

If I got your descriptions correctly, you can use this code in the Begin Experiment Tab to create a condition file.

This condition file satisfies your two constraints:

  1. It randomly presents all combinations of factor levels

  2. However, It shows C1 followed by C2



import pandas as pd
import numpy as np

# define factors and levels
factorA = [1,2]
factorB = [1,2]
factorC= [1,2]
whichC= ['one'] * 2 + ['two'] * 2 + ['three'] * 2 + ['four'] * 2

# create a factoial 2 x 2 x 2 dataframe
trials = pd.DataFrame ([{'factorA':f1, 'factorB':f2, 'factorC':f3} for f1 in factorA for f2 in factorB for f3 in factorC])
trials ['whichC']= np.array(whichC)

# shuffle rows to satisfy your first condition
shuffle_trials = trials.sample(frac=1)

#shuffle rows to alternate factor C levels
shuffle_trials= shuffle_trials.groupby('whichC', sort= False).apply(pd.DataFrame.sort_values, 'factorC', ascending=False)

This code gives you a condition file like this (ignore the whichC column):

Screen Shot 2020-02-16 at 12.24.58 pm

All this code is doing is first, grouping you file based on factor c (whichC) and then sort it.

Finally, you need to add shuffle_trials as your condition file in the loop window. Set the order of the loop presentation as sequential not random.

Hope that helps.

2 Likes