Files
experiments-database/analysis/matlab/tests/tLme.m
T
Experiments DB Dev 89bb95088f feat(matlab): add 0-13 day window (lme_*_d0_13) + coverage warning
Box-A2 has no data past ~day 13, so the full-range days x tDCS interaction
extrapolates A2's slope and is confounded (spuriously significant, wrong sign).
Add the _d0_13 window (equal anchor coverage) and a report warning when the two
groups' day coverage is unequal. In the fair 0-13 window the interaction is n.s.
with a POSITIVE slope diff and the Day-1 tDCS effect is n.s. -- matching the
paper's 'equal on Day 1' direction (underpowered at n=8). Suite 32/32.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-20 09:58:19 -04:00

80 lines
3.9 KiB
Matlab

classdef tLme < matlab.unittest.TestCase
%TLME Tests for the linear "days x tDCS" mixed model (tdcs_lme) and its
% switch/case entry. Golden values computed from this dataset; the
% mergeA2_full interaction closely reproduces the paper's F=7.12/p=0.008.
methods (Test)
function testSubjectCountsAndDayEffect(testCase)
% N (Box-A2 + Box-B2 subjects) per scenario, and the always-present
% strong learning (day) effect.
cfg = tdcs_config();
expectedN = struct('unmerged_full', 6, 'mergeA2_full', 8, 'mergeB2_full', 8);
for sc = fieldnames(expectedN)'
L = tdcs_lme(tdcs_scenario_data(sc{1}), cfg);
testCase.verifyEqual(L.nSubjects, expectedN.(sc{1}), ...
sprintf('%s: wrong subject count', sc{1}));
testCase.verifyEqual(L.day.df1, 1); % F(1) term
testCase.verifyLessThan(L.day.p, 1e-6, ...
sprintf('%s: day (learning) effect should be highly significant', sc{1}));
end
end
function testMergeA2ReplicatesPaperInteraction(testCase)
% mergeA2_full reproduces the paper's days x tDCS interaction
% (paper: F(1)=7.12, p=0.008; |t|=2.68).
cfg = tdcs_config();
L = tdcs_lme(tdcs_scenario_data('mergeA2_full'), cfg);
testCase.verifyEqual(L.interaction.df1, 1);
testCase.verifyEqual(L.interaction.F, 7.09, 'AbsTol', 0.1);
testCase.verifyEqual(L.interaction.p, 0.009, 'AbsTol', 0.002);
testCase.verifyEqual(abs(L.interaction.t), 2.66, 'AbsTol', 0.05);
% single-df term: F == t^2
testCase.verifyEqual(L.interaction.F, L.interaction.t^2, 'RelTol', 1e-6);
end
function testEffectStructShape(testCase)
cfg = tdcs_config();
L = tdcs_lme(tdcs_scenario_data('unmerged_full'), cfg);
for f = {'interaction', 'day', 'tDCS'}
e = L.(f{1});
for g = {'estimate', 'se', 't', 'df', 'p', 'F', 'df1', 'df2', 'Fp'}
testCase.verifyTrue(isfield(e, g{1}), ...
sprintf('%s missing field %s', f{1}, g{1}));
end
end
end
function testSwitchRunsAndWritesResults(testCase)
% The lme_* switch cases run end-to-end and write results/<name>.txt.
here = fileparts(fileparts(mfilename('fullpath'))); % analysis/matlab
outFile = fullfile(here, 'results', 'lme_mergeA2_full.txt');
if exist(outFile, 'file'); delete(outFile); end
evalc("tdcs_glm('lme_mergeA2_full')");
testCase.verifyTrue(exist(outFile, 'file') == 2, ...
'lme_mergeA2_full.txt was not written');
end
function testBadScenarioErrors(testCase)
testCase.verifyError(@() tdcs_glm('lme_nonsense'), 'tdcs_glm:badScenario');
end
function testD0_13WindowFairCoverageAndParallel(testCase)
% The 0-13 window caps both anchor groups at day 13 (equal coverage),
% unlike the full window where Box-A2 stops at 13 but Box-B2 runs on.
cfg = tdcs_config();
Lfull = tdcs_lme(tdcs_scenario_data('mergeA2_full'), cfg);
L13 = tdcs_lme(tdcs_scenario_data('mergeA2_d0_13'), cfg);
% Full window: unequal coverage (A2 ends ~13, B2 runs later).
testCase.verifyGreaterThan(abs(Lfull.maxDayB - Lfull.maxDayA), 2);
% 0-13 window: equal coverage and the (truncation-driven) interaction
% is no longer significant -- the fair-window result.
testCase.verifyLessThanOrEqual(L13.maxDayA, 13);
testCase.verifyLessThanOrEqual(L13.maxDayB, 13);
testCase.verifyGreaterThan(L13.interaction.p, 0.05);
testCase.verifyLessThan(L13.day.p, 1e-6); % learning still strong
end
end
end