-
Notifications
You must be signed in to change notification settings - Fork 0
/
_0f-merge-greenmark-condofacils.Rmd
285 lines (258 loc) · 11.8 KB
/
_0f-merge-greenmark-condofacils.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
---
title: "Merge Greenmark with Transactions"
output: #html_notebook
---
This notebook will merge the cleaned greenmark dataset with the private residential properties dataset.
```{r setup}
source("misc/load_libraries.R")
```
```{r load data}
transactions <- read_rds("data/intermediate/transactions_with_dist_mrt.rds")
greenmark <- read_rds("data/intermediate/greenmark_cleaned.RDS")
cpi <- read_csv("data/raw/consumer-price-index-base-year-2014-100-monthly.csv")
condo_facilities <- read_dta("data/raw/CondoFacility.dta")
```
```{r check for dupes in gm}
greenmark %>%
group_by(project_name, award, year_award) %>%
summarise(count = n()) %>%
group_by(project_name) %>%
summarise(count = n()) %>%
filter(count > 1)
```
```{r fix dupes in gm}
greenmark <- greenmark %>%
filter(!(project_name == "LUXUS HILLS" & year_award == "FY10" & year_award == "Green Mark Certified")) %>%
filter(!(project_name == "RIVIERA 38" & year_award == "FY11")) %>%
filter(!(project_name == "TERRENE AT BUKIT TIMAH" & year_award == "FY08"))
```
```{r merge on project name}
merged <- transactions %>% left_join(greenmark, on = "project_name")
merged
```
```{r get all postal codes associated with each project name}
with_awards <- merged %>%
filter(!is.na(award)) %>%
distinct(project_name, postal_code) %>%
filter(!is.na(postal_code))
with_awards
```
```{r expand greenmark dataset to include all postal codes from each project/condo}
greenmark_expanded <- with_awards %>%
left_join(greenmark, by = "project_name")
greenmark_expanded
```
```{r some basic cleaning neglected in the cleaning script}
greenmark_expanded <- greenmark_expanded %>%
mutate(year_award = as.integer(str_replace(year_award, "FY", "20")),
award = str_replace_all(award, "Green Mark ", "")) %>%
distinct(project_name, postal_code, year_award, award)
greenmark_expanded
```
```{r check for invalid year of award}
greenmark_expanded %>% distinct(year_award) %>% arrange(year_award)
```
```{r check for invalid award type}
greenmark_expanded %>% distinct(award)
```
```{r perform second merge}
second_merge <- transactions %>% left_join(greenmark_expanded, by = c("project_name", "postal_code"))
second_merge
```
```{r clean up data types}
final <- second_merge %>%
mutate(project_name = as.factor(project_name),
type_of_area = as.factor(type_of_area),
property_type = as.factor(property_type),
tenure = as.factor(str_replace(tenure, " From .*", "")),
type_of_sale = as.factor(type_of_sale),
hdb_buyer = as.integer(hdb_buyer == "HDB"),
planning_region = as.factor(planning_region),
planning_area = as.factor(planning_area),
award = as.factor(award)) %>%
select(-nett_price)
final
```
```{r add dates of GM certification}
bca_award_dates <- ymd(
"2005-01-12",
"2006-04-19",
"2007-05-04",
"2008-05-22",
"2009-05-27",
"2010-05-26",
"2011-05-19",
"2012-05-20",
"2013-05-16",
"2014-04-29",
"2015-05-14",
"2016-05-26"
)
greenmark_dates_df <- tibble(year_award = 2005:2016, date_award = bca_award_dates)
greenmark_dates_df
```
```{r get cpi ready for merging}
cpi <- cpi %>%
filter(level_1 == "All Items") %>%
select(-level_1) %>%
rename("cpi" = value, "sale_ym" = month) %>%
mutate(cpi = as.double(cpi))
cpi
```
```{r add dates of award and completion}
completion_dates <- final %>%
distinct(project_name, completion_date) %>%
arrange(project_name, completion_date) %>%
filter(completion_date != "Uncompleted" & completion_date != "Unknown" & project_name != "N.A.") %>%
group_by(project_name) %>%
filter(row_number() == n()) %>%
ungroup() %>%
mutate(completion_date = as.integer(completion_date))
final_with_dates <- final %>%
left_join(greenmark_dates_df, by = "year_award") %>%
select(-completion_date) %>%
left_join(completion_dates, by = "project_name")
final_with_dates
```
```{r get placebo resales}
firstsales <- final_with_dates %>%
select(address, sale_date, date_award, award, project_name) %>%
filter(!is.na(address) & project_name != "N.A.") %>%
group_by(address) %>%
arrange(sale_date) %>%
filter(row_number() == 1) %>%
mutate(sale_ym = str_sub(as.character(sale_date), 1, 7),
award_dummy = as.numeric(!is.na(award)),
award_ym = str_sub(as.character(date_award), 1, 7),
placebo_after = as.integer(sale_ym >= award_ym),
placebo_after = replace(placebo_after, is.na(placebo_after), 0),
placebo_interaction = placebo_after * award_dummy) %>%
select(address, placebo_interaction) %>%
ungroup()
firstsales
```
```{r add more variables needed for regressions/summary stats}
topfloors <- final_with_dates %>%
filter(project_name != "N.A.") %>%
group_by(project_name) %>%
mutate(ctrl_floor = as.integer(str_match(address, "#(\\d{2})")[,2])) %>%
summarise(top_floor = max(ctrl_floor, na.rm = TRUE)) %>%
ungroup()
final_with_dates_proportions <- final_with_dates %>%
filter(type_of_area == "Strata" & no_units == 1) %>%
rename(award_type = award, award_date = date_award,
ctrl_postal_sector = postal_sector, ctrl_hdb_buyer = hdb_buyer,
ctrl_prop_type = property_type,
sale_type = type_of_sale,
ctrl_postal_code = postal_code) %>%
left_join(topfloors, by = "project_name") %>%
mutate(tmp = str_match(tenure, "\\d+"),
tmp = replace(tmp, tenure == "Freehold", "Freehold"),
tmp = replace(tmp, as.integer(tmp) < 800, "99"),
ctrl_lease = replace(tmp, as.integer(tmp) >= 800, "999"),
tmp = replace(tmp, as.integer(tmp) >= 800, "Freehold"),
ctrl_freehold = as.integer(tmp == "Freehold"),
ctrl_floor = as.integer(str_match(address, "#(\\d{2})")[,2]),
ctrl_floor2 = as.integer(ctrl_floor ^ 2),
ctrl_firstfloor = as.integer(ctrl_floor == 1),
ctrl_topfloor = as.integer(ctrl_floor == top_floor),
ctrl_ln_area = log(area),
ctrl_yrs_to_completion = year(sale_date) - completion_date,
sale_ym = str_sub(as.character(sale_date), 1, 7),
award_dummy = as.numeric(!is.na(award_type)),
award_ym = str_sub(as.character(award_date), 1, 7),
award_after = as.integer(sale_ym >= award_ym),
ctrl_postal_sector = factor(ctrl_postal_sector),
ctrl_postal_first4 = factor(str_sub(ctrl_postal_code, 1, 4))) %>%
left_join(cpi, by = "sale_ym") %>%
mutate(ln_price = log(trans_price / cpi * 100),
ln_price_psm = log(price_psm / cpi * 100),
award_after = replace(award_after, is.na(award_after), 0)) %>%
#mutate(placebo_after_gm_flag = as.integer(award_after == 0),
#placebo_ym_6mo = str_sub(as.character(award_date %m-% months(6)), 1, 7),
#placebo_ym_12mo = str_sub(as.character(award_date %m-% months(12)), 1, 7),
#placebo_after_6mo = as.integer(sale_ym >= placebo_ym_6mo),
#placebo_after_12mo = as.integer(sale_ym >= placebo_ym_12mo)) %>%
#mutate(#placebo_after_6mo = replace(placebo_after_6mo, is.na(placebo_after_6mo), 0),
#placebo_after_12mo = replace(placebo_after_12mo, is.na(placebo_after_12mo), 0)) %>%
left_join(firstsales, by = "address") %>%
mutate(placebo_resale_before_gm = as.integer((sale_type == "Resale") & (sale_ym < award_ym)),
placebo_resale_before_gm = replace(placebo_resale_before_gm, is.na(placebo_resale_before_gm), 0)) %>%
select(project_name, address, ln_price, ln_price_psm,
starts_with("award"),
starts_with("ctrl"),
starts_with("placebo"),
starts_with("sale"),
top_year = completion_date)
projects_with_resale_before_gm <- final_with_dates_proportions %>%
filter(placebo_resale_before_gm == 1) %>%
distinct(project_name) %>%
mutate(placebo_flag = 0)
final_with_dates_proportions <- final_with_dates_proportions %>%
left_join(projects_with_resale_before_gm, by = "project_name") %>%
mutate(placebo_flag = replace(placebo_flag, is.na(placebo_flag), 1))
final_with_dates_proportions
```
```{r prepare condo facilities dataset}
has_facility <- function(x) {
return(as.integer(x == "Y"))
}
condo_facilities_cleaned <- condo_facilities %>%
rename("project_name" = projectname) %>%
select(-year, -nofunit) %>%
group_by(project_name) %>%
mutate_all(funs(has_facility)) %>%
ungroup()
names(condo_facilities_cleaned) <- names(condo_facilities_cleaned) %>% map(~str_c("facil_", .x))
condo_facilities_cleaned <- condo_facilities_cleaned %>% rename("project_name" = facil_project_name)
condo_facilities_cleaned
```
```{r merge onto condo facilities}
final_with_facilities_info <- final_with_dates_proportions %>%
mutate(project_name = str_replace_all(project_name, c(
"CONDO$" = "CONDOMINIUM",
"APARTMENTS" = "APARTMENT",
"RESIDENCES" = "RESIDENCE",
"GARDENS" = "GARDEN",
"COURTS" = "COURT",
"MASIONS" = "MANSION",
"TOWERS" = "TOWER",
" +SINGAPORE" = "",
" +AT +" = "@",
" ?' ?" = "'",
" ?@ ?" = "@",
" ?# ?" = "#",
"\\." = "",
"-" = "",
" +" = " ",
"\\bRD\\b" = "ROAD",
"\\bPRIVE\\b" = "PRIVÉ"
))) %>%
mutate(address = str_to_upper(address),
project_name = replace(project_name, str_detect(address, "\\b120 GRANGE ROAD"), "GRANGE 120"),
project_name = replace(project_name, str_detect(address, "\\b17 LORONG 9 GEYLANG"), "17 LORONG 9 GEYLANG"),
project_name = replace(project_name, str_detect(address, "\\b21 LORONG TAHAR"), "21 LORONG TAHAR"),
project_name = replace(project_name, str_detect(address, "\\b30 BRISTOL ROAD"), "30 BRISTOL ROAD"),
project_name = replace(project_name, str_detect(address, "\\b32 PENHAS ROAD"), "32 PENHAS ROAD"),
project_name = replace(project_name, str_detect(address, "\\b38 DRAYCOTT DRIVE"), "38 DRAYCOTT DRIVE"),
project_name = replace(project_name, str_detect(address, "\\b38 ROBERTS LANE"), "38 ROBERTS LANE"),
project_name = replace(project_name, str_detect(address, "\\b48 SHANGHAI ROAD"), "48 SHANGHAI ROAD"),
project_name = replace(project_name, str_detect(address, "\\b62 WILKIE ROAD"), "62 WILKIE ROAD"),
project_name = replace(project_name, str_detect(address, "\\b7 DRAYCOTT DRIVE"), "7 DRAYCOTT DRIVE"),
project_name = replace(project_name, str_detect(address, "\\b8 NASSIM HILL"), "8 NASSIM HILL"),
project_name = replace(project_name, str_detect(address, "\\b90 HOLLAND ROAD"), "90 HOLLAND ROAD"),
project_name = replace(project_name, str_detect(address, "\\b23 HILLVIEW AVENUE"), "GLENDALE PARK"),
project_name = replace(project_name, str_detect(address, "\\b18 RAMBUTAN ROAD"), "EAST VIEW 18"),
project_name = replace(project_name, str_detect(address, "\\b1 ELIZABETH DRIVE"), "HILLVISTA"),
project_name = replace(project_name, str_detect(address, "\\b275 JOO CHIAT PLACE"), "JOO CHIAT VERBENA"),
project_name = replace(project_name, str_detect(address, "\\b27 EWE BOON ROAD"), "GISBORNE LIGHT"),
project_name = replace(project_name, str_detect(address, "\\b323C BALESTIER ROAD"), "BELMONDO VIEW"),
project_name = replace(project_name, str_detect(address, "\\b325C BALESTIER ROAD"), "BELMONDO VIEW"),
project_name = replace(project_name, str_detect(address, "\\b72 GRANGE ROAD"), "72 GRANGE"),
project_name = replace(project_name, str_detect(address, "\\b23 RAMBUTAN ROAD"), "23 RAMBUTAN")) %>%
left_join(condo_facilities_cleaned, by = "project_name")
final_with_facilities_info
```
```{r}
final_with_facilities_info %>% write_rds("data/intermediate/transactions_with_gm_and_condo_facilities.RDS")
```