0todd0000 / spm1d

One-Dimensional Statistical Parametric Mapping in Python
GNU General Public License v3.0
60 stars 21 forks source link

Error in nonparam ttest_paired #262

Closed mrrezaie closed 10 months ago

mrrezaie commented 11 months ago

Hi, sorry for this post. I have 2 conditions (J=20, Q=101) and intend to use ttest_paired. The data are angle, moment, and power. First, I checked the normality using spm1d.stats.normality.ttest_paired(Con1,Con2).inference(0.05) and in case of h0 rejection, I used non parametric test.

I found that there are two nonparametric versions of ttest_paired: spm1d.stats.nonparam.ttest_paired and spm1d.stats.nonparam.stats.ttest_paired and both methods seem identical. In any case and force_iterations=True, I get this error:

ValueError: setting an array element with a sequence. 
The requested array has an inhomogeneous shape after 1 dimensions. 
The detected shape was (1048576,) + inhomogeneous part.

This is my implementation:

spm1d.stats.nonparam.stats.ttest_paired(Con1,Con2).inference(
     alpha=0.05, two_tailed=True, force_iterations=True)

By default, iterations=-1 which I presume there will be no limitation in the number of iterations and the spm1d version is 0.4.19. Any idea what the problem is? I'm looking forward to hearing from you and any help is greatly appreciated.

Regards, Mohammadreza

0todd0000 commented 11 months ago

Thank you for reporting the version number and also for a very clear description of the problem!



I found that there are two nonparametric versions of ttest_paired: spm1d.stats.nonparam.ttest_paired and spm1d.stats.nonparam.stats.ttest_paired and both methods seem identical.

This is correct. spm1d.stats.nonparam.ttest_paired just points to the deeper ttest_paired function and doesn't itself contain any code. Using spm1d.stats.nonparam.ttest_paired is just the quickest way to access the function.



Regarding the ValueError:

import numpy as np
import spm1d

np.random.seed(0)
yA  = np.random.randn(20, 101)
yB  = np.random.randn(20, 101)
tn  = spm1d.stats.nonparam.ttest_paired(yA, yB)
tni = tn.inference(0.05, two_tailed=True, force_iterations=True)
print(tni)
tni = tn.inference(0.05, two_tailed=True, iterations=1000)
mrrezaie commented 11 months ago

Thanks a lot for your response. Your example works well in my system, but the test doesn't work with my data. The error also occurs with lower iteration value and no memory-hungry application is active. May I please ask you to test this sample? data.zip

yA = np.load('data.npz')['con1']
yB = np.load('data.npz')['con2']

Also, I have a question about whether or not use nonparametric test. Please see the graph below (the uploaded data): Figure_1 The normality test indicates that the data are not normally distributed in two small regions after 60% of gait, and the parametric paired t-test reveals a significant difference between the conditions before 60% of gait. Since there is no overlap between the regions of significance for the two tests, it seems unreasonable to apply a nonparametric test to the entire data set. In other words, the data are normally distributed in the frames where the parametric test detects a significant difference. In this case, reporting the result of the parametric test doesn't seem incorrect. I would appreciate your feedback on this.

mrrezaie commented 11 months ago

Hi, I think I found the issue, please see my pull request https://github.com/0todd0000/spm1d/pull/263. I'm no longer getting the error.

mrrezaie commented 10 months ago

Hi, I found your thorough response in another post, on the use of parametric and nonparametric implementations: https://github.com/0todd0000/spm1d/issues/173#issuecomment-848716020 Thank you.