Data Scientist with a Wine hobby (Part II)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Getting introduced to Google and their API
Previously we constructed a data frame
that consisted out of the following variables that we might want to perform analysis on:
Now, one of the variables in the datasets, will be used to extend the information in our feature set even further. I will use the farms’ names to collect information on the farms’ location, by using an API
made available through Google’s Developer Console. The specifc API
I am refering to here, is their Google Place API and the documentation on the parameters of this call can be found here.
I do recommend having a look as they have quite a few other unique location based API
’s which might be of interest.
To use their api server, one needs to register an account and then set up what is know as a project. If you already have a google identity through something like Gmail, then this setup is a breeze.
Logging onto the website’s developer page, the first step in setting up, is to have a look at the credentials tab. Here you can see I have set up a server which I with a unique key I will use to call there API service:
One of the important steps in using google’s API
, is that you have to white list the ip’s which will be calling the API
service. For me you can see at the bottom of the picture, that I have worked on this project from 2 different locations.
You will also see in the insert the credential key which we will be using as part of the API
service. This key is important when you are doing the call to their server as a credential check. Be aware you only have a limited amount of calls per day as a free user.
So, having set up the server, I am now ready to head into R to create the function that will retreive the GPS coordinates of the wine farms extracted and enrich our dataset’s features. The library that I will be relying on the most here is the RJSONIO
. The reason for this is that the query will return the information in a json format.
The function takes 2 parameters, the first being the vector of place names to look up. The second parameter is useful to see which place you are doing a coordinate lookup on. The default is set to FALSE
as one generally knows the vector you are inputting.
My function is now ready to use, so I create the vector of unique wine farm names as this will serve as our input. I first filter the wine database to just include reviews from 2013 onwards. This is just to make sure we don’t work with too outdated information.
If our server setup was done correctly, the following code will now retreive the place name’s coordinates
Now, lets add these coordinates to our dataframe
. This can obviously be placed within the pipe in the previous step, but for illustrative purposes I seperate these 2 steps.
Using ggmap to visualise our data
When chosing to go wine tasting, a person commonly confronted with a problem known as the traveling salesman problem. In our case, it is trying to figure out the shortest route that will result in the best wine tastings.
My aim here is to plot the wine farm locations I have collected to try and see if there are clusters or pockets of wine farms where good rated wines are prevelent. So, with this idea in mind I need to create a dataset that contains the farms, their average wine ratings and the coordinates.
Now that I have that ready, I go and get a map of stellenbosch using the very handy ggmap
package.
Next I start exploring the functions of ggmap and I add my markers to the map as a layer. I specify that the size of the points need to relate the the average wine rating at that farm. I also choose that my color should be a gradient following the same idea.
Ok, so from this map we can see that the dots are sort of scattered all over the map with a potential “good wine” cluster in the bottom left corner out on the M12. To confirm this, I construct a heatmap over the points of the map. The heatmap is weighted for the wine region’s average rating, not the amount of points within the area
If we look at the map now, a much clearer picture can be seen. There are indeed two avenues when going on a wine tour in Stellenbosch. You can either take the M12 out, or the R44.
Finally, one of the things that I am most interested in is to see whether this particular wine region, in the bottom left corner, has always produced wine of top quality. It might just be that from 2013 upwards the region was lucky in terms of climat. The image belows shows a heatmap of the previous years from 2001 to 2015, tha captures the top rated wine farms each year.
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.