-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLinearModels.py
More file actions
153 lines (122 loc) · 5.93 KB
/
LinearModels.py
File metadata and controls
153 lines (122 loc) · 5.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# Main Analysis 3.2
# Code for fitting linear models on modal judgment data and computing partial R-squares
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from LinearModels_func import *
# Load data
data = pd.read_csv("output/combinedData_withFA.csv")
#%%
# Goodness / Physical Possibility X Time Interaction
# ===================================================
# Fit linear model to each model judgment (speeded and reflective) and compare full model
# with reduced models without goodness/possibility x time interactions
# full model formula: 'response ~ Factor_1 + Factor_2 + time_cond + Factor_1:time_cond + Factor_2:time_cond + Factor_1:Factor_2:time_cond + C(trialNo)'
# reduced model 1 (without goodness x time): 'response ~ Factor_1 + Factor_2 + time_cond + Factor_2:time_cond + Factor_1:Factor_2:time_cond + C(trialNo)'
# reduced model 2 (without possibility x time): 'response ~ Factor_1 + Factor_2 + time_cond + Factor_1:time_cond + Factor_1:Factor_2:time_cond + C(trialNo)'
results = []
modals = ['poss', 'could', 'might', 'may', 'should', 'ought']
for i in range(len(modals)):
result = lm_compare(modals[i], data)
results.append(result)
results_df = pd.DataFrame(results)
results_df.to_csv('output/3_lm_factorXtime.csv',index=False)
#%%
# Plot partial r-squares (explained variance) for goodness / physical possibility X time interaction
results_df = results_df.sort_values(by='partial_r-sq_f1xtime',ascending=False)
results_df['modal judgment'] = results_df['modal judgment'].str.replace('poss', 'possibility', regex=False)
f1xtime = results_df['partial_r-sq_f1xtime']
f2xtime = results_df['partial_r-sq_f2xtime']
y = np.arange(6)
bar_width = 0.4
plt.figure(figsize=(8,5))
plt.barh(modals, f1xtime, label='Goodness x Time', color='coral', alpha=0.7)
plt.barh(modals, -f2xtime, label='Physical Possibility x Time', color='#0e6e9e', alpha=0.7)
plt.axvline(0, color='grey', linewidth=1, linestyle='--')
ticks = np.round(np.linspace(-0.06, 0.1, 9), decimals=3)
plt.xticks(ticks, [abs(tick) for tick in ticks])
yticks = results_df['modal judgment'].str.capitalize()
plt.yticks(y, yticks)
plt.yticks(fontsize=12)
plt.xlim([-0.065,0.11])
plt.legend(frameon=False)
plt.xlabel(r'Partial $R^2$', fontsize=14)
plt.title(r'Explained variance (partial $R^2$) of Factor x Time Condition', fontsize=14, pad=28)
plt.text(-0.07, 5.9, r'$\leftarrow$ Effect decreases with time pressure Effect increases with time pressure$\rightarrow$', fontsize=9)
plt.show()
#plt.savefig(f"output/fig/3_2_factorXtime.png", dpi=300, bbox_inches='tight')
#%%
# Goodness / Physical Possibility X Modal judgment (pairwise)
# ===========================================================
# Fit linear model to pairs of model judgment (reflective only) and compare full model
# with reduced models without goodness/possibility x modal term
# full model formula: 'response ~ Factor_1 + Factor_2 + modal + Factor_1:modal + Factor_2:modal + Factor_1:Factor_2:modal + C(trialNo)'
# reduced model 1 (without goodness x modal): 'response ~ Factor_1 + Factor_2 + modal + Factor_2:modal + Factor_1:Factor_2:modal + C(trialNo)'
# reduced model 2 (without possibility x modal): 'response ~ Factor_1 + Factor_2 + modal + Factor_1:modal + Factor_1:Factor_2:modal + C(trialNo)'
results = []
modals = ['poss', 'could', 'may', 'might', 'ought', 'should']
for i in range(6):
for j in range(i+1,6):
result = factor_modal_interation(data, modals[i], modals[j])
results.append(result)
results_df = pd.DataFrame(results)
results_df.replace('poss', 'possibility', inplace=True)
#results_df.to_csv('output/3_lm_factorXmodal.csv',index=False)
#%%
# Plot partial r-squares for each pair of modal terms
# reformat r-sq values as matrix
modals = ['possibility', 'could', 'may', 'might', 'ought', 'should']
modal_to_idx = {id_: i for i, id_ in enumerate(modals)}
matrix_f1 = np.full((6, 6), np.nan)
matrix_f2 = np.full((6, 6), np.nan)
for _, row in results_df.iterrows():
i = modal_to_idx[row['modal1']]
j = modal_to_idx[row['modal2']]
r2_f1xmodal = row['partial_r-sq_f1xmodal']
matrix_f1[i, j] = r2_f1xmodal
matrix_f1[j, i] = r2_f1xmodal
r2_f2xmodal = row['partial_r-sq_f2xmodal']
matrix_f2[i, j] = r2_f2xmodal
matrix_f2[j, i] = r2_f2xmodal
#%% Plot partial r-square for Goodness x Modal
np.fill_diagonal(matrix_f1, np.nan)
nan_mask = np.isnan(matrix_f1)
fig, ax = plt.subplots()
np.fill_diagonal(matrix_f1, 0)
cax = ax.matshow(abs(matrix_f1), cmap="Blues", vmin=0, vmax=0.48) # Adjust color map as needed
fig.colorbar(cax)
for (i, j), is_nan in np.ndenumerate(nan_mask):
if is_nan:
rect = plt.Rectangle((j - 0.5, i - 0.5), 1, 1,
hatch='//', fill=False, edgecolor='grey', linewidth=0.0, alpha=0.7)
ax.add_patch(rect)
ax.set_xticks(np.arange(6))
ax.set_yticks(np.arange(6))
labels = [m.capitalize() for m in modals]
ax.set_xticklabels(labels)
ax.set_yticklabels(labels)
plt.tick_params(bottom=False)
plt.title(r'Partial $R^2$ of Goodness x Modal Terms')
plt.show()
#plt.savefig(f"output/fig/3_2_goodnessXmodal.png", dpi=300, bbox_inches='tight')
#%% Plot partial r-square for Physical Possibility x Modal
np.fill_diagonal(matrix_f2, np.nan)
nan_mask = np.isnan(matrix_f2)
fig, ax = plt.subplots()
np.fill_diagonal(matrix_f2, 0)
cax = ax.matshow(abs(matrix_f2), cmap="Blues", vmin=0, vmax=0.48) # Adjust color map as needed
fig.colorbar(cax)
for (i, j), is_nan in np.ndenumerate(nan_mask):
if is_nan:
rect = plt.Rectangle((j - 0.5, i - 0.5), 1, 1,
hatch='//', fill=False, edgecolor='grey', linewidth=0.0, alpha=0.7)
ax.add_patch(rect)
ax.set_xticks(np.arange(6))
ax.set_yticks(np.arange(6))
labels = [m.capitalize() for m in modals]
ax.set_xticklabels(labels)
ax.set_yticklabels(labels)
plt.tick_params(bottom=False)
plt.title(r'Partial $R^2$ of Physical Possibility x Modal Terms')
plt.show()
#plt.savefig(f"output/fig/3_2_possXmodal.png", dpi=300, bbox_inches='tight')