<jupyter_start><jupyter_text>Load data<jupyter_code>a = !pwd
baseDir = '/'.join(a[0].split('/')[0:-2])
print(baseDir)
WS_dat = pd.read_csv(baseDir + '/Data/Cleaned/WS_dat.csv',index_col = 0)
quiz_dat = pd.read_csv(baseDir + '/Data/Cleaned/quiz_dat.csv',index_col = 0)
SPG_dat = pd.read_csv(baseDir + '/Data/Cleaned/SPG_dat.csv',index_col = 0)
WS_cond_dat = WS_dat.groupby(['subID','player_type'],as_index=False).mean()
subIDs = WS_dat['subID'].unique()
SPG_scores = SPG_dat.groupby(['subID','player_type'],as_index=False
                            ).mean()[['subID','player_type','score']]
SPG_scores_wide = SPG_scores.pivot(index = 'subID', columns = 'player_type', values = 'score').reset_index()
SPG_scores_wide['diff'] = SPG_scores_wide['opt'] - SPG_scores_wide['pess']
SPG_scores_wide['mean'] = (SPG_scores_wide['opt'] + SPG_scores_wide['pess'])/2
SPG_scores_wide.head()<jupyter_output><empty_output><jupyter_text>Quantify work/shirk effect<jupyter_code>IPs = WS_dat.query('trial >9').groupby(['subID','player_type'],as_index=False).mean()[['subID','player_type','cost']]
IPs_wide = IPs.pivot(index = 'subID', columns = 'player_type', values = 'cost').reset_index()
IPs_wide['diff'] = IPs_wide['opt'] - IPs_wide['pess']
IPs_wide.head()<jupyter_output><empty_output><jupyter_text>Merge across tasks<jupyter_code>SPG_pess_order = SPG_dat[['subID','block','player_type']].drop_duplicates().pivot(index='subID',columns = 'player_type', values='block').reset_index()[['subID','pess']]
SPG_pess_order.columns = ['subID','pess_order']
task_dat = SPG_scores_wide.merge(IPs_wide, on = 'subID', suffixes = ['_SPG','_WS']).merge(SPG_pess_order, on='subID')<jupyter_output><empty_output><jupyter_text>Relationship between SPG self-report and IG earnings<jupyter_code># Median split on IG distinguishing between WTP for Opt and Pess:
WTP_Q1 = task_dat['diff_WS'].describe()['25%']
WTP_D1 = task_dat['diff_WS'].describe(percentiles = [.1,.9])['10%']
WTP_Q3 = task_dat['diff_WS'].describe()['75%']
WTP_D9 = task_dat['diff_WS'].describe(percentiles = [.1,.9])['90%']
WTP_median = task_dat['diff_WS'].median()
subs_good_IG = task_dat.query('diff_WS > @WTP_D9')['subID'].tolist()
len(subs_good_IG)
subs_bad_IG = task_dat.query('diff_WS < @WTP_D1')['subID'].tolist()
len(subs_bad_IG)
!pip install wordcloud
import wordcloud
from wordcloud import WordCloud, STOPWORDS# Generate word cloud
self_report_text = SPG_dat[['subID','player_type','self-report']].drop_duplicates()
self_report_text.head()
all_text = pd.DataFrame()
for performance in ['good','bad']:
    if performance == 'good':
        perf_list = subs_good_IG
    else:
        perf_list = subs_bad_IG
        
    for player in ['opt','pess']:
        perf_text = self_report_text.loc[self_report_text['subID'].isin(perf_list),:]
        
        play_text = perf_text.query('player_type == @player')['self-report'].values.tolist()
        play_text = ' '.join(play_text).lower()
        
        tmp = pd.DataFrame([[performance,player,play_text]],columns = ['performance','player','text'])
        all_text = all_text.append(tmp).reset_index(drop=True)<jupyter_output><empty_output><jupyter_text>Plot<jupyter_code># Define a function to plot word cloud
def plot_cloud(wordcloud):
    # Set figure size
    plt.figure(figsize=(10, 5))
    # Display image
    plt.imshow(wordcloud) 
    # No axis details
    plt.axis("off");
def red_hi_color_func(word, font_size, position, orientation, random_state=1,
                    **kwargs):
    return "hsl(5, 80%, 50%)"
def blue_hi_color_func(word, font_size, position, orientation, random_state=1,
                    **kwargs):
    return "hsl(220, 80%, 50%)"
def red_lo_color_func(word, font_size, position, orientation, random_state=1,
                    **kwargs):
    return "hsl(5, 40%, 70%)"
def blue_lo_color_func(word, font_size, position, orientation, random_state=1,
                    **kwargs):
    return "hsl(220, 40%, 70%)"
col_dict = {'good':{'opt':red_hi_color_func,'pess':blue_hi_color_func},
            'bad':{'opt':red_lo_color_func,'pess':blue_lo_color_func}}
player_names = {'opt':'Greedy','pess':'Risk-Averse'}

all_clouds = pd.DataFrame()
for perfi,performance in enumerate(['good','bad']):
    for playi,player in enumerate(['opt','pess']):
        text = all_text.query('performance == @performance and player == @player')['text'].values[0]
        print(performance,player,text[:100])
        tmp = WordCloud(width = 1500, height = 1000, random_state=6,
                      background_color='white', colormap='RdBu', max_words = 20,
                      collocations=False, stopwords = STOPWORDS)
        tmp = tmp.generate(text)
        tmp = pd.DataFrame([[performance,player,tmp]],columns = ['performance','player','cloud'])
        all_clouds = all_clouds.append(tmp).reset_index(drop=True)
fig,ax = plt.subplots(2,2,figsize=[24,16])
# colfunx = [red_color_func,blue_color_func]
perf_group_indicator = ['top 10%','bottom 10%']
for perfi,performance in enumerate(['good','bad']):
    for playi,player in enumerate(['opt','pess']):
        print(performance,player)
        cloud = all_clouds.query('performance == @performance and player == @player')['cloud'].values[0]
        axcur = ax[perfi,playi]
#         axcur.imshow(cloud)
        axcur.imshow(cloud.recolor(color_func=col_dict[performance][player]))
        axcur.axis('off')
#         axcur.set(title = '%simist (%s participants)'%(player,performance))
        axcur.set_title(player_names[player].upper() + ' (%s generalization)'%perf_group_indicator[perfi],
                        fontdict = {'size':30})<jupyter_output>good opt
good pess
bad opt
bad pess
{'fraction_non_alphanumeric': 0.17061946902654868, 'fraction_numerical': 0.01504424778761062, 'mean_word_length': 5.063304721030043, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 22, 'https://': 0, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '424656', 'license': 'MIT', 'n_tokens_mistral': 2104, 'n_tokens_neox': 1990, 'n_words': 302, 'path': 'Study4_InspectionGame/Analysis_scripts/1.GameAnalysis/2.Analyze_self_report.ipynb', 'repo_name': 'jeroenvanbaar/NHB_motives_structure', 'size': '5650'}