analysis(matlab): per-variation subfolders (grouping x window) for the paper LME

Add make_variations.m + variation_analyze.m generating 20 self-contained
subfolders under analysis/matlab/variations/, one per grouping x day-window,
each with exactly three files: data.csv (curated subset), analyze.m (a simple
standalone script fitting behavior ~ stim + day + stim:day + (1|rat) on the
success COUNT), and result.txt (its output). Plus variations/SUMMARY.csv.

Groupings (stim=1 / stim=0, other groups dropped):
  unmerge     B2 vs A2
  right_only  B2+Right vs A2            (Box-A dropped)
  naive_a2    B2 vs A2+Naive           (Right, Box-A dropped)
  naive_boxa  B2 vs A2+Naive+Box-A     (Right dropped)
Windows: 0-10, 0-13, 0-5, 6-10, 6-13. All 20 have equal day coverage.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Experiments DB Dev
2026-07-20 20:49:54 -04:00
parent 963fd889b2
commit 969c0205e8
63 changed files with 4343 additions and 0 deletions
@@ -0,0 +1,74 @@
% Variation analysis -- the paper's linear mixed model on the successful-reach
% COUNT, fit on this folder's curated data subset.
%
% model: behavior ~ stim + day + stim:day + (1|rat)
% behavior = successful reaches (count per session)
% stim = 1 for the treatment group(s), 0 for the control group(s)
% day = training day within this window (0 = first analyzed day)
% rat = subject (random intercept)
%
% Self-contained: reads data.csv beside this script and writes result.txt.
% Run headless from this folder with: matlab -batch "analyze"
% (This is a copy of analysis/matlab/variation_analyze.m; see make_variations.m.)
here = fileparts(mfilename('fullpath'));
if isempty(here); here = pwd; end
vname = regexprep(here, '.*[/\\]', ''); % folder name = variation id
D = readtable(fullfile(here, 'data.csv'), 'TextType', 'string');
tbl = table(D.success, D.day - min(D.day), double(D.stim), categorical(D.subject), ...
'VariableNames', {'behavior', 'day', 'stim', 'rat'});
m = fitlme(tbl, 'behavior ~ stim + day + stim:day + (1|rat)');
C = m.Coefficients; A = anova(m); ci = coefCI(m);
gi = @(t) find(strcmp(C.Name, t), 1);
ga = @(t) find(strcmp(A.Term, t), 1);
row = @(nm, t) sprintf('%-26s t(%d)=%6.2f F(%d)=%8.3f p=%.4g\n', nm, ...
C.DF(gi(t)), C.tStat(gi(t)), A.DF1(ga(t)), A.FStat(ga(t)), C.pValue(gi(t)));
maxT = max(D.day(D.stim == 1)); minT = min(D.day(D.stim == 1));
maxC = max(D.day(D.stim == 0)); minC = min(D.day(D.stim == 0));
if abs(maxT - maxC) > 2
cov = '** WARNING: unequal day coverage -- interaction may be confounded. **';
else
cov = '(equal day coverage over this window)';
end
ii = gi('day:stim'); pI = C.pValue(ii); eI = C.Estimate(ii);
if pI >= 0.05
verdict = 'n.s. -- slopes parallel (no differential learning rate)';
elseif eI > 0
verdict = 'SIGNIFICANT positive -- treatment improves FASTER (benefit accumulates)';
else
verdict = 'SIGNIFICANT negative -- treatment improves SLOWER (groups converge)';
end
bar = repmat('=', 1, 78);
raw = regexprep(evalc('disp(m)'), '</?strong>', '');
s = sprintf('%s\nVARIATION: %s\n%s\n', bar, vname, bar);
s = [s sprintf('model: behavior ~ stim + day + stim:day + (1|rat) (behavior = success COUNT)\n')];
s = [s sprintf('day = training day within window (0 = first analyzed day)\n')];
s = [s sprintf('treatment (stim=1): %s\n', strjoin(cellstr(unique(D.group(D.stim == 1))), ', '))];
s = [s sprintf('control (stim=0): %s\n', strjoin(cellstr(unique(D.group(D.stim == 0))), ', '))];
s = [s sprintf('N = %d rats, %d sessions raw day coverage: treat %d..%d, control %d..%d\n', ...
numel(unique(D.subject)), height(D), minT, maxT, minC, maxC)];
s = [s sprintf('%s\n\n%s\nFULL MODEL SUMMARY -- fitlme\n%s\n%s\n', cov, bar, bar, raw)];
s = [s sprintf('%-26s %-13s %-13s %s\n%s\n', 'effect', 't (df)', 'F (df1)', 'p', repmat('-', 1, 66))];
s = [s row('stim x day (interaction)', 'day:stim')];
s = [s row('day (learning)', 'day')];
s = [s row('stim (main, window start)', 'stim')];
s = [s sprintf('interaction 95%% CI: [%+.2f, %+.2f]\n', ci(ii, 1), ci(ii, 2))];
s = [s sprintf('INTERPRETATION: stim x day interaction %s (p=%.4g, slope diff=%+.2f)\n', verdict, pI, eI)];
s = [s sprintf('Paper (N=24): interaction t(227)=2.68, F(1)=7.12, p=0.008.\n')];
fprintf('%s', s);
fid = fopen(fullfile(here, 'result.txt'), 'w');
fprintf(fid, '%s', s);
fclose(fid);
% Machine-readable handoff for the summary table (see make_variations.m).
VARRESULT = struct('name', vname, 'nRats', numel(unique(D.subject)), ...
'nObs', height(D), 'interP', pI, 'interEst', eI, ...
'stimP', C.pValue(gi('stim')), 'dayP', C.pValue(gi('day')), ...
'covEqual', abs(maxT - maxC) <= 2);
@@ -0,0 +1,105 @@
subject,group,day,success,total,stim
Banh-mi-1,Electrode-Box-B2,0,13,84,1
Banh-mi-1,Electrode-Box-B2,1,35,86,1
Banh-mi-1,Electrode-Box-B2,2,47,110,1
Banh-mi-1,Electrode-Box-B2,3,65,140,1
Banh-mi-1,Electrode-Box-B2,4,70,127,1
Banh-mi-1,Electrode-Box-B2,5,102,142,1
Banh-mi-1,Electrode-Box-B2,6,90,131,1
Banh-mi-1,Electrode-Box-B2,7,109,148,1
Banh-mi-1,Electrode-Box-B2,8,104,137,1
Banh-mi-1,Electrode-Box-B2,9,119,150,1
Banh-mi-1,Electrode-Box-B2,10,121,158,1
Banh-mi-2,Electrode-Box-A2,0,25,97,0
Banh-mi-2,Electrode-Box-A2,1,22,101,0
Banh-mi-2,Electrode-Box-A2,2,22,119,0
Banh-mi-2,Electrode-Box-A2,3,29,118,0
Banh-mi-2,Electrode-Box-A2,4,27,136,0
Banh-mi-2,Electrode-Box-A2,5,43,146,0
Banh-mi-2,Electrode-Box-A2,6,74,146,0
Banh-mi-2,Electrode-Box-A2,7,70,148,0
Banh-mi-2,Electrode-Box-A2,8,65,130,0
Banh-mi-2,Electrode-Box-A2,9,79,151,0
Banh-mi-2,Electrode-Box-A2,10,93,152,0
Egg-tart-1,Electrode-Box-B2,0,7,56,1
Egg-tart-1,Electrode-Box-B2,1,16,78,1
Egg-tart-1,Electrode-Box-B2,2,23,103,1
Egg-tart-1,Electrode-Box-B2,3,63,120,1
Egg-tart-1,Electrode-Box-B2,4,69,132,1
Egg-tart-1,Electrode-Box-B2,5,83,136,1
Egg-tart-1,Electrode-Box-B2,6,71,142,1
Egg-tart-1,Electrode-Box-B2,7,79,138,1
Egg-tart-1,Electrode-Box-B2,8,98,142,1
Egg-tart-1,Electrode-Box-B2,9,89,139,1
Egg-tart-1,Electrode-Box-B2,10,96,143,1
Egg-tart-2,Electrode-Box-A2,0,9,32,0
Egg-tart-2,Electrode-Box-A2,1,2,38,0
Egg-tart-2,Electrode-Box-A2,2,31,93,0
Egg-tart-2,Electrode-Box-A2,3,44,101,0
Egg-tart-2,Electrode-Box-A2,4,54,131,0
Egg-tart-2,Electrode-Box-A2,5,84,139,0
Egg-tart-2,Electrode-Box-A2,6,85,145,0
Egg-tart-2,Electrode-Box-A2,7,79,143,0
Egg-tart-2,Electrode-Box-A2,8,76,131,0
Egg-tart-2,Electrode-Box-A2,9,88,149,0
Egg-tart-2,Electrode-Box-A2,10,81,151,0
Khoai-lang-2,Naive,0,0,0,0
Khoai-lang-2,Naive,1,0,0,0
Khoai-lang-2,Naive,2,10,47,0
Khoai-lang-2,Naive,3,11,52,0
Khoai-lang-2,Naive,4,9,56,0
Khoai-lang-2,Naive,5,34,95,0
Khoai-lang-2,Naive,6,21,72,0
Khoai-lang-2,Naive,7,23,99,0
Khoai-lang-2,Naive,8,64,136,0
Khoai-lang-2,Naive,9,75,131,0
Khoai-lang-2,Naive,10,63,134,0
Khoai-tay-2,Naive,1,21,68,0
Khoai-tay-2,Naive,2,6,79,0
Khoai-tay-2,Naive,3,0,83,0
Khoai-tay-2,Naive,4,28,87,0
Khoai-tay-2,Naive,5,31,125,0
Khoai-tay-2,Naive,6,62,144,0
Khoai-tay-2,Naive,7,87,141,0
Khoai-tay-2,Naive,8,105,152,0
Khoai-tay-2,Naive,9,75,148,0
Khoai-tay-2,Naive,10,101,149,0
OM-2,Naive,0,1,7,0
OM-2,Naive,1,11,33,0
OM-2,Naive,2,19,62,0
OM-2,Naive,3,29,117,0
OM-2,Naive,4,73,126,0
OM-2,Naive,5,53,135,0
OM-2,Naive,6,73,138,0
OM-2,Naive,7,80,131,0
OM-2,Naive,8,91,141,0
OM-2,Naive,9,90,135,0
OM-2,Naive,10,95,142,0
Root-beer-1,Electrode-Box-B2,0,11,85,1
Root-beer-1,Electrode-Box-B2,1,18,76,1
Root-beer-1,Electrode-Box-B2,2,40,105,1
Root-beer-1,Electrode-Box-B2,3,55,134,1
Root-beer-1,Electrode-Box-B2,4,75,136,1
Root-beer-1,Electrode-Box-B2,5,64,133,1
Root-beer-1,Electrode-Box-B2,6,104,139,1
Root-beer-1,Electrode-Box-B2,7,98,148,1
Root-beer-1,Electrode-Box-B2,8,81,145,1
Root-beer-1,Electrode-Box-B2,9,89,156,1
Root-beer-1,Electrode-Box-B2,10,105,158,1
Root-beer-2,Electrode-Box-A2,0,22,74,0
Root-beer-2,Electrode-Box-A2,1,31,87,0
Root-beer-2,Electrode-Box-A2,2,49,134,0
Root-beer-2,Electrode-Box-A2,3,31,89,0
Root-beer-2,Electrode-Box-A2,4,60,140,0
Root-beer-2,Electrode-Box-A2,5,84,147,0
Vu-vuong,Naive,0,18,61,0
Vu-vuong,Naive,1,35,91,0
Vu-vuong,Naive,2,61,127,0
Vu-vuong,Naive,3,34,146,0
Vu-vuong,Naive,4,61,141,0
Vu-vuong,Naive,5,37,151,0
Vu-vuong,Naive,6,49,131,0
Vu-vuong,Naive,7,65,149,0
Vu-vuong,Naive,8,67,141,0
Vu-vuong,Naive,9,73,140,0
Vu-vuong,Naive,10,72,131,0
1 subject group day success total stim
2 Banh-mi-1 Electrode-Box-B2 0 13 84 1
3 Banh-mi-1 Electrode-Box-B2 1 35 86 1
4 Banh-mi-1 Electrode-Box-B2 2 47 110 1
5 Banh-mi-1 Electrode-Box-B2 3 65 140 1
6 Banh-mi-1 Electrode-Box-B2 4 70 127 1
7 Banh-mi-1 Electrode-Box-B2 5 102 142 1
8 Banh-mi-1 Electrode-Box-B2 6 90 131 1
9 Banh-mi-1 Electrode-Box-B2 7 109 148 1
10 Banh-mi-1 Electrode-Box-B2 8 104 137 1
11 Banh-mi-1 Electrode-Box-B2 9 119 150 1
12 Banh-mi-1 Electrode-Box-B2 10 121 158 1
13 Banh-mi-2 Electrode-Box-A2 0 25 97 0
14 Banh-mi-2 Electrode-Box-A2 1 22 101 0
15 Banh-mi-2 Electrode-Box-A2 2 22 119 0
16 Banh-mi-2 Electrode-Box-A2 3 29 118 0
17 Banh-mi-2 Electrode-Box-A2 4 27 136 0
18 Banh-mi-2 Electrode-Box-A2 5 43 146 0
19 Banh-mi-2 Electrode-Box-A2 6 74 146 0
20 Banh-mi-2 Electrode-Box-A2 7 70 148 0
21 Banh-mi-2 Electrode-Box-A2 8 65 130 0
22 Banh-mi-2 Electrode-Box-A2 9 79 151 0
23 Banh-mi-2 Electrode-Box-A2 10 93 152 0
24 Egg-tart-1 Electrode-Box-B2 0 7 56 1
25 Egg-tart-1 Electrode-Box-B2 1 16 78 1
26 Egg-tart-1 Electrode-Box-B2 2 23 103 1
27 Egg-tart-1 Electrode-Box-B2 3 63 120 1
28 Egg-tart-1 Electrode-Box-B2 4 69 132 1
29 Egg-tart-1 Electrode-Box-B2 5 83 136 1
30 Egg-tart-1 Electrode-Box-B2 6 71 142 1
31 Egg-tart-1 Electrode-Box-B2 7 79 138 1
32 Egg-tart-1 Electrode-Box-B2 8 98 142 1
33 Egg-tart-1 Electrode-Box-B2 9 89 139 1
34 Egg-tart-1 Electrode-Box-B2 10 96 143 1
35 Egg-tart-2 Electrode-Box-A2 0 9 32 0
36 Egg-tart-2 Electrode-Box-A2 1 2 38 0
37 Egg-tart-2 Electrode-Box-A2 2 31 93 0
38 Egg-tart-2 Electrode-Box-A2 3 44 101 0
39 Egg-tart-2 Electrode-Box-A2 4 54 131 0
40 Egg-tart-2 Electrode-Box-A2 5 84 139 0
41 Egg-tart-2 Electrode-Box-A2 6 85 145 0
42 Egg-tart-2 Electrode-Box-A2 7 79 143 0
43 Egg-tart-2 Electrode-Box-A2 8 76 131 0
44 Egg-tart-2 Electrode-Box-A2 9 88 149 0
45 Egg-tart-2 Electrode-Box-A2 10 81 151 0
46 Khoai-lang-2 Naive 0 0 0 0
47 Khoai-lang-2 Naive 1 0 0 0
48 Khoai-lang-2 Naive 2 10 47 0
49 Khoai-lang-2 Naive 3 11 52 0
50 Khoai-lang-2 Naive 4 9 56 0
51 Khoai-lang-2 Naive 5 34 95 0
52 Khoai-lang-2 Naive 6 21 72 0
53 Khoai-lang-2 Naive 7 23 99 0
54 Khoai-lang-2 Naive 8 64 136 0
55 Khoai-lang-2 Naive 9 75 131 0
56 Khoai-lang-2 Naive 10 63 134 0
57 Khoai-tay-2 Naive 1 21 68 0
58 Khoai-tay-2 Naive 2 6 79 0
59 Khoai-tay-2 Naive 3 0 83 0
60 Khoai-tay-2 Naive 4 28 87 0
61 Khoai-tay-2 Naive 5 31 125 0
62 Khoai-tay-2 Naive 6 62 144 0
63 Khoai-tay-2 Naive 7 87 141 0
64 Khoai-tay-2 Naive 8 105 152 0
65 Khoai-tay-2 Naive 9 75 148 0
66 Khoai-tay-2 Naive 10 101 149 0
67 OM-2 Naive 0 1 7 0
68 OM-2 Naive 1 11 33 0
69 OM-2 Naive 2 19 62 0
70 OM-2 Naive 3 29 117 0
71 OM-2 Naive 4 73 126 0
72 OM-2 Naive 5 53 135 0
73 OM-2 Naive 6 73 138 0
74 OM-2 Naive 7 80 131 0
75 OM-2 Naive 8 91 141 0
76 OM-2 Naive 9 90 135 0
77 OM-2 Naive 10 95 142 0
78 Root-beer-1 Electrode-Box-B2 0 11 85 1
79 Root-beer-1 Electrode-Box-B2 1 18 76 1
80 Root-beer-1 Electrode-Box-B2 2 40 105 1
81 Root-beer-1 Electrode-Box-B2 3 55 134 1
82 Root-beer-1 Electrode-Box-B2 4 75 136 1
83 Root-beer-1 Electrode-Box-B2 5 64 133 1
84 Root-beer-1 Electrode-Box-B2 6 104 139 1
85 Root-beer-1 Electrode-Box-B2 7 98 148 1
86 Root-beer-1 Electrode-Box-B2 8 81 145 1
87 Root-beer-1 Electrode-Box-B2 9 89 156 1
88 Root-beer-1 Electrode-Box-B2 10 105 158 1
89 Root-beer-2 Electrode-Box-A2 0 22 74 0
90 Root-beer-2 Electrode-Box-A2 1 31 87 0
91 Root-beer-2 Electrode-Box-A2 2 49 134 0
92 Root-beer-2 Electrode-Box-A2 3 31 89 0
93 Root-beer-2 Electrode-Box-A2 4 60 140 0
94 Root-beer-2 Electrode-Box-A2 5 84 147 0
95 Vu-vuong Naive 0 18 61 0
96 Vu-vuong Naive 1 35 91 0
97 Vu-vuong Naive 2 61 127 0
98 Vu-vuong Naive 3 34 146 0
99 Vu-vuong Naive 4 61 141 0
100 Vu-vuong Naive 5 37 151 0
101 Vu-vuong Naive 6 49 131 0
102 Vu-vuong Naive 7 65 149 0
103 Vu-vuong Naive 8 67 141 0
104 Vu-vuong Naive 9 73 140 0
105 Vu-vuong Naive 10 72 131 0
@@ -0,0 +1,65 @@
==============================================================================
VARIATION: naive_a2_d0_10
==============================================================================
model: behavior ~ stim + day + stim:day + (1|rat) (behavior = success COUNT)
day = training day within window (0 = first analyzed day)
treatment (stim=1): Electrode-Box-B2
control (stim=0): Electrode-Box-A2, Naive
N = 10 rats, 104 sessions raw day coverage: treat 0..10, control 0..10
(equal day coverage over this window)
==============================================================================
FULL MODEL SUMMARY -- fitlme
==============================================================================
Linear mixed-effects model fit by ML
Model information:
Number of observations 104
Fixed effects coefficients 4
Random effects coefficients 10
Covariance parameters 2
Formula:
behavior ~ 1 + day*stim + (1 | rat)
Model fit statistics:
AIC BIC LogLikelihood Deviance
869.17 885.04 -428.58 857.17
Fixed effects coefficients (95% CIs):
Name Estimate SE tStat DF pValue
{'(Intercept)'} 10.33 4.4952 2.298 100 0.023646
{'day' } 8.0905 0.53615 15.09 100 1.598e-27
{'stim' } 12.019 8.117 1.4807 100 0.14184
{'day:stim' } 1.4337 0.92893 1.5434 100 0.12589
Lower Upper
1.4115 19.248
7.0268 9.1542
-4.0853 28.123
-0.40926 3.2767
Random effects covariance parameters (95% CIs):
Group: rat (10 Levels)
Name1 Name2 Type Estimate
{'(Intercept)'} {'(Intercept)'} {'std'} 8.753
Lower Upper
5.0208 15.26
Group: Error
Name Estimate Lower Upper
{'Res Std'} 13.78 11.942 15.902
effect t (df) F (df1) p
------------------------------------------------------------------
stim x day (interaction) t(100)= 1.54 F(1)= 2.382 p=0.1259
day (learning) t(100)= 15.09 F(1)= 227.710 p=1.598e-27
stim (main, window start) t(100)= 1.48 F(1)= 2.192 p=0.1418
interaction 95% CI: [-0.41, +3.28]
INTERPRETATION: stim x day interaction n.s. -- slopes parallel (no differential learning rate) (p=0.1259, slope diff=+1.43)
Paper (N=24): interaction t(227)=2.68, F(1)=7.12, p=0.008.