Journey of Analytics

Deep dive into data analysis tools, theory and projects

Social Network Visualization with R

In this month’s we are going to look at data analysis and visualization of social networks using R programming.

Social Networks – Data Visualization

Friendster Networks Mapping

Friendster was a yesteryear social media network, something akin to Facebook. I’ve never used it but it is one of those easily available datasets where you have a list of users and all their connections. So it is easy to create a viz and look at whose networks are strong and whose are weak, or even the bridge between multiple networks.

The dataset and code files are added on the Projects Page here , under “social network viz”.

For this analysis, we will be using the following library packages:

  • visNetwork
  • geomnet
  • igraph

Steps:

  1. Load the datafiles. The list of users is given in the file named “nodes” as each user is a node in the graph. The connection list is given in the file named “edges” as a 1-to-1 mapping. So if user Miranda has 10 friends, there would be 10 records for Miranda in the “edges” file, one for each friend. The friendster datafile has been anonymized, so there are numbers (id) rather than names.
  2. Convert the dataframes into a very specific format. We do some prepwork so that we can directly use the graph visualization functions.
  3. Create a graph object. This will also help to create clusters. Since the dataset is anonymized it might seem irrelevant, but imagine this in your own social network. You might have one cluster of friends who are from your school, another bunch from your office, one set who are cousins and family members and some random folks. Creating a graph object allows us to look at where those clusters lie automatically.
  4. Visualize using functions specific to graph objects. The first function is visNetwork() which generates an interactive color coded cluster graph. When you click on any of th nodes (colored circles), it will highlight all the connections radiating from the node. (In the image below, I have highlighted node for user 17. nwk-viz-highlight
  5. You can also use the same function with a bunch of different parameters, as shown below:

In the image below you can see the 3 colored clusters and the central (light blue) node. The connections in blue are the ones that do not have a lot of direct connections. The yellow and red clusters are tigher, indicating they have internal connections with each other. (similar to a bunch of classmates who all know each other)

network clusters
network clusters

That’s it. Again the code is available on the Projects Page.

Code Extensions

Feel free to play around with the code. One extensions of this idea would be to download Facebook or LinkedIn data (premium account needed) and create similar visualizations.

Or if you have a list of airports and routes, you could create something like this as a flight network map, to know the minimum number of hops between 2 destinations and alternative routes.

You could also do a counter to see which nodes have the most number of friends and increase the size of the circle. This would make it easier to view which nodes are the most well-connected.

Of course, do not be over-mesmerized by the data. In real-life, the strength of the relationship also matters. This is hard to quantify or collect, even though its easy to depict once you have the data in hand/ For example, I have a 1000 connections who I’ve met at conferences or random events. If I needed a job, most may not really be useful. But my friend Sarah has only 300 but super-loyal friends who literally found her a job in 2 days when she had to move back to her hometown to take care of a sick parent.

With that thought, do take a look at the code and have fun coding! 🙂

PHLAI – Philly Artificial Conference Summary

I had the opportunity to attend the third annual PHLAI 2019 conference on Artificial Intelligence and Machine Learning. Organized by Comcast, this was an amazing full-day event (on Aug 15th) with a theme of using AI to improve customer experience.

ai-cust-service
AI for customer service

For those who are not in the Greater Philadelphia area, I have to point out that most AI/Machine learning or even tech conferences typically happen either on the West Coast or in NYC. So the cost of travel/lodging can quickly add up if you want to attend as an individual.

Always been a point of contention to me that there aren’t any interesting conferences held in the Philly area. After all, we are home to a large number of startups (RJMetrics, Blackfynn, Stitch, Seer Interactive), Fortune500 employers like Nasdaq, Comcast, Vanguard and most of the big banks (JPMorgan, Barclays, etc.)

So here is a big shoutout to Comcast (esp. Ami Ehlenberger) for making the magic happen in Philly!

PHLAI 2019 Conference tracks and schedule

Coming back to the conference, here is a summary of my takeaways of the event:
1) Keynote – morning & evening
2) Speaker & Workshop Sessions – DataRobot, H20, Humane AI, Tableau and more..
3) Networking
4) Key Takeaways
5) Overall

