|
490 | 490 | 'no' 'no' 'yes' 'no' 'no' 'no' 'no' 'no' 'yes'}'; |
491 | 491 | babble = [4.6 4.4 3.9 5.6 5.1 5.5 3.9 3.5 3.7... |
492 | 492 | 5.6 4.7 5.9 6.0 5.4 6.6 5.8 5.3 5.7]'; |
493 | | - |
494 | 493 | STATS = bootlm (babble, {sugar, milk}, 'model', 'full', 'display', 'off', ... |
495 | 494 | 'varnames', {'sugar', 'milk'}); |
496 | 495 | % bootlm:test:8 |
497 | 496 | % Unbalanced three-way design (3x2x2). The data is from a study of the |
498 | 497 | % effects of three different drugs, biofeedback and diet on patient blood |
499 | 498 | % pressure, adapted* from Maxwell, Delaney and Kelly (2018): Ch 8, Table 12 |
500 | | - |
501 | 499 | drug = {'X' 'X' 'X' 'X' 'X' 'X' 'X' 'X' 'X' 'X' 'X' 'X' ... |
502 | 500 | 'X' 'X' 'X' 'X' 'X' 'X' 'X' 'X' 'X' 'X' 'X' 'X'; |
503 | 501 | 'Y' 'Y' 'Y' 'Y' 'Y' 'Y' 'Y' 'Y' 'Y' 'Y' 'Y' 'Y' ... |
|
764 | 762 | pval7 = randtest2 (X, Y, false, [], @(A, B) log (var (A) ./ var (B)), 1); |
765 | 763 |
|
766 | 764 | % bootridge:test:1 |
| 765 | + % Basic functionality: univariate, intercept auto-add, field shapes |
767 | 766 | m = 30; |
768 | 767 | x = linspace (-1, 1, m).'; |
769 | | - X = x; |
770 | | - randn (123); |
| 768 | + X = x; % No intercept provided |
| 769 | + randn ('seed', 123); |
771 | 770 | y = 1.0 + 0.8 * x + 0.1 * randn (m,1); |
772 | 771 | S = bootridge (y, X, [], 200, 0.05, [], 1.1, 777); |
773 | | - % bootridge:test:2 |
| 772 | + % Check expected fields and sizes |
| 773 | + assert (isfield (S, 'coefficient')); |
| 774 | + assert (~ isfield (S, 'estimate')); |
| 775 | + assert (size (S.coefficient, 2) == 1); |
| 776 | + assert (size (S.coefficient, 1) == 2); % intercept + slope |
| 777 | + assert (isfinite (S.lambda) && (S.lambda > 0)); |
| 778 | + assert (isfinite (S.df_lambda) && (S.df_lambda > 0) && ... |
| 779 | + (S.df_lambda <= m)); |
| 780 | + assert (all (S.CI_lower(:) <= S.coefficient(:) + eps)); |
| 781 | + assert (all (S.CI_upper(:) + eps >= S.coefficient(:))); |
| 782 | + assert (isfinite (S.Sigma_Y_hat) && (S.Sigma_Y_hat > 0)); |
| 783 | + assert (iscell (S.Sigma_Beta) && (numel (S.Sigma_Beta) == 1)); |
| 784 | + assert (all (size (S.Sigma_Beta{1}) == [2, 2])); |
| 785 | + assert (S.nboot == 200); |
| 786 | + assert (S.Deff == 1.1); |
| 787 | + % Hypothesis matrix L: return linear estimate instead of coefficients |
774 | 788 | m = 28; |
775 | 789 | x = linspace (-1.5, 1.5, m).'; |
776 | | - X = [ones(m,1), x]; |
777 | | - rng('default'); |
| 790 | + X = [ones(m,1), x]; % Explicit intercept is first column |
| 791 | + randn ('seed', 123); |
778 | 792 | y = 3.0 + 0.4 * x + 0.15 * randn (m,1); |
| 793 | + % Contrast to extract only the slope (second coefficient) |
779 | 794 | L = [0; 1]; |
780 | 795 | S = bootridge (y, X, [], 100, 0.10, L, 1, 99); |
781 | | - % bootridge:test:3 |
| 796 | + assert (~ isfield (S, 'coefficient')); |
| 797 | + assert (isfield (S, 'estimate')); |
| 798 | + assert (all (size (S.estimate) == [1, 1])); |
| 799 | + assert (all (size (S.CI_lower) == [1, 1])); |
| 800 | + assert (all (size (S.CI_upper) == [1, 1])); |
| 801 | + assert (all (size (S.BF10 ) == [1, 1])); |
| 802 | + assert (iscell (S.prior) && all (size (S.prior) == [1, 1])); |
| 803 | + % Categorical predictor supplied via CATEGOR (no scaling) |
782 | 804 | m = 36; |
| 805 | + % Two-level factor coded as centered +/-0.5 (column 2), plus a continuous |
783 | 806 | g = repmat ([ -0.5; 0.5 ], 18, 1); |
784 | 807 | x = linspace (-2, 2, m).'; |
785 | 808 | X = [ones(m,1), g, x]; |
786 | 809 | beta = [1.0; 0.7; -0.2]; |
787 | | - rng('default'); |
| 810 | + randn ('seed', 123); |
788 | 811 | y = X * beta + 0.25 * randn (m, 1); |
789 | | - categor = 2; |
| 812 | + categor = 2; % column 2 is categorical (excludes intercept) |
790 | 813 | S = bootridge (y, X, categor, 100, 0.05, [], 1, 2024); |
791 | | - % bootridge:test:4 |
| 814 | + assert (isfield (S, 'coefficient')); |
| 815 | + assert (size (S.coefficient, 1) == 3); |
| 816 | + assert (isfinite (S.lambda) && (S.lambda > 0)); |
| 817 | + % Check CI bracketing for all coefficients |
| 818 | + assert (all (S.CI_lower(:) <= S.coefficient(:) + eps)); |
| 819 | + assert (all (S.CI_upper(:) + eps >= S.coefficient(:))); |
| 820 | + % Multivariate outcomes and Deff scaling: Sigma_Y_hat should scale by Deff |
792 | 821 | m = 32; |
793 | 822 | x = linspace (-1, 1, m).'; |
794 | 823 | X = [ones(m,1), x]; |
795 | 824 | B = [2.0, -1.0; 0.5, 0.8]; |
796 | | - rng('default'); |
| 825 | + randn ('seed', 123); |
797 | 826 | Y = X * B + 0.2 * randn (m, 2); |
798 | 827 | S1 = bootridge (Y, X, [], 100, 0.10, [], 1, 42); |
799 | 828 | S2 = bootridge (Y, X, [], 100, 0.10, [], 2, 42); |
| 829 | + assert (all (size (S1.Sigma_Y_hat) == [2, 2])); |
800 | 830 |
|
801 | 831 | % credint:test:1 |
802 | 832 | heights = [183, 192, 182, 183, 177, 185, 188, 188, 182, 185].'; |
|
0 commit comments