-
Notifications
You must be signed in to change notification settings - Fork 1
/
ss01 C+H simulation.R
232 lines (155 loc) · 6.34 KB
/
ss01 C+H simulation.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
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
## Simulation for testing the power of the c+h estimator
## The steps in this estimator are:
## 1. Grab child that moved nhood and record their age at move (ages)
## 2. For each age, regress their adult outcome on average outcome of their nhood (at childhood)
## 3. Get the coef of nhood in step 2
## 4. Find out if the coef in step 3 changes with age (e.g. decreases with age) -- possibly using anova or lm
## 4a. Step 4 is basically a statistical test on the regression outputs of 3 onto age
## This simulation simulates the null hypothesis that across age group the regression coef in 3 doesn't change
## Under this null nhood has no effects ever
## Four things determine statistical power: 1) number of age groups, 2) people per age group, 3) variance of nhood and 4) outcome y
## We assume age groups = 10 and variance of y (Whcih we fix to 1)
library(tidyverse)
library(broom)
# default nhood variance = 20% of unconditional variance (and has no effect)
## what is the real variance in nhood wages compared to the variance in wages (for an age group)
# Background --------------------------------------------------------------
## Default we use 1000 case over 9 - 18
## However Scotland has roughly 600,000 9 - 18 year olds (https://en.wikipedia.org/wiki/Demography_of_Scotland, 2011 census)
## In
## In 1991, assume 10% of people moved house that year (12% in 2011 census, https://www.ons.gov.uk/peoplepopulationandcommunity/populationandmigration/internationalmigration/articles/internalandinternationalmigrationfortheunitedkingdomintheyearpriortothe2011census/2014-11-25)
## since sls = 5% sample
## We get 600 * 0.1 * 0.05 = 3k sample at best
## however how do we record outcomes
# 1.1 Simulation with continuous var --------------------------------------
simCH <-
function(nSample = 1e3, varNhood = 0.1) {
ages <- sample.int(10, nSample, replace = T) + 8
## 10 age groups, at default nSample this means 100 people in each age group
yNhood <- rnorm(nSample, sd = sqrt(varNhood))
y <- rnorm(nSample)
## betas for each regression
simulation_df <-
data.frame(ages, yNhood, y)
## Get reg results by subset
## see this post
results <-
simulation_df %>%
group_by(ages) %>%
do(avgY = tidy(lm(y ~ yNhood, data = .))) %>%
unnest(avgY)
results <-
results %>%
filter(term == 'yNhood')
stat <- (lm(estimate ~ ages, results) %>% coef)[2]
return(stat)
}
# 1.2 simulation with proportion (@50%) -----------------------------------
## defaulte = 10% sd in nhood proportions
simCH_prop <-
function(nSample = 1e3, varNhood = 0.1) {
ages <- sample.int(10, nSample, replace = T) + 8
## 10 age groups, at default nSample this means 100 people in each age group
yNhood <- rnorm(nSample, sd = sqrt(varNhood))
y <- sample.int(2,size = nSample, replace = T) - 1
## betas for each regression
simulation_df <-
data.frame(ages, yNhood, y)
## Get reg results by subset
## see this post
results <-
simulation_df %>%
group_by(ages) %>%
do(avgY = tidy(lm(y ~ yNhood, data = .))) %>%
unnest(avgY)
results <-
results %>%
filter(term == 'yNhood')
stat <- (lm(estimate ~ ages, results) %>% coef)[2]
return(stat)
}
# 1.3 Simulation using percentiles (unconditional) ------------------------
## E.g. percentiles based on national not sample or age group
## 1 = top 1%, 0.01 = bottom 1%,
simCH_perc <-
function(
nSample = 1e3,
varNhood = 1 ## ynHood = percentile rank now; 1-100 in the pop
) {
ages <- sample.int(10, nSample, replace = T) + 8
## 10 age groups, at default nSample this means 100 people in each age group
yNhood <- rnorm(nSample, sd = sqrt(varNhood)) * 10 + 50 # so basically centred at 50th percntile with sd = 10 at std. normal dist.
y <- sample.int(100, size = nSample, replace = T)
## betas for each regression
simulation_df <-
data.frame(ages, yNhood, y)
## Get reg results by subset
## see this post
results <-
simulation_df %>%
group_by(ages) %>%
do(avgY = tidy(lm(y ~ yNhood, data = .))) %>%
unnest(avgY)
results <-
results %>%
filter(term == 'yNhood')
stat <- (lm(estimate ~ ages, results) %>% coef)[2]
return(stat)
}
# 2. replicate it ---------------------------------------------------------
## Interpretation is change in outcome due to a 1SD change in yhood
## For continuous Y
simulatedStat <- replicate(1e3, simCH(nSample = 3e3)) %>% round(3)
simulatedStat %>% abs() %>% summary #
simulatedStat %>% abs() %>% quantile( c(0.95) )
## For proportion Y
simulatedStat_prop <- replicate(1e3, simCH_prop(nSample = 3e3)) %>% round(3)
simulatedStat_prop %>% abs() %>% summary #
simulatedStat_prop %>% abs() %>% quantile( c(0.95) )
## For percentiles
simulatedStat_perc <- replicate(1e3, simCH_perc(nSample = 3e3)) %>% round(3)
simulatedStat_perc %>% abs() %>% summary #
simulatedStat_perc %>% abs() %>% quantile( c(0.95) )
## chetty slope is like 0.044 for percentiles
# 3. Bootstrap SE for a single simulation ---------------------------------
simCH_data <-
function(nSample = 1e3, varNhood = 0.1) {
ages <- sample.int(10, nSample, replace = T) + 8
## 10 age groups, at default nSample this means 100 people in each age group
yNhood <- rnorm(nSample, sd = sqrt(varNhood))
y <- rnorm(nSample)
## betas for each regression
simulation_df <-
data.frame(ages, yNhood, y)
return(simulation_df)
}
simData <- simCH_data(3e3)
simData %>% summary
simData %>%
group_by(ages) %>%
summarise_all(
mean
)
bootStat <-
function(data, index){
## Get reg results by subset
## see this post
data <- data[index, ]
results <-
data %>%
group_by(ages) %>%
do(avgY = tidy(lm(y ~ yNhood, data = .))) %>%
unnest(avgY)
results <-
results %>%
filter(term == 'yNhood')
stat <- (lm(estimate ~ ages, results) %>% coef)[2]
return(stat)
}
simData[1:10,] %>% bootStat()
## The bootstrap
?boot::boot
bootResults <-
boot::boot(simData, bootStat, R = 1e3)
bootResults$t %>% abs() %>% round(3) %>% summary
bootResults$t %>% abs() %>% round(3) %>% quantile(0.95)