How to get Adjusted R Square for Linear Regression

て烟熏妆下的殇ゞ 提交于 2019-12-10 23:36:23

问题


Using sklearn.metrics I can compute R square.How I can compute Adjusted Adjusted R square using Linear Regression model?


回答1:


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 the r2, the formula would be:

adj_r2 = 1 - (1 - r2 ** 2) * ((train.shape[1] - 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



来源:https://stackoverflow.com/questions/51023806/how-to-get-adjusted-r-square-for-linear-regression

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!