S E l i n e = ∑ i = 1 n y i 2 − 2 m x i y i − 2 b y i + m 2 x i 2 + 2 m b x i + b 2 SE_{line} = \sum_{i=1}^{n}y_{i}^{2}-2mx_{i}y_{i}-2by_{i}+m^{2}x_{i}^{2}+2mbx_{i}+b^{2} SEline=∑i=1nyi2−2mxiyi−2byi+m2xi2+2mbxi+b2
∵ ∑ y i 2 n = y 2 ‾ , ∑ x i y i n = x y ‾ , ∑ x i 2 n = x 2 ‾ , ∑ x i n = x ‾ , ∑ y i n = y ‾ \because \frac{\sum y_{i}^{2}} {n} = \overline{y^{2}},\frac{\sum x_{i}y_{i}} {n} = \overline{xy},\frac{\sum x_{i}^{2}} {n} = \overline{x^{2}},\frac{\sum x_{i}} {n} = \overline{x},\frac{\sum y_{i}} {n} = \overline{y} ∵n∑yi2=y2,n∑xiyi=xy,n∑xi2=x2,n∑xi=x,n∑yi=y
∴ S E l i n e = n y 2 ‾ − 2 m n x y ‾ + n m 2 x 2 ‾ − 2 b n y ˉ + 2 m n b x ˉ + n b 2 \therefore SE_{line} = n\overline{y^{2}}-2mn\overline{xy}+nm^{2}\overline{x^{2}}-2bn\bar{y}+2mnb\bar{x}+nb^{2} ∴SEline=ny2−2mnxy+nm2x2−2bnyˉ+2mnbxˉ+nb2
假设 x , y x,y x,y均已知,这条方程就可以看成是 m , n , S E l i n e m,n,SE_{line} m,n,SEline值构成的曲面,其中
S E l i n e SE_{line} SEline为纵轴,最小化平方误差就是找出曲面的最低点,即找出对应的 m m m和 n n n。则
问题可转化为 ∂ S E l i n e ∂ m = 0 \frac{\partial SE_{line}} {\partial m}=0 ∂m∂SEline=0且 ∂ S E l i n e ∂ b = 0 \frac{\partial SE_{line}} {\partial b}=0 ∂b∂SEline=0
∴ − 2 n x y ‾ + 2 m n x 2 ‾ + 2 b n x ˉ = 0 , − 2 n ˉ y + 2 m n x ˉ + 2 n b = 0 \therefore -2n\overline{xy}+2mn\overline{x^{2}}+2bn\bar{x}=0,-2n\bar{}y+2mn\bar{x}+2nb=0 ∴−2nxy+2mnx2+2bnxˉ=0,−2nˉy+2mnxˉ+2nb=0
∴ − x y ‾ + m x 2 ‾ + b x ˉ = 0 , − y ˉ + m x ˉ + b = 0 \therefore -\overline{xy}+m\overline{x^{2}}+b\bar{x}=0,-\bar{y}+m\bar{x}+b=0 ∴−xy+mx2+bxˉ=0,−yˉ+mxˉ+b=0
∴ m x 2 ‾ + b x ˉ = x y ‾ , m x ˉ + b = y ˉ \therefore m\overline{x^{2}}+b\bar{x}=\overline{xy},m\bar{x}+b=\bar{y} ∴mx2+bxˉ=xy,mxˉ+b=yˉ
从第二个等式可以看出点 ( x ˉ , y ˉ ) (\bar{x},\bar{y}) (xˉ,yˉ)必然存在于最优直线上
而由第一个等式可知 m x 2 ‾ x ˉ + b = x y ‾ x ˉ m\frac{\overline{x^{2}}} {\bar{x}}+b=\frac{\overline{xy}}{\bar{x}} mxˉx2+b=xˉxy,便可得到最优直线上第二个点 ( x 2 ‾ x ˉ , x y ‾ x ˉ ) (\frac{\overline{x^{2}}} {\bar{x}},\frac{\overline{xy}}{\bar{x}}) (xˉx2,xˉxy)
求解方程得: m = x ˉ y ˉ − x y ‾ ( x ˉ ) 2 x 2 ‾ , b = y ˉ − m x ˉ = x y ‾ x ˉ − x 2 ‾ y ˉ x ˉ 2 − x 2 ‾ m=\frac{\bar{x}\bar{y}-\overline{xy}}{(\bar{x})^{2}\overline{x^{2}}},b=\bar{y}-m\bar{x}=\frac{\overline{xy} \bar{x}-\overline{x^{2}}\bar{y}}{\bar{x}^{2}-\overline{x^{2}}} m=(xˉ)2x2xˉyˉ−xy,b=yˉ−mxˉ=xˉ2−x2xyxˉ−x2yˉ
Contingency Table | Herb 1 | Herb 2 | Placebo | Total |
---|---|---|---|---|
sick | 20 | 30 | 30 | 80 |
Expected | 25.3 | 29.4 | 25.3 | 21% |
not sick | 100 | 110 | 90 | 300 |
Expected | 94.7 | 110.6 | 94.7 | 79% |
Total | 120 | 140 | 120 | 380 |