CompareSlopes - Perform linear regression y = ax on two groups and compare slopes. USAGE % To compare two slopes: [p,t,df,slope1,slope2,sem1,sem2] = CompareSlopes(x1,y1,x2,y2) % To compare a slope to a theoretical value: [p,t,df,slope,sem] = CompareSlopes(x,y,s) x1,y1 data for group 1 x2,y2 data for group 2 s expected (theoretical) slope OUTPUT p test probability (e.g. p<0.05 => significant) t t-test statistics df degrees of freedom slope1,slope2 regression slopes
0001 function [p,t,df,slope1,slope2,sem1,sem2] = CompareSlopes(x1,y1,x2,y2) 0002 0003 %CompareSlopes - Perform linear regression y = ax on two groups and compare slopes. 0004 % 0005 % USAGE 0006 % 0007 % % To compare two slopes: 0008 % [p,t,df,slope1,slope2,sem1,sem2] = CompareSlopes(x1,y1,x2,y2) 0009 % 0010 % % To compare a slope to a theoretical value: 0011 % [p,t,df,slope,sem] = CompareSlopes(x,y,s) 0012 % 0013 % x1,y1 data for group 1 0014 % x2,y2 data for group 2 0015 % s expected (theoretical) slope 0016 % 0017 % OUTPUT 0018 % 0019 % p test probability (e.g. p<0.05 => significant) 0020 % t t-test statistics 0021 % df degrees of freedom 0022 % slope1,slope2 regression slopes 0023 0024 % Copyright (C) 2011-2013 by Michaƫl Zugaro 0025 % 0026 % This program is free software; you can redistribute it and/or modify 0027 % it under the terms of the GNU General Public License as published by 0028 % the Free Software Foundation; either version 3 of the License, or 0029 % (at your option) any later version. 0030 0031 if nargin < 3, 0032 error('Incorrect number of parameters (type ''help <a href="matlab:help CompareSlopes">CompareSlopes</a>'' for details).'); 0033 end 0034 0035 if nargin == 3, 0036 0037 % One sample against theoretical value 0038 0039 % Compute linear regression 0040 s1 = regstats(y1(:),x1(:),1,'tstat'); 0041 0042 % Get N, slope and SEM 0043 n1 = length(x1); 0044 slope1 = s1.tstat.beta; 0045 sem1 = s1.tstat.se; 0046 0047 % T-test 0048 df = n1-2; 0049 s = x2; 0050 t = (slope1-s)/sqrt(n1*sem1^2); 0051 p = 2*tcdf(-abs(t),df); % (two-tailed test) 0052 0053 % Return sem 0054 slope2 = sem1; 0055 0056 else 0057 0058 % Two samples 0059 0060 % Compute linear regressions 0061 s1 = regstats(y1(:),x1(:),1,'tstat'); 0062 s2 = regstats(y2(:),x2(:),1,'tstat'); 0063 0064 % Get N, slopes and SEM 0065 n1 = length(x1); 0066 n2 = length(x2); 0067 slope1 = s1.tstat.beta; 0068 slope2 = s2.tstat.beta; 0069 sem1 = s1.tstat.se; 0070 sem2 = s2.tstat.se; 0071 0072 % T-test 0073 df = n1+n2-4; 0074 t = (slope1-slope2)/sqrt(n1*sem1^2+n2*sem2^2); 0075 p = 2*tcdf(-abs(t),df); % (two-tailed test) 0076 0077 end 0078 0079 0080 0081 0082 0083 % x = [x1(:);x2(:)]; 0084 % y = [y1(:);y2(:)]; 0085 % 0086 % group = [zeros(length(x1),1);ones(length(x2),1)]; 0087 % 0088 % X = [group x x.*group]; 0089 % 0090 % s = regstats(y,X); 0091 % 0092 % r = [s.tstat.beta s.tstat.se s.tstat.t s.tstat.pval]; 0093 0094 0095 0096 0097 % [slope1,u1,u2,u3,stats1] = regress(y1(:),x1(:)); 0098 % [slope2,u1,u2,u3,stats2] = regress(y2(:),x2(:)); 0099 % 0100 % n1 = length(x1); 0101 % sse1 = stats1(end)/n1; 0102 % n2 = length(x2); 0103 % sse2 = stats2(end)/n2; 0104 % 0105 % t = (slope1-slope2)/sqrt(sse1+sse2); 0106 % p = 2*tcdf(-abs(t),n1+n2-4); 0107 % 0108 % r = [slope1 slope2 t p]; 0109 0110 0111 0112 % x = [x1(:);x2(:)]; 0113 % y = [y1(:);y2(:)]; 0114 % 0115 % group = [zeros(length(x1),1);ones(length(x2),1)]; 0116 % 0117 % X = [group x x.*group]; 0118 % 0119 % [slope2,u1,u2,u3,stats2] = regress(y,X); 0120 % slope2, stats2