LoginSignup
1
1

More than 1 year has passed since last update.

scipy.stats.mannwhitneyuのデフォルト設定に注意

Last updated at Posted at 2022-03-11

結論

scipyでマンホイットニーU検定をする際は,alternative=‘two-sided’を省略すると結果表示が変わってくる可能性があるので,省略せずに明示した方が良い.

現時点での最新版(scipy 1.8.0)のデフォルト設定は,

# scipy version 1.8.0
# https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.mannwhitneyu.html
scipy.stats.mannwhitneyu(x, y, alternative="two-sided")

であるが,過去のバージョン(scipy 1.6.1)では

# scipy version 1.6.1
# https://docs.scipy.org/doc/scipy-1.6.1/reference/generated/scipy.stats.mannwhitneyu.html
scipy.stats.mannwhitneyu(x, y, alternative=None)

である.
None: computes p-value half the size of the ‘two-sided’ p-value and a different U statistic.
との説明があり,alternativeの記述を省略すると,同じコードでも環境により異なる計算結果が出てくる可能性がある.
(alternative=Noneは最新版だと非推奨.動作しない?)

気づいたきっかけ

stat_annotパッケージのadd_stat_annotationで計算されるp値と,scipy.stats.mannwhitneyuで計算したp値が違う現象に遭遇し,原因究明のためソースコードを確認した.

その結果,
・add_stat_annotationのtest='Mann-Whitney'の中身ではalternative=‘two-sided’と書かれている一方で,自分で計算したコードでは宣言が省略されていること,
・環境に入っていたScipyのバージョンが1.6.1であり,デフォルトの設定がalternative=Noneになること
が原因であることが分かった.

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1