countsig.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. function[H,P,M1,M2,N1,N2] = countsig(data1,data2,T1,T2,parametric,p,quiet)
  2. % Give the program two spike data sets and one
  3. % or two time intervals and it will decide if
  4. % the counts are significantly different.
  5. % this is either with a non-parametric method
  6. % or with a sqrt transformation followed by a
  7. % t-test
  8. % Usage: [H,P,M1,M2,N1,N2] = countsig(data1,data2,T1,T2,parametric,p,quiet)
  9. %
  10. % Input:
  11. % Note that all times have to be consistent. If data
  12. % is in seconds, so must be sig and t. If data is in
  13. % samples, so must sig and t. The default is seconds.
  14. %
  15. % data1 - structure array of spike times (required)
  16. % data2 - structure array of spike times (required)
  17. % T1 - time interval (default all)
  18. % T2 - time interval (default T1)
  19. % parametric - 0 = non-parametric (Wilcoxon)
  20. % - 1 = ttest on sqrt of counts
  21. % - 2 = Poisson assumption
  22. % (default = 0)
  23. % p - significance level (0.05)
  24. % quiet - 1 = no display 0 = display
  25. %
  26. % Output:
  27. %
  28. % H - 1 if different 0 if not
  29. % P - prob of result if same
  30. % M1 - mean count for data1
  31. % M2 - mean count for data2
  32. % N1 - counts for data1
  33. % N2 - counts for data2
  34. if nargin < 2;error('I need 2 sets of spike data');end
  35. data1=padNaN(data1); % create a zero padded data matrix from input structural array
  36. data2=padNaN(data2); % create a zero padded data matrix from input structural array
  37. data1=data1'; data2=data2'; % transpose data to get it into a form acceptable to Murray's routine
  38. if nargin < 3,
  39. T1 = [min(data1(:,1)) max(max(data1))];
  40. end
  41. if nargin < 4,
  42. T2 = T1;
  43. end
  44. if nargin < 5,
  45. parametric = 0;
  46. end
  47. if nargin < 6; p = 0.05;end
  48. if nargin < 7; quiet = 0; end
  49. if isempty(T1),
  50. T1 = [min(data1(:,1)) max(max(data1))];
  51. end
  52. if isempty(T2)
  53. T2 = T1;
  54. end
  55. if isempty(parametric),
  56. parametric = 0;
  57. end
  58. if isempty(p)
  59. p = 0.05;
  60. end
  61. if isempty(quiet),
  62. quiet = 0;
  63. end
  64. NT1 = length(data1(:,1));
  65. NT2 = length(data2(:,2));
  66. if (NT1 < 4 || NT2 < 4) && parametric ~= 2,
  67. disp('Low number of trials : switch to Poisson test')
  68. parametric = 2;
  69. end
  70. if abs((T1(2)-T1(1)) - (T1(2)-T1(1))) > 10.^-6
  71. error('Time intervals for analysis are different')
  72. end
  73. % get counts...
  74. N1=zeros(1,NT1);
  75. for n=1:NT1
  76. N1(n) = length(find(data1(n,:) >= T1(1) & ...
  77. data1(n,:) <= T1(2) & ~isnan(data1(n,:))));
  78. end
  79. N2=zeros(1,NT2);
  80. for n=1:NT2
  81. N2(n) = length(find(data2(n,:) >= T2(1) & ...
  82. data2(n,:) <= T2(2) & ~isnan(data1(n,:))));
  83. end
  84. M1 = mean(N1);
  85. M2 = mean(N2);
  86. % do non parametric test...
  87. if parametric == 0
  88. [P H] = ranksum(N1,N2,p);
  89. end
  90. % parametric test (with stabilizing transform)...
  91. % use sqrt transformation from
  92. % Cox and Lewis to make data more Gaussian
  93. % the statistical analysis of series of events pg 44
  94. if parametric == 1
  95. X = sqrt(N1 +0.25);
  96. Y = sqrt(N2 +0.25);
  97. [H,P] = ttest2(X,Y,p,0);
  98. end
  99. % Poisson test. Use method from Zar
  100. % pg 580 Ed 3. Z bahaves as a normal variate under
  101. % null of same process mean and Poisson processes
  102. if parametric == 2
  103. X = sum(N1);
  104. Y = sum(N2);
  105. Z = abs(X-Y)./sqrt(X+Y);
  106. P = 2*(1-normcdf(Z));
  107. if P < p; H = 1;else H = 0;end
  108. end
  109. if quiet == 0
  110. if H == 1
  111. disp('Counts are signifcantly different')
  112. else
  113. disp('Counts are not signifcantly different')
  114. end
  115. disp(['Mean count for data1 = ' num2str(M1)])
  116. disp(['Mean count for data2 = ' num2str(M2)])
  117. disp(['P value = ' num2str(P)])
  118. end