如何获得线性回归的调整后R平方 [英] How to get Adjusted R Square for Linear Regression
问题描述
使用sklearn.metrics可以计算R平方。如何使用线性回归模型计算调整后的调整R平方?
Scikit-Learn的线性回归不返回调整后的R平方。但是,根据R平方,您可以根据公式计算调整后的R平方:
其中p是预测变量的数量(也称为特征或解释变量),n是数据点的数量。因此,如果您的数据位于一个名为train的数据框中,并且具有r平方, r2
,则公式为:
< pre class = lang-py prettyprint-override>
adj_r2 =(1-(1-r2)*((train.shape [0]-1)/
(train.shape [ 0]-train.shape [1]-1))))
train .shape [0]
是观测值的数量, train.shape [1]
是要素的数量。
StatsModels 库具有线性回归方法,该方法确实返回调整后的R平方(在许多其他指标中)。 此处是文档。
上面的公式来自此Stack Exchange答案,它说这是已知的如 Wherry Formula-1 。有许多不同的公式用于计算调整后的R平方,但这是R中使用的方法。有关调整后的R平方方法之间差异的更多信息,请参见链接到的答案或论文估计
R ^ 2回归中的收缩
Using sklearn.metrics I can compute R square.How I can compute Adjusted Adjusted R square using Linear Regression model?
Scikit-Learn's Linear Regression does not return the adjusted R squared. However, from the R -squared you can calculate the adjusted R squared from the formula:
Where p is the number of predictors (also known as features or explanatory variables) and n is the number of data points. So if your data is in a dataframe called train and you have r-squared, r2
, the formula would be:
adj_r2 = (1 - (1 - r2) * ((train.shape[0] - 1) /
(train.shape[0] - train.shape[1] - 1)))
train.shape[0]
is the number of observations, and train.shape[1]
is the number of features.
The StatsModels library has a linear regression method that does return the adjusted R squared (among many other metrics). Here is the documentation.
The formula above is from this Stack Exchange Answer which says this is known as Wherry Formula - 1. There are a number of different formulas used to calculate adjusted R squared, but this is the method used in R. For more on the differences between adjusted R squared methods, see the answer linked to or the paper Estimating R^2 Shrinkage in Regression
这篇关于如何获得线性回归的调整后R平方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!