1. ๋์ํ๋ณธ(์์ฒด) T ๊ฒ์ : ๋์ผํ ๊ฐ์ฒด์ ์ vs ํ ํ๊ท ๋น๊ต
import pandas as pd
import numpy as np
import scipy.stats as stats
from scipy.stats import shapiro
shapiro๋ฅผ ๋จผ์ ๋ถ๋ฌ์จ๋ค
# 1. ๊ฐ์ค์ค์
# H0 : ์ฝ์ ๋จน๊ธฐ์ ๊ณผ ๋จน์ ํ์ ํ์ ํ๊ท ์ ๊ฐ๋ค(ํจ๊ณผ๊ฐ ์๋ค)
# H1 : ์ฝ์ ๋จน๊ธฐ์ ๊ณผ ๋จน์ ํ์ ํ์ ํ๊ท ์ ๊ฐ์ง ์๋ค(ํจ๊ณผ๊ฐ ์๋ค)
# 2. ์ ์์์ค ํ์ธ : ์ ์์์ค 5%๋ก ํ์ธ
# 3. ์ ๊ท์ฑ ๊ฒ์ (์ฐจ์ด๊ฐ์ ๋ํด ์ ๊ท์ฑ ํ์ธ)
statistic, pvalue = stats.shapiro(df['after']-df['before'])
print(round(statistic,4), round(pvalue,4))
์ฌ๊ธฐ์ ์ฃผ์ํ ์ : ๋์ํ๋ณธ์ ์ ๊ท์ฑ ๊ฒ์ ํ ๋ stats.shapiro()์ ๊ฐ์ df['ํ']-df['์ ']์ ๋ฃ๋๋ค
# 4.1 (์ ๊ท์ฑO) ๋์ํ๋ณธ(์์ฒด) t๊ฒ์ (paired t-test)
statistic, pvalue = stats.ttest_rel(df['after'], df['before'], alternative='two-sided') # alternative='two-side
print(round(statistic,4), round(pvalue,4) )
๋ชจํ๊ท ๊ฒ์ - ๋ชจ์ง๋จ 2๊ฐ - ๋์ํ๋ณธ - ์ ๊ท์ฑ O - ttest
stats.ttest_rel() ํจ์ ์ฌ์ฉ
# 4.2 (์ ๊ท์ฑX) wilcoxon ๋ถํธ์์ ๊ฒ์
statistic, pvalue = stats.wilcoxon(df['after']-df['before'], alternative='two-sided')
print(round(statistic,4), round(pvalue,4) )
# alternative (๋๋ฆฝ๊ฐ์ค H1) ์ต์
: 'two-sided', 'greater', 'less'
๋ชจํ๊ท ๊ฒ์ - ๋ชจ์ง๋จ 2๊ฐ - ๋์ํ๋ณธ - ์ ๊ท์ฑ X - wilcoxon
stats.wilcoxn() ํจ์ ์ฌ์ฉ
df['ํ'] - df['์ ]) ๊ฐ์ ๋ฃ๋๋ค
2. ๋ ๋ฆฝํ๋ณธ T ๊ฒ์ : A์ง๋จ์ ํ๊ท vs B์ง๋จ์ ํ๊ท (๋์ผํ ์ง๋จ์ด ์๋)
# 1. ๊ฐ์ค์ค์
# H0 : A๊ทธ๋ฃน๊ณผ B๊ทธ๋ฃน์ ํ์ ํ๊ท ์ ๊ฐ๋ค. (A = B)
# H1 : A๊ทธ๋ฃน๊ณผ B๊ทธ๋ฃน์ ํ์ ํ๊ท ์ ๊ฐ์ง ์๋ค. (A ≠ B)
# 2. ์ ์์์ค ํ์ธ : ์ ์์์ค 5%๋ก ํ์ธ
# 3. ์ ๊ท์ฑ ๊ฒ์
# H0(๊ท๋ฌด๊ฐ์ค) : ์ ๊ท๋ถํฌ๋ฅผ ๋ฐ๋ฅธ๋ค.
# H1(๋๋ฆฝ๊ฐ์ค) : ์ ๊ท๋ถํฌ๋ฅผ ๋ฐ๋ฅด์ง ์๋๋ค.
statisticA, pvalueA = stats.shapiro(df['A'])
statisticB, pvalueB = stats.shapiro(df['B'])
print(round(statisticA,4), round(pvalueA,4))
print(round(statisticB,4), round(pvalueB,4))
๋์ํ๋ณธ์ df['ํ']-df['์ '] ๊ฐ ๋ฉ์ด๋ฆฌ๋ฅผ shaprio ํจ์์ ๋ฃ์ด์ ์ ๊ท์ฑ์ ๊ฒ์ ํ๋ค๋ฉด
๋ ๋ฆฝํ๋ณธ์ A,B ๊ฐ์ ๊ฐ๊ฐ shapiro ํจ์์ ๋ฃ์ด ์ ๊ท์ฑ์ ๊ตฌํจ
๋ง์ฝ ํ๋๋ผ๋ ์ ๊ท๋ถํฌ๋ฅผ ๋ฐ๋ฅด์ง ์๋๋ค๋ฉด ๋น๋ชจ์ ๊ฒ์ ๋ฐฉ๋ฒ(์์ฝ์จ)์ ์จ์ผ ํจ >> ๊ทผ๋ฐ ๋น๋ชจ์๋ ์ํ์ ์ถ์ ๋ ํ๋ฅ ์ด ์ ์
# 4. ๋ฑ๋ถ์ฐ์ฑ ๊ฒ์
# H0(๊ท๋ฌด๊ฐ์ค) : ๋ฑ๋ถ์ฐ ํ๋ค.
# H1(๋๋ฆฝ๊ฐ์ค) : ๋ฑ๋ถ์ฐ ํ์ง ์๋๋ค.
statistic, pvalue = stats.bartlett(df['A'], df['B'])
print(round(statistic,4), round(pvalue,4) )
๋ ๋ฆฝํ๋ณธ์ ์ ๊ท์ฑ ๊ฒ์ฌ ์ธ์ ๋ฑ๋ถ์ฐ์ฑ๋ ๊ฒ์ ํด์ผ ํ๋๋ฐ
stats.barlett(df['์นผ๋ผ'], df['์นผ๋ผ']) ํจ์๋ฅผ ์ฌ์ฉํ๋ค
# 5.1 (์ ๊ท์ฑO, ๋ฑ๋ถ์ฐ์ฑ O) t๊ฒ์
statistic, pvalue = stats.ttest_ind(df['A'], df['B'], equal_var=True, alternative='two-sided')
print(round(statistic,4), round(pvalue,4) )
๋ชจํ๊ท ๊ฒ์ - ๋ชจ์ง๋จ 2๊ฐ - ๋ ๋ฆฝํ๋ณธ - ์ ๊ท์ฑ O - ๋ฑ๋ถ์ฐ์ฑ O - ttest
stats.ttest_ind() ํจ์ ์ฌ์ฉ, equal_var = True
# 5.1 (์ ๊ท์ฑO, ๋ฑ๋ถ์ฐ์ฑ X) t๊ฒ์
statistic, pvalue = stats.ttest_ind(df['A'], df['B'], equal_var=False, alternative='two-sided')
print(round(statistic,4), round(pvalue,4) )
๋ชจํ๊ท ๊ฒ์ - ๋ชจ์ง๋จ 2๊ฐ - ๋ ๋ฆฝํ๋ณธ - ์ ๊ท์ฑ O - ๋ฑ๋ถ์ฐ์ฑ X - ttest
stats.ttest_ind() ํจ์ ์ฌ์ฉ, equal_var = False
# 5.2 (์ ๊ท์ฑX)์์ฝ์จ์ ์์ํฉ ๊ฒ์
statistic, pvalue = stats.ranksums(df['A'], df['B'], alternative='two-sided')
print(round(statistic,4), round(pvalue,4) )
๋ชจํ๊ท ๊ฒ์ - ๋ชจ์ง๋จ 2๊ฐ - ๋ ๋ฆฝํ๋ณธ - ์ ๊ท์ฑ X - ranksums
stats.ranksums() ํจ์ ์ฌ์ฉ
ํท๊ฐ๋ฆฌ๋๊น ํ๋ก ์ ๋ฆฌํด์ ์ธ์์ผ๊ฒ ์