1. Keynote

There were 2 keynotes – morning and afternoon. Both were brilliant, and engaging and filled with useful information.

Morning Keynote

This was given by Nij Chawla of Cognitive Scale. Snippets from his talk that resonated most with me:

  • Practical tips on how to use AI to create individual customization, especially to improve conversions on business websites.
  • For getting most ROI on AI investments, begin with smaller AI projects and deploy/iterate. Most projects fail because they aim at initiating a massive overhaul that take so long to move to production, that half the assumptions and data become invalid. Divide and conquer.
  • Don’t use AI just for the sake of it.
  • Real time data is crucial. 360 degrees view by aggregating multiple sources will help to drive maximum effectiveness, especially in banking and finance.
Morning Keynote – Cognitive Scale

He also presented some interesting examples of AI projects that are already in use today in various domains:

  • Early screening of autism & parkinson using front camera of phone. Apparently the typing/ tapping on phone is different for such patients and when combined with facial recognition technology, the false positives have been very low.
  • Drones in insurance. One company used drones to take pictures to assess damage after hurricane Harvey and proactively filed claims on behalf of their customers. This meant that the papaperwork and claim money was released for affected customers. Home repair contractors are in heavy demand after such natural disaster but the customers from this company had no trouble getting help because everything was already set up! What better example of extraordinary customer service than helping your customers re-build their life and homes? WOW!
  • Drone taxi in Dubai. Basically a driverless helicopter. Scary yet fascinating!
  • VR glasses for shopping. Allows customers to interact with different products and choose color/ design/ style. Ultimate customization without the hassles of carrying gigantic amounts of inventory.

Go back to Top of Page

Evening Keynote

This was presented by Piers Lingle & David Monnerat from Comcast. They spoke about the AI projects implemented at Comcast including projects to proactively present options to customers that increase customer experience, reduce call center volumes and complaints. I loved the idea of using automation to present 2-way dialog using multiple means of communication. The also spoke about chatbots and strategies that help chatbots learn at scale.

Using AI to augment customer service & wow them!

Some other salient points:

  • Automation is a part of the experience, not an add-on or an extra bother
  • Voice remote for Comcast TV users allows customers to fast forward for commercial breaks. Cool! 🙂
  • Think beyond NLP. AI works better in troubleshooting tasks when customers are struggling with describing problems, for example troubleshooting internet connectivity issues.
  • Measure from the beginning . Measure the right thing. Easy to say, hard to do.
  • Done correctly, AI does seem like magic. However, it takes a lot of work, science and complexity. So appreciate it! 🙂 Above all, trust the tech experts hired for this role instead of pigeoning them with hyper-narrow constraints but high expectations.

Go back to Top of Page

2. Speaker & Workshop sessions

The afternoon had 3 different tracks of speaker sessions with 3 talks each – Product, Customer Service & Real-World Experience. Attendees were allowed to attend talks from all 3 tracks which was what I did. The speaker lineup was so spectacular, I noticed a lot of folks being spoilt for choice and scratching their heads after every talk, on where to go! 🙂

I am only writing about the ones I attended. However, I did receive positive feedback overall for all the other sessions too! (No offense to the folks who presented but are not mentioned on here! )

Workshop by Datarobot.

This is an interesting software that helps you explore data, automate data cleansing and most important test by running multiple models in parallet. So you could test with hundreds of models and algorithms. The workshop was presented by the VP of DataScience, Gourab De and was extremely appealing.

It does have features for tweaking parameters, and excellent documentation so personally I think it would greatly augment the work of an experienced data scientists. Given the shortage of talent, tools like these would allow smaller teams to become faster and more efficient.

The availability of logs is another fabulous feature for banks (since I work in one) due to the stringent regulatory requirements around compliance, ethics and model governance.

