Skip to content

Instantly share code, notes, and snippets.

@HughParsonage
Last active February 19, 2018 11:57
Show Gist options
  • Save HughParsonage/5badddc134fca6dc05627d998bd87cec to your computer and use it in GitHub Desktop.
Save HughParsonage/5badddc134fca6dc05627d998bd87cec to your computer and use it in GitHub Desktop.
Price 2 income vs Housing per capita (The Satisfaction of Cowgill)
library(rsdmx)
library(data.table)
library(hutils)
library(magrittr)
library(ggplot2)
library(ggrepel)
ISO3166 <- Census2016.spec:::ISO3166 # hughparsonage/Census2016.spec
to_name <- function(a3) {
ISO3166[["name"]][match(a3, ISO3166[["alpha_3"]])]
}
testthat::expect_equal(to_name(c("AUS", "USA", "AUS")),
c("Australia", "United States of America", "Australia"))
price2income_ratios_since_1990 <-
readSDMX("http://stats.oecd.org/restsdmx/sdmx.ashx/GetData/HOUSE_PRICES/AUS+AUT+BEL+CAN+CHL+CZE+DNK+EST+FIN+FRA+DEU+GRC+HUN+ISL+IRL+ISR+ITA+JPN+KOR+LVA+LUX+MEX+NLD+NZL+NOR+POL+PRT+SVK+SVN+ESP+SWE+CHE+TUR+GBR+USA+EA+OECD+BRA+CHN+COL+IND+IDN+LTU+RUS+ZAF.HPI_YDH/all?startTime=1990&endTime=2015") %>%
as.data.frame %>%
as.data.table
price2income_ratios_1990_2000_2015 <-
price2income_ratios_since_1990 %>%
.[obsTime %chin% c("1990", "2000", "2010", "2015")] %>%
.[, Country := to_name(COU)] %>%
.[complete.cases(.), .(Country, Year = obsTime, price2income = obsValue)] %>%
.[Country == "United States of America", Country := "United States"] %>%
.[Country == "United Kingdom of Great Britain and Northern Ireland", Country := "United Kingdom"] %>%
.[Country == "Korea (Republic of)", Country := "Korea"] %>%
.[Country == "Slovak Republic", Country := "Slovakia"] %>%
.[Country == "Slovak Republic", Country := "Slovakia"] %>%
setkey(Country, Year) %>%
.[]
Houses_per_100 <-
fread("
1990 (1) 2000 (2) 2010 (3) 2015 (4)
377 407 411 401
440 482 530 525
358 453 527 546
370 407 428 ..
249 281 280 290
329 387 451 ..
378 352 487 510
393 425 454 ..
437 453 463 464
353 414 495 ..
441 487 531 536
474 506 533 546
.. 467 506 510
.. .. 573 ..
371 398 441 449
335 372 440 437
441 479 526 ..
346 402 477 ..
.. .. 357 383
.. 413 493 ..
310 373 437 445
376 390 406 414
340 478 539 ..
.. .. 314 ..
393 420 440 449
339 353 339 398
414 436 437 448
294 327 350 363
416 486 554 571
.. .. 432 ..
.. .. 360 ..
.. .. 412 410
.. 515 540 ..
474 482 485 476
475 498 527 527
408 431 439 436
412 413 416 419",
sep = "\t",
header = TRUE,
na.strings = "..",
colClasses = list(integer = 1:4)) %>%
setnames(1:4, c("1990", "2000", "2010", "2015")) %>%
.[, "1990" := as.integer(`1990`)]
Countries <- fread(
"Country
Australia
Austria
Bulgaria
Canada
Chile
Croatia
Cyprus (a,b)
Czech Republic
Denmark
Estonia
Finland
France
Germany
Greece
Hungary
Ireland
Italy
Japan
Korea
Latvia
Lithuania
Luxembourg
Malta
Mexico
Netherlands
New Zealand
Norway
Poland
Portugal
Romania
Slovak Republic
Slovenia
Spain
Sweden
Switzerland
United Kingdom
United States", header = TRUE, sep = ",")
cbind(Countries,Houses_per_100) %>%
melt.data.table(id.vars = "Country",
variable.factor = FALSE,
variable.name = "Year",
value.name = "HousingPer1000") %>%
.[Country %pin% "Cyprus", Country := "Cyprus"] %>%
.[price2income_ratios_1990_2000_2015, on = c("Country", "Year"), nomatch=0L] %>%
.[!is.na(HousingPer1000)] %>%
.[, is_min_yr := min(Year, na.rm = TRUE), keyby = "Country"] %>%
.[Year == is_min_yr, label := as.character(Country)] %>%
ggplot(aes(x = HousingPer1000, y = price2income,
group = Country, color = Country,
label = label)) +
geom_path(arrow = arrow(), na.rm = TRUE) +
scale_alpha_continuous(range = c(0.5, 1)) +
geom_text_repel(na.rm = TRUE) +
theme(legend.position = "none")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment