text_html_output_1.png
from plotly.subplots import make_subplots

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import plotly.express as px

import plotly.graph_objects as go

data = pd.read_csv('/kaggle/input/associativeinference/Associative Inference.csv')

data.columns

age_group = data[["AgeGroup", "AB1-BC1"]]
age_group.head()

grouped = age_group.groupby('AgeGroup', as_index=False)
summary = grouped.mean()
summary

temp = grouped.describe()["AB1-BC1"][["count", 'std']]
temp

summary["std_err"] = temp["std"] / np.sqrt(temp["count"])
summary = summary.reindex([1,0,3,2])

fig = px.bar(summary, x="AgeGroup", y="AB1-BC1", error_y="std_err", width=500, title="AB1-BC1")
fig.show()

age_group = data[["AgeGroup", 'AB_acc_1', 'AB_acc_2', 'AB_acc_3', 'AB_acc_4', 
                  'BC_acc_1', 'BC_acc_2', 'BC_acc_3', 'BC_acc_4']]
age_group.head()

grouped = age_group.groupby('AgeGroup', as_index=False)
summary = grouped.mean()
summary

col_names = ['AB_acc_1', 'AB_acc_2', 'AB_acc_3', 'AB_acc_4', 'BC_acc_1', 'BC_acc_2', 'BC_acc_3', 'BC_acc_4']

# add standard error for each accuracy
for name in col_names:
    temp = grouped.describe()[name][["count", 'std']]
    summary["se_" + name[:2] + name[-1]] = temp["std"] / np.sqrt(temp["count"])
    
# get AB, BC values from AgeGroup
child_AB = summary[summary["AgeGroup"] == "child"][['AB_acc_1', 'AB_acc_2', 'AB_acc_3', 'AB_acc_4']].values[0]
child_BC = summary[summary["AgeGroup"] == "child"][['BC_acc_1', 'BC_acc_2', 'BC_acc_3', 'BC_acc_4']].values[0]

ado_AB = summary[summary["AgeGroup"] == "adolescent"][['AB_acc_1', 'AB_acc_2', 'AB_acc_3', 'AB_acc_4']].values[0]
ado_BC = summary[summary["AgeGroup"] == "adolescent"][['BC_acc_1', 'BC_acc_2', 'BC_acc_3', 'BC_acc_4']].values[0]

y_adl_AB = summary[summary["AgeGroup"] == "younger adult"][['AB_acc_1', 'AB_acc_2', 'AB_acc_3', 'AB_acc_4']].values[0]
y_adl_BC = summary[summary["AgeGroup"] == "younger adult"][['BC_acc_1', 'BC_acc_2', 'BC_acc_3', 'BC_acc_4']].values[0]

o_adl_AB = summary[summary["AgeGroup"] == "older adult"][['AB_acc_1', 'AB_acc_2', 'AB_acc_3', 'AB_acc_4']].values[0]
o_adl_BC = summary[summary["AgeGroup"] == "older adult"][['BC_acc_1', 'BC_acc_2', 'BC_acc_3', 'BC_acc_4']].values[0]

# get standard error for AB, BC values from AgeGroup
child_AB_se = summary[summary["AgeGroup"] == "child"][['se_AB1', 'se_AB2', 'se_AB3', 'se_AB4']].values[0]
child_BC_se = summary[summary["AgeGroup"] == "child"][['se_BC1', 'se_BC2', 'se_BC3', 'se_BC4']].values[0]

ado_AB_se = summary[summary["AgeGroup"] == "adolescent"][['se_AB1', 'se_AB2', 'se_AB3', 'se_AB4']].values[0]
ado_BC_se = summary[summary["AgeGroup"] == "adolescent"][['se_BC1', 'se_BC2', 'se_BC3', 'se_BC4']].values[0]

y_adl_AB_se = summary[summary["AgeGroup"] == "younger adult"][['se_AB1', 'se_AB2', 'se_AB3', 'se_AB4']].values[0]
y_adl_BC_se = summary[summary["AgeGroup"] == "younger adult"][['se_BC1', 'se_BC2', 'se_BC3', 'se_BC4']].values[0]

o_adl_AB_se = summary[summary["AgeGroup"] == "older adult"][['se_AB1', 'se_AB2', 'se_AB3', 'se_AB4']].values[0]
o_adl_BC_se = summary[summary["AgeGroup"] == "older adult"][['se_BC1', 'se_BC2', 'se_BC3', 'se_BC4']].values[0]

