JupyterNotebook (JupyterHub)에서 아래 code를 실행하면서 통계 분석 과정을 이해합니다.
Libraroy import
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
import seaborn as sns
임의 변수 생성
x = np.random.rand(100)
y = np.random.rand(100)
z = x + (0.5 - np.random.rand(100)) * 0.5
변수 값 시각화
plt.style.use('_mpl-gallery')
fig, ax = plt.subplots()
ax.scatter(x, y, alpha=0.5, label='y')
ax.scatter(x, z, alpha=0.5, label='z')
ax.legend()
Pearson’s correlation test
rho, pv = stats.pearsonr(x, y)
print('y', rho, pv)
rho, pv = stats.pearsonr(x, z)
print('z', rho, pv)
Normality tests
fig = plt.figure(figsize=(7, 3))
ax = fig.add_subplot(1,3,1)
ax2 = fig.add_subplot(1,3,2)
ax3 = fig.add_subplot(1,3,3)
sns.histplot(x, label='x', bins=20, ax=ax)
sns.histplot(y, label='y', bins=20, ax=ax2)
sns.histplot(z, label='y', bins=20, ax=ax3)
fig.tight_layout()
Normality tests – QQ plot
fig = plt.figure(figsize=(7, 3))
ax = fig.add_subplot(1,3,1)
ax2 = fig.add_subplot(1,3,2)
ax3 = fig.add_subplot(1,3,3)
stats.probplot(x, dist=stats.norm, plot=ax)
stats.probplot(y, dist=stats.norm, plot=ax2)
stats.probplot(z, dist=stats.norm, plot=ax3)
Normality tests – Shapiro-Wilk test
# Shapiro-Wilk test
st, pv = stats.shapiro(x)
print('x', st, pv)
st, pv = stats.shapiro(y)
print('y', st, pv)
st, pv = stats.shapiro(z)
print('z', st, pv)
Spearman’s correlation test (for non-normally distributed variable)
rho, pv = stats.spearmanr(x, y)
print('y', rho, pv)
rho, pv = stats.spearmanr(x, z)
print('z', rho, pv)
