Les erreurs standard des coefficients du modèle sont les racines carrées des entrées diagonales de la matrice de covariance. Considérer ce qui suit:
X = ⎡⎣⎢⎢⎢⎢⎢11⋮1x1,1x2,1⋮xn,1……⋱…x1,px2,p⋮xn,p⎤⎦⎥⎥⎥⎥⎥xi,jji
(REMARQUE: cela suppose un modèle avec une interception.)
- V = ⎡⎣⎢⎢⎢⎢⎢π^1(1−π^1)0⋮00π^2(1−π^2)⋮0……⋱…00⋮π^n(1−π^n)⎤⎦⎥⎥⎥⎥⎥π^ii
La matrice de covariance peut s'écrire:
(XTVX)−1
Cela peut être implémenté avec le code suivant:
import numpy as np
from sklearn import linear_model
# Initiate logistic regression object
logit = linear_model.LogisticRegression()
# Fit model. Let X_train = matrix of predictors, y_train = matrix of variable.
# NOTE: Do not include a column for the intercept when fitting the model.
resLogit = logit.fit(X_train, y_train)
# Calculate matrix of predicted class probabilities.
# Check resLogit.classes_ to make sure that sklearn ordered your classes as expected
predProbs = resLogit.predict_proba(X_train)
# Design matrix -- add column of 1's at the beginning of your X_train matrix
X_design = np.hstack([np.ones((X_train.shape[0], 1)), X_train])
# Initiate matrix of 0's, fill diagonal with each predicted observation's variance
V = np.diagflat(np.product(predProbs, axis=1))
# Covariance matrix
# Note that the @-operater does matrix multiplication in Python 3.5+, so if you're running
# Python 3.5+, you can replace the covLogit-line below with the more readable:
# covLogit = np.linalg.inv(X_design.T @ V @ X_design)
covLogit = np.linalg.inv(np.dot(np.dot(X_design.T, V), X_design))
print("Covariance matrix: ", covLogit)
# Standard errors
print("Standard errors: ", np.sqrt(np.diag(covLogit)))
# Wald statistic (coefficient / s.e.) ^ 2
logitParams = np.insert(resLogit.coef_, 0, resLogit.intercept_)
print("Wald statistics: ", (logitParams / np.sqrt(np.diag(covLogit))) ** 2)
Tout cela étant dit, ce statsmodels
sera probablement un meilleur package à utiliser si vous voulez accéder à BEAUCOUP de diagnostics "prêts à l'emploi".