# make 2x2 subplots
fig = make_subplots(rows=2, cols=2,
                    subplot_titles=("Children", "Adolescent", "Younger Adults", "Older Adults"))

fig.add_trace(go.Scatter(x=['1', '2', '3', '4'], y=child_AB, name="AB",
                         line=dict(color='firebrick', width=2),
                         error_y=dict(array=child_AB_se)),  row=1, col=1)
fig.add_trace(go.Scatter(x=['1', '2', '3', '4'], y=child_BC, name="BC",
                         line=dict(color='royalblue', width=2),
                         error_y=dict(array=child_BC_se)),row=1, col=1)   

fig.add_trace(go.Scatter(x=['1', '2', '3', '4'], y=ado_AB, name="AB",
                         line=dict(color='firebrick', width=2),
                         error_y=dict(array=ado_AB_se)),row=1, col=2)
fig.add_trace(go.Scatter(x=['1', '2', '3', '4'], y=ado_BC, name="BC",
                         line=dict(color='royalblue', width=2),
                         error_y=dict(array=ado_BC_se)),row=1, col=2)  

fig.add_trace(go.Scatter(x=['1', '2', '3', '4'], y=y_adl_AB, name="AB",
                         line=dict(color='firebrick', width=2),
                         error_y=dict(array=y_adl_AB_se)),row=2, col=1)
fig.add_trace(go.Scatter(x=['1', '2', '3', '4'], y=y_adl_BC, name="BC",
                         line=dict(color='royalblue', width=2),
                         error_y=dict(array=y_adl_BC_se)),row=2, col=1)  

fig.add_trace(go.Scatter(x=['1', '2', '3', '4'], y=o_adl_AB, name="AB",
                         line=dict(color='firebrick', width=2),
                         error_y=dict(array=o_adl_AB_se)),row=2, col=2)
fig.add_trace(go.Scatter(x=['1', '2', '3', '4'], y=o_adl_BC, name="BC",
                         line=dict(color='royalblue', width=2),
                         error_y=dict(array=o_adl_BC_se)),row=2, col=2)  

fig.update_layout(width=850, height=600)
fig.show()

age_group = data[["AgeGroup", 'AB_rt_1', 'AB_rt_2', 'AB_rt_3', 'AB_rt_4', 
                  'BC_rt_1', 'BC_rt_2', 'BC_rt_3', 'BC_rt_4']]
age_group.head()

grouped = age_group.groupby('AgeGroup', as_index=False)
summary = grouped.mean()
summary

col_names = ['AB_rt_1', 'AB_rt_2', 'AB_rt_3', 'AB_rt_4', 'BC_rt_1', 'BC_rt_2', 'BC_rt_3', 'BC_rt_4']

# add standard error for each accuracy
for name in col_names:
    temp = grouped.describe()[name][["count", 'std']]
    summary["se_" + name[:2] + name[-1]] = temp["std"] / np.sqrt(temp["count"])
    
# get AB, BC values from AgeGroup
child_AB = summary[summary["AgeGroup"] == "child"][['AB_rt_1', 'AB_rt_2', 'AB_rt_3', 'AB_rt_4']].values[0]
child_BC = summary[summary["AgeGroup"] == "child"][['BC_rt_1', 'BC_rt_2', 'BC_rt_3', 'BC_rt_4']].values[0]

ado_AB = summary[summary["AgeGroup"] == "adolescent"][['AB_rt_1', 'AB_rt_2', 'AB_rt_3', 'AB_rt_4']].values[0]
ado_BC = summary[summary["AgeGroup"] == "adolescent"][['BC_rt_1', 'BC_rt_2', 'BC_rt_3', 'BC_rt_4']].values[0]

y_adl_AB = summary[summary["AgeGroup"] == "younger adult"][['AB_rt_1', 'AB_rt_2', 'AB_rt_3', 'AB_rt_4']].values[0]
y_adl_BC = summary[summary["AgeGroup"] == "younger adult"][['BC_rt_1', 'BC_rt_2', 'BC_rt_3', 'BC_rt_4']].values[0]

