有些講得太爛了,我來通俗的梳理一下R2.
在線性回歸的模型下,我們可以計算SE(line), SE(y均值)。
The statistic R2describes the proportion of variance in the response variable explained by the predictor variable
如何理解這句話,Y本身就有自己的SE,在模型下,Y與其預測值之間又有一個SE,如果模型完全擬合,那么SE(line)=0. 此時的R2就是1,也就是所有的方差都被該模型解釋了(可以想象成一種完全過擬合的模型。)
決定系數(coefficient ofdetermination),有的教材上翻譯為判定系數,也稱為擬合優度。
決定系數反應了y的波動有多少百分比能被x的波動所描述,即表征依變數Y的變異中有多少百分比,可由控制的自變數X來解釋.
決定系數的數值恰巧等於相關系數的平方。
表達式:R2=SSR/SST=1-SSE/SST
其中:SST=SSR+SSE,SST(total sum of squares)為總平方和,SSR(regression sum of squares)為回歸平方和,SSE(error sum of squares) 為殘差平方和。
數據的組間變異/總變異*100%,就是所謂的R-square.
組內變異(SSE)+組間變異(SSA)=總變異(SST),可以推出公式R squared=1-SSE/SST;具體組內變異和組間變異及總變異的計算估計你會的就不寫了。
回歸平方和:SSR(Sum of Squares forregression) = ESS (explained sum of squares)
殘差平方和:SSE(Sum of Squares for Error) = RSS(residual sum of squares)
總離差平方和:SST(Sum of Squares fortotal) = TSS(total sum of squares)
SSE+SSR=SST RSS+ESS=TSS
意義:擬合優度越大,自變量對因變量的解釋程度越高,自變量引起的變動占總變動的百分比高。觀察點在回歸直線附近越密集。
取值范圍:0-1.
舉例:
假設有10個點,如下圖:
我們R來實現如何求線性方程和R2:
# 線性回歸的方程 mylr = function(x,y){ plot(x,y) x_mean = mean(x) y_mean = mean(y) xy_mean = mean(x*y) xx_mean = mean(x*x) yy_mean = mean(y*y) m = (x_mean*y_mean - xy_mean)/(x_mean^2 - xx_mean) b = y_mean - m*x_mean f = m*x+b# 線性回歸方程 lines(x,f) sst = sum((y-y_mean)^2) sse = sum((y-f)^2) ssr = sum((f-y_mean)^2) result = c(m,b,sst,sse,ssr) names(result) = c('m','b','sst','sse','ssr') return(result) } x = c(60,34,12,34,71,28,96,34,42,37) y = c(301,169,47,178,365,126,491,157,202,184) f = mylr(x,y) f['m'] f['b'] f['sse']+f['ssr'] f['sst'] R2 = f['ssr']/f['sst']
最后方程為:f(x)=5.3x-15.5
R2為99.8,說明x對y的解釋程度非常高。
---------------------
作者:snowdroptulip
來源:CSDN
原文:https://blog.csdn.net/snowdroptulip/article/details/79022532
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!