Correlations of provincial house prices in Canada 1990-2014

A common opinion that I come across is that there is no such thing as a “Canadian housing market”.  Instead, the various housing markets across the country behave independently of one another.  I used the CMHC webcrawler to gather data on annual median provincial prices of absorbed houses from 1990-2014.  I then used the corrgram package in R to determine the degree of linear association of house prices between provinces and the 95% confidence interval.  The results show a very high correlation across all provinces suggesting to me that there is in fact a “Canadian housing market”.

Results:

-AP_Corr-page-001

-AP_Corr2-page-001

Code:

library(reshape2)
library(reshape)
library(ggplot2)
library(corrgram)

#set the directory for the input files
DataDirectory <- "~/Documents/Scrapy Projects/cmhc1/"

#Bring in data for absorbed units
#This data is in characters with comma seperated values and commas in the numbers, so it's a bit trickier to format
AbsorbedPrices <- read.csv(paste(DataDirectory,"absorbed_unit_prices.csv",sep=""))
AbsorbedPrices[,1] <- as.numeric(gsub(",","",AbsorbedPrices[,1]))
AbsorbedPrices[,2] <- as.numeric(gsub(",","",AbsorbedPrices[,2]))
AbsorbedPrices[,3] <- as.numeric(gsub(",","",AbsorbedPrices[,3]))
AbsorbedPrices[,4] <- as.numeric(gsub(",","",AbsorbedPrices[,4]))
AbsorbedPrices[,5] <- as.numeric(gsub(",","",AbsorbedPrices[,5]))
AbsorbedPrices[,7] <- as.numeric(gsub(",","",AbsorbedPrices[,7]))
AbsorbedPrices[,9] <- as.numeric(gsub(",","",AbsorbedPrices[,9]))

#Melt the data to stack the variables we want to measure in a single column.  This makes creating graphics easier.
AP_Melted <- melt(AbsorbedPrices, id.vars=c("year","geo"))
AP_Melted_Medians <- AP_Melted[AP_Melted$variable=="median",]
AP_Recast <- cast(AP_Melted_Medians, year~geo, fun.aggregate=NULL)
AP_Recast <- AP_Recast[,-1]
AP_Recast <- AP_Recast[,c(3,2,1,11,4,8,10,5,9,7,6)]
AP_Recast_Labels <- c("Canada", "B.C.", "Alta.", "Sask.", "Man.","Ont.","Que.","N.B.","P.E.I.","N.S.","N.L.") #be very careful that this matches the order of the columns in the AB_Recast dataframe
a <- log(AP_Recast)

#I found the between the two hash lines at: http://ryouready.wordpress.com/2009/02/17/r-good-practice-adding-footnotes-to-graphics/
#it's a lovely bit of code that creates a custom function to add a footer to the plots
##############################################################################################################
source <- "Source: CMHC"
author <- "PosNorm"
footnote <- paste(source, format(Sys.time(), "%d %b %Y"),
                  author, sep=" / ")

# default footnote is today's date, cex=.7 (size) and color
# is a kind of grey

makeFootnote <- function(footnoteText=
                           format(Sys.time(), "%d %b %Y"),
                         size= .7, color= grey(.5))
{
  require(grid)
  pushViewport(viewport())
  grid.text(label= footnoteText ,
            x = unit(1,"npc") - unit(2, "mm"),
            y= unit(2, "mm"),
            just=c("right", "bottom"),
            gp=gpar(cex= size, col=color))
  popViewport()
}
##############################################################################################################

pdf(paste(DataDirectory,"AP_Corr.pdf"))
title <- "Correlations of median annual absorbed house prices in Canada 1990-2014"
#generate a correlogram
#confidence intervals are given at 95%
corrgram(AP_Recast, order=FALSE, lower.panel=panel.pie, upper.panel=panel.conf, cex.main = 1, main = title, labels=AP_Recast_Labels)
#corrgram(a, order=NULL, lower.panel=panel.pie, upper.panel=panel.conf)
makeFootnote(footnote)     #adds the footnote through the above custom function
dev.off()

pdf(paste(DataDirectory,"AP_Corr2.pdf"))
title <- "Correlations of median annual absorbed house prices in Canada 1990-2014"
#generate a correlogram
#confidence intervals are given at 95%
corrgram(AP_Recast, order=FALSE, lower.panel=panel.pts, upper.panel=panel.ellipse, main = title, cex.main = 1,labels=AP_Recast_Labels)
#corrgram(a, order=NULL, lower.panel=panel.pie, upper.panel=panel.conf)
makeFootnote(footnote)     #adds the footnote through the above custom function
dev.off()

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s