Chaos Engineering & Machine Learning

Presented by Sudhir Borra of Comcast itself. The talk was mainly about the concept of chaos engineering; used to test resiliency of systems. This is quite crucial for serverless AI and automated CI/CD pipelines when models are tested and deployed in the cloud. This was quite technical yet practical to teams and companies looking to implement robust AI systems within their organizations.

Chaos Engg – initially developed at Netflix

Data Stories Tableau Session

Presented by Vidya Setlur of Tableau, who spoke of some interesting features with Tableau. For example, did you know Tableau gives logical color palettes where it makes sense, rather than to default colors? For example for a dataviz on vegetables, brocolli is assigned color green, whereas carrots would be assigned orange. She also spoke about “stroop” effect, which I urge every data scientist or Tableau developer to look up and understand.

There were also some interesting Qs about expanding the natural langauge questions to be activated via voice. The answer was that it is currently not available because the folks who would truly find this useful are senior executives who would possibly only use it over phone (not desktop) and hence also require touchscreen capabilities for using drill-down features.

This was a perspective I had not thought about, so I was quite impressed at how far the company has thought about features users might want far into the future. Like Apple, Tableau’s elegant design, ease of use and intuitive features make it a class apart in terms of functionality and customer experience.

The Humane Machine: Building AI Systems with Emotional Intelligence

Extremely engaging talk by Arpit Mathur of Comcast on some interesting work that Comcast is doing.

He also spoke of LIWC library – linguistic inquiry and word count and how to apply those on natural langauge data to gauge sentiments, similar to the Plutchiks wheel of emotions. Also on how to use these for creating more “sentient” chatbots and applications, since the context matters as much as words when we (humans) communicate. For example, how some cultures use words literally, whereas others could use it to mean the exact opposite!

He also discussed ethics in AI, which naturally was a “hot” topic and sparked multiple questions and discussions.

Handson Labs – H20 – Automated AI/ML

I’d heard many rave reviews about this product, so it was wonderful to be able to attend a handson workshop on how to use the tool, available features and check effectiveness of the models it can generate. Personally I found it to have just the right mix of automated model generation (great for newbies) and model customization (for advanced users).

Go back to Top of Page

3. Networking

The conference included two rounds of networking time, once with free breakfast, and second at 5:15 pm in a formal networking reception. Both were great opportunities to meet with other attendees and speakers.

I was not attending with any aim for job hunting, but I did see a list of open roles at Comcast. Plus, I did see folks talking about roles in their org, and describing their work, so certainly the event was an excellent venue to have informational interviews and reach out for potential collaboration.

Not to mention the new contacts will be useful in the future, should I choose to look.

I met a wide variety of folks – from students to software developers to product leads and even senior engineering leaders who wanted to learn best practices on how best to get ROI on future AI investments. There were folks who were just interested in learning more about AI, some looking for their next big idea and everyone in between. It was not just tech folks who attended, and folks were attending from domains like adtech, marketing, automobiles, healthcare and of course banking.

Amazing to see the turnout and community. Overall, I think job seekers should attend such events!

Oh, and I have to mention the gorgeous Comcast building – amazing working space and the central atrium was both futuristic and functional. I definitely regretted not taking the Comcast tour! (In fairness, the workshop I attended was fabulous too! )

Of course there was also time to chitchat between sessions and I am thankful to everyone who interacted with me! Thanks for your sparkling conversations.

Networking opportunities galore

Go back to Top of Page

