课程回放:(https://www.cctalk.com/v/15401191323766?xh_fshareuid=87b7f11f-6ceb-c339-6ae2-1632fe5f61ea&xh_preshareid=61f5865c-a0ff-470f-b32b-8464b20d9cd3)

双变量关系及建模

散点图-双变量关系的图形化表示

双变量关系的特征

相关性 correlation

接近完美相关(正)

强相关(正)

弱相关相关(正)

不相关

负相关

非线性

相关系数计算公式(标准化的协方差)

\[correlation = r(x, y)=\frac{Cov(x, y)}{\delta_x*\delta_y}\]

\[Cov(x, y) = \frac{\sum ^n _{i=1} (x_i-\bar{x})* (y_i-\bar{y})}{n}\]

\[\delta _x = \sqrt{\frac{\sum ^n _{i=1} (x_i-\bar{x})^2}{n}}\]

相关性的理解

尼古拉斯凯奇的电影与在游泳池溺亡的人数有关系吗?

摇滚乐与美国石油产量有关系吗?

高速路死亡率与鲜柠檬进口量有关系吗?

一般的模型表达式

\[应变量=f(解释变量)+噪音\]

线性回归模型表达式

\[应变量=截距+斜率*解释变量+噪音\]

线性回归模型–为什么这一条线最合适?

\[y_i= a + b*x_i+e\]

最小二乘法(least squares method )

简单地说,最小二乘的思想就是要使得

** 观测点和估计点的距离的平方和达到最小 **.

这里的“二乘”指的是用平方来度量观测点与估计点的远近(在古汉语中“平方”称为“二乘”),“最小”指的是参数的估计值要保证各个观测点与估计点的距离的平方和达到最小。

最小二乘法(least squares method )

线性回归模型:

\[y_i= a + b*x_i+e_i\]

\[e_i ~ N(0, \sigma)\]

预测值:

\[\hat{y_i} = \hat{a} + \hat{b}x_i\]

残差(error):

\[e_i = y_i-\hat{y_i}\]

拟合的过程:有n个样本量,也就是n个数据对 \((x_i,y_i)\), 找到 截距 \(a\) 和斜率 \(b\) ,使得残差平方和最小:

\[\sum_1^{n} e_i^2\]

** 关键概念 ** :

\(\hat{y_i}\) 是给定 \(x_i\) 值时, \(y\) 的期望值

\(\hat{b}\) 是真实但是未知的 \(b\) 的估计值

残差 \(e\):residual, error, noise

最小二乘法求线性回归系数

\[y_i= a + b*x_i+e_i\]

\[\hat{y_i} = \hat{a} + \hat{b}x_i\]

\[e_i = y_i-\hat{y_i}\]

\[\sum_1^{n} e_i^2 = \sum (y_i-\hat{y_i})^2=\sum (y_i- \hat{a} -\hat{b}x_i)^2\]

最小二乘法求线性回归系数:

\[f=\sum_1^{n} e_i^2 =\sum (y_i- \hat{a} -\hat{b}x_i)^2\]

一阶导数等于 0:

\[\frac{\partial f}{\partial\hat{a}} = -2 \sum (y_i- \hat{a} -\hat{b}x_i)=0\]

\[\frac{\partial f}{\partial\hat{b}} = -2 \sum (y_i- \hat{a} -\hat{b}x_i)x_i=0\]

联立上面的二元一次方程组,求解 \(\hat{a}\)\(\hat{b}\)

\[\hat{b}=\frac{\sum (x_i-\bar{x}) (y_i-\bar{y})}{\sum (x_i-\bar{x})^2}\]

\[\hat{a}= \bar{y} - \hat{b}\bar{x}\]

最小二乘法的特点:

\((\bar{x}, \bar{y})\)

out <- lm(mpg~wt,data=mtcars)
summary(out)
## 
## Call:
## lm(formula = mpg ~ wt, data = mtcars)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.5432 -2.3647 -0.1252  1.4096  6.8727 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  37.2851     1.8776  19.858  < 2e-16 ***
## wt           -5.3445     0.5591  -9.559 1.29e-10 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.046 on 30 degrees of freedom
## Multiple R-squared:  0.7528, Adjusted R-squared:  0.7446 
## F-statistic: 91.38 on 1 and 30 DF,  p-value: 1.294e-10

轶事:回归的由来

Regression to the mean is a concept attributed to Sir Francis Galton. The basic idea is that extreme random observations will tend to be less extreme upon a second trial. This is simply due to chance alone. Note that “regression to the mean” and “linear regression” are not the same thing.