<jupyter_start><jupyter_text>In the following, we want to partition responses according to how many times a player has miscoordinated before. For a start, we concatenate all three experiments into one dataframe:<jupyter_code>#df = pd.concat([
#                df_MTurk.join(pd.Series(['MTurk']*len(df_MTurk), name='experiment')), 
#                df_DTU1.join(pd.Series(['DTU1']*len(df_DTU1), name='experiment')),
#                df_DTU2.join(pd.Series(['DTU2']*len(df_DTU2), name='experiment'))],
#                ignore_index=True)
#df.head(), len(df)<jupyter_output><empty_output><jupyter_text>Now we group the data into response pairs<jupyter_code>df = df_MTurk.groupby(['session', 'group', 'round'], as_index=False)[['code', 'id_in_group', 'arrival', 'choice', 'certainty']].agg(lambda x: tuple(x))
df.head()
#df0 = pd.DataFrame(columns=df.columns)
#df1_ = pd.DataFrame(columns=df.columns)
#sessions = df.session.unique()
#for session in sessions:
#    df_session = df[df.session == session]
#    groups = df_session.group.unique()
#    for group in groups:
#        df_group = df_session[df_session.group == group]
#        miss = 0
#        for idx, row in df.iterrows():
#            if sum(row['choice']) != 1:
#                row['miss'] = miss
#                df0 = df0.append(row, ignore_index=True)
#            else:
#                miss += 1
#                df1_ = df1_.append(row, ignore_index=True)
#df.head()
# initialize new dataframes that will hold data with a condition. df0 will contain all choices 
# having had zero previous miscoordinations, while df1_ will contain all choices having had one
# or more previous miscoordinations
df0 = pd.DataFrame(columns=df.columns)
df1_ = pd.DataFrame(columns=df.columns)
# partition the dataframe into two bins - the first having 0 and 1, and the other 2 or more
# miscoordinations:
sessions = df.session.unique()
for session in sessions:
    df_session = df[df.session == session]
    groups = df_session.group.unique()
    for group in groups:
        df_group = df_session[df_session.group == group]
        miss = 0
        for idx, row in df_group.iterrows():
            if sum(row['choice']) != 1:
                if miss == 0:
                    df0 = df0.append(row, ignore_index=True)
                else:
                    df1_ = df1_.append(row, ignore_index=True)
            else:
                if miss == 0:
                    df0 = df0.append(row, ignore_index=True)
                else:
                    df1_ = df1_.append(row, ignore_index=True)
                miss += 1<jupyter_output><empty_output><jupyter_text>Now a bit of magic: we need to separate the tuples again and create rows for each person: (see ref at https://stackoverflow.com/questions/53218931/how-to-unnest-explode-a-column-in-a-pandas-dataframe<jupyter_code>def unnesting(df, explode):
    idx = df.index.repeat(df[explode[0]].str.len())
    df1 = pd.concat([
        pd.DataFrame({x: np.concatenate(df[x].values)}) for x in explode], axis=1)
    df1.index = idx

    return df1.join(df.drop(explode, 1), how='left')


dfnew0 = unnesting(df0,['code', 'id_in_group', 'arrival', 'choice', 'certainty'])
dfnew1_ = unnesting(df1_,['code', 'id_in_group', 'arrival', 'choice', 'certainty'])
dfnew0['arrival'].replace({9.0: 8.6, 9.1: 8.7}, inplace=True)
dfnew1_['arrival'].replace({9.0: 8.6, 9.1: 8.7}, inplace=True)
len(dfnew0), len(dfnew1_)
dfnew0.head()
sns.regplot(x='arrival', y='choice', data=dfnew0, ci=95, logistic=True)
sns.despine()
sns.regplot(x='arrival', y='choice', data=dfnew1_, ci=95, logistic=True)
sns.despine()
dfall = pd.concat([dfnew0.join(pd.Series(['zero']*len(dfnew0), name='miss')), 
                   dfnew1_.join(pd.Series(['one_or_more']*len(dfnew1_), name='miss'))],
                ignore_index=True)
dfall.head()
pal = dict(zero="blue", one_or_more="orange")
g = sns.lmplot(x="arrival", y="choice", hue="miss", data=dfall, palette=pal, 
               logistic=True, ci=95, n_boot=10000, x_estimator=np.mean, x_ci="ci",
               y_jitter=.2, legend=False, height=3, aspect=1.5)
#g = sns.FacetGrid(dfall, hue='miscoordinations', palette=pal, height=5);
#g.map(plt.scatter, "arrival", "choice", marker='o', s=50, alpha=.7, linewidth=.5, color='white', edgecolor="white")
#g.map(sns.regplot, "arrival", "choice", scatter_kws={"color": "grey"}, ci=95, n_boot=100, logistic=True, height=3, aspect=1.5)
g.set(xlim=(7.98, 8.72))
g.set(ylabel='frequency of canteen choice')
my_ticks = ["8:00", "8:10", "8:20", "8:30", "8:40", "8:50", "9:00", "9:10"]
g.set(xticks=[8,8.1,8.2,8.3,8.4,8.5,8.6,8.7], yticks=[0,.1,.2,.3,.4,.5,.6,.7,.8,.9,1])
g.set(xticklabels = my_ticks) #, yticklabels = ["office","","","","",".5","","","","","canteen"])

# make my own legend:
name_to_color = {
    '  0 (n=2762)': 'blue',
    '>0 (n=1498)': 'orange',
}
patches = [patch.Patch(color=v, label=k) for k,v in name_to_color.items()]
plt.legend(title='# miscoordinations', handles=patches, loc='lower left')
plt.title('MTurk', fontsize=16)
plt.tight_layout()

plt.rcParams["font.family"] = "sans-serif"
PLOTS_DIR = '../plots'

if not os.path.exists(PLOTS_DIR):
    os.makedirs(PLOTS_DIR)

plt.savefig(os.path.join(PLOTS_DIR, 'figS6_logit_2bins_MTurk.png'),
            bbox_inches='tight', transparent=True, dpi=300)
plt.savefig(os.path.join(PLOTS_DIR, 'figS6_logit_2bins_MTurk.pdf'), transparent=True, dpi=300)

sns.despine()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17591738889913333, 'fraction_numerical': 0.034482758620689655, 'mean_word_length': 3.7537248028045576, 'pattern_counts': {'":': 1, '<': 13, '<?xml version=': 0, '>': 14, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '1703', 'license': 'MIT', 'n_tokens_mistral': 2127, 'n_tokens_neox': 1998, 'n_words': 399, 'path': 'python/figS6_logit_twobins_MTurk.ipynb', 'repo_name': 'thomasnicolet/Paper_canteen_dilemma', 'size': '5423'}