4. Key Takeways

  • Focus on the customer and the end goal. Technology is only a tool to enable better outcomes and efficiencies. AI tools have matured, so make full use of it.
  • For the best return and impacts from AI projects, quick action is key. Choose a specific usecase, choose a metric to evaluate success and then proceed. If the model works, BRAVO! If they do not, you get valuable feedback on your initial assumptions, so you can make better decisions in the future.
  • Clean, reliable data is paramount. Garbage in, garbage out!
  • Collaboration is important. You do need to get the blessings from multiple stakeholders like IT, business, product, customer service, etc. But do not let collaboration equate to decision paralysis.
  • Done right, AI can be magical and live up to the hype. The critical component is getting started.
  • Data and user preferences change quickly, so think about automating model deployments and/or implementing CI/CD using technologies like kubernetes or the myriad tools and software available on the market to get to production quicker.
  • There is a huge talent shortage of experienced machine learning and artificial intelligence developers, so think about “upskilling” existing employees who already know the business and customer pain points.

Go back to Top of Page

5. Overall

Overall I found the event to be incredibly useful and I plan to attend again next year. I am also looking forward to use the takeaways in my own role and evangelize AI products within my org.

Go back to Top of Page

DataScience Portfolio Ideas for Students & Beginners

A lot has been written on the importance of a portfolio if you are looking for a DataScience role. Ideally, you should document your learning journey so that you can reuse code, write well-documented code and also improve your data storytelling skills.

DataScience Portfolio Ideas

However, most students and beginners get stumped on what to include in their portfolio, as their projects are all the same that their classmates, bootcamp associates and seniors have created. So, in this post I am going to tell you what projects you should have in your portfolio kitty, as well as a list of ideas that you can use to construct a collection of projects that will help you stand out on LinkedIn, Github and in the eyes of prospective hiring managers.

Job Search Guide

You can find many interesting projects on the “Projects” page of my website JourneyofAnalytics. I’ve also listed 50+ sources for free datasets in this blogpost.

In this post though, I am classifying projects based on skill level along with sample ideas for DIY projects that you can attempt on your own.

On that note, if you are already looking for a job, or about to do so, do take a look at my book “DataScience Jobs“, available on Amazon. This book will help you reduce your job search time and quickly start a career in analytics.

Since I prefer R over Python, all the project lists in this post will be coded in R. However, feel free to implement these ideas in Python, too!

a. Entry-level / Rookie Stage

  1. If you are just starting out, and are not very comfortable with even syntax, your main aim is to learn how to code along with DataScience concepts. At this stage, just try to write simple scripts in R that can pull data, clean it up and calculate mean/median and create basic exploratory graphs. Pick up any competition dataset on Kaggle.com and look at the highest voted EDA script. Try to recreate it on your own, read through and understand the hows and whys of the code. One excellent example is the Zillow EDA by Philipp Spachtholz.
  2. This will not only teach you the code syntax, but also how to approach a new dataset and slice/dice it to identify meaningful patterns before any analysis can begin.
  3. Once you are comfortable, you can move on to machine learning algorithms. Rather than Titanic, I actually prefer the Housing Prices Dataset. Initially, run the sample submission to establish a baseline score on the leaderboard. Then apply every algorithm you can look up and see how it works on the dataset. This is the fastest way to understand why some algorithms work on numerical target variables versus categorical versus time series.
  4. Next, look at the kernels with decent leaderboard score and replicate them. If you applied those algorithms but did not get the same result, check why there was a mismatch.
  5. Now pick a new dataset and repeat. I prefer competition datasets since you can easily see how your score moves up or down. Sometimes simple decision trees work better than complex Bayesian logic or Xgboost. Experimenting will help you figure out why.

Sample ideas –

  • Survey analysis: Pick up a survey dataset like the Stack overflow developer survey and complete a thorough EDA – men vs women, age and salary correlation, cities with highest salary after factoring in currency differences and cost of living. Can your insights also be converted into an eye-catching Infographic? Can you recreate this?
  • Simple predictions: Apply any algorithms you know on the Google analytics revenue predictor dataset. How do you compare against the baseline sample submission? Against the leaderboard?
  • Automated reporting: Go for end-to-end reporting. Can you automate a simple report, or create a formatted Excel or pdf chart using only R programming? Sample code here.

