-
Notifications
You must be signed in to change notification settings - Fork 0
/
IV_Theory.R
162 lines (152 loc) · 4.22 KB
/
IV_Theory.R
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
### Measurement Error in regressors fixed by using Instrumental Variables
# generate variables for simulation
x = rnorm(10000,10,2)
epsilon = rnorm(10000, 0, 10^{1/2})
x_1 = c()
x_2 = c()
y = c()
for (i in 1:10000) {
x_1[i] = x[i] + rnorm(1,0,1) # instrument
x_2[i] = x[i] + rnorm(1,0,2) # weaker instrument
y[i] = 3 + 1*x[i] + epsilon[i]
}
# function for regressions to easily extract important variables after every simulation
regression <- function(y,x){
beta_0 = c()
beta_1 = c()
se_beta_0 = c()
se_beta_1 = c()
for (j in 1:1000) {
normal = lm(y~x)
normal_s = summary(normal)
beta_0[j] = normal_s$coefficients[1, 1]
beta_1[j] = normal_s$coefficients[2, 1]
se_beta_0[j] = normal_s$coefficients[1, 2]
se_beta_1[j] = normal_s$coefficients[2, 2]
}
b_0 = round(mean(beta_0), 4)
b_1 = round(mean(beta_1), 4)
se_0 = round(mean(se_beta_0), 4)
se_1 = round(mean(se_beta_1), 4)
results = c(b_0,b_1,se_0, se_1)
return(results)
}
# regression results (a-d)
regression(y,x)
regression(y, x_1)
regression(y, x_2)
# Two staged least squares regression
SLS <- function(y,iv,iv2){
beta_0 = c()
beta_1 = c()
se_beta_0 = c()
se_beta_1 = c()
gamma_0 = c()
gamma_1 = c()
se_gamma_0 = c()
se_gamma_1 = c()
for (j in 1:1000) {
sls = lm(iv~iv2)
sls_s = summary(sls)
gamma_0[j] = sls_s$coefficients[1, 1]
gamma_1[j] = sls_s$coefficients[2, 1]
se_gamma_0[j] = sls_s$coefficients[1, 2]
se_gamma_1[j] = sls_s$coefficients[2, 2]
normal = lm(y~sls[["fitted.values"]])
normal_s = summary(normal)
beta_0[j] = normal_s$coefficients[1, 1]
beta_1[j] = normal_s$coefficients[2, 1]
se_beta_0[j] = normal_s$coefficients[1, 2]
se_beta_1[j] = normal_s$coefficients[2, 2]
}
b_0 = round(mean(beta_0), 4)
b_1 = round(mean(beta_1), 4)
se_0 = round(mean(se_beta_0), 4)
se_1 = round(mean(se_beta_1), 4)
g_0 = round(mean(gamma_0), 4)
g_1 = round(mean(gamma_1), 4)
se_g_0 = round(mean(se_gamma_0),4)
se_g_1 = round(mean(se_gamma_1),4)
results = c(b_0,b_1,se_0, se_1, g_0,g_1,se_g_0, se_g_1)
return(results)
}
# better function for two staged least squares to get more relevant statistics
SLSH <- function(){
beta_0 = c()
beta_1 = c()
se_beta_0 = c()
se_beta_1 = c()
gamma_0 = c()
gamma_1 = c()
se_gamma_0 = c()
se_gamma_1 = c()
for (j in 1:1000) {
x = rnorm(10000,10,2)
epsilon = rnorm(10000, 0, 10^{1/2})
x_1 = c()
x_2 = c()
y = c()
for (i in 1:10000) {
x_1[i] = x[i] + rnorm(1,0,1)
x_2[i] = x[i] + rnorm(1,0,2)
y[i] = 3 + 1*x[i] + epsilon[i]
}
sls = lm(x_2~x_1)
sls_s = summary(sls)
gamma_0[j] = sls_s$coefficients[1, 1]
gamma_1[j] = sls_s$coefficients[2, 1]
se_gamma_0[j] = sls_s$coefficients[1, 2]
se_gamma_1[j] = sls_s$coefficients[2, 2]
normal = lm(y~sls[["fitted.values"]])
normal_s = summary(normal)
beta_0[j] = normal_s$coefficients[1, 1]
beta_1[j] = normal_s$coefficients[2, 1]
se_beta_0[j] = normal_s$coefficients[1, 2]
se_beta_1[j] = normal_s$coefficients[2, 2]
}
b_0 = round(mean(beta_0), 4)
b_1 = round(mean(beta_1), 4)
se_0 = round(mean(se_beta_0), 4)
se_1 = round(mean(se_beta_1), 4)
g_0 = round(mean(gamma_0), 4)
g_1 = round(mean(gamma_1), 4)
se_g_0 = round(mean(se_gamma_0),4)
se_g_1 = round(mean(se_gamma_1),4)
e_se_b0 = round(sd(beta_0),4)
e_se_b1 = round(sd(beta_1),4)
empirical_se_b0 = sd(beta_0)
empirical_se_b1 = sd(beta_1)
results = c(b_0,b_1,se_0, se_1, g_0,g_1,se_g_0, se_g_1, e_se_b0, e_se_b1)
return(results)
}
SLSH
SLS(y, x_2,x_1)
library("ivreg")
# simulation!
beta_0 = c()
beta_1 = c()
se_beta_0 = c()
se_beta_0 = c()
for (k in 1:1000){
x = rnorm(10000,10,2)
epsilon = rnorm(10000, 0, 10^{1/2})
x_1 = c()
x_2 = c()
y = c()
for (i in 1:10000) {
x_1[i] = x[i] + rnorm(1,0,1)
x_2[i] = x[i] + rnorm(1,0,2)
y[i] = 3 + 1*x[i] + epsilon[i]
}
iv = ivreg(y~x|x+ x_1+x_2)
ivs = summary(iv)
beta_0[k] = iv[["coefficients"]][["(Intercept)"]]
beta_1[k] = iv[["coefficients"]][["x"]]
se_beta_0[k] = ivs[["coefficients"]][1,2]
se_beta_1[k] = ivs[["coefficients"]][2,2]
}
# results for using x_1 vs x_2
avg_se_beta_0 = mean(se_beta_0)
avg_se_beta_1 = mean(se_beta_1)
e_se_beta_0 = sd(beta_0)
e_se_beta_1 = sd(beta_1)