如何在 Statsmodels 中获得稳健回归 (RLM) 的 R 平方? [英] How to get R-squared for robust regression (RLM) in Statsmodels?

查看:56
本文介绍了如何在 Statsmodels 中获得稳健回归 (RLM) 的 R 平方?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在衡量拟合优度时 - R-Squared 似乎是对简单"线性模型的普遍理解(并接受)的衡量标准.但是对于 statsmodels(以及其他统计软件)RLM 不包括 R 平方和回归结果.有没有办法手动"计算它,可能类似于 Stata?

When it comes to measuring goodness of fit - R-Squared seems to be a commonly understood (and accepted) measure for "simple" linear models. But in case of statsmodels (as well as other statistical software) RLM does not include R-squared together with regression results. Is there a way to get it calculated "manually", perhaps in a way similar to how it is done in Stata?

或者是否有另一种度量可以从 sm.RLS 产生的结果中使用/计算?

Or is there another measure that can be used / calculated from the results produced by sm.RLS?

这是 Statsmodels 正在生成的内容:

This is what Statsmodels is producing:

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
==============================================================================

推荐答案

为什么不使用model.predict来获取r2?例如:

Why not use model.predict to obtain the r2? For Example:

r2=1. - np.sum(np.abs(model.predict(X) - y) **2) / np.sum(np.abs(y - np.mean(y)) ** 2)

这篇关于如何在 Statsmodels 中获得稳健回归 (RLM) 的 R 平方?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