b. Senior Analyst/Coder

  1. At this stage simple competitions should be easy for you. You dont need to be in the top 1%, even being in the Top 30-40% is good enough. Although, if you can win a competition even better!
  2. Now you can start looking at non-tabular data like NLP sentiment analysis, image classification, API data pulls and even dataset mashup. This is also the stage when you probably feel comfortable enough to start applying for roles, so building unique projects are key.
  3. For sentiment analysis, nothing beats Twitter data, so get the API keys and start pulling data on a topic of interest. You might be limited by the daily pull limits on the free tier, so check if you need 2 accounts and aggregate data over a couple days or even a week. A starter example is the sentiment analysis I did during the Rio Olympics supporting Team USA.
  4. You should also start dabbling in RShiny and automated reports as these will help you in actual jobs where you need to present idea mockups and standardizing weekly/ daily reports.
Yelp College Search App

Sample ideas –

  • Twitter Sentiment Analysis: Look at the Twitter sentiments expressed before big IPO launches and see whether the positive or negative feelings correlated with a jump in prices. There are dozens of apps that look at the relation between stock prices and Twitter sentiments, but for this you’d need to be a little more creative since the IPO will not have any historical data to predict the first day dips and peaks.
  • API/RShiny Project: Develop a RShiny dashboard using Yelp API, showing the most popular restaurants around airports. You can combine a public airport dataset and merge it with filtered data from the Yelp API. A similar example (with code) is included in this Yelp College App dashboard.
  • Lyrics Clustering: Try doing some text analytics using song lyrics from this dataset with 50,000+ songs. Do artists repeat their lyrics? Are there common themes across all artists? Do male singers use different words versus female solo tracks? Do bands focus on a totally different theme? If you see your favorite band or lead singer, check how their work has evolved over the years.
  • Image classification starter tutorial is here. Can you customize the code and apply to a different image database?

c. Expert Data Scientist

DataScience Expert portfolio
  1. By now, you should be fairly comfortable with analyzing data from different datasource types (image, text, unstructured), building advanced recommender systems and implementing unsupervised machine learning algorithms. You are now moving from analyze stage to build stage.
  2. You may or may not already have a job by now. If you do, congratulations! Remember to keep learning and coding so you can accelerate your career further.
  3. If you have not, check out my book on how to land a high-paying ($$$) Data Science job job within 90 days.
  4. Look at building Deep learning using keras and apps using artificial intelligence. Even better, can you fully automate your job? No, you wont “downsize” yourself. Instead your employer will happily promote you since you’ve shown them a superb way to improve efficiency and cut costs, and they will love to have you look at other parts of the business where you can repeat the process.

Sample project ideas –

  • Build an App: College recommender system using public datasets and web scraping in R. (Remember to check terms of service as you do not want to violate any laws!) Goal is to recreate a report like the Top 10 cities to live in, but from a college perspective.
  • Start thinking about what data you need – college details (names, locations, majors, size, demographics, cost), outlook (Christian/HBCU/minority), student prospects (salary after graduation, time to graduate, diversity, scholarship, student debt ) , admission process (deadlines, average scores, heavy sports leaning) and so on. How will you aggregate this data? Where will you store it? How can you make it interactive and create an app that people might pay for?
  • Upwork Gigs: Look at Upwork contracts tagged as intermediate or expert, esp. the ones with $500+ budgets. Even if you dont want to bid, just attempt the project on your own. If you fail, you will know you still need to master some more concepts, if you succeed then it will be a superb confidence booster and learning opportunity.
  • Audio Processing: Use the VOX celebrity dataset to identify the speaker based on audio/speech dataset. Audio files are an interesting datasource with applications in customer recognition (think bank call centers to prevent fraud), parsing for customer complaints, etc.
  • Build your own package: Think about the functions and code you use most often. Can you build a package around it? The most trending R-packages are listed here. Can you build something better?

Do you have any other interesting ideas? If so, feel free to contact me with your ideas or send me a link with the Github repo.

Mapping Anthony Bourdain’s Travels

