-
Notifications
You must be signed in to change notification settings - Fork 4
/
rental_xkcd.Rmd
204 lines (173 loc) · 8.59 KB
/
rental_xkcd.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
---
title: "Vacancy rates and rent change (xkcd style)"
author: "Jens von Bergmann"
date: "18/02/2022"
output: github_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(
echo = FALSE,
message = FALSE,
warning = FALSE,
dpi=300
)
```
This graph is based on [CMHC Rms data](https://doodles.mountainmath.ca/blog/2022/02/18/vacancy-rates-and-rent-change-2021-update/), combining Edmonton and Calgary to reduce noise. Edmonton and Calgary are good examples for highlighting the relationship between rents and vacancy rates because they don't have rent control, fairly elastic housing supply, and have gone through several economic boom-bust cycles.
The rent change is fixed-sample rent change adjusted for inflation. Fixed sample means that for each cycle it compares rents across the same set of physical units, avoiding it getting skewed by compositional effects like a lot of new units coming online in a particular year.
The code for the graph is [available for anyone to reproduce or adapt](https://github.com/mountainMath/xkcd_exponential/blob/main/rental_xkcd.Rmd).
```{r fig.height=6, fig.width=6}
library(tidyverse)
library(xkcd)
library(cmhc)
library(cansim)
set.seed(123)
# generate the exponential curves with random noise
get_timeseries <- function(table_id,region_params) {
d1 <- get_cmhc(cmhc_timeseries_params(table_id = table_id,
region=region_params,
filter = list(season="October"))) %>%
mutate(Month="October")
d2 <- get_cmhc(cmhc_timeseries_params(table_id = table_id,
region=region_params,
filter = list(season="April"))) %>%
mutate(Month="April")
bind_rows(d1,d2) %>%
rename(Date=X1) %>%
mutate(Date=str_extract(Date,"\\d{4}")) %>%
mutate(Date=as.Date(paste0(Date," ",Month," 01"),format="%Y %B %d")) %>%
select(-Month)
}
region_params <- cmhc_region_params_from_census("48825")
region_params2 <- cmhc_region_params_from_census("48835")
bedroom_vars <- c("Bachelor", "1 Bedroom", "2 Bedroom", "3 Bedroom +", "Total")
inflation <- get_cansim_vector("v108785713","1990-01-01") %>%
rename(CPI=val_norm) %>%
select(Date,CPI) %>%
mutate(CPI=CPI/100)
d1 <- bind_rows(
dat_vacancy <- get_timeseries(cmhc_table_list$`Rms Vacancy Rate Time Series`,region_params) %>%
mutate(Series="Vacancy rate"),
get_timeseries(cmhc_table_list$`Rms Rent Change Time Series`,region_params) %>%
mutate(Series="Rent change")) %>%
mutate(name="Calgary")
d2 <- bind_rows(
dat_vacancy <- get_timeseries(cmhc_table_list$`Rms Vacancy Rate Time Series`,region_params2) %>%
mutate(Series="Vacancy rate"),
get_timeseries(cmhc_table_list$`Rms Rent Change Time Series`,region_params2) %>%
mutate(Series="Rent change")) %>%
mutate(name="Edmonton")
data <- bind_rows(d1,d2) %>%
standardize_cmhc_data() %>%
mutate(CV=case_when(Quality=="a" ~ 1,
Quality=="b" ~ 3,
Quality=="c" ~ 6,
Quality=="d" ~ 8,
TRUE ~ 15)) %>%
mutate(Value=Value/100) %>%
group_by(Series,Date,Bedrooms) %>%
summarize(Value=weighted.mean(Value,w=1/CV,na.rm=TRUE),
CV=weighted.mean(CV,w=1/CV,na.rm=TRUE),
.groups="drop") %>%
group_by(Series,Date) %>%
mutate(mean=weighted.mean(Value,w=1/CV,na.rm=TRUE)) %>%
left_join(inflation,by="Date") %>%
mutate(name="Calgary/Edmonton")
reference_date <- as.Date("2000-01-01")
plot_data <- data %>%
filter(Bedrooms=="Total") %>%
mutate(Value=ifelse(Series=="Rent change",Value-CPI,Value)) %>%
group_by(Series,name,Bedrooms) %>%
mutate(Next=lead(Value,order_by = Date)) %>%
mutate(Previous=lag(Value,order_by = Date)) %>%
mutate(diff=difftime(Date,lag(Date,order_by = Date)) %>% as.integer) %>%
ungroup %>%
mutate(keep=!is.na(Value)| !(diff<200 & !is.na(Previous))) %>%
filter(keep) %>%
mutate(Value=coalesce(Value,mean)) %>%
mutate(day=difftime(Date,reference_date) %>% as.integer) %>%
select(Date,day,Series,Value)
l1 <- "Everyone else"
l2 <- "Critical urban geographers"
text_items <- tibble(Type=factor(c(l1,l1,l2,l2),
levels=c(l1,l2)),
Date=as.Date(c("2015-01-01","2001-01-01","2015-01-01","2001-01-01")),
Value=c(0.18,-0.06,0.18,-0.06),
label=c("Rents rise when\nvacancy rates are low",
"Rents fall when\nvacancy rates are high",
"Financialization\n& gentrification",
"altruistic\nmom & pop landlords")) %>%
# label=c("Rents rise when\nvacancy rates are low",
# "Rents fall when\nvacancy rates are high",
# "Landlords are greedy\nand commodified",
# "Landlords are altruistic\nand decommodified")) %>%
mutate(day=difftime(Date,reference_date) %>% as.integer)
curve_data_calgary <- tibble(x=as.Date(c("2011-01-01","2011-01-01","2015-01-01","1996-10-01","2005-05-01","2005-05-01")),
xend=as.Date(c("1998-12-01","2006-12-01","2013-10-01","1993-10-01","2010-02-01","2016-08-01")),
Value=c(0.18,0.18,0.15,-0.07,-0.07,-0.07),
Valueend=c(0.135,0.16,0.075,-0.04,-0.074,-0.09),
curvature = c(0.1,0.1,-0.1,-0.2,0.1,0.1)) %>%
mutate(day=difftime(x,reference_date) %>% as.integer,
dayend=difftime(xend,reference_date) %>% as.integer)
curve_data_ce <- tibble(x=as.Date(c("2011-01-01","2011-01-01","2015-01-01","1996-06-01","2005-09-01","2005-09-01")),
xend=as.Date(c("1998-12-01","2007-12-01","2014-10-01","1994-10-01","2010-01-01","2016-07-01")),
Value=c(0.18,0.18,0.15,-0.07,-0.07,-0.07),
Valueend=c(0.085,0.16,0.07,-0.04,-0.065,-0.075),
curvature = c(0.1,0.1,-0.1,-0.2,0.1,0.1)) %>%
mutate(day=difftime(x,reference_date) %>% as.integer,
dayend=difftime(xend,reference_date) %>% as.integer)
baselines <- tibble(Series=c("Rent change","Vacancy rate"),
Value=c(0,0.03))
mapping <- aes(x, y,
scale,ratioxy,angleofspine,
anglerighthumerus,anglelefthumerus,anglerightradius,
angleleftradius,angleleftleg,anglerightleg,angleofneck)
dataman <- data.frame( x= as.integer(difftime(as.Date("1992-01-01"),reference_date)),
y=0.2,
scale = 0.05 ,
ratioxy = 15000,
angleofspine = -pi/2 ,
anglerighthumerus = c(-pi/6, -pi/6),
anglelefthumerus = c(-pi/2 - pi/6, -pi/2 - pi/6),
anglerightradius = c(pi/5, -pi/5),
angleleftradius = c(pi/5, -pi/5),
angleleftleg = 3*pi/2 + pi / 12 ,
anglerightleg = 3*pi/2 - pi / 12,
angleofneck = runif(1, 3*pi/2-pi/10, 3*pi/2+pi/10),
Type=c(l2,l1))
g <- ggplot(plot_data,aes(x=day,y=Value))+
geom_hline(data=baselines,aes(linetype=Series,yintercept=Value),size=0.5) +
geom_line(aes(linetype=Series,size=Series)) +
theme_xkcd() +
scale_size_manual(values=c("Rent change"=1,"Vacancy rate"=0.75),
guide="none") +
theme(legend.position = "bottom") +
geom_point(size=1.2,shape=21) +
scale_y_continuous(labels=scales::percent,breaks=seq(-0.05,0.15,0.05)) +
scale_x_continuous(breaks=NULL,minor_breaks = NULL) +
labs(x=NULL,y=NULL,
#caption="MountainMath, Data: CMHC Rms (Calgary)",
linetype=NULL) +
geom_text(data=text_items,family="xkcd",
aes(label=label)) +
scale_linetype_manual(values=c("Rent change"="solid","Vacancy rate"="dashed")) +
theme(panel.spacing.y = unit(2, "lines")) +
xkcdman(mapping,dataman,inherit.aes=FALSE) +
facet_wrap(~Type, ncol=1) +
geom_curve(data=curve_data_ce[c(1),],
aes(x=day,xend=dayend,yend=Valueend),
arrow = arrow(length=unit(0.25,"cm")),
curvature = 0.2) +
geom_curve(data=curve_data_ce[c(2,5,6),],
aes(x=day,xend=dayend,yend=Valueend),
arrow = arrow(length=unit(0.25,"cm")),
curvature = 0.1) +
geom_curve(data=curve_data_ce[3,],
aes(x=day,xend=dayend,yend=Valueend),
arrow = arrow(length=unit(0.25,"cm")),
curvature = -0.1) +
geom_curve(data=curve_data_ce[4,],
aes(x=day,xend=dayend,yend=Valueend),
arrow = arrow(length=unit(0.25,"cm")),
curvature = -0.2)
g
```