: 회귀 분석에서 독립변수들 사이에 강한 상관관계가 존재하는 문제
import statsmodels.api as sm
sel_col = [x for x in df1.columns if 'Mean' in x]
X = df1[sel_col]
Y = df1['Mean Radius']
X.drop(columns=['Mean Radius'], inplace=True)
X_const = sm.add_constant(X)
model = sm.OLS(Y, X_const)
res = model.fit()
res.summary()

▶ 다중공선성 발생
“The condition number is large, 2.73e+05. This might indicate that there are strong multicollinearity or other numerical problems.”
일관되지 않은 계수(sign flip)
암 데이터에서는 보통 커질수록 악성 가능성이 높아지고 radius도 커지는 경향인데, 일부 계수가 음수로 튀는 것은 설명변수 간 강한 상관관계로 인한 계수 불안정성을 의미
표준오차가 커짐
std err가 크면 → t값이 작아져 → 유의성이 떨어짐
from statsmodels.stats.outliers_influence import variance_inflation_factor
df_VIF = pd.DataFrame()
df_VIF['variable'] = X_const.columns
# VIF 계산
df_VIF['VIF'] = [variance_inflation_factor(X_const.values, i) for i in range(X_const.shape[1])]
df_VIF.sort_values(by = 'VIF', ascending = False)

# VIF가 10 이하인 변수를 선택
cond1 = df_VIF['VIF'] <= 10
sel_col2 = df_VIF.loc[cond1]['variable'].values
X = df1[sel_col2]
Y = df1['Mean Radius']
X_const = sm.add_constant(X)
model = sm.OLS(Y, X_const)
result = model.fit()
result.summary()
