法一:采用factor_analyzer模块方法:
from factor_analyzer import factor_analyzer
# KMO值
print round(factor_analyzer.calculate_kmo(X_basic)[1],5)
# 巴特利特球形度值
print round(factor_analyzer.calculate_bartlett_sphericity(X_basic)[1], 5)
法二:自己实现代码检验:
def kmo():
# 相关系数
corr = X_basic.corr().values
# print corr
# 逆矩阵
corr_inv = np.linalg.inv(corr)
# 对角线取倒数,其他为0
S2 = np.diag(corr_inv)
S2 = np.linalg.inv(np.diag(S2))
# 反映像协方差矩阵
AIS = np.dot(S2, corr_inv)
AIS = np.dot(AIS, S2)
# 是映像协方差矩阵
IS = corr + AIS - 2 * S2
# 将矩阵AIS对角线上的元素开平方,并且将其余元素都变成0
Dai = np.diag(np.sqrt(np.diag(AIS)))
# IR是映像相关矩阵
IR = np.dot(np.linalg.inv(Dai), IS)
IR = np.dot(IR, np.linalg.inv(Dai))
# AIR是反映像相关矩阵
AIR = np.dot(np.linalg.inv(Dai), AIS)
AIR = np.dot(AIR, np.linalg.inv(Dai))
a = np.power((AIR - np.diag(np.diag(AIR))), 2)
a = np.sum(a, axis=1)
AA = np.sum(a)
b = corr - np.identity(15)
b = np.power(b, 2)
b = np.sum(b, axis=1)
BB = np.sum(b)
MSA = b / (b + a)
AIR = AIR - np.identity(15) + np.diag(MSA)
kmo = BB / (AA + BB)
return kmo
def bart():
corr = X_basic.corr().values
# 计算结果有问题
# bart=st.bartlett(*corr)
detCorr = np.linalg.det(corr)
n = len(X_basic)
p = len(X_basic.columns)
statistic = -math.log(detCorr) * (n - 1 - (2 * p + 5) / 6)
df = p * (p - 1) / 2
# 双侧概率
pval = (1.0 - st.chi2.cdf(statistic, df)) * 2
return statistic,pval