-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMasterCodeofCashmereProject_plm_robustness.Rmd
333 lines (256 loc) · 12.2 KB
/
MasterCodeofCashmereProject_plm_robustness.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
---
title: "CoefPlots of ParallelTrendsTest"
author: "Peng Sun"
date: "10/03/2022"
output: html_document
---
#Load packages
```{r load packages, message=FALSE, warning=FALSE}
#install.packages("clubSandwich")
library(readr)
library(did)
library(fixest)
library(DRDID)
library(tidyverse)
library(plm)
library(stargazer)
library(sandwich)
library(lmtest)
library(jtools)
library(clubSandwich)
```
#read data
```{r}
setwd("~/R")
df2012<-read_csv("~/R/df2012_20210920.csv")
```
```{r}
# set levels of timedummy variables so that Y2012 will be used as
names(df2012)
unique(df2012$timedummy)
```
```{r}
df2012$timedummy<-fct_relevel(df2012$timedummy,levels="Y2012","Y2013","Y2014","Y2015","Y2016","Y2017","janapr18","t1","t2")
#set levels of groups dummies so that Group C will be used as base
df2012$groups<-fct_relevel(df2012$groups, levels="C","A","B","D")
# set levels of period dummies so that t0 period will be used as base
df2012$periods<-fct_relevel(df2012$periods, levels="t0","t1","t2")
df2012<-df2012%>%select(-1)
```
```{r}
df2012<-df2012 %>%
mutate(timedummy2=case_when(year==2012~"Y2012",
year==2013~"Y2013",
year==2014~"Y2014",
year==2015~"Y2015",
year==2016~"Y2016",
year==2017~"Y2017",
janapr18==1~"Y2018janapr",
t1==1~"Yt1",
t2==1~"Yt2"))
df2012<- df2012%>%
mutate(timedummy3=as.factor(yearqrt))
class(df2012$timedummy3)
unique(df2012$timedummy3)
unique(df2012$timedummy2)
df2012<-df2012%>%
mutate(timedummy6= case_when(timedummy3=="2012 Q1"~ "Y2012Q1",
timedummy3=="2012 Q2"~ "Y2012Q2",
timedummy3=="2012 Q3"~ "Y2012Q3",
timedummy3=="2012 Q4"~ "Y2012Q4",
timedummy3=="2013 Q1"~ "Y2013Q1",
timedummy3=="2013 Q2"~ "Y2013Q2",
timedummy3=="2013 Q3"~ "Y2013Q3",
timedummy3=="2013 Q4"~ "Y2013Q4",
timedummy3=="2014 Q1"~ "Y2014Q1",
timedummy3=="2014 Q2"~ "Y2014Q2",
timedummy3=="2014 Q3"~ "Y2014Q3",
timedummy3=="2014 Q4"~ "Y2014Q4",
timedummy3=="2015 Q1"~ "Y2015Q1",
timedummy3=="2015 Q2"~ "Y2015Q2",
timedummy3=="2015 Q3"~ "Y2015Q3",
timedummy3=="2015 Q4"~ "Y2015Q4",
timedummy3=="2016 Q1"~ "Y2016Q1",
timedummy3=="2016 Q2"~ "Y2016Q2",
timedummy3=="2016 Q3"~ "Y2016Q3",
timedummy3=="2016 Q4"~ "Y2016Q4",
timedummy3=="2017 Q1"~ "Y2017Q1",
timedummy3=="2017 Q2"~ "Y2017Q2",
timedummy3=="2017 Q3"~ "Y2017Q3",
timedummy3=="2017 Q4"~ "Y2017Q4",
janapr18==1~"Y2018janapr",
t1==1~"Yt2",
t2==1~"Yt3"
))
unique(df2012$timedummy6)
df2012$timedummy6<-fct_relevel(df2012$timedummy6, levels="Y2012Q1","Y2012Q2","Y2012Q3","Y2012Q4","Y2013Q1","Y2013Q2","Y2013Q3","Y2013Q4","Y2014Q1","Y2014Q2","Y2014Q3","Y2014Q4","Y2015Q1","Y2015Q2","Y2015Q3","Y2015Q4","Y2016Q1","Y2016Q2","Y2016Q3","Y2016Q4","Y2017Q1","Y2017Q2","Y2017Q3","Y2017Q4","Y2018janapr","Yt2","Yt3")
```
```{r}
mean(df2012$land_area, na.rm="true")
```
```{r}
mean(df2012$floor_area)
# only keep three significant figures
```
```{r}
df2012_mutated <- df2012
df2012_mutated$contour=replace_na(df2012_mutated$contour,"Unreported")
df2012_mutated$deck=replace_na(df2012_mutated$deck,"Unreported")
df2012_mutated$roof_material=replace_na(df2012_mutated$roof_material,"Unreported")
df2012_mutated$wall_material=replace_na(df2012_mutated$wall_material,"Unreported")
df2012_mutated$building_age=replace_na(df2012_mutated$building_age,"Unreported")
df2012_mutated$land_area=replace_na(df2012_mutated$land_area,0.069)
df2012_mutated$floor_area=replace_na(df2012_mutated$floor_area,136)
# use a mean for land_area and floor_are
```
```{r}
#df2012_mutated$garages_mainroof=replace_na(df2012_mutated$garages_mainroof,"Unreported")
```
```{r}
#head(garage)
```
```{r}
garage=df2012_mutated$garages_mainroof
```
```{r}
# replace msising values with Unreported
garage[is.na(garage)]<-"Unreported"
```
```{r}
which(is.na(garage))
```
```{r}
df2012_mutated$garages_mainroof<-garage
```
#Get the same obs for the pooled cross section models(fix the 30 obs difference)
```{r}
bedrooms_min=df2012_mutated$bedrooms_min
# replace msising values with Unreported
bedrooms_min[is.na(bedrooms_min)]<-"Unreported"
#head(bedrooms_min)
df2012_mutated$bedrooms_min<- bedrooms_min
```
```{r}
#write.csv(df2012, "chapter1_df2012.csv")
```
```{r}
#write.csv(df2012_mutated, "chapter1_df2012mutated_bedrooms.csv")
```
# Replication of Table 6 results
## Replication of Table 6 results_column 1
```{r}
plm_1<-plm(lp~t1+t2+A+B+D+t1*A+t2*A+t1*B+t2*B+t1*D+t2*D,data=df2012, model="pooling", index=c("houseid","year") )
summary(plm_1)
```
```{r}
coefplot(plm_1,keep=c("t1:A","t2:A","t1:B","t2:B","t1:D","t2:D"), ylim=c(-0.4,0.4), axis.text.x = element_text(size=14))
```
## Replication of Table 6 results_Column 2 (same obs # with Col 1, 3 and 5)
```{r}
lm_housefeature2<-lm(lp~t1+t2+t1*A+t2*A+t1*B+t2*B+t1*D+t2*D+floor_area+land_area+bedrooms_min+as.factor(building_age)+ as.factor(deck)+ as.factor(wall_material)
+as.factor(roof_material)+as.factor(garages_mainroof)+ as.factor(contour)+as.factor(timedummy2) +as.factor(suburb),data=df2012_mutated)
summary(lm_housefeature2)
```
```{r}
coefplot(lm_housefeature2,keep=c("t1:A","t2:A","t1:B","t2:B","t1:D","t2:D"), ylim=c(-0.4,0.4), axis.text.x = element_text(size=14))
```
#Replication of Table 6 results_Column 3
```{r}
plm_year<-plm(lp~yr2012+yr2013+yr2014+yr2015+yr2016+yr2017+t1+t2+A+B+D+t1*A+t2*A+t1*B+t2*B+t1*D+t2*D,data=df2012, model="within",effect="individual", index=c("houseid","year"))
summary(plm_year)
```
```{r}
coefplot(plm_year,keep=c("t1:A","t2:A","t1:B","t2:B","t1:D","t2:D"), ylim=c(-0.4,0.4), axis.text.x = element_text(size=14))
```
```{r}
#plm_year_coef<-coef_test(plm_year, vcov="CR2", cluster=df2012$houseid, test="Satterthwaite")
```
# Replication of Table 6 results_Column 4 (same obs# with Col 1, 3,5)
```{r}
housefeature_pretrends<-plm(lp~yr2012+yr2013+yr2014+yr2015+yr2016+yr2017+yr2012*A+yr2013*A+yr2014*A+yr2015*A+yr2016*A+yr2017*A+yr2012*B+yr2013*B+yr2014*B+yr2015*B+yr2016*B+yr2017*B+yr2012*D+yr2013*D+yr2014*D+yr2015*D+yr2016*D+yr2017*D+t1+t2+A+B+D+t1*A+t2*A+t1*B+t2*B+t1*D+t2*D+floor_area+land_area+bedrooms_min+as.factor(building_age)+ as.factor(deck)+ as.factor(wall_material)
+as.factor(roof_material)+as.factor(garages_mainroof)+ as.factor(contour)+as.factor(suburb),data=df2012_mutated,model="pooling", index=c("houseid","year"))
summary(housefeature_pretrends)
```
#coef graph of Column 4
```{r}
coefplot(housefeature_pretrends,ylim=c(-0.4,0.4), keep=c("yr2012:A","yr2013:A","yr2014:A","yr2015:A","yr2016:A","yr2017:A","A:t1", "A:t2"), axis.text.x = element_text(size=14))
```
```{r}
coefplot(housefeature_pretrends,keep=c("yr2012:B","yr2013:B","yr2014:B","yr2015:B","yr2016:B","yr2017:B", "B:t1","B:t2"), axis.text.x = element_text(size=14))
```
```{r}
coefplot(housefeature_pretrends, keep=c("yr2012:D","yr2013:D","yr2014:D","yr2015:D","yr2016:D","yr2017:D", "D:t1", "D:t2"), axis.text.x = element_text(size=14))
```
## Replication of Table 6_Column 5, pretrends controlled, with 2018JanApr and C as baseline
```{r}
plm_year_house_parallel2018<-plm(lp~yr2012+yr2013+yr2014+yr2015+yr2016+yr2017+yr2012*A+yr2013*A+yr2014*A+yr2015*A+yr2016*A+yr2017*A+yr2012*B+yr2013*B+yr2014*B+yr2015*B+yr2016*B+yr2017*B+yr2012*D+yr2013*D+yr2014*D+yr2015*D+yr2016*D+yr2017*D+t1+t2+A+B+D+t1*A+t2*A+t1*B+t2*B+t1*D+t2*D,data=df2012, model="within",effect="individual", index=c("houseid","year"))
summary(plm_year_house_parallel2018)
```
#Coef graph of column 5
```{r}
coefplot(plm_year_house_parallel2018, ylim=c(-0.4,0.4),keep=c("yr2012:A","yr2013:A","yr2014:A","yr2015:A","yr2016:A","yr2017:A","janapr18:A"), axis.text.x = element_text(size=14))
```
# Robustness Check using Group D as the base group
## Robustness Check using Group D as the base group of Table 6 column 1
```{r}
plm_1D<-plm(lp~t1+t2+A+B+C+t1*A+t2*A+t1*B+t2*B+t1*D+t2*C,data=df2012, model="pooling", index=c("houseid","year") )
summary(plm_1D)
```
```{r}
coefplot(plm_1D,keep=c("t1:A","t2:A","t1:B","t2:B","t1:C","t2:C"), ylim=c(-0.4,0.4), axis.text.x = element_text(size=14))
```
## Robustness Check using Group D as the base group of Table 6 column2 (same obs # with Col 1, 3 and 5)
```{r}
lm_housefeature2D<-lm(lp~t1+t2+t1*A+t2*A+t1*B+t2*B+t1*C+t2*C+floor_area+land_area+bedrooms_min+as.factor(building_age)+ as.factor(deck)+ as.factor(wall_material)
+as.factor(roof_material)+as.factor(garages_mainroof)+ as.factor(contour)+as.factor(timedummy2) +as.factor(suburb),data=df2012_mutated)
summary(lm_housefeature2D)
```
```{r}
coefplot(lm_housefeature2D,keep=c("t1:A","t2:A","t1:B","t2:B","t1:D","t2:D"), ylim=c(-0.4,0.4), axis.text.x = element_text(size=14))
```
## Robustness Check using Group D as the base group of Table 6 column 3
```{r}
plm_yearD<-plm(lp~yr2012+yr2013+yr2014+yr2015+yr2016+yr2017+t1+t2+A+B+D+t1*A+t2*A+t1*B+t2*B+t1*C+t2*C,data=df2012, model="within",effect="individual", index=c("houseid","year"))
summary(plm_yearD)
```
```{r}
coefplot(plm_yearD,keep=c("t1:A","t2:A","t1:B","t2:B","t1:C","t2:C"), ylim=c(-0.4,0.4), axis.text.x = element_text(size=14))
```
## Robustness Check using Group D as the base group of Table 6 column4 (same obs# with Col 1, 3,5)
```{r}
housefeature_pretrendsD<-plm(lp~yr2012+yr2013+yr2014+yr2015+yr2016+yr2017+yr2012*A+yr2013*A+yr2014*A+yr2015*A+yr2016*A+yr2017*A+yr2012*B+yr2013*B+yr2014*B+yr2015*B+yr2016*B+yr2017*B+yr2012*C+yr2013*C+yr2014*C+yr2015*C+yr2016*C+yr2017*C+t1+t2+A+B+D+t1*A+t2*A+t1*B+t2*B+t1*C+t2*C+floor_area+land_area+bedrooms_min+as.factor(building_age)+ as.factor(deck)+ as.factor(wall_material)
+as.factor(roof_material)+as.factor(garages_mainroof)+ as.factor(contour)+as.factor(suburb),data=df2012_mutated,model="pooling", index=c("houseid","year"))
summary(housefeature_pretrendsD)
```
```{r}
coefplot(housefeature_pretrendsD,ylim=c(-0.4,0.4), keep=c("yr2012:A","yr2013:A","yr2014:A","yr2015:A","yr2016:A","yr2017:A","A:t1", "A:t2"), axis.text.x = element_text(size=14))
```
```{r}
coefplot(housefeature_pretrendsD,keep=c("yr2012:B","yr2013:B","yr2014:B","yr2015:B","yr2016:B","yr2017:B", "B:t1","B:t2"), axis.text.x = element_text(size=14))
```
```{r}
coefplot(housefeature_pretrendsD, keep=c("yr2012:C","yr2013:C","yr2014:C","yr2015:C","yr2016:C","yr2017:C", "C:t1", "C:t2"), axis.text.x = element_text(size=14))
```
## Robustness Check using Group D as the base group of Table 6 column 5, pretrends controlled, with 2018JanApr and C as baseline
```{r}
plm_year_house_parallel2018D<-plm(lp~yr2012+yr2013+yr2014+yr2015+yr2016+yr2017+yr2012*A+yr2013*A+yr2014*A+yr2015*A+yr2016*A+yr2017*A+yr2012*B+yr2013*B+yr2014*B+yr2015*B+yr2016*B+yr2017*B+yr2012*C+yr2013*C+yr2014*C+yr2015*C+yr2016*C+yr2017*C+t1+t2+A+B+D+t1*A+t2*A+t1*B+t2*B+t1*C+t2*C,data=df2012, model="within",effect="individual", index=c("houseid","year"))
summary(plm_year_house_parallel2018D)
```
```{r}
coefplot(plm_year_house_parallel2018, ylim=c(-0.4,0.4),keep=c("yr2012:A","yr2013:A","yr2014:A","yr2015:A","yr2016:A","yr2017:A","janapr18:A"), axis.text.x = element_text(size=14))
```
```{r}
coefplot(plm_year_house_parallel2018D,keep=c("yr2012:B","yr2013:B","yr2014:B","yr2015:B","yr2016:B","yr2017:B"), axis.text.x = element_text(size=14))
```
```{r}
coefplot(plm_year_house_parallel2018D, keep=c("yr2012:C","yr2013:C","yr2014:C","yr2015:C","yr2016:C","yr2017:C"), axis.text.x = element_text(size=14))
```
# Robustness check by dropping houses with less than two bedrooms
```{r}
df2012_2beds<- df2012 %>% filter(bedrooms_min<=1|bedrooms_min =="NA")
dim(df2012_2beds)
```
```{r}
df2012_2morebeds<- df2012 %>% filter(bedrooms_min>=1|bedrooms_min !="NA")
dim(df2012_2morebeds)
```