Travel maps tutorial

Anthony Bourdain was an amazing personality – chef, author, world traveler, TV showhost. I loved his shows as much for the exotic locations as for the yummilicious local cuisine. So I was delighted to find a dataset that included all travel location data, from all episodes of his 3 hit TV shows. Dataset attributed to Christine Zhang for publishing the dataset on Github.

In today’s tutorial, we are going to plot this extraordinary person’s world travels in R. So our code will cover geospatial data mapping using 2 methods:

  • Leaflets package to create zoomable maps with markers
  • Airplane route style maps to see the paths traveled.

Step 1 – Prepare the Workspace

Here we will load all the required library packages, and import the dataset.

places <- data.frame(fread(‘bourdain_travel_places.csv’), stringsAsFactors = F)

Step 2 – Basic Exploration

Our dataset has data for 3 of Bourdain’s shows:

  • No Reservations
  • Parts Unknown – which I personally loved.
  • The Layover

Let us take a sneak peak into the data:

dataset preview

How many countries did Bourdain visit? We can calculate this for the whole dataset or by show:

numshow <- sqldf(“select show , count(distinct(country)) as num_ctry from places group by show”) # Num countries by show.

numctry <- nrow(table(places$country)) # Total countries visited
numstates <- nrow(table(places$state[places$country == ‘United States’])) ## Total states visited in the US.

Wow! Bourdain visited 93 countries overall, and 68 countries for his show “No Reservations”. Talk about world travel.

I did notice some records have state names as countries, for example California, Washington and Massachussets. But these are exceptions, and overall the dataset is extremely clean. Even disregarding those records, 80+ countries is nothing to be scoffed at, and I had never even heard of some of these exotic locations.

P.S.: You know who else gets to travel a lot? Data scientists earning $100k+ per year. Here’s my new book which will help you how to land such a dream job.

Step 3 – Create a Leaflet to View Sites on World Map

Thankfully, the data already has geographical coordinates, so we don’t need to add any processing steps. However, if you have cities which are missing coordinates then use the “worldcities” file from the Projects page under “Rent Analysis”.

We do have some duplicates, where Bourdain visited the same location in 2 or more shows. So we will de-duplicate before plotting.

Next we will add an info column to list the city and state name that we can use on the marker icons.

places4$info <- paste0(places4$city_or_area, “, “, places4$country) # marker icons

mapcity <- leaflet(places4) %>%
setView(2.35, 48.85, zoom = 3) %>%
addTiles() %>%
addMarkers(~long, ~lat, popup = ~info,
options = popupOptions(closeButton = T),
clusterOptions = markerClusterOptions())
mapcity # Show the leaflet

leaflet view – the markers are interactive in R

Step 4 – Flight Route View

Can we plot the cities in flight view style? Yes, we can as long as we transform the dataframe where each record has a departure and arrival city. We do have the show and episode number so this is quite easy.

Once we do that we will use a custom function which basically plots a circle marker at the two cities and a curved line between the two.

plot_my_connection=function( dep_lon, dep_lat, arr_lon, arr_lat, …){
inter <- gcIntermediate(c(dep_lon, dep_lat), c(arr_lon, arr_lat), n=50, addStartEnd=TRUE, breakAtDateLine=F) inter=data.frame(inter) diff_of_lon=abs(dep_lon) + abs(arr_lon) if(diff_of_lon > 180){
lines(subset(inter, lon>=0), …)
lines(subset(inter, lon<0), …)
}else{
lines(inter, …)
}
} # custom function

For the actual map view, we will create a background world map image, then use the custom function in a loop to plot each step of Bourdain’s travels. Depending on how we create the transformed dataframe, we can plot Bourdain’s travels for a single show, single season or all travels.

Here are two maps separately for the show “Parts Unknown” and “The Layover” respectively. Since the former had more seasons, the map is a lot more congested.

Parts Unknown seasons – travel maps