o_adl_AB = summary[summary["AgeGroup"] == "older adult"][['AB_rt_1', 'AB_rt_2', 'AB_rt_3', 'AB_rt_4']].values[0]
o_adl_BC = summary[summary["AgeGroup"] == "older adult"][['BC_rt_1', 'BC_rt_2', 'BC_rt_3', 'BC_rt_4']].values[0]

# get standard error for AB, BC values from AgeGroup
child_AB_se = summary[summary["AgeGroup"] == "child"][['se_AB1', 'se_AB2', 'se_AB3', 'se_AB4']].values[0]
child_BC_se = summary[summary["AgeGroup"] == "child"][['se_BC1', 'se_BC2', 'se_BC3', 'se_BC4']].values[0]

ado_AB_se = summary[summary["AgeGroup"] == "adolescent"][['se_AB1', 'se_AB2', 'se_AB3', 'se_AB4']].values[0]
ado_BC_se = summary[summary["AgeGroup"] == "adolescent"][['se_BC1', 'se_BC2', 'se_BC3', 'se_BC4']].values[0]

y_adl_AB_se = summary[summary["AgeGroup"] == "younger adult"][['se_AB1', 'se_AB2', 'se_AB3', 'se_AB4']].values[0]
y_adl_BC_se = summary[summary["AgeGroup"] == "younger adult"][['se_BC1', 'se_BC2', 'se_BC3', 'se_BC4']].values[0]

o_adl_AB_se = summary[summary["AgeGroup"] == "older adult"][['se_AB1', 'se_AB2', 'se_AB3', 'se_AB4']].values[0]
o_adl_BC_se = summary[summary["AgeGroup"] == "older adult"][['se_BC1', 'se_BC2', 'se_BC3', 'se_BC4']].values[0]

# make 2x2 subplots
fig = make_subplots(rows=2, cols=2,
                    subplot_titles=("Children", "Adolescent", "Younger Adults", "Older Adults"))

fig.add_trace(go.Scatter(x=['1', '2', '3', '4'], y=child_AB, name="AB",
                         line=dict(color='firebrick', width=2),
                         error_y=dict(array=child_AB_se)),  row=1, col=1)
fig.add_trace(go.Scatter(x=['1', '2', '3', '4'], y=child_BC, name="BC",
                         line=dict(color='royalblue', width=2),
                         error_y=dict(array=child_BC_se)),row=1, col=1)   

fig.add_trace(go.Scatter(x=['1', '2', '3', '4'], y=ado_AB, name="AB",
                         line=dict(color='firebrick', width=2),
                         error_y=dict(array=ado_AB_se)),row=1, col=2)
fig.add_trace(go.Scatter(x=['1', '2', '3', '4'], y=ado_BC, name="BC",
                         line=dict(color='royalblue', width=2),
                         error_y=dict(array=ado_BC_se)),row=1, col=2)  

fig.add_trace(go.Scatter(x=['1', '2', '3', '4'], y=y_adl_AB, name="AB",
                         line=dict(color='firebrick', width=2),
                         error_y=dict(array=y_adl_AB_se)),row=2, col=1)
fig.add_trace(go.Scatter(x=['1', '2', '3', '4'], y=y_adl_BC, name="BC",
                         line=dict(color='royalblue', width=2),
                         error_y=dict(array=y_adl_BC_se)),row=2, col=1)  

fig.add_trace(go.Scatter(x=['1', '2', '3', '4'], y=o_adl_AB, name="AB",
                         line=dict(color='firebrick', width=2),
                         error_y=dict(array=o_adl_AB_se)),row=2, col=2)
fig.add_trace(go.Scatter(x=['1', '2', '3', '4'], y=o_adl_BC, name="BC",
                         line=dict(color='royalblue', width=2),
                         error_y=dict(array=o_adl_BC_se)),row=2, col=2)  

fig.update_layout(width=850, height=600)
fig.show()

data_new = data.copy()
data_new['AB_acc_4_T'] = data['AB_acc_4'] == 1
data_new['AB_acc_4_T'] = data_new['AB_acc_4_T'].apply(lambda x: 'correct' if x else 'incorrect')

fig = px.histogram(data_new, x='BC_acc_4', color='AB_acc_4_T', marginal='rug', barmode='overlay', width=700, title='BC_acc_4 distribution between AB_acc_4 = 1 or not 1')
fig.show()