The housing bubble by city

[This article was first published on Decision Science News » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

DIFFERENT CITIES TELL DIFFERENT STORIES

Inspired by a tutorial called 5 Minute Analysis in R: Case-Shiller Indices, we decided to redo the analysis with 1) More up to date data and 2) Hadley Wickham’s super ggplot2 graphics package.

The above plot shows the general trend in home prices, relative to January 2000, in various cities, but is otherwise a mess.

Much like Olympic sports coverage, we next focus in on a few exceptional stories. It is much easier to read.

Miami sailed high and fell far. Detroit rose modestly and but dropped more than it went up.  Dallas held steady. DC is enjoying a bit of renewed growth, but are it and New York yet to fall?

The graphs were surprisingly easy to create in R, and the full code is below. If you’d like to do your own analysis, the Standard & Poor’s Web site has data that you can download yourself. We chose the “seasonally adjusted” data set.

New to us in the code below was the “droplevels‘ command which was added to R’s version 12. It dispenses with unused factor levels that you sometimes end up with when subsetting data. Extra levels are often harmless, but in ggplot2 they show up in the legend.  Without droplevels, the second graph would have the legend of the first graph.

library(ggplot2) ## Read in data, available from: #www.standardandpoors.com/indices/sp-case-shiller-home-price-indices/en/us/?indexId=spusa-cashpidff--p-us---- dat=read.csv("CSHomePrice_History.csv") mdf=melt(dat,id.vars="YEAR") mdf$Date=as.Date(paste("01-",mdf$YEAR,sep=""),"%d-%b-%y") names(mdf)=c("MonthYear","City","IndexValue","Date") ggplot(data=mdf,aes(x=Date,y=IndexValue)) + geom_line(aes(color=City),size=1.25) + scale_x_date("Year", minor="years") + scale_y_continuous("Case Schiller Index") sm=subset(mdf,City %in% c('NY.New.York','FL.Miami','CA.Los Angeles','MI.Detroit', 'TX.Dallas','IL.Chicago','DC.Washington')) sm$City=droplevels(sm$City) ggplot(data=sm,aes(x=Date,y=IndexValue)) + geom_line(aes(color=City),size=1.5) + scale_x_date("Year", minor="years") + scale_y_continuous("Case Schiller Index")

To leave a comment for the author, please follow the link and comment on their blog: Decision Science News » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)