Justin

Rent-scraping: How I saved money using R

(TD;DR: Received letter from real estate increasing my weekly rent. Reason for increase was not supported by evidence. Used R to look for evidence supporting real estate’s claim. Fought the power.)

Gather ‘round, gather ‘round. Come hear a story of one man’s triumph against the establishment. Read in disbelief as he goes to great lengths to avoid paying a $5 per week rent increase on the grounds that it could not justified by empirical evidence.

(On a serious note: this is about webscraping in R)

“…based on the current market conditions…”

The 12 month lease for my current apartment was coming to an end. Just when I was wondering when the annual email from my real estate agent offering me a new 12 month lease was going to arrive, I received this:

You are currently leasing your property for $500.00 per week and the lease is due to expire on the 18/11/2017. We have recently completed a rental review for your property and advised the owner that based on current market conditions, the rent for your property should increase by $5 per week.

Craving specificity, my mind goes into overdrive: What market conditions? How have they defined them? $5 is so little - what change in market conditions could justify a $5 per week increase?

Having had my rent increased by $20 per week a year ago, a further $5 increase didn’t seem to make sense to me. I also perceived a power imbalance which I didn’t like. On the one hand, we have the real estate agent, who has access to databases of property prices and reports. On the other hand, we have the tennant who does not have access to the same amount of information. This information asymmetry would have traditionally put the tennant in a weaker position and thus more likely to accept the advice of the real estate agent.

Unfortunately for my real estate agent, I’m a little bit crazy.

Where the data at?

I live in a two bedroom apartment with a car space and a single bathroom. I wanted to look for data on apartments with a similar configuration.

NSW Government’s Family & Community Services - “Rent Tables”

I knew that the NSW Government publishes reports on newly rented out houses and apartments on a quarterly basis. There’s a tab in report that shows the first quartile, median and third quartile of the distribution of weekly rent paid for one and two bedroom apartments.

This, of course, isn’t precise enough for me. I know that my rent would more than likely be cheaper if I had no car space. I also know that my rent would be more expensive if I had two bathrooms. So I continue my quest.

Gimme some’in’ to scrape

After some Googling, I found AuHousePrices. Take a look at this screenshot:

It has all the info that I need:

the weekly rent,

the month in which the apartment was leased

the apartment configuration in terms of the number of bedrooms, bathrooms and car spaces

Another attractive quality of this site was the loop-friendly URL. This is the URL for the first page:

All hail Hadley: R’s rvest package

rvest is a web scraping R package by R royalty, Hadley Wickham. Like most of Hadley’s packages, rvest is simple to use with its intuitive syntax. The package is built to work with the piping operator from magrittr (which users of the dplyr will be familar with), allowing us to create elegant data munging pipelines.

Getting started with rvest is easy:

Find a page you want to scrape

Get that page’s url

Read that page into R using rvest’s read_html() function

Access the CSS selector containing the data you want using either the html_node() or html_nodes() functions (more on finding the relevant selector and the difference between the functions, below).

Access the text contained within the CSS selector(s) extracted in the prior step using the html_text() function.

Use regex to extract relevant text within the CSS selector text accessed in the prior step.

How can we find out which CSS selector contains the data we’re after?

CSS selectors with SelectorGadget

SelectorGadget is a Google Chrome extension that allows you to quickly discover which CSS selector contains the data you’re targeting by simply clicking on it. You no longer have to dig through HTML in your browser’s developer tools. It’s a huge time saver.

Install it…now!

Let’s get-a-scrapin’

We now have all the ingredients necessary to scrape data from websites using R. The output that I ultimately want to create is a time series of weekly rent per the months in which the apartments were leased. I want to make sure that my scraping is working as intended so I want to extract the addresses of the apartments so that I can visually inspect my data against a sample of webpages in Chrome.

Target: the addresses

Go into Chrome, and click on the SelectorGadget icon in the top right hand corner. Click on one of the addresses, and you should see something like this pop up in the bottom right hand corner of your browser.

This is the CSS selector that contains the address data. To access the data contained within this selector, we can pipe the page into html_nodes(), specifying the CSS selector name as its argument. To access the text contained within, we can pipe this result into html_text().

Target: the rent per week and month leased

Clicking on the weekly rent and the month in which the apartments were leased presents us with a challenge. The selector that contains the data we’re after (“li”) is shared by a whole bunch of other data. Take a look at the stuff that is highlighted:

Let’s take a look at the first few elements of what we can extract from the “li” selector:

We know the pattern of text we want to extract. It goes something like this:

Rent $###/week on Mmm yyyy

