forked from djangraw/MoodDrift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestControlHypotheses.py
executable file
·1625 lines (1392 loc) · 73.4 KB
/
TestControlHypotheses.py
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
TestControlsHypotheses.py
Test the control analyses preregistered on osf.
Created on Thu Dec 16 15:27:29 2021
@author: djangraw
- Updated 12/22/21 by DJ - finished script, commented.
- Updated 1/5/22 by DJ - added cohen's d calculations, effect size reports,
and activities summary
- Updated 8-9/22 by DN & DJ - switch to within- and between-subject R2 values, many updates
"""
# Import packages
import pandas as pd
import numpy as np
from scipy import stats
from sklearn.decomposition import PCA
from matplotlib import pyplot as plt
import seaborn as sns
# Import pymer functions
from pymer4.models import Lmer
from rpy2 import robjects
mumin = robjects.r('library(MuMIn)') # for F-test
# %% Declare functions
# LMER comparison function from Dylan
def compare_lmers(pymer_input, lm_string_a, lm_string_ab):
"""
Run an anova comparing to mixed effects models on the same data.
Note that the mixed effects structrure for both models should be the same.
Also calculate r-squared and f-squared based on marginal r-squared.
Parameters
----------
pymer_input : Pandas.DataFrame
Data to run the models on
lm_string_a : str
lme4 model string for the reduced model
lm_string_ab : str
lme4 model string for the more complex model
Returns
-------
anova_res : Pandas.DataFrame
Results of the anova including marginal and conditional r-squared and f-squared.
References
----------
Formula for f-squared:
https://www.ncbi.nlm.nih.gov/pmc/articles/PMC3328081/#:~:text=Cohen's%20f%202%20(Cohen%2C%201988,R%202%201%20%2D%20R%202%20.
Where I found the r-squared for mixed effects models function:
https://stats.stackexchange.com/a/347908
"""
# fit more complex LME model to to the data
print('*** Fitting more complex model... ***')
model_ab = Lmer(lm_string_ab, data=pymer_input)
_ = model_ab.fit(REML=False, old_optimizer=True)
dfFit_ab = model_ab.coefs
dfFixef_ab = model_ab.fixef
# fit reduced complex LME model to to the data
print('*** Fitting reduced model... ***')
model_a = Lmer(lm_string_a, data=pymer_input)
_ = model_a.fit(REML=False, old_optimizer=True)
dfFit_a = model_a.coefs
dfFixef_a = model_a.fixef
# run anova to compare variance explained
anova_res = pd.DataFrame((robjects.r('anova')(model_a.model_obj, model_ab.model_obj, refit=False)))
# double-check that reduced model is actually smaller
if len(dfFit_ab) <= (len(dfFit_a)):
raise ValueError("Model AB should be a larger model than Model A, but based on the fixed effects, that's not the case.")
# get rsquared values
rsquaredab = robjects.r('r.squaredGLMM')(model_ab.model_obj)[0]
rsquareda = robjects.r('r.squaredGLMM')(model_a.model_obj)[0]
r2lr = robjects.r('''
function (object)
{
r.squaredLR(object, null.RE = TRUE, adj.r.squared=TRUE)
}
''')
rsquaredab_lr = r2lr(model_ab.model_obj)[0]
rsquareda_lr = r2lr(model_a.model_obj)[0]
# add rsquared and F-test results to anova table
anova_res['marginal_R2'] = (rsquareda[0], rsquaredab[0])
anova_res['conditional_R2'] = (rsquareda[1], rsquaredab[1])
anova_res['lr_marginal_R2'] = (rsquareda_lr, rsquaredab_lr)
anova_res['f2'] = (np.nan, (rsquaredab[0] - rsquareda[0]) / (1 - (rsquaredab[0])))
anova_res['lr_f2'] = (np.nan, (rsquaredab_lr - rsquareda_lr) / (1 - (rsquaredab_lr)))
#dfFit_a['VIF'] = np.insert(robjects.r('vif')(model_a.model_obj),0,0)
#dfFit_ab['VIF'] = np.insert(robjects.r('vif')(model_ab.model_obj),0,0)
# return results
return anova_res, dfFit_a, dfFit_ab, dfFixef_a
# Function to get summary boredom scores for each participant
def GetBoredomScores(df_boredom_probes):
# Extract unique list of participant & blocks
participants = np.unique(df_boredom_probes.participant)
blocks = np.unique(df_boredom_probes.iBlock).astype(int)
# Set up summary table
df_summary = pd.DataFrame(columns=['participant']+[f'block{x}' for x in blocks])
df_summary['participant'] = participants
# Fill table with boredom score in each block
for participant_index,participant in enumerate(participants):
df_this = df_boredom_probes.loc[df_boredom_probes.participant==participant,:]
for block in blocks:
boredom_score = np.sum(df_this.loc[df_this.iBlock==block,'rating'])
df_summary.loc[participant_index,f'block{block}'] = boredom_score
# Return results
return df_summary
# Calculate Cohen's D
def GetCohensD(x,y):
# calculate the effect size (Cohen's D) given readings from
# x (treatment group) and y (control group).
# calculate d without weighting stddevs according to number of elements
# (simpler if numel are the same in x and y)
if len(x)==len(y):
d = (np.mean(x) - np.mean(y)) / np.sqrt((np.std(x,ddof=1) ** 2 + np.std(y,ddof=1) ** 2) / 2)
return d
# weight stddevs according to number of elements
else:
# calculate intermediate values
x_count = len(x)
y_count = len(y)
dof = x_count + y_count - 2 # degrees of freedom
# calculate & return cohen's D
d = (np.mean(x) - np.mean(y)) / np.sqrt(((x_count-1)*np.std(x, ddof=1) ** 2 + (y_count-1)*np.std(y, ddof=1) ** 2) / dof)
return d
# Perform two one-sided t-tests to check if effect size magnitude of treatment is within specified bounds.
def TestMeanNeededForD(x,y,D=0.5):
# check whether the mean of X (treatment group) given Y (control group)
# indicates an effect size (Cohen's D) that is significantly -|D|<actualD<|D|.
# calculate d without weighting stddevs according to number of elements
# (simpler if numel are the same in x and y)
if len(x)==len(y):
# get limits
meanX_lower = np.mean(y) - np.abs(D) * np.sqrt( (np.std(x,ddof=1)**2 + np.std(y,ddof=1) **2) / 2)
meanX_upper = np.mean(y) + np.abs(D) * np.sqrt( (np.std(x,ddof=1)**2 + np.std(y,ddof=1) **2) / 2)
dof = len(x)+len(y)-2
# weight stddevs according to number of elements
else:
# get in-between quantities
x_count = len(x)
y_count = len(y)
dof = x_count + y_count - 2 # degrees of freedom
# get limits
meanX_lower = np.mean(y) - np.abs(D) * np.sqrt( ((x_count-1)*np.std(x,ddof=1)**2 + (y_count-1)*np.std(x,ddof=1)**2) / dof) # mean of X corresponding to d_actual=-|D|
meanX_upper = np.mean(y) + np.abs(D) * np.sqrt( ((x_count-1)*np.std(x,ddof=1)**2 + (y_count-1)*np.std(x,ddof=1)**2) / dof) # mean of X corresponding to d_actual=|D|
# Run 2 one-sided t-tests
t_more,p_more = stats.ttest_1samp(x,popmean=meanX_lower,alternative='greater') # H1: d_actual>-|D|. H0: d_actual<=-|D|.
t_less,p_less = stats.ttest_1samp(x,popmean=meanX_upper,alternative='less') # H1: d_actual<|D|. H0: d_actual>=|D|.
# return results
return t_more,p_more,t_less,p_less,dof
# Print effect that a change of 1std would have on mood slope
def PrintEffectOf1StdChange(pymer_input,dfFit_h1,new_factor):
mean_val = np.mean(pymer_input.loc[:,new_factor])
std_val = np.std(pymer_input.loc[:,new_factor])
before = (dfFit_h1.loc['Time','Estimate'] + dfFit_h1.loc[f'Time:{new_factor}','Estimate'] * mean_val) * 100
change = (dfFit_h1.loc[f'Time:{new_factor}','Estimate'] * std_val) * 100
after = before+change
print(f'** An increase in {new_factor} of 1 std ({std_val:.03g}) from the mean ({mean_val:.03g}) \n'+
f'** would change the estimated mood slope by {change:.03g} %mood/min, \n'+
f'** from {before:.03g} to {after:.03g}, a change of {change/before*100:.03g}%.')
# Correlate new factor in fixed-effects pymer model with LME Time factor from reduced model
def PrintFactorSlopeCorrelations(dfFixef_h0,new_factor,factor_name,cohort_name='unknown'):
print('')
print(f'Correlating {factor_name} with LME slope in reduced model:')
lme_slope = dfFixef_h0.Time.values
MakeJointPlot(new_factor,lme_slope,factor_name,'lme_slope',cohort_name=cohort_name)
# r,p = stats.pearsonr(new_factor,lme_slope)
# print(f'Pearson r2={r**2:.3g},p={p:.3g}')
# r_s,p_s = stats.spearmanr(new_factor,lme_slope)
# print(f'Spearman r2={r_s**2:.3g},p={p_s:.3g}')
# print('')
# Get
def GetBeforeAndAfterBoredom(df_boredom,pymer_input):
# Set up
participants = np.unique(df_boredom.participant)
initial_boredom = np.zeros(len(participants))
final_boredom = np.zeros(len(participants))
delta_boredom = np.zeros(len(participants))
# Loop through subjects
for participant_index,participant in enumerate(participants):
# crop to this participant
df_this = df_boredom.loc[df_boredom.participant==participant,:]
# get change in boredom scores
initial_boredom[participant_index] = np.sum(df_this.loc[df_this.iBlock==-1,'rating']) # after first block
final_boredom[participant_index] = np.sum(df_this.loc[df_this.iBlock==0,'rating']) # after first block
delta_boredom[participant_index] = final_boredom[participant_index] - initial_boredom[participant_index]
# add to pymer_input table
pymer_input.loc[pymer_input.Subject==participant,'initialBoredom'] = initial_boredom[participant_index]
pymer_input.loc[pymer_input.Subject==participant,'finalBoredom'] = final_boredom[participant_index]
pymer_input.loc[pymer_input.Subject==participant,'deltaBoredom'] = delta_boredom[participant_index]
df_summary = pd.DataFrame({'participant':participants,
'initial_boredom':initial_boredom,
'final_boredom':final_boredom,
'delta_boredom':delta_boredom})
return pymer_input,df_summary
# Get last-mood minus first-mood for each participant in a list
def GetDeltaMood(pymer_input,participants):
# get last-mood minus first-mood
delta_mood = np.zeros(len(participants))
for participant_index, participant in enumerate(participants):
# pull out 1st-vs-last mood
mood = pymer_input.loc[pymer_input.Subject==participant,'Mood'].values
delta_mood[participant_index] = mood[-1]-mood[0]
return delta_mood
# Make a Seaborn joint plot with a regression line, printing stats for each factor and the regression
def MakeJointPlot(stat_a,stat_b,stat_a_name,stat_b_name,cohort_name='unknown'):
print(f'= {stat_a_name}:')
D = np.mean(stat_a)/np.std(stat_a)
print(f' mean={np.mean(stat_a):.3g}, std={np.std(stat_a):.3g}, D={D:.3g}')
t,p = stats.ttest_1samp(stat_a,0)
print(f' 2-sided t-test against 0: t={t:.3g},p={p:.3g}')
print(f'= {stat_b_name}:')
D = np.mean(stat_b)/np.std(stat_b)
print(f' mean={np.mean(stat_b):.3g}, std={np.std(stat_b):.3g}, D={D:.3g}')
t,p = stats.ttest_1samp(stat_b,0)
print(f' {stat_b_name} ~=0: t={t:.3g},p={p:.3g}')
print('= Correlation:')
r,p = stats.pearsonr(stat_a,stat_b)
print(f' Pearson r2={r**2:.3g},p={p:.3g}')
r_s,p_s = stats.spearmanr(stat_a,stat_b)
print(f' Spearman r2={r_s**2:.3g},p={p_s:.3g}')
# Do joint plot
df_stat = pd.DataFrame()
df_stat[stat_a_name] = stat_a
df_stat[stat_b_name] = stat_b
sns.jointplot(x=stat_a_name,y=stat_b_name,data=df_stat,kind="reg")
# annotate plot
plt.suptitle(f'Cohort {cohort_name}: {stat_a_name} vs. {stat_b_name}\n'+
f'r_s^2={r_s**2:.3g},p_s={p_s:.3g}')
plt.tight_layout()
plt.subplots_adjust(top=0.90) # Reduce plot to make room
# save plot
fig_file = f'{figures_dir}/{cohort_name}_{stat_a_name}-vs-{stat_b_name}_jointplot.png'
print(f'=Saving {stat_a_name} vs. {stat_b_name} jointplot as {fig_file}....')
plt.savefig(fig_file)
fig_file = f'{figures_dir}/{cohort_name}_{stat_a_name}-vs-{stat_b_name}_jointplot.pdf'
print(f'=Saving {stat_a_name} vs. {stat_b_name} jointplot as {fig_file}....')
plt.savefig(fig_file)
# Crop to exclude all mood ratings after miniumum rating (for floor effects)
def CropToMinRating(pymer_input):
# get list of subjects
participants = np.unique(pymer_input.Subject)
for participant_index,participant in enumerate(participants):
# crop to this subject's data
df_this = pymer_input.loc[pymer_input.Subject==participant,:]
min_index = np.argmin(df_this['Mood']) # find index of minimum rating
pymer_input = pymer_input.drop(df_this.index[min_index+1:])
return pymer_input
# Declare file locations
results_dir = '../Data/OutFiles'
figures_dir = '../Figures' # where figures should be saved
# %% Demographics of new cohorts
in_file = f'{results_dir}/Mmi-Batches.csv'
df_batches = pd.read_csv(in_file)
is_control_batch = df_batches.endDate>'2021-01-01'
subj_attempted_count = np.sum(df_batches.loc[is_control_batch,'nSubjAttempted'])
subj_completed_count = np.sum(df_batches.loc[is_control_batch,'nSubjCompleted'])
print('=======================================')
print('=== DEMOGRAPHICS ===')
print(f'{subj_attempted_count} participants completed these tasks online. ' +
f'{subj_attempted_count-subj_completed_count} participants were ' +
'excluded because their task or survey data was incomplete or did not ' +
'save, because they completed the task more than once despite ' +
'instructions to the contrary, or because they failed to answer one ' +
'or more "catch" questions correctly on the survey.')
# get batch names
batches = df_batches.loc[is_control_batch,'batchName']
# batches = ['Activities','AllMw','AllBoredom']
# Get age/gender info
participant_count = 0
female_count = 0
age_sum = 0
max_age = 0
min_age = 99
for batch in batches:
in_file = f'{results_dir}/Mmi-{batch}_Survey.csv'
df_survey = pd.read_csv(in_file)
participant_count += df_survey.shape[0]
female_count += np.sum(df_survey.gender=='Female')
age_sum += np.sum(df_survey.age)
max_age = np.max([max_age,np.max(df_survey.age)])
min_age = np.min([min_age,np.min(df_survey.age)])
# print results
print(f'Of the {participant_count} remaining participants, {female_count} ' +
f'were female ({female_count/participant_count*100:.3g}%). Participants ' +
f'had a mean age of {age_sum/participant_count:.3g} years (range: {min_age:.0f}-{max_age:.0f}).')
# %% Hyp 1.1: Boredom Repeat Administraion
print('=======================================')
print('')
print('=======================================')
print("""
Boredom Hypotheses:
1.1) In the validation of short-interval state boredom scale repeat
administration,we hypothesize that the effect of including an initial
administration will have an absolute effect size (cohen’s d) less than 0.5.
We will test this with two, one-sided t-tests (TOST).
""")
print('=== BOREDOM REPEAT ADMINISTRATION ===')
# Declare batches
batch_ba = 'BoredomBeforeAndAfter' # before-and-after group, got thought probes both before and after rest block
batch_ao = 'BoredomAfterOnly' # after-only group, got thought probes only after rest block
# Before and after group: Load probes file
in_file = f'{results_dir}/Mmi-{batch_ba}_Probes.csv'
df_boredom_probes_ba = pd.read_csv(in_file)
# Get boredom scores in each block
df_summary_ba = GetBoredomScores(df_boredom_probes_ba)
# After-only group: Load probes file
in_file = f'{results_dir}/Mmi-{batch_ao}_Probes.csv'
df_boredom_probes_ao = pd.read_csv(in_file)
# Get boredom scores in each block
df_summary_ao = GetBoredomScores(df_boredom_probes_ao)
# Perform t-tests to check for differences between groups
block_names = df_summary_ao.columns[1:]
for block_to_check in block_names:
print(f'= {block_to_check} =')
# calculate cohen's d
cohens_d = GetCohensD(df_summary_ba[block_to_check],df_summary_ao[block_to_check])
# Check mean of before-and-after group against cohen's D from preregistration
D_cutoff = 0.5
t_more,p_more,t_less,p_less,dof = TestMeanNeededForD(df_summary_ba[block_to_check],df_summary_ao[block_to_check],D=D_cutoff)
# Print results
print(f'{batch_ba} vs. {batch_ao}: Cohens D={cohens_d:.03g}')
print(f'Is {batch_ba} < {batch_ao} with Cohens D>{-np.abs(D_cutoff):.03g}: T_{dof}={t_more:.03g}, p={p_more:.03g}')
print(f'Is {batch_ba} > {batch_ao} with Cohens D<{np.abs(D_cutoff):.03g}: T_{dof}={t_less:.03g}, p={p_less:.03g}')
if block_to_check=='block0':
# Print conclusions
if (p_less<0.05) and (p_more>0.05):
print(f'** Presenting boredom questions before start of task leads to DECREASED responses after {block_to_check}.')
use_both_boredom = False
print(f'** because we cannot exclude H0:|D|>={np.abs(D_cutoff):.03g}, we will use only the {batch_ao} cohort in subsequent analyses.')
elif (p_more<0.05) and (p_less>0.05):
print(f'** Presenting boredom questions before start of task leads to INCREASED responses after {block_to_check}.')
use_both_boredom = False
print(f'** because we cannot exclude H0:|D|>={np.abs(D_cutoff):.03g}, we will use only the {batch_ao} cohort in subsequent analyses.')
else:
print(f'** Presenting boredom questions before start of task DOES NOT change responses after {block_to_check}.')
use_both_boredom = True
print(f'** because we can exclude H0:|D|>{np.abs(D_cutoff):.03g}, we will use both boredom cohorts in subsequent analyses.')
# %% Hyp 1.2: Effect of finalBoredom on mood
print('=======================================')
print('')
print('=======================================')
print("""
1.2) We hypothesize that final state boredom will explain variance in
subject-level POTD slope. This is a one-sided hypothesis.
We will test this with an ANOVA comparing the following two mixed effects
models (difference highlighted in bold):
H0: Mood ~ 1 + Time * (isMale + meanIRIOver20 + fracRiskScore + isAge40to100) + (1 + Time|Subject)
H1: Mood ~ 1 + Time * (finalBoredom + isMale + meanIRIOver20 + fracRiskScore + isAge40to100) + (1 + Time|Subject)
""")
# If repeat administration changes results, we'll use the after-only group.
# Otherwise, use both.
if use_both_boredom:
batch = 'AllBoredom'
else:
batch = 'BoredomAfterOnly'
print(f'=== Batch {batch}: Comparing LME models with and without finalBoredom ===')
# Load pymer input file
in_file = f'{results_dir}/Mmi-{batch}_pymerInput-full.csv'
print(f'Opening {in_file}...')
pymer_input = pd.read_csv(in_file, index_col=0)
anova_res = None
for do_premin_only in [False,True]:
# Control for floor effects
if do_premin_only:
if anova_res.loc[1,'Pr(>Chisq)']<0.05:
print('=======================================\n\n'+
'=======================================')
print('=== 4.1.2: Control for floor effects by using excluding ratings after a subject''s minimum')
# Crop to exclude all mood ratings after miniumum rating (for floor effects)
pymer_input = CropToMinRating(pymer_input)
cohort_name = f'{batch}-premin'
else:
print('=== Skipping floor effects control because original was not significant.')
break
else:
cohort_name = batch
# Add finalBoredom scores
in_file = f'{results_dir}/Mmi-{batch}_Probes.csv'
print(f'Opening {in_file}...')
df_boredom = pd.read_csv(in_file)
participants = np.unique(df_boredom.participant)
final_boredom = np.zeros(len(participants))
for participant_index,participant in enumerate(participants):
# crop to this participant
df_this = df_boredom.loc[df_boredom.participant==participant,:]
# get final boredom score
final_boredom[participant_index] = np.sum(df_this.loc[df_this.iBlock==0,'rating']) # after first block
# add to pymer_input table
pymer_input.loc[pymer_input.Subject==participant,'finalBoredom'] = final_boredom[participant_index]
# Plot stat vs. change in mood
delta_mood = GetDeltaMood(pymer_input,participants)
MakeJointPlot(final_boredom,delta_mood,'final_boredom','delta_mood',cohort_name=cohort_name)
# Fit models and run ANOVA to compare
lm_string_h0 = 'Mood ~ 1 + Time * (isMale + meanIRIOver20 + fracRiskScore + isAge40to100) + (1 + Time|Subject)'
lm_string_h1 = 'Mood ~ 1 + Time * (finalBoredom + isMale + meanIRIOver20 + fracRiskScore + isAge40to100) + (1 + Time|Subject)'
anova_res, dfFit_h0, dfFit_h1, dfFixef_h0 = compare_lmers(pymer_input,lm_string_h0,lm_string_h1)
print('ANOVA:')
print(anova_res)
# correlate new factor with subject LME slopes in reduced model
PrintFactorSlopeCorrelations(dfFixef_h0,final_boredom,'finalBoredom',cohort_name)
# Print results and pymer fit
if anova_res.loc[1,'Pr(>Chisq)']<0.05:
print('** Final state boredom DOES explain added variance in subject-level POTD slope.')
else:
print('** Final state boredom does NOT explain added variance in subject-level POTD slope.')
print(dfFit_h1.loc[['Time','finalBoredom','Time:finalBoredom']])
# Print effect that a change of 1std would have on mood slope
PrintEffectOf1StdChange(pymer_input,dfFit_h1,'finalBoredom')
# %% Hyp 1.3: Effect of deltaBoredom on mood
print('=======================================')
print('')
print('=======================================')
print("""
1.3) We hypothesize that the change in boredom will explain variance in
subject-level POTD slope. This is a one-sided hypothesis.
We will test this with an ANOVA comparing the following two mixed effects
models (difference highlighted in bold):
H0: Mood ~ 1 + Time * (isMale + meanIRIOver20 + fracRiskScore + isAge40to100) + (1 + Time|Subject)
H1: Mood ~ 1 + Time * (deltaBoredom + isMale + meanIRIOver20 + fracRiskScore + isAge40to100) + (1 + Time|Subject)
If we fail to reject the null for hypothesis 1.1 (absolute cohen’s d is less
than 0.5) we will have to interpret the results of this hypothesis with the
caveat that it is possible that repeated administration of the state
boredom measure may have altered the results of the subsequent administration.
""")
# Analyzing change in boredom requires before-and-after group
batch = 'BoredomBeforeAndAfter'
print(f'=== Batch {batch}: Comparing LME models with and without deltaBoredom ===')
# Load pymer input file
in_file = f'{results_dir}/Mmi-{batch}_pymerInput-full.csv'
print(f'Opening {in_file}...')
pymer_input = pd.read_csv(in_file, index_col=0)
anova_res = None
for do_premin_only in [False,True]:
# Control for floor effects
if do_premin_only:
if anova_res.loc[1,'Pr(>Chisq)']<0.05:
print('=======================================\n\n'+
'=======================================')
print('=== 4.1.3: Control for floor effects by using excluding ratings after a subject''s minimum')
# Crop to exclude all mood ratings after miniumum rating (for floor effects)
pymer_input = CropToMinRating(pymer_input)
cohort_name = f'{batch}-premin'
else:
print('=== Skipping floor effects control because original was not significant.')
break
else:
cohort_name = batch
# Add deltaBoredom scores
in_file = f'{results_dir}/Mmi-{batch}_Probes.csv'
print(f'Opening {in_file}...')
df_boredom = pd.read_csv(in_file)
pymer_input,df_summary = GetBeforeAndAfterBoredom(df_boredom,pymer_input)
# Plot stat vs. change in mood
delta_mood = GetDeltaMood(pymer_input,df_summary['participant'])
MakeJointPlot(df_summary['delta_boredom'],delta_mood,'delta_boredom','delta_mood',cohort_name=cohort_name)
# for completeness, also do initial & final for this group
MakeJointPlot(df_summary['initial_boredom'],delta_mood,'initial_boredom','delta_mood',cohort_name=cohort_name)
MakeJointPlot(df_summary['final_boredom'],delta_mood,'final_boredom','delta_mood',cohort_name=cohort_name)
# Fit models and run ANOVA to compare
lm_string_h0 = 'Mood ~ 1 + Time * (isMale + meanIRIOver20 + fracRiskScore + isAge40to100) + (1 + Time|Subject)'
lm_string_h1 = 'Mood ~ 1 + Time * (deltaBoredom + isMale + meanIRIOver20 + fracRiskScore + isAge40to100) + (1 + Time|Subject)'
anova_res, dfFit_h0, dfFit_h1, dfFixef_h0 = compare_lmers(pymer_input,lm_string_h0,lm_string_h1)
print('ANOVA:')
print(anova_res)
# correlate new factor with subject LME slopes in reduced model
PrintFactorSlopeCorrelations(dfFixef_h0,df_summary['delta_boredom'],'deltaBoredom',cohort_name)
# Print results and pymer fit
if anova_res.loc[1,'Pr(>Chisq)']<0.05:
print('** Change in state boredom DOES explain added variance in subject-level POTD slope.')
else:
print('** Change in state boredom does NOT explain added variance in subject-level POTD slope.')
print(dfFit_h1.loc[['Time','deltaBoredom','Time:deltaBoredom']])
# Print effect that a change of 1std would have on mood slope
PrintEffectOf1StdChange(pymer_input,dfFit_h1,'deltaBoredom')
# %% Hyp 1.4: Effect of traitBoredom on mood
print('=======================================')
print('')
print('=======================================')
print("""
1.4) We hypothesize that trait boredom will explain variance in subject-level
POTD slope.This is a one-sided hypothesis.
We will test this with an ANOVA comparing the following two mixed effects
models (difference highlighted in bold):
H0: Mood ~ 1 + Time * (isMale + meanIRIOver20 + fracRiskScore + isAge40to100) + (1 + Time|Subject)
H1: Mood ~ 1 + Time * (traitBoredom + isMale + meanIRIOver20 + fracRiskScore + isAge40to100) + (1 + Time|Subject)
""")
# If repeat administration changes results, we'll use the after-only group.
# Otherwise, use both.
if use_both_boredom:
batch = 'AllBoredom'
else:
batch = 'BoredomAfterOnly'
print(f'=== Batch {batch}: Comparing LME models with and without traitBoredom ===')
# Load pymer input file
in_file = f'{results_dir}/Mmi-{batch}_pymerInput-full.csv'
print(f'Opening {in_file}...')
pymer_input = pd.read_csv(in_file, index_col=0)
anova_res = None
for do_premin_only in [False,True]:
# Control for floor effects
if do_premin_only:
if anova_res.loc[1,'Pr(>Chisq)']<0.05:
print('=======================================\n\n'+
'=======================================')
print('=== 4.1.4: Control for floor effects by using excluding ratings after a subject''s minimum')
# Crop to exclude all mood ratings after miniumum rating (for floor effects)
pymer_input = CropToMinRating(pymer_input)
cohort_name = f'{batch}-premin'
else:
print('=== Skipping floor effects control because original was not significant.')
break
else:
cohort_name = batch
# Add traitBoredom scores
in_file = f'{results_dir}/Mmi-{batch}_Survey.csv'
print(f'Opening {in_file}...')
df_boredom = pd.read_csv(in_file)
participants = np.unique(df_boredom.participant)
trait_boredom = np.zeros(len(participants))
delta_mood = GetDeltaMood(pymer_input,participants)
for participant_index,participant in enumerate(participants):
trait_boredom[participant_index] = df_boredom.loc[df_boredom.participant==participant,'BORED'].values[0]
pymer_input.loc[pymer_input.Subject==participant,'traitBoredom'] = trait_boredom[participant_index]
# Fit models and run ANOVA to compare
lm_string_h0 = 'Mood ~ 1 + Time * (isMale + meanIRIOver20 + fracRiskScore + isAge40to100) + (1 + Time|Subject)'
lm_string_h1 = 'Mood ~ 1 + Time * (traitBoredom + isMale + meanIRIOver20 + fracRiskScore + isAge40to100) + (1 + Time|Subject)'
anova_res, dfFit_h0, dfFit_h1, dfFixef_h0 = compare_lmers(pymer_input,lm_string_h0,lm_string_h1)
print('ANOVA:')
print(anova_res)
# correlate new factor with subject LME slopes in reduced model
PrintFactorSlopeCorrelations(dfFixef_h0,trait_boredom,'traitBoredom',cohort_name)
# Print results and pymer fit
if anova_res.loc[1,'Pr(>Chisq)']<0.05:
print('** Trait boredom DOES explain added variance in subject-level POTD slope.')
else:
print('** Trait boredom does NOT explain added variance in subject-level POTD slope.')
print(dfFit_h1.loc[['Time','traitBoredom','Time:traitBoredom']])
# Print effect that a change of 1std would have on mood slope
PrintEffectOf1StdChange(pymer_input,dfFit_h1,'traitBoredom')
# %% Get MW principal components
print('=======================================')
print('')
print('=======================================')
print("""
Mind Wandering Hypotheses:
2.1) In the validation of short-interval MDES repeat administration, we
hypothesize that the effect of including an initial administration will
have an absolute effect size (cohen’s d) less than 0.5.
We will test this with two, one-sided t-tests (TOST).
""")
print('=== MW PCA ===' )
# Get all probes and run PCA
# batch = 'MwBeforeAndAfter' # before-and-after group
batch = 'MwAfterOnly' # after-only group
# Load probes file
in_file = f'{results_dir}/Mmi-{batch}_Probes.csv'
df_mw = pd.read_csv(in_file)
# Extract ratings and center scale at 0
X = df_mw['rating'].values.reshape([-1,13])-0.5
# Fit PCA to these ratings
pca = PCA(n_components=13,whiten=True)
pca.fit(X)
# print(pca.explained_variance_ratio_)
# print(pca.singular_values_)
# make plot of variance explained
fig = plt.figure(23,clear=True)
plt.plot(np.cumsum(pca.explained_variance_ratio_)*100)
plt.xlabel('component')
plt.ylabel('% variance explained')
plt.title('MW probe PCA')
# Save figure
fig_file = f'{figures_dir}/{batch}_MwPca_VarExplained.png'
print(f'Saving figure as {fig_file}...')
fig.savefig(fig_file)
fig_file = f'{figures_dir}/{batch}_MwPca_VarExplained.pdf'
print(f'Saving figure as {fig_file}...')
fig.savefig(fig_file)
# === Plot PC loadings
# Set up figure
pc_count = pca.n_components
question_labels = np.array(['task','future','past','myself','people','emotion','images','words','vivid','detailed','habit','evolving','deliberate'])
ticks = np.arange(len(question_labels))
fig,axes = plt.subplots(4,4,num=24,figsize=[12,8],clear=True,sharex=False,sharey=True)
axes = axes.flatten()
# Plot bars of loadings
for plot_index,ax in enumerate(axes):
if plot_index<pc_count:
# make bar plot
ax.bar(ticks,pca.components_[plot_index,:])
# set title
variance_explained = pca.explained_variance_ratio_[plot_index]*100
ax.set_title(f'Comp {plot_index}: varex={variance_explained:.1f}')
# annotate plot
ax.grid(True)
ax.set_xlabel('question')
ax.set_ylabel('loading')
ax.set_xticks(ticks)
ax.set_xticklabels(labels=question_labels,ha='right',rotation=45)
else:
# remove extra plots
ax.set_visible(False)
plt.tight_layout()
# Save figure
fig_file = f'{figures_dir}/{batch}_MwPcaLoadings.png'
print(f'Saving figure as {fig_file}...')
fig.savefig(fig_file)
fig_file = f'{figures_dir}/{batch}_MwPcaLoadings.pdf'
print(f'Saving figure as {fig_file}...')
fig.savefig(fig_file)
# Note the most emotion-related PC
# defined as the one with the largest magnitude loading on the emotion question
emotion_pc_index = np.argmax(np.abs(pca.components_[:,question_labels=='emotion'])) # 4
print(f'PC #{emotion_pc_index} appears to be emotion component.')
# %% Hyp 2.1: MW emotion repeat administration
# get summary scores
print('===')
print('')
print('=== MW REPEAT ADMINISTRATION ===')
# Function to get summary boredom scores for each participant
def GetMwScores(df_mw_probes,pca):
# Extract unique list of participant & blocks
participants = np.unique(df_mw_probes.participant)
blocks = np.unique(df_mw_probes.iBlock).astype(int)
# Set up summary table
df_summary = pd.DataFrame(columns=['participant']+[f'block{x}' for x in blocks])
df_summary['participant'] = participants
# Fill table with boredom score in each block
for participant_index,participant in enumerate(participants):
df_this = df_mw_probes.loc[df_mw_probes.participant==participant,:]
for block in blocks:
mw_ratings = np.atleast_2d(df_this.loc[df_this.iBlock==block,'rating'])-0.5 # make 1x13, move center of scale to 0
mw_score = pca.transform(mw_ratings)[0,emotion_pc_index] # use pca transformation to get MW score
df_summary.loc[participant_index,f'block{block}'] = mw_score
return df_summary
batch_ba = 'MwBeforeAndAfter'
batch_ao = 'MwAfterOnly'
in_file = f'{results_dir}/Mmi-{batch_ba}_Probes.csv'
df_mw_ba = pd.read_csv(in_file)
df_summary_ba = GetMwScores(df_mw_ba,pca)
in_file = f'{results_dir}/Mmi-{batch_ao}_Probes.csv'
df_mw_ao = pd.read_csv(in_file)
df_summary_ao = GetMwScores(df_mw_ao,pca)
block_names = df_summary_ao.columns[1:]
for block_to_check in block_names:
print(f'= {block_to_check} =')
# calculate cohen's d
cohens_d = GetCohensD(df_summary_ba[block_to_check],df_summary_ao[block_to_check])
# Check mean of before-and-after group against cohen's D from preregistration
D_cutoff = 0.5
t_more,p_more,t_less,p_less,dof = TestMeanNeededForD(df_summary_ba[block_to_check],df_summary_ao[block_to_check],D=D_cutoff)
# Print results
print(f'{batch_ba} vs. {batch_ao}: Cohens D={cohens_d:.03g}')
print(f'Is {batch_ba} < {batch_ao} with Cohens D>{-np.abs(D_cutoff):.03g}: T_{dof}={t_more:.03g}, p={p_more:.03g}')
print(f'Is {batch_ba} > {batch_ao} with Cohens D<{np.abs(D_cutoff):.03g}: T_{dof}={t_less:.03g}, p={p_less:.03g}')
if block_to_check=='block0':
# Print conclusions
if (p_less<0.05) and (p_more>0.05):
print(f'** Presenting MW questions before start of task leads to DECREASED responses after {block_to_check}.')
use_both_mw = False
print(f'** because we cannot exclude H0:|D|>={np.abs(D_cutoff):.03g}, we will use only the {batch_ao} cohort in subsequent analyses.')
elif (p_more<0.05) and (p_less>0.05):
print(f'** Presenting MW questions before start of task leads to INCREASED responses after {block_to_check}.')
use_both_mw = False
print(f'** because we cannot exclude H0:|D|>={np.abs(D_cutoff):.03g}, we will use only the {batch_ao} cohort in subsequent analyses.')
else:
print(f'** Presenting MW questions before start of task DOES NOT change responses after {block_to_check}.')
use_both_mw = True
print(f'** because we can exclude H0:|D|>{np.abs(D_cutoff):.03g}, we will use both MW cohorts in subsequent analyses.')
# %% Hyp 2.2: Effect of finalEmoDim on mood
print('=======================================')
print('')
print('=======================================')
print("""
2.2) We hypothesize that the final emotion dimension score will explain
variance in subject-level POTD slope. This is a one-sided hypothesis.
We will test this with an ANOVA comparing the following two mixed effects
models (difference highlighted in bold):
H0: Mood ~ 1 + Time * (isMale + meanIRIOver20 + fracRiskScore + isAge40to100) + (1 + Time|Subject)
H1: Mood ~ 1 + Time * (finalEmoDim + isMale + meanIRIOver20 + fracRiskScore + isAge40to100) + (1 + Time|Subject)
""")
# If repeat administration changes results, we'll use the after-only group.
# Otherwise, use both.
if use_both_mw:
batch = 'AllMw'
else:
batch = 'MwAfterOnly'
print(f'=== Batch {batch}: Comparing LME models with and without finalEmoDim ===')
in_file = f'{results_dir}/Mmi-{batch}_pymerInput-full.csv'
print(f'Opening {in_file}...')
pymer_input = pd.read_csv(in_file, index_col=0)
anova_res = None
for do_premin_only in [False,True]:
# Control for floor effects
if do_premin_only:
if anova_res.loc[1,'Pr(>Chisq)']<0.05:
print('=======================================\n\n'+
'=======================================')
print('=== 4.2.2: Control for floor effects by using excluding ratings after a subject''s minimum')
# Crop to exclude all mood ratings after miniumum rating (for floor effects)
pymer_input = CropToMinRating(pymer_input)
cohort_name = f'{batch}-premin'
else:
print('=== Skipping floor effects control because original was not significant.')
break
else:
cohort_name = batch
# Add finalEmoDim scores
in_file = f'{results_dir}/Mmi-{batch}_Probes.csv'
print(f'Opening {in_file}...')
df_mw = pd.read_csv(in_file)
participants = np.unique(df_mw.participant)
final_mw = np.zeros(len(participants))
delta_mood = GetDeltaMood(pymer_input,participants)
for participant_index,participant in enumerate(participants):
# crop to this participant
df_this = df_mw.loc[df_mw.participant==participant,:]
# get final MW score
X_this = np.atleast_2d(df_this.loc[df_this.iBlock==0,'rating'])-0.5 # iBlock==0: after first block. -0.5: Move center of scale to 0
final_mw[participant_index] = pca.transform(X_this)[0,emotion_pc_index]
# Add to pymer_input table
pymer_input.loc[pymer_input.Subject==participant,'finalEmoDim'] = final_mw[participant_index]
# Plot stat vs. change in mood
delta_mood = GetDeltaMood(pymer_input,participants)
MakeJointPlot(final_mw,delta_mood,'final_mw','delta_mood',cohort_name=cohort_name)
# Fit models and run ANOVA to compare
lm_string_h0 = 'Mood ~ 1 + Time * (isMale + meanIRIOver20 + fracRiskScore + isAge40to100) + (1 + Time|Subject)'
lm_string_h1 = 'Mood ~ 1 + Time * (finalEmoDim + isMale + meanIRIOver20 + fracRiskScore + isAge40to100) + (1 + Time|Subject)'
anova_res, dfFit_h0, dfFit_h1, dfFixef_h0 = compare_lmers(pymer_input,lm_string_h0,lm_string_h1)
print('ANOVA:')
print(anova_res)
# correlate new factor with subject LME slopes in reduced model
PrintFactorSlopeCorrelations(dfFixef_h0,final_mw,'finalEmoDim',cohort_name)
# Print results and fit
if anova_res.loc[1,'Pr(>Chisq)']<0.05:
print('** Final MW emotion DOES explain added variance in subject-level POTD slope.')
else:
print('** Final MW emotion does NOT explain added variance in subject-level POTD slope.')
print(dfFit_h1.loc[['Time','finalEmoDim','Time:finalEmoDim']])
# Print effect that a change of 1std would have on mood slope
PrintEffectOf1StdChange(pymer_input,dfFit_h1,'finalEmoDim')
# %% Hyp 2.3: Effect of deltaEmoDim on mood
print('=======================================')
print('')
print('=======================================')
print("""
2.3) We hypothesize that the change in emotion dimension score will explain
variance in subject-level POTD slope. This is a one-sided hypothesis.
We will test this with an ANOVA comparing the following two mixed effects
models (difference highlighted in bold):
H0: Mood ~ 1 + Time * (isMale + meanIRIOver20 + fracRiskScore + isAge40to100) + (1 + Time|Subject)
H1: Mood ~ 1 + Time * (deltaEmoDim + isMale + meanIRIOver20 + fracRiskScore + isAge40to100) + (1 + Time|Subject)
If we fail to reject the null for hypothesis 2.1 (absolute cohen’s d is
less than 0.5) we will have to interpret the results of this hypothesis
with the caveat that it is possible that repeated administration of the
MDES measure may have altered the results of the subsequent administration.
""")
# Analyzing change requires before-and-after batch
batch = 'MwBeforeAndAfter'
print(f'=== Batch {batch}: Comparing LME models with and without deltaEmoDim ===')
# Load pymyer input file
in_file = f'{results_dir}/Mmi-{batch}_pymerInput-full.csv'
print(f'Opening {in_file}...')
pymer_input = pd.read_csv(in_file, index_col=0)
anova_res = None
for do_premin_only in [False,True]:
# Control for floor effects
if do_premin_only:
if anova_res.loc[1,'Pr(>Chisq)']<0.05:
print('=======================================\n\n'+
'=======================================')
print('=== 4.2.3: Control for floor effects by using excluding ratings after a subject''s minimum')
# Crop to exclude all mood ratings after miniumum rating (for floor effects)
pymer_input = CropToMinRating(pymer_input)
cohort_name = f'{batch}-premin'
else:
print('=== Skipping floor effects control because original was not significant.')
break
else:
cohort_name = batch
# Add deltaEmoDim scores
in_file = f'{results_dir}/Mmi-{batch}_Probes.csv'
print(f'Opening {in_file}...')
df_mw = pd.read_csv(in_file)
participants = np.unique(df_mw.participant)
initial_mw = np.zeros(len(participants))
final_mw = np.zeros(len(participants))
delta_mw = np.zeros(len(participants))
delta_mood = GetDeltaMood(pymer_input,participants)
for participant_index,participant in enumerate(participants):
# crop to this participant
df_this = df_mw.loc[df_mw.participant==participant,:]
# get initial MW score
X_initial = np.atleast_2d(df_this.loc[df_this.iBlock==-1,'rating'])-0.5 # Before 1st block. Move center of scale to 0
initial_mw[participant_index] = pca.transform(X_initial)[0,emotion_pc_index]
# get final MW score
X_final = np.atleast_2d(df_this.loc[df_this.iBlock==0,'rating'])-0.5 # After 1st block. Move center of scale to 0
final_mw[participant_index] = pca.transform(X_final)[0,emotion_pc_index]
# Add to pymer_input table
delta_mw[participant_index] = final_mw[participant_index] - initial_mw[participant_index]
pymer_input.loc[pymer_input.Subject==participant,'deltaEmoDim'] = delta_mw[participant_index]
# Plot stat vs. change in mood
delta_mood = GetDeltaMood(pymer_input,participants)
MakeJointPlot(delta_mw,delta_mood,'delta_mw','delta_mood',cohort_name=cohort_name)
# for completeness, also do initial & final for this group
MakeJointPlot(initial_mw,delta_mood,'initial_mw','delta_mood',cohort_name=cohort_name)
MakeJointPlot(final_mw,delta_mood,'final_mw','delta_mood',cohort_name=cohort_name)
# Fit models and run ANOVA to compare
lm_string_h0 = 'Mood ~ 1 + Time * (isMale + meanIRIOver20 + fracRiskScore + isAge40to100) + (1 + Time|Subject)'
lm_string_h1 = 'Mood ~ 1 + Time * (deltaEmoDim + isMale + meanIRIOver20 + fracRiskScore + isAge40to100) + (1 + Time|Subject)'
anova_res, dfFit_h0, dfFit_h1, dfFixef_h0 = compare_lmers(pymer_input,lm_string_h0,lm_string_h1)
print('ANOVA:')
print(anova_res)
# correlate new factor with subject LME slopes in reduced model
PrintFactorSlopeCorrelations(dfFixef_h0,delta_mw,'deltaEmoDim',cohort_name)
# Print results and fit
if anova_res.loc[1,'Pr(>Chisq)']<0.05:
print('** Change in MW emotion DOES explain added variance in subject-level POTD slope.')
else:
print('** Change in MW emotion does NOT explain added variance in subject-level POTD slope.')
print(dfFit_h1.loc[['Time','deltaEmoDim','Time:deltaEmoDim']])
# Print effect that a change of 1std would have on mood slope
PrintEffectOf1StdChange(pymer_input,dfFit_h1,'deltaEmoDim')
# %% Hyp 2.4: Effect of traitMW on mood
print('=======================================')
print('')
print('=======================================')
print("""
2.4) We hypothesize that trait mind wandering will explain variance in
subject-level POTD slope. This is a one-sided hypothesis.
We will test this with an ANOVA comparing the following two mixed effects
models (difference highlighted in bold):
H0: Mood ~ 1 + Time * (isMale + meanIRIOver20 + fracRiskScore + isAge40to100) + (1 + Time|Subject)
H1: Mood ~ 1 + Time * (traitMW + isMale + meanIRIOver20 + fracRiskScore + isAge40to100) + (1 + Time|Subject)
""")
# If repeat administration changes results, we'll use the after-only group.
# Otherwise, use both.
if use_both_mw:
batch = 'AllMw'
else:
batch = 'MwAfterOnly'
print(f'=== Batch {batch}: Comparing LME models with and without traitMW ===')
# load pymer input tables
in_file = f'{results_dir}/Mmi-{batch}_pymerInput-full.csv'
print(f'Opening {in_file}...')
pymer_input = pd.read_csv(in_file, index_col=0)
anova_res = None
for do_premin_only in [False,True]:
# Control for floor effects
if do_premin_only:
if anova_res.loc[1,'Pr(>Chisq)']<0.05:
print('=======================================\n\n'+
'=======================================')
print('=== 4.2.4: Control for floor effects by using excluding ratings after a subject''s minimum')
# Crop to exclude all mood ratings after miniumum rating (for floor effects)
pymer_input = CropToMinRating(pymer_input)
cohort_name = f'{batch}-premin'
else:
print('=== Skipping floor effects control because original was not significant.')
break