-
Notifications
You must be signed in to change notification settings - Fork 2
/
ols.Rmd
273 lines (202 loc) · 6.28 KB
/
ols.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
---
title: "Learning Science Bootcamp"
subtitle: "Ordinary Least Square"
author: "冯俊晨"
date: "2018/09/11"
output:
xaringan::moon_reader:
css: [default, zh-CN.css]
nature:
highlightStyle: github
highlightLines: true
countIncrementalSlides: false
---
```{r setup, include=FALSE}
options(htmltools.dir.version = FALSE)
library(readr)
library(dplyr)
library(car)
library(ggplot2)
library(stargazer)
chfs = read_csv("../../data/ret2edu.csv")
chfs$xueli = 0
chfs$xueli[chfs$degree=='primary'] = 1
chfs$xueli[chfs$degree=='secondary'] = 2
chfs$xueli[chfs$degree=='high'|chfs$degree=='tech'] = 3
chfs$xueli[chfs$degree=='associate'|chfs$degree=='bachelor'] = 4
chfs$xueli[chfs$degree=='master'|chfs$degree=='phd'] = 5
chfs$xueli = factor(chfs$xueli, labels=c('noschool','primary','secondary','highschool','college','master+'))
chfs$exp = chfs$age - chfs$edu
exp_result = read_delim("../../data/exp_result.csv", delim='\t')
exp_result$duration = as.numeric(exp_result$duration)
exp_result = exp_result %>% filter(duration<=360000) %>% filter(duration>=1000) %>% filter(!is.na(duration))
exp_result = exp_result %>% transform(course_id = factor(course_id)) %>% transform(posttest=factor(posttest))
```
---
# OLS的基本公式
$$\Large Y=\beta X+\epsilon$$
其中 $Y$ 表示**自变量**, $X$表示**因变量**(矩阵),这都是常量
$\epsilon$ 是**随机干扰**(有时又被称为残差项),这是个随机变量
---
class: middle
# 教育回报
.pull-left[
模型(或者叫数据生成过程)
$$\Large Income=\beta Edu+\epsilon$$
]
.pull-right[
估计结果
$$\Large Income=\hat{\beta} Edu$$
注意到
- $\beta$上的帽子表示估计值
- $\epsilon$ "消失"了
- 事实上 $\hat{\beta}$ 是 $\epsilon$ 的函数,因此它是个随机变量
]
---
# 教育回报
.pull-left[
数据来源:中国家庭金融调研(2013),有收入的成年人,未经调权
```{r, echo=FALSE}
basic_model = lm(data=chfs, income~edu)
stargazer(basic_model, type='text', single.row = TRUE)
```
]
.pull-right[
- 每增加一年教育,工资涨3520
- 模型解释了收入数据8.4%的变化
- 参数估计的标准误是76.053
]
---
# log转化
.pull-left[
$$\Large log(Income)=\beta Edu+\epsilon$$
```{r, echo=FALSE}
basic_log_model = lm(data=chfs, log(income)~edu)
stargazer(basic_log_model, type='text', single.row = TRUE)
```
]
.pull-right[
- 每增加一年教育,工资涨8%
]
---
# 离散变量
```{r, echo=FALSE}
discrete_1 = lm(data=chfs, log(income)~xueli)
stargazer(discrete_1, type='text', single.row = TRUE)
```
---
# 离散变量
和没上过学的相比
- 上过小学的**多**赚24.7%
- 上过初中的**多**赚40.7%
综上推论:和上过小学的相比,上过初中的大约多赚16%
**思考**:之前教育年限的线性假设合理么?
---
# 遗漏变量
收入不仅和受教育年限有关,跟工作经验也有关。定义工作经验为年龄-受教育年限
$$\Large log(Income)=\beta_1 Edu+\beta_2 Exp+\epsilon$$
```{r,echo=FALSE}
stargazer(lm(data=chfs, log(income)~edu+exp), type='text', single.row = TRUE)
```
---
# 线性模型中的非线性元素
$$\Large log(Income)=\beta_1 Edu+\beta_2 Exp+\beta_3 Exp^2+\epsilon$$
**思考** 经验对工资的影响是什么?
```{r,echo=FALSE}
chfs = chfs %>% mutate(exp2=exp^2)
mincer_model = lm(data=chfs, log(income)~edu+exp+exp2)
stargazer(mincer_model, type='text', single.row = TRUE)
```
---
# 比较模型
- 变量的不同阶次(exp的平方项)
- 变量的不同形式(edu VS xueli)
```{r, echo=FALSE}
discrete_2 = lm(data=chfs, log(income)~xueli+exp+exp2)
stargazer(basic_log_model, mincer_model, discrete_2, type='text',
keep.stat = c("adj.rsq"), single.row = TRUE,
dep.var.labels.include = FALSE,
model.numbers = FALSE)
```
---
# 上述模型还有什么问题?
- 遗漏变量:其他影响工资的因素
- 模型形式:edu和exp是否线性可加?比如是否存在edu和exp的交互项
- (*)错误的标准误
- **内生性**
一种特殊的遗漏变量错误。残差项和自变量相关!
.pull-left[
真实模型
$$\Large Y = \alpha Edu + \gamma IQ + \eta$$
$$\Large Edu = \delta IQ + \xi$$
]
.pull-right[
回归模型
$$\Large Y = \beta Edu + \epsilon$$
]
---
# 为什么我们还要用OLS
All models are wrong, but some are useful. By George P Box
- (主要的)因果推断工具
- 大部分情况下,简单的OLS就能做到60分;不能超过OLS就可以洗洗睡了
- 业界和学界的通用语言:统计学方向和社会科学方向必修
我们这里介绍OLS的主要目的是用来做之前学过的很多统计检验
---
# OLS假设检验(1)
四个组:
- 长视频 (long video)
- 短视频 (short video)
- 轻交互 (interactive)
- 控制组 (control)
两道题:
- 类题 (similar)
- 简单变式 (transfer)
---
# OLS假设检验(2)
```{r, echo=FALSE}
similar_stat = lm(data=exp_result %>% filter(posttest=='similar'), master~course_id)
transfer_stat = lm(data=exp_result %>% filter(posttest=='transfer'), master~course_id)
stargazer(similar_stat, transfer_stat, type='text',
column.labels = c("similar", "transfer"),
model.numbers=F, dep.var.labels.include = FALSE,
report=("vcp*"))
```
---
# OLS中的假设检验
H0: 对于类题而言,短视频的教学效果和控制组一样
```{r}
linearHypothesis(similar_stat,
"course_idshort_video = 0")
```
---
H0:对于类题而言,短视频的教学效果比控制组高5%
```{r}
linearHypothesis(similar_stat,
"course_idshort_video = 0.05")
```
---
H0:对于类题而言,长视频的教学效果和轻交互一样好
```{r}
linearHypothesis(similar_stat,
"course_idlong_video = course_idinteractive")
```
---
H0:对于类题而言,长视频的教学效果比轻交互好5%
```{r}
linearHypothesis(similar_stat,
"course_idlong_video -course_idinteractive = 0.05")
```
---
H0:对于迁移而言,长视频的教学效果比轻交互好1%
```{r}
linearHypothesis(transfer_stat,
"course_idlong_video - course_idinteractive = 0.01")
```
---
H0: 交互手段和教学效果无关 (Pearson Test)
```{r}
linearHypothesis(transfer_stat,
c("course_idlong_video = 0",
"course_idinteractive =0",
"course_idshort_video=0"))
```