Let’s use some regex and the str_extract() function from the stringr package (all Hail Hadley, again) to extract any text matching this pattern in our vector of elements in the “li” selector. Note that str_extract() returns a vector of the same length as the input with NAs for the elements that didn’t match our pattern. We we will filter out the NAs as a final step.

What are all those symbols? A regex to English translation

My claim is that the above pattern can be expressed in regex like this:

^Rent.+/week.*\\d{4}$

Don’t be afraid. It looks crazy, but all this is saying is this:

^ - we should match what follows from the beginning of the target string.

Rent.+ - look for the word “Rent” followed by one or more of any character. The period is a special character in regex which means “match any character”. The plus sign means “one or more” of whatever immediately preceeded it.

/week.* - look for “per week”, followed by zero or more of any character. The zero or more is expressed using the asterisk, which is the less-strict cousin of the plus sign.

\\d{4} - the target string should end with 4 digits. “” is another special character in regex which means “match any digit from 0 to 9”. The extra slash is there because we need to escape the backslash in R. “{4}” means “repeat what came immediately before 4 times”. In other words, match 4 digits.

$ - the target string should end with what came immediately before me. In this case, 4 digits.

Woohoo! We have 12 sets of data for the 12 addresses. We can easily break this string apart using more regex to extract the month and the rent per week. I’ll show you how I did it, but try to break it down like I did above and all those symbols will appear less daunting. Firstly, extracting the month…

str_extract(price_month,'[A-Z][a-z]{2} \\d{4}$')%>%head(5)

## [1] "Aug 2018" "Aug 2018" "Aug 2018" "Aug 2018" "Aug 2018"

…and then the rent per week:

str_extract(price_month,'(?<=Rent \\$).*(?=/week)')%>%head(5)

## [1] "420" "510" "490" "500" "550"

Target: the property configuration

We set out wanting to analyse apartments similar to mine. That is, we want to analyse apartments with 2 bedrooms, 1 bathroom and 1 car space. A look at the search results reveals to us that we need to cater for 2 scenarios:

most properties have at least one bathroom and at least one car space, so we will have 3 numbers - number of bedrooms, number of bathrooms, and number of car spaces.

some properties don’t have a car space. So we will have 2 numbers - number of bedrooms, followed by number of bathrooms.

I want to capture both patterns so that I have 12 sets of apartment configurations that they can be easily combined with the sets of 12 addresses, months and weekly rent amounts. Once they have been combined into a data frame, I can easily filter out those apartments that don’t meet my target apartment configuration.

Form like Voltron: creating our final dataset

So, what have we learnt?

We know what the urls for the first n pages are.

We know how to extract the data of interest on any of these n pages.

All we need to do before we can analyse our data is to write a loop that loops through n pages of apartments while appending each page’s data onto some master dataset. We will then subset this master dataset for apartments that match our desired apartment configuration. Describing this this process is not the focus of this article so I will simply provide my final code at the end of this article.

Dear real estate agent…

Newer apartments cost more than older apartments to rent - a quick look at the photos of some of the more expensive apartments revealed that they were located in newly constructed apartments right near the train station.

Apartments closer to the train station and shopping district are more expensive to rent than those that are further away from these.

My apartment is quite old, and about 10 minutes away from the train station. It is by no means exceptional so I felt that a “typical” price would best describe my apartment’s “true” weekly rent. So I played around with some smoothing methods.

I personally love using LOESS via ggplot’s geom_smooth().

However, I didn’t want to have to explain to my real estate agent how LOESS works. So I settled on a quarterly median rental price. Not as simple as simple as taking an average. But I thought the median was appropriate given the outliers and that it would be easy to explain if I were questioned about my choice of statistic.

So I put this into email:

Rent was increased last year by $20 per week and this appears to be in excess of the market rate at the time (more on this below).

I have performed a review of the data myself and have found that the current rent we pay is fair:

I have used webscraping to collect data on 346 newly rented “two bedroom, one bathroom, one car space” apartments in Ashfield from January-2016 to August-2017. Rent ranges from ~$400 per week for older apartments to +$600 per week for brand new apartments.

Since January 2017, median rent prices appear to be hovering ust above or just below the $500 mark (please see the below chart). In fact, the median rent per week over this period is $495 per week.

I have cross-checked this with the latest Housing NSW Rent and Sales report. The median rent for a 2 bedroom apartment in Ashfield is $500 over the last quarter. It makes sense that the median is slightly higher than my number as Housing NSW data would include apartments with more than one bathroom or more than one car spot.

When our rent was raised to $500 per week last year, median prices were hovering at about $480 per week. I have cross-checked this with the Housing NSW Rent and Sales report for December and can confirm this to be accurate.