<jupyter_start><jupyter_text>Analyse Psylab Sarah, Ginny, Emily  Preparation: Imports and Stuff, load Dataset<jupyter_code>import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from IPython.display import display
import seaborn as sns
print(abspath("."))

df = pd.read_csv(os.path.join("..","..","Data","main","results_281_Group8_Conformity_Pilot_Group8_final.csv"))
with pd.option_context('display.max_columns', 9999, 'display.max_rows', 10):
    display(df.head())<jupyter_output><empty_output><jupyter_text>Clean Dataset* remove superflous columns and rows* make cleaner format (one row per participant, ...)<jupyter_code>df = df.drop(columns=["QUD","RT", "age", "comments", "education", "endTime", "experiment_id", "gender", "id", "languages", "startDate", "startTime", "text2", "timeSpent", "trial_name",  "trial_number"])
df.head()
#for all participants, drop fith row (irrelevant)
df = df.drop(index=df[df["question"] == "How do you feel about your choice?"].index)
df = df.drop(columns=["option1", "option2", "option3", "option4", "option5", "s1", "s2", "s3", "s4", "s5"])
df.head()
df.loc[df["question"].isna(), "question"] = "statement"
df.head()
#so now there's 4 rows per participant, and we'll in a next step use the .pivot()-method to create one row per participant. However what's left there 
#would be the text1-column which doesn't fit the scheme, so in a previous step we'll add a fith row per participant where the response is text1, 
#and we'll remove the column text1. Note that text1 IS NO RESPONSE, but we'll behave like it is to not break pandas' .pivot() method.
text1_cols = df[~df["text1"].isna()][["submission_id", "text1"]].set_index("submission_id").rename(columns={"text1": "response"})
text1_cols["question"] = "text1"
text1_cols.head()
df = df.set_index("submission_id").append(text1_cols).sort_index().reset_index().drop(columns="text1")
df.head()
tidy_df = df.pivot(index="submission_id", columns="question", values="response")
tidy_df.loc[:,"What do you do?"] = tidy_df["What do you do?"].astype("int")
tidy_df.loc[:,"statement"] = tidy_df["statement"].astype("int")
display(tidy_df.head())
len(tidy_df)<jupyter_output><empty_output><jupyter_text>Exclude participants etc<jupyter_code>#exclude participants that failed the understanding question or/and were neutral towards their chosen topic
clean_df = tidy_df[(tidy_df["To ensure you understood the assignment, please click on which of the following statements is true."]=="Participants chose which action they preferred.") & ~(tidy_df["statement"]==0)]
len(clean_df)
clean_df.rename(columns = {'What do you do?':'preference'}, inplace = True)
clean_df.rename(columns = {'text1':'condition'}, inplace = True)
clean_df.head()<jupyter_output>C:\Users\sarah\Miniconda3\envs\psylab\lib\site-packages\pandas\core\frame.py:5034: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  return super().rename(<jupyter_text>Make everything shorter and cleaner and save it to a new CSV<jupyter_code># show the whole text for the different conditions
with pd.option_context('display.max_colwidth', None):
    display(clean_df["condition"].head())
# differentiate between conditions

clean_df["only_ingroup_norm_show"] = clean_df["condition"].isin([" <br/><br/>60% of the other participants who agree with you on your chosen issue chose to report the robber.", " <br/><br/>Approximately 60% of the other participants who agree with you on your chosen issue chose to let the robber go."])
clean_df["let_go_norm_show"] = clean_df["condition"].isin([" <br/><br/>Approximately 60% of the other participants who agree with you on  your chosen issue chose to let the robber go. <br/><br/> Approximately 85% of participants in a previous study who disagreed with you on  your chosen issue chose to report the robber.", " <br/><br/>Approximately 60% of the other participants who agree with you on your chosen issue chose to let the robber go."])
clean_df = clean_df.drop(columns="condition")
clean_df.head()<jupyter_output>C:\Users\sarah\AppData\Local\Temp/ipykernel_11192/1067831111.py:3: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  clean_df["only_ingroup_norm_show"] = clean_df["condition"].isin([" <br/><br/>60% of the other participants who agree with you on your chosen issue chose to report the robber.", " <br/><br/>Approximately 60% of the other participants who agree with you on your chosen issue chose to let the robber go."])
C:\Users\sarah\AppData\Local\Temp/ipykernel_11192/1067831111.py:4: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-vie[...]<jupyter_text>Plotting stuff<jupyter_code># compute mean of choice for every condition
mean_ingroup = clean_df.groupby(["only_ingroup_norm_show", "let_go_norm_show"])["preference"].mean() 
display(pd.DataFrame(mean_ingroup))
# plot means of choice for every condition with barplot which looks shitty
fig, ax = plt.subplots(1,2, figsize=(10,3))

mean_ingroup[False].plot.bar(title="Both_norms_shown", ax=ax[0], ylim=[-3,3])
mean_ingroup[True].plot.bar(title="Only_ingroup_norm_shown", ax=ax[1], ylim=[-3,3]);
tmp = pd.DataFrame(mean_ingroup).reset_index()
tmp
sns.catplot(x="let_go_norm_show", y="preference", col="only_ingroup_norm_show", data=tmp, kind="bar").set(ylim=[-3,3])
p = sns.catplot(x="let_go_norm_show", y="preference", col="only_ingroup_norm_show", data=clean_df, kind="bar")

#p.set_titles(row_template = 'row', col_template = 'col')
p = sns.catplot(x="let_go_norm_show", y="preference", col="only_ingroup_norm_show", data=clean_df, kind="violin").set(ylim=[-3,3])
p.set_axis_labels("Ingroup Norm", "Preference")
p.set_yticklabels(['Definitely report','','','','','','Definitely leave alone'])
g = sns.FacetGrid(data=clean_df, col ='only_ingroup_norm_show', height=6).set(ylim=[-3,3])

g.map_dataframe(sns.violinplot, x="let_go_norm_show", y="preference")

g.map_dataframe(sns.swarmplot, x="let_go_norm_show", y="preference", color="red")

g.map_dataframe(sns.boxplot, x="let_go_norm_show", y="preference", showmeans=True,
            meanline=True,
            meanprops={'color': 'k', 'ls': '-', 'lw': 2},
            medianprops={'visible': False},
            whiskerprops={'visible': False},
            showfliers=False,
            showbox=False,
            showcaps=False,)
g.set_axis_labels("Ingroup Norm", "Participant's preference")
g.set_yticklabels(['Definitely report','- -','-','o','+','++','Definitely leave alone'])
# black line is mean, red dots are single participants

g.set(ylim=(-3.3, 3.3))

g.set_xticklabels(['Report', 'Don\'t report'])


axes = g.axes.flatten()
axes[0].set_title("Both Norms Shown")
axes[1].set_title("Only Ingroup Norm ")<jupyter_output><empty_output><jupyter_text>Working on the dataset again: make the dataset look like the one from the study.<jupyter_code>clean_df.head()
assert len(set(clean_df["To ensure you understood the assignment, please click on which of the following statements is true."])) == 1
clean_df = clean_df.drop(columns=["To ensure you understood the assignment, please click on which of the following statements is true."])
clean_df["bothShown"] = ~clean_df["only_ingroup_norm_show"]
clean_df = clean_df.drop(columns=["only_ingroup_norm_show"])
clean_df.head(10)
clean_df["ingroupAgree"] = (clean_df["let_go_norm_show"] & (clean_df["preference"] > 0)) | (~clean_df["let_go_norm_show"] & (clean_df["preference"] < 0))
clean_df.head(10)
clean_df["outgroupDisagree"] = ~clean_df["ingroupAgree"]
clean_df["ingroupNorm"] = clean_df["let_go_norm_show"]
clean_df = clean_df.drop(columns="let_go_norm_show")
clean_df.head(10)
clean_df.to_csv(os.path.join("..","..","Analyses","main","df_for_modeling.csv"))
print(os.path.abspath(os.path.join("..","..","Analyses","main","df_for_modeling.csv")))<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup