数据来自Regression Analysis by Example第12章的表12.1,一个破产概率的估计的例子。
检查了一下发现statsmodels的Logic中还没有诊断工具,但可以调用GLM,所以就实现了一下。
先加载上需要用到的包
import pandas as pd import numpy as np import statsmodels.api as sm import matplotlib.pyplot as plt
读取数据
dt = pd.read_csv('D://python/regression/P322.csv') print(dt)
作图
x = dt[['X1', 'X2', 'X3']] y = dt[['Y']] X1 = sm.add_constant(x) m1 = sm.GLM(y, X1, family=sm.families.Binomial(sm.families.links.logit())).fit() outliers = m1.get_influence(observed=False) DBETA = outliers.dfbetas DBETAi = [] for i in range(0, len(y)): DBETAi.append(np.maximum(np.sum(DBETA[i]), -np.sum(DBETA[i]))) index = [] for i in range(1, len(y)+1): index.append(i) plt.figure(figsize=(6, 5)) plt.scatter(index, DBETAi) plt.xlabel('Index', fontsize=20) plt.ylabel('$DBETA_i$', fontsize=20) n = np.array([9, 14, 52, 53]) plt.show()
DFBETA序列图就完美呈现了。