오늘로 통계학 내용은 마무리가 되었다. 정말 어렵고 복잡한 내용이 많았지만 묘하게 재미가 있어서 이후에 강사님께서 추천해주신 책을 구매해서 통계학도 공부해보고 싶다.
aug = hds.stat.augment(model=model)
fitted : 추정값resid : 잔차hat : 레버리지sigma : 해당 관측값 제거한 후 표준편차 추정값cooksd : 쿡의 거리std_resid : 표준화 잔차
sigma가 작을수록,cooksd가 클수록 해당 관측값을 이상치로 판단

out_index = aug.loc[aug['cooksd'].gt(4 / X.shape[0])].index
plt.figure(figsize=(4, 4))
sns.regplot(
data=df, x='Age', y='Price', ci=None,
scatter_kws={'color': '0.8', 's': 10, 'ec': '0.8'},
line_kws={'color': 'red', 'lw': 1.5}
)
sns.scatterplot(
data=df.loc[out_index, :], x='Age', y='Price',
fc='red', ec='red', s=20, label='Outlier'
)
plt.legend()
plt.show()

X = X.drop(index=out_index)
y = y.drop(index=out_index)
model = hds.stat.ols(X=X, y=y)
model.summary()

stats.shapiro(model.resid)
# ShapiroResult(statistic=np.float64(0.9987108551069022), pvalue=np.float64(0.5564148139317799))
hds.stat.breushpagan(model=model)
# Statistic P-Value F-Value F P-Value
# 0 14.124654 0.049008 2.028417 0.048685
변수 ⬆️ → ⬆️ → MSE ⬇️
변수 ⬇️ → ⬇️ → MSE ⬆️
hds.stat.vif(model=model)
# Age KM HP MetColor Doors Weight Petrol
# 0 1.335567 1.609369 3.123682 1.015065 1.633648 4.762933 6.615611
X = X.drop(columns='Petrol')
model = hds.stat.ols(X=X, y=y)
hds.stat.vif(model=model)
# Age KM HP MetColor Doors Weight
# 0 1.333597 1.498871 1.13305 1.013634 1.279331 1.457494
model = hds.stat.stepwise(X=X, y=y, direction='both')
model.summary()

hds.stat.breushpagan(model=model)
# Statistic P-Value F-Value F P-Value
# 0 10.220546 0.069222 2.051491 0.069082
탐색적 데이터 분석 → 목표-입력변수 관계 확인 → 회귀계수 추정(가능도 방법 GLM)
→ 유의성 검정(모형, 계수) → 다중공선성 해결(분산팽창지수) → 오즈비 확인(모형 해석)
hds.plot.bar_freq(
data=df, x='admit', palette=['skyblue', 'orange']
)

hds.plot.box_group(
data=df, x='admit', y='gre',
palette=['skyblue', 'orange']
)

hds.plot.bar_dodge_freq(
data=df, x='rank', g='admit',
palette=['skyblue', 'orange']
)

hds.plot.bar_stack_freq(
data=df, x='rank', g='admit',
palette=['skyblue', 'orange']
)

hds.plot.bar_stack_prop(
data=df, x='rank', g='admit',
palette=['skyblue', 'orange']
)

pd.pivot_table(
data=df, index='admit', values='gre',
aggfunc=['count', 'mean', 'std'], margins=True
).round(2)
# count mean std
# gre gre gre
# admit
# Fail 1163 556.08 96.36
# Pass 524 614.75 88.92
# All 1687 574.30 97.92
pg.normality(data=df, dv='gre', group='admit')
# W pval normal
# admit
# Fail 0.990857 0.000001 False
# Pass 0.992274 0.008138 False
pg.homoscedasticity(data=df, dv='gre', group='admit')
# W pval equal_var
# levene 3.596208 0.058082 True
y1 = df.loc[df['admit'].eq('Fail'), 'gre']
y2 = df.loc[df['admit'].eq('Pass'), 'gre']
pg.ttest(x=y1, y=y2, correction=False)

pd.crosstab(index=df['rank'], columns=df['admit'], margins=True, normalize='index')
# admit Fail Pass
# rank
# 1 0.450549 0.549451
# 2 0.644366 0.355634
# 3 0.779630 0.220370
# 4 0.826797 0.173203
# All 0.689389 0.310611
pg.chi2_independence(data=df, x='rank', y='admit')[2]

df = pd.get_dummies(
data=df, columns=['rank', 'admit'],
prefix=['rank', None],
dtype=int, drop_first=True
)
# gre gpa rank_2 rank_3 rank_4 Pass
# 0 380.0 3.61 0 1 0 0
# 1 660.0 3.67 0 1 0 1
# 2 800.0 4.00 0 0 0 1
# 3 640.0 3.19 0 0 1 1
# 4 520.0 2.93 0 0 1 0
yvar = 'Pass'
X = df.drop(columns=yvar)
y = df[yvar].copy()
display(X)
display(y)

model = hds.stat.glm(X=X, y=y)
model.summary()

Deviance : 모형 검정(카이제곱)Pseudo R-squ : 유사 계수devGap = model.null_deviance - model.deviance
devGap
# np.float64(259.97760909804174)
dofGap = model.df_model
dofGap
# np.int64(5)
1 - stats.chi2.cdf(x=devGap, df=dofGap)
# np.float64(0.0)
회귀계수를 자연상수가 밑인 지수변환 → 오즈비 얻음
오즈비는 해당 변수가 1 증가할 때 합격 오즈가 몇배인지 나타낸 값
합격 오즈를 알면 확률을 얻을 수 있음
내일 ADsP 시험을 보고 와서 이번 통계 파트에 대해 쭉 정리를 해봐야 될 것 같다.