谈到拟合优度的衡量 - R-Squared 似乎是“简单”线性模型常用(并且被接受)的一种度量方式。
但是在 statsmodels
(以及其他统计软件)中,RLM 并没有包括 R-squared 与回归结果。
是否有一种“手动”计算的方法,可以类似于 Stata 中的方法?
或者是否有另一种度量方法,可以从 sm.RLS
生成的结果中使用 / 计算?
这就是 Statsmodels 产生的结果:
import numpy as np
import statsmodels.api as sm
# Sample Data with outliers
nsample = 50
x = np.linspace(0, 20, nsample)
x = sm.add_constant(x)
sig = 0.3
beta = [5, 0.5]
y_true = np.dot(x, beta)
y = y_true + sig * 1. * np.random.normal(size=nsample)
y[[39,41,43,45,48]] -= 5 # add some outliers (10% of nsample)
# Regression with Robust Linear Model
res = sm.RLM(y, x).fit()
print(res.summary())
输出结果为:
Robust linear Model Regression Results
==============================================================================
Dep. Variable: y No. Observations: 50
Model: RLM Df Residuals: 48
Method: IRLS Df Model: 1
Norm: HuberT
Scale Est.: mad
Cov Type: H1
Date: Mo, 27 Jul 2015
Time: 10:00:00
No. Iterations: 17
==============================================================================
coef std err z P>|z| [95.0% Conf. Int.]
------------------------------------------------------------------------------
const 5.0254 0.091 55.017 0.000 4.846 5.204
x1 0.4845 0.008 61.555 0.000 0.469 0.500
==============================================================================
wls_results = WLS(mod.endog, mod.exog, weights=mod.weights).fit()
,其中mod
是拟合后的RLM模型。但无法保证其准确性。WLS结果的R平方值具有加权残差的R平方值,这将是降低异常值影响的度量标准。但是,如果它们的权重不同,我认为您不能通过R平方比较模型。 - Josefmod = sm.RLS(y, x); r2_wls = sm.WLS(mod.endog, mod.exog, weights=mod.fit().weights).fit().rsquared
帮了我,使得 R2=0.948。与OLS
的 R2=0.731 相比,看起来有点“过于完美了” :-) - Primer.scale
属性进行访问的。然而,我找不到任何关于如何解释这个参数以及它实际上意味着什么的说明。在搜索过程中,我发现了一些可能感兴趣的论文:R平方的鲁棒版本,鲁棒AIC。 - Primer