par(mar=c(0,0,0,0)) # background map
map(‘world’,col=”#262626″, fill=TRUE, bg=”white”, lwd=0.05,mar=rep(0,4),border=0, ylim=c(-80,80) ) # other cols = #262626; #f2f2f2; #727272
for(i in 1:nrow(citydf3)){
plot_my_connection(citydf3$Deplong[i], citydf3$Deplat[i], citydf3$Arrlong[i], citydf3$Arrlat[i],
col=”gold”, lwd=1)
} # add every connections:
points(x=citydf$long, y=citydf$lat, col=”blue”, cex=1, pch=20) # add points and names of cities
text(citydf$city_or_area, x=citydf$long, y=citydf$lat, col=”blue”, cex=0.7, pos=2) # plot city names

As always, the code files are available on the Projects Page. Happy Coding!

Call to Action:

If you read this far and also want a job or promotion in the DataScience field, then please do take a look at my new book “Data Science Jobs“. It will teach you how to optimize your profile to land great jobs with high salary; 100+ interview Qs and niche job sites which everybody else overlooks.

Email Automation for Google Trends

This blogpost will teach you set up automated email reports to view how search volumes i.e. Google Trends vary over time.

Email automation for Google Trends over time

The email report will also include important search terms that are “rising” or near a “breakout point”. This can be really useful as the breakout keywords indicate users across the globe have recently started paying great attention to these search terms. So you could create original content to attract people using these phrases in their Google searches. This is an amazing way to boost traffic/ sales leads, etc. by taking advantage of new trends.

Note, that the results are relative, not absolute search volume terms. But it is still quite useful, as you could narrow down your keywords list, and login to Adsense/ keyword planner to get the exact search volume. Export the list from this script to a .csv and simply upload in Adsense. It also gives you a clear indication of how search volumes compare across similar terms – classic SEO !

We will perform all our data pull requests and manipulations in R. The best part is that you do not need any API keys or logins for this tutorial!

[ Do you know what else is trending? My book on how to get hired as a datascientist. Check it out on Amazon, where it is the #1 NEW RELEASE and top 10 in its categories. Whether you are currently enrolled in a data science course or actively job searching, this book is sure to help you attract multiple job offers in this lucrative niche. ]

Without further interruptions – let’s dive right in to the trend analysis.

Step 1 – Load Libraries

Start by loading the libraries – “gtrends” is the main library package which will help us pull data for search results.

library (gtrendsR)
library(ggplot2)
library(plotly)
library(dplyr)
library(sqldf)
library(reshape2)

Step 2 – Search terms and timeframe

We need to specify the variables we need to pass to the search query:

  • search terms of interest. Unfortunately, you can only pass 5 terms at one time. Theoretically you could store the value and repeat the searches, but since numbers are relative, make sure that you keep at least one term in common and then normalize.
  • Remember, we get relative search volumes (max = 100); NOT absolute volumes!
  • timeframe under consideration, has to be in the format “YYYY-mm-dd” ONLY. No exceptions. End date = today, whereas start date will be the the first of the month, six months ago.
  • country codes – I’ve only used “US”, but you can search for multiple countries using a string array like c(“US”, “CA”, “DE”) which stands for US, Canada and Germany.
  • Channels – default is “web”, but you can search for “new” or “images”. Images would be good for those promoting content in Pinterest or Instagram.

date6mthpast <- Sys.Date() – 180
startdate <- paste0( substr(as.character(date6mthpast), 1,7), “-01”, sep = “”)
currdate = as.character(Sys.Date())
timeframe = paste( startdate, currdate)
keywords <- c(“Data Science jobs”, “MS Analytics Jobs”, “Analytics jobs”,
“Data Scientist course”, “job search”)
country=c(‘US’)
channel=’web’

We will use the gtrends() function to pull the data. This function returns a list of variables and dataframes. Of main interest to us is the “interest_over_time” data frame, “interest_by_region” (state names mostly). There is also a dataframe named “interest_by_city”. The first one does hold data with date value, while the others do not, so it will be aggregated over the entire data frame. I worked around this by pulling once for 6 months, and again for the last 45 days.

trends = gtrends(keywords, gprop =channel,geo=country, time = timeframe )

Step 3 – Search Volume over time

We will use the ggplot function to get a graphical representation of the search volume.

ggplot(data=time_trend, aes(x=date, y=hitval, group=keyword, col=keyword))+
geom_line()+xlab(‘Time’)+ylab(‘Relative Interest’)+ theme_bw() +
theme(legend.title = element_blank(), legend.position=”bottom”, legend.text=element_text(size=12)) +
ggtitle(“Google Search Volume over time”)

Google search volume over time
Google trends over time

Notice that the interest in term “job search” is significantly higher than any other keyword and spike in April and May (people graduating maybe?) This makes sense as it is a more generic term, and applies to larger number of users. On a similar vein, look at the volumes for “MS analytics jobs” . It is almost nil, so clearly a non-starter in terms of targeting.

Step 4 – Interest by Regions

Let us look at interest by region (state names in the US ) . This might be useful to target folks by region, or even location-based ads.

locsearch = trends$interest_by_region
plot2 <- ggplot(subset(locsearch, locsearch$keyword != “job search”),
aes(x=as.factor(location), y= hits, fill=as.factor(keyword) )) +
geom_bar(position=”dodge”, stat=”identity”) +
coord_flip() +
theme(legend.title = element_blank(), legend.position=”bottom”, legend.text=element_text(size=9)) +
xlab(‘Region’)+ ylab(‘Relative Interest’) +
ggtitle(“Google Search Volumes by Region/State”)

Note, that “data scientist course” has almost zero interest compared to broader terms like “analytics jobs” or “datascience Jobs”. Interestingly, “analytics jobs” seems to be a preferred term over “data science” only in NY, MA and Washington DC.

Step 5 – Trending Terms

The original list also returns a dataframe titled “related_queries”. Some of these can have tags as “rising” or “breakout”. These are the terms we would like to know as they occur.

breakoutdf = reltopicdf[reltopicdf$related_queries == ‘rising’,]
volbrkdf = breakoutdf[1:10,]
row.names(volbrkdf) = NULL
volbrkdf

trending seach terms related to keywords of interest
trending seach terms related to keywords of interest

Step 6 – Create pdf for email attachments

We will use the pdf() command to create an attachment with all the graphs and add to the email we want to send/receive on a daily basis.

filename2 = paste0(“Google Search Trends – “, Sys.Date(), “.pdf”)
pdf(paste0(“Google Search Trends – “, Sys.Date(), “.pdf”))
plot1
plot2
print(volbrkdf)
dev.off()
dev.off()

If you want to embed one of these figures in the email itself, instead of attachment then please see my post on automated reports.

Step 7 – Send the email

We use the RDCOM library to send out emails. You do need to have Outlook mail client on your PC.

library(RDCOMClient)
OutApp <- COMCreate(“Outlook.Application”)
outMail = OutApp$CreateItem(0)
outMail[[“To”]] = “an**@gmail.com”
outMail[[“subject”]] = paste(“Google Search Trend Report -“, Sys.Date())
email_body1 <- “Write email body content with correct html tags”
outMail[[“HTMLBody”]] = email_body1
file_location = paste0(paste0(abs_path,”/”),filename2)
outMail[[“Attachments”]]$Add(file_location)
outMail
outMail$Send()

The first time you run this, you might need to “allow” RStudio to access your email account. Just add this script to a task scheduler, and choose frequency of delivery, and you are all set! Note, a dummy email address is used, so remember to change the recipient address.

Try it out and let me know if you have any questions, or run into errors. The script is available on the Projects page – link here

Last but not the least, if you are interested in getting hired in a data science field, then please do take at my job-search book. Here is the Amazon link.

Happy Coding!

« Older posts
Facebook
LinkedIn