9.2 回归方程的显著性检验 (edu-edu.com.cn)
容易发现: SST = SSR + SSE \textbf{SST}=\textbf{SSR}+\textbf{SSE} SST=SSR+SSE
F = SSR SSE / ( n − 2 ) ∼ F ( 1 , n − 2 ) \textbf{F}=\frac{\textbf{SSR}}{\textbf{SSE}/(n-2)} \sim F(1,n-2) F=SSE/(n−2)SSR∼F(1,n−2)
其中 n n n 是样本数量。
设有以下数据:
X | Y |
---|---|
1 | 2 |
2 | 3 |
3 | 5 |
4 | 7 |
首先需要计算出平均值:
x ˉ = 1 + 2 + 3 + 4 4 = 2.5 \bar{x} = \frac{1+2+3+4}{4} = 2.5 xˉ=41+2+3+4=2.5
y ˉ = 2 + 3 + 5 + 7 4 = 4.25 \bar{y} = \frac{2+3+5+7}{4} = 4.25 yˉ=42+3+5+7=4.25
接下来,计算回归方程的斜率:
b = ∑ i = 1 n ( x i − x ˉ ) ( y i − y ˉ ) ∑ i = 1 n ( x i − x ˉ ) 2 = ( 1 − 2.5 ) ∗ ( 2 − 4.25 ) + ( 2 − 2.5 ) ∗ ( 3 − 4.25 ) + ( 3 − 2.5 ) ∗ ( 5 − 4.25 ) + ( 4 − 2.5 ) ∗ ( 7 − 4.25 ) ( 1 − 2.5 ) 2 + ( 2 − 2.5 ) 2 + ( 3 − 2.5 ) 2 + ( 4 − 2.5 ) 2 = 2.25 b = \frac{\sum_{i=1}^n(x_i-\bar{x})(y_i-\bar{y})}{\sum_{i=1}^n(x_i-\bar{x})^2} = \frac{(1-2.5)*(2-4.25)+(2-2.5)*(3-4.25)+(3-2.5)*(5-4.25)+(4-2.5)*(7-4.25)}{(1-2.5)^2+(2-2.5)^2+(3-2.5)^2+(4-2.5)^2} = 2.25 b=∑i=1n(xi−xˉ)2∑i=1n(xi−xˉ)(yi−yˉ)=(1−2.5)2+(2−2.5)2+(3−2.5)2+(4−2.5)2(1−2.5)∗(2−4.25)+(2−2.5)∗(3−4.25)+(3−2.5)∗(5−4.25)+(4−2.5)∗(7−4.25)=2.25
回归方程的截距:
c = y ˉ − b x ˉ = 4.25 − 2.25 ∗ 2.5 = 3.5 c = \bar{y}-b\bar{x} = 4.25-2.25*2.5 = 3.5 c=yˉ−bxˉ=4.25−2.25∗2.5=3.5
因此回归方程为:
y = 2.25 x − 3.5 y = 2.25x - 3.5 y=2.25x−3.5
计算残差:
e 1 = 2 − ( 2.25 ∗ 1 − 3.5 ) = − 0.5 e_1 = 2 - (2.25*1 - 3.5) = -0.5 e1=2−(2.25∗1−3.5)=−0.5
e 2 = 3 − ( 2.25 ∗ 2 − 3.5 ) = 0.25 e_2 = 3 - (2.25*2 - 3.5) = 0.25 e2=3−(2.25∗2−3.5)=0.25
e 3 = 5 − ( 2.25 ∗ 3 − 3.5 ) = 1.75 e_3 = 5 - (2.25*3 - 3.5) = 1.75 e3=5−(2.25∗3−3.5)=1.75
e 4 = 7 − ( 2.25 ∗ 4 − 3.5 ) = 3.5 e_4 = 7 - (2.25*4 -3.5) = 3.5 e4=7−(2.25∗4−3.5)=3.5
计算总平方和:
S S T = ∑ i = 1 n ( y i − y ˉ ) 2 = ∑ i = 1 n ( y i − 4.25 ) 2 = 16.5 SS_T = \sum_{i=1}^n(y_i-\bar{y})^2 = \sum_{i=1}^n(y_i-4.25)^2 = 16.5 SST=∑i=1n(yi−yˉ)2=∑i=1n(yi−4.25)2=16.5
计算回归平方和:
S S R = ∑ i = 1 n ( y i ^ − y ˉ ) 2 = ∑ i = 1 n ( b x i + c − y ˉ ) 2 = ∑ i = 1 n ( 2.25 x i − 3.5 − 4.25 ) 2 = 14.0625 SS_R = \sum_{i=1}^n(\hat{y_i}-\bar{y})^2 = \sum_{i=1}^n(bx_i+c-\bar{y})^2 = \sum_{i=1}^n(2.25x_i - 3.5 - 4.25)^2 = 14.0625 SSR=∑i=1n(yi^−yˉ)2=∑i=1n(bxi+c−yˉ)2=∑i=1n(2.25xi−3.5−4.25)2=14.0625
计算剩余平方和:
S S E = ∑ i = 1 n ( y i − y i ^ ) 2 = ∑ i = 1 n ( y i − ( 2.25 x i − 3.5 ) ) 2 = 2.4375 SS_E = \sum_{i=1}^n(y_i-\hat{y_i})^2 = \sum_{i=1}^n(y_i-(2.25x_i-3.5))^2 = 2.4375 SSE=∑i=1n(yi−yi^)2=∑i=1n(yi−(2.25xi−3.5))2=2.4375
最终,可得到回归平方和与总平方和之比:
R 2 = S S R S S T = 14.0625 16.5 = 0.853 R^2 = \frac{SS_R}{SS_T} = \frac{14.0625}{16.5} = 0.853 R2=SSTSSR=16.514.0625=0.853
这个结果表明,通过回归方程可以解释数据的85.3%方差。
Python代码:
x_list = [2, 3, 4, 5, 6, 8, 10, 12, 14, 16]
y_list = [15, 20, 25, 30, 35, 45, 60, 80, 80, 110]
y_pre = []
n = len(x_list)
a = sum(y_list)/n
def func(x):
return -1.4288+6.4286*x
for i in range(n):
y_pre.append(func(x_list[i]))
sst = 0
for y in y_list:
sst += (y-a)**2
print(sst)
ssr = 0
for y in y_pre:
ssr += (y-a)**2
print(ssr)
sse = sst - ssr
print(sse)
f = ssr/(sse / (n-2))
print(f)