Marginal Effect 정리

Marginal Effect는 Independent Variable에 변화가 발생했을 때 Dependent Variable이 어떻게 변하는를 보여준다. 이 때 다른 Independent Variable은 모두 상수로 고정이 되어 있어야 한다는 것이다.  좀 더 간결한 정의는 다음과 같다.

Marginal effects are partial derivatives of the regression equation with respect to each variable in the model for each unit in the data.

쉽게 이야기 하면 회귀분석을 했을 때 계수를 상상하면 된다.  특정 Independent Variable의 계수는 해당 변수가 1단위(unit) 증가하거나, 감소했을 때 Dependent Variable에 미치는 영향을 설명해준다.

물론 Independent Variable가 Continuous Variable인 경우에는 Marginal Effect는 미분계수와 비슷한 의미로 변화율로 해석할 수 있지만, Discrete Variable은 1/0 형태의 이산 형태로 해석할 수가 있다.

대표적인 Marginal Effect는 1) Average Marginal Effect(AME), 2) Marginal Effect at the Mean (MEM), 3)Marginal effects at representative values (MER)가 있다. 그 외에도 Conditional Marginal Effects, Counterfactual Marginal Effects 등등이 있다.

AME는 이름이 의미하는 것처럼 관측치의 각 Independent Variable 별로 Marginal Effect를 계산한 이후에, 평균을 취하는 것이고,

MEM은 AEM에서 측정하고자 하는 Independent Variable 외에 다른 Independent Variable에 대해서 관측치 대신에, Independent Variable의 Mean이나 Mod값을 취하고 Marginal Effect를 계산한다.

마지막으로 MER은 MEM처럼 Mean이나 Mod값을 취하는 대신, 관심있는 값을 직접 선택한 이후 Marginal Effect를 계산한다.

예제는 다음과 같다.

import statsmodels.api as sm
import pandas as pd
import numpy as np
from sklearn.datasets import fetch_openml

X, y = fetch_openml("titanic", version=1, as_frame=True, return_X_y=True)

df = pd.concat([X.loc[:,['age','parch' ,'fare']],y],axis=1)
df = df.apply(pd.to_numeric)
print(df.shape)
df.head()

formula = "survived ~ age + parch + fare"
model = logit(formula, data = df).fit()

#https://www.statsmodels.org/dev/generated/statsmodels.discrete.discrete_model.DiscreteResults.get_margeff.html
AME = model.get_margeff(at = "overall", method = "dydx")
print(AME.summary())

Copy

References