-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.R
429 lines (335 loc) · 22.4 KB
/
README.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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
## ----setup, include=FALSE--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
knitr::opts_chunk$set(echo = TRUE)
## ----packages, include = FALSE---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
library(tidyverse)
library(readr)
library(lubridate)
library(jtools)
## ----import, message = FALSE, echo = FALSE---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#import data as objects
nineties_data <- read_csv("~/Desktop/Data Stuff/singapore-hdb-resale-prices/Data/resale-flat-prices-based-on-approval-date-1990-1999.csv")
feb2012_data <- read_csv("~/Desktop/Data Stuff/singapore-hdb-resale-prices/Data/resale-flat-prices-based-on-approval-date-2000-feb-2012.csv")
dec2014_data <- read_csv("~/Desktop/Data Stuff/singapore-hdb-resale-prices/Data/resale-flat-prices-based-on-registration-date-from-mar-2012-to-dec-2014.csv")
dec2016_data <- read_csv("~/Desktop/Data Stuff/singapore-hdb-resale-prices/Data/resale-flat-prices-based-on-registration-date-from-jan-2015-to-dec-2016.csv")
latest_data <- read_csv("~/Desktop/Data Stuff/singapore-hdb-resale-prices/Data/resale-flat-prices-based-on-registration-date-from-jan-2017-onwards.csv")
## ----data exploration, echo = FALSE----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
head(nineties_data)
head(feb2012_data)
head(dec2014_data)
head(dec2016_data)
head(latest_data)
## ----drop excess columns, include = FALSE----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
dec2016_data <- dec2016_data %>%
select(!remaining_lease)
head(dec2016_data)
latest_data <- latest_data %>%
select(!remaining_lease)
head(latest_data)
## ----union datasets, include = FALSE---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
resale_data <- bind_rows(nineties_data,feb2012_data,dec2014_data,dec2016_data,latest_data)
head(resale_data)
## ----storey_ranges, warning = FALSE, message = FALSE, echo = FALSE, fig.align = "center"-----------------------------------------------------------------------------------------------------------------------------------------------
#examine the values in storey_range for each year period
resale_data %>%
group_by(storey_range) %>%
summarise(count = n()) %>%
ggplot(aes(x = storey_range, y = count)) +
geom_col() +
coord_flip() +
labs(y = "Count of flats",
x = "Storey Ranges",
title = "Number of flats per storey range in 1990 to 2022")
## ----relabel storey_ranges, include = FALSE--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#create new variable
resale_data <- resale_data %>%
mutate(storeys = factor(case_when(
storey_range == "01 TO 03" | storey_range == "01 TO 05" | storey_range == "04 TO 06" ~ "low",
storey_range == "06 TO 10" | storey_range == "07 TO 09" ~ "mid",
TRUE ~ "high"
),
levels = c("low", "mid", "high"))) %>%
relocate(storeys, .after = storey_range)
head(resale_data)
## ----plot storeys and price, echo = FALSE, fig.align = "center"------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ggplot(resale_data, aes(x = storeys, y = resale_price/1000)) +
geom_boxplot() +
coord_flip() +
labs(x = "Storeys",
y = "Prices (thousands; SGD)",
title = "Spread of HDB Resale Flat Prices by Storey Levels") +
scale_y_continuous(breaks = seq(0, 1500, by = 200))
## ----plot towns and price, echo = FALSE, fig.align = "center"--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ggplot(resale_data, aes(x = reorder(town, resale_price, median), y = resale_price/1000)) +
geom_boxplot() +
coord_flip() +
labs(x = "Towns",
y = "Prices (thousands; SGD)",
title = "Spread of HDB Resale Flat Prices by HDB Towns") +
scale_y_continuous(breaks = seq(0, 1500, by = 200))
## ----create year variable, include = FALSE---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
resale_data <- resale_data %>%
mutate(year = as.integer(str_sub(month, end = 4))) %>%
relocate(year, .after = month)
## ----clean flat_type data, include = FALSE---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
resale_data %>%
group_by(flat_type) %>%
summarise(n())
resale_data <- resale_data %>%
mutate(flat_type2 = factor(case_when(
flat_type == "1 ROOM" ~ "1-room",
flat_type == "2 ROOM" ~ "2-room",
flat_type == "3 ROOM" ~ "3-room",
flat_type == "4 ROOM" ~ "4-room",
flat_type == "5 ROOM" ~ "5-room",
flat_type == "EXECUTIVE" ~ "Executive",
TRUE ~ "Others"
),
levels = c("1-room", "2-room", "3-room", "4-room", "5-room", "Executive", "Others"))) %>%
relocate(flat_type2, .after = flat_type)
## ----filter data, echo = FALSE---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
filtered_data <- resale_data %>%
filter(year >= 2010,
flat_type2 == c("4-room", "5-room", "Executive"),
storeys != "low")
head(filtered_data)
## ----plot storeys/towns and price with filtered data, echo = FALSE, fig.show="hold", out.width="50%"-----------------------------------------------------------------------------------------------------------------------------------
ggplot(filtered_data, aes(x = storeys, y = resale_price/1000)) +
geom_boxplot() +
coord_flip() +
labs(x = "Storeys",
y = "Prices (thousands; SGD)",
title = "Spread of HDB Resale Flat Prices by Storey Levels") +
scale_y_continuous(breaks = seq(0, 1500, by = 200))
ggplot(filtered_data, aes(x = reorder(town, resale_price, median), y = resale_price/1000)) +
geom_boxplot() +
coord_flip() +
labs(x = "Towns",
y = "Prices (thousands; SGD)",
title = "Spread of HDB Resale Flat Prices by HDB Towns") +
scale_y_continuous(breaks = seq(0, 1500, by = 200))
## ----compare median prices of storeys, include = FALSE---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
median_price_high <- filtered_data %>%
filter(storeys == "high") %>%
summarise(median(resale_price)) %>%
pull()
median_price_mid <- filtered_data %>%
filter(storeys == "mid") %>%
summarise(median(resale_price)) %>%
pull()
median_diff <- format((median_price_high - median_price_mid),
big.mark = ",", big.interval = 3L,
scientific = FALSE)
## ----plot flat type and price with filtered data, echo = FALSE, fig.align = "center"---------------------------------------------------------------------------------------------------------------------------------------------------
ggplot(filtered_data, aes(x = flat_type2, y = resale_price/1000)) +
geom_boxplot() +
coord_flip() +
labs(x = "Flat Type",
y = "Prices (thousands; SGD)",
title = "Spread of HDB Resale Flat Prices by Flat Types") +
scale_y_continuous(breaks = seq(0, 1500, by = 200))
## ----plot towns and price side, echo = FALSE, fig.show="hold", out.width="50%"---------------------------------------------------------------------------------------------------------------------------------------------------------
ggplot(resale_data, aes(x = reorder(town, resale_price, median), y = resale_price/1000)) +
geom_boxplot() +
coord_flip() +
labs(x = "Towns",
y = "Prices (thousands; SGD)",
title = "Spread of HDB Resale Flat Prices by HDB Towns for 1990 to 2022") +
scale_y_continuous(breaks = seq(0, 1500, by = 200)) +
theme(plot.title = element_text(size = 12))
ggplot(filtered_data, aes(x = reorder(town, resale_price, median), y = resale_price/1000)) +
geom_boxplot() +
coord_flip() +
labs(x = " ",
y = "Prices (thousands; SGD)",
title = "Spread of HDB Resale Flat Prices by HDB Towns for 2010 to 2022") +
scale_y_continuous(breaks = seq(0, 1500, by = 200)) +
theme(plot.title = element_text(size = 12))
## ----plot lease date and price by towns, echo = FALSE, message = FALSE, fig.align = "center"-------------------------------------------------------------------------------------------------------------------------------------------
filtered_data %>%
group_by(town, lease_commence_date) %>%
mutate(median_price_by_lease = median(resale_price)) %>%
ungroup() %>%
ggplot(aes(x = lease_commence_date, y = median_price_by_lease/1000)) +
geom_point() +
geom_smooth(method = lm, se = FALSE, color = "red") +
labs(x = "Lease Commencement Year",
y = "Prices (thousands; SGD)",
title = "Median HDB Resale Flat Prices by Lease Date, grouped by HDB town") +
scale_y_continuous(breaks = seq(0, 1500, by = 200)) +
scale_x_continuous(breaks = seq(1960, 2020, by = 20)) +
theme(plot.title = element_text(size = 12)) +
facet_wrap(~town)
## ----spearman correlation for correlation between price and lease year, echo = FALSE---------------------------------------------------------------------------------------------------------------------------------------------------
filtered_data <- filtered_data %>%
group_by(town, lease_commence_date) %>%
mutate(median_price_by_lease = median(resale_price)) %>%
ungroup()
corr_median_price_lease <- format(round(cor(filtered_data$lease_commence_date,
filtered_data$median_price_by_lease,
method = "spearman"), 3), nsmall = 3)
## ----spearman correlation test for correlation between price and lease year, echo = FALSE----------------------------------------------------------------------------------------------------------------------------------------------
cor.test(filtered_data$lease_commence_date,
filtered_data$median_price_by_lease,
method = "spearman",
exact = FALSE)
## ----plot lease date and price, echo = FALSE, message = FALSE, fig.align = "center"----------------------------------------------------------------------------------------------------------------------------------------------------
filtered_data %>%
group_by(town, lease_commence_date) %>%
mutate(median_price_by_lease = median(resale_price)) %>%
ungroup() %>%
ggplot(aes(x = lease_commence_date, y = median_price_by_lease/1000)) +
geom_point() +
geom_smooth(method = lm, se = FALSE, color = "red") +
labs(x = "Lease Commencement Year",
y = "Prices (thousands; SGD)",
title = "Median HDB Resale Flat Prices by Lease Date") +
scale_y_continuous(breaks = seq(0, 1500, by = 200)) +
scale_x_continuous(breaks = seq(1960, 2020, by = 20)) +
theme(plot.title = element_text(size = 12))
## ----create date and month variable and then plot time series, echo = FALSE, message = FALSE, fig.align = "center"---------------------------------------------------------------------------------------------------------------------
filtered_data <- filtered_data %>%
mutate(month_year = ym(month)) %>%
relocate(month_year, .after = month)
#plot time series with month and year against resale value
filtered_data %>%
group_by(month_year, flat_type2) %>% #all flat sizes
summarise(mean_resale_price_hundredk = mean(resale_price)/1000) %>%
ggplot(aes(month_year, mean_resale_price_hundredk)) +
geom_line(color = "red3") +
labs(title = "Monthly Mean HDB Resale Prices over 2010 to 2022",
y = "HDB Resale Prices (SGD; thousands)",
x = "Month and Year") +
theme_bw(base_size = 15) +
theme(plot.title = element_text(size = 12),
axis.text.x = element_text(angle = 60, vjust = 1, hjust = 1),
axis.title.y = element_text(size = 13)) +
facet_grid(rows = vars(flat_type2)) +
scale_x_date(date_breaks = "1 year", date_minor_breaks = "1 month", date_labels = "%b %Y")
filtered_data %>%
filter(flat_type2 == "4-room") %>% #4-room flats
group_by(month_year, storeys) %>%
summarise(mean_resale_price_hundredk = mean(resale_price)/1000) %>%
ggplot(aes(month_year, mean_resale_price_hundredk)) +
geom_line(color = "red3") +
labs(title = "Monthly Mean HDB Resale Prices for 4-room apartments over 2010 to 2022",
y = "HDB Resale Prices (SGD; thousands)",
x = "Month and Year") +
theme_bw(base_size = 15) +
theme(plot.title = element_text(size = 12),
axis.text.x = element_text(angle = 60, vjust = 1, hjust = 1),
axis.title.y = element_text(size = 13)) +
facet_grid(rows = vars(storeys)) +
scale_x_date(date_breaks = "1 year", date_minor_breaks = "1 month", date_labels = "%b %Y")
filtered_data %>%
filter(flat_type2 == "5-room") %>% #5-room flats
group_by(month_year, storeys) %>%
summarise(mean_resale_price_hundredk = mean(resale_price)/1000) %>%
ggplot(aes(month_year, mean_resale_price_hundredk)) +
geom_line(color = "red3") +
labs(title = "Monthly Mean HDB Resale Prices for 5-room apartments over 2010 to 2022",
y = "HDB Resale Prices (SGD; thousands)",
x = "Month and Year") +
theme_bw(base_size = 15) +
theme(plot.title = element_text(size = 12),
axis.text.x = element_text(angle = 60, vjust = 1, hjust = 1),
axis.title.y = element_text(size = 13)) +
facet_grid(rows = vars(storeys)) +
scale_x_date(date_breaks = "1 year", date_minor_breaks = "1 month", date_labels = "%b %Y")
filtered_data %>%
filter(flat_type2 == "Executive") %>% #exec flats
group_by(month_year, storeys) %>%
summarise(mean_resale_price_hundredk = mean(resale_price)/1000) %>%
ggplot(aes(month_year, mean_resale_price_hundredk)) +
geom_line(color = "red3") +
labs(title = "Monthly Mean HDB Resale Prices for Executive apartments over 2010 to 2022",
y = "HDB Resale Prices (SGD; thousands)",
x = "Month and Year") +
theme_bw(base_size = 15) +
theme(plot.title = element_text(size = 12),
axis.text.x = element_text(angle = 60, vjust = 1, hjust = 1),
axis.title.y = element_text(size = 13)) +
facet_grid(rows = vars(storeys)) +
scale_x_date(date_breaks = "1 year", date_minor_breaks = "1 month", date_labels = "%b %Y")
## ----create variable for remaining length of lease, echo = FALSE-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
filtered_data <- filtered_data %>%
mutate(year = as.numeric(year)) %>%
mutate(remaining_lease_length = 99-(year-lease_commence_date)) %>%
relocate(remaining_lease_length, .after = lease_commence_date)
## ----create variable for region, echo = FALSE------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
filtered_data <- filtered_data %>%
mutate(region = factor(case_when(
town %in% c("BISHAN", "BUKIT MERAH", "BUKIT TIMAH", "CENTRAL AREA", "GEYLANG", "KALLANG/WHAMPOA",
"MARINE PARADE", "QUEENSTOWN", "TOA PAYOH") ~ "central",
town %in% c("BEDOK", "PASIR RIS", "TAMPINES") ~ "east",
town %in% c("SEMBAWANG", "WOODLANDS", "YISHUN") ~ "north",
town %in% c("ANG MO KIO", "HOUGANG", "PUNGGOL", "SENGKANG", "SERANGOON") ~ "north-east",
town %in% c("BUKIT BATOK", "BUKIT PANJANG", "CHOA CHU KANG", "CLEMENTI", "JURONG EAST", "JURONG WEST") ~ "west"
),
levels = c("central", "east", "north", "north-east", "west"))) %>%
relocate(region, .after = town)
## ----create variable for month of sale, echo = FALSE-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
filtered_data <- filtered_data %>%
mutate(month_2 = as.character(month_year, "%B")) %>%
relocate(month_2, .after = month_year) %>%
mutate(month_2 = factor(month_2,
levels = c("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")))
## ----anova for flat type and floor area, echo = FALSE----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
floor_area_vs_flat_type <- lm(floor_area_sqm ~ flat_type2, data = filtered_data) #define model for ANOVA
anova(floor_area_vs_flat_type)
## ----pairwise t-test, echo = FALSE-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
pairwise.t.test(filtered_data$floor_area_sqm, filtered_data$flat_type2, p.adjust.method = "bonferroni")
## ----multiple linear regression for resale prices Full Model, echo = FALSE-------------------------------------------------------------------------------------------------------------------------------------------------------------
glm_resale_prices <- glm(resale_price ~ flat_type2 + floor_area_sqm + remaining_lease_length + region + storeys + month_2,
data = filtered_data,
family = gaussian()) #define linear regression model
summ(glm_resale_prices) #Review the results
## ----multiple linear regression for resale prices Model 2, echo = FALSE----------------------------------------------------------------------------------------------------------------------------------------------------------------
glm_resale_prices_2 <- glm(resale_price ~ floor_area_sqm + remaining_lease_length + region + storeys + month_2,
data = filtered_data,
family = gaussian()) #define linear regression model 2 without flat_type variable
summ(glm_resale_prices_2) #Review the results 2
## ----multiple linear regression for resale prices Model 3, echo = FALSE----------------------------------------------------------------------------------------------------------------------------------------------------------------
glm_resale_prices_3 <- glm(resale_price ~ flat_type2 + floor_area_sqm + remaining_lease_length + region + storeys,
data = filtered_data,
family = gaussian()) #define linear regression model 3 without month of year
summ(glm_resale_prices_3) #Review the results 3
## ----effect plot, echo = FALSE, fig.align = "center"-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
summ(glm_resale_prices)
effect_plot(glm_resale_prices, pred = flat_type2, interval = TRUE, plot.points = TRUE, jitter = 0.05) #plot flat type predictor
effect_plot(glm_resale_prices, pred = floor_area_sqm, interval = TRUE, plot.points = TRUE, jitter = 0.05) #plot floor area predictor
effect_plot(glm_resale_prices, pred = remaining_lease_length, interval = TRUE, plot.points = TRUE, jitter = 0.05) #plot remaining lease length predictor
effect_plot(glm_resale_prices, pred = region, interval = TRUE, plot.points = TRUE, jitter = 0.05) #plot region predictor
effect_plot(glm_resale_prices, pred = storeys, interval = TRUE, plot.points = TRUE, jitter = 0.05) #plot storeys predictor
effect_plot(glm_resale_prices, pred = month_2, interval = TRUE, plot.points = TRUE, jitter = 0.05) #plot month predictor
## ----review the linear regression, echo = FALSE----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
summ(glm_resale_prices) #Review the results
## ----visualise prices by region over time, echo = FALSE, message = FALSE, fig.align = "center"-----------------------------------------------------------------------------------------------------------------------------------------
filtered_data %>%
select(resale_price, region, month_year) %>%
group_by(region, month_year) %>%
summarise(mean_price = mean(resale_price), region, month_year) %>%
ggplot(aes(x = month_year, y = mean_price/1000)) +
geom_line(color = "red") +
labs(title = "Monthly Mean HDB Resale Prices over 2010 to 2022 by region",
y = "HDB Resale Prices (SGD; thousands)",
x = "Month and Year") +
theme_bw(base_size = 15) +
theme(plot.title = element_text(size = 12),
axis.text.x = element_text(angle = 60, vjust = 1, hjust = 1),
axis.title.y = element_text(size = 13)) +
facet_grid(rows = vars(region)) +
scale_x_date(date_breaks = "1 year", date_minor_breaks = "1 month", date_labels = "%b %Y")
## ----visualise prices by month of year, echo = FALSE, fig.align = "center"-------------------------------------------------------------------------------------------------------------------------------------------------------------
filtered_data %>%
select(resale_price, month_2) %>%
group_by(month_2) %>%
ggplot(aes(x = month_2, y = resale_price/1000, fill = month_2)) +
geom_boxplot() +
labs(title = "Monthly Mean HDB Resale Prices over 2010 to 2022 by Month of Sale",
y = "HDB Resale Prices (SGD; thousands)",
x = "Month of Sale") +
theme_bw(base_size = 15) +
theme(plot.title = element_text(size = 12),
axis.text.x = element_text(angle = 60, vjust = 1, hjust = 1),
axis.title.y = element_text(size = 13)) +
scale_fill_brewer(palette = "Paired")