{"id":68,"date":"2022-10-24T08:01:26","date_gmt":"2022-10-24T08:01:26","guid":{"rendered":"https:\/\/geopsyresearch.org\/blogs\/?p=68"},"modified":"2022-10-24T08:09:45","modified_gmt":"2022-10-24T08:09:45","slug":"exploring-political-sentiments-from-twitter-data","status":"publish","type":"post","link":"https:\/\/geopsyresearch.org\/blogs\/2022\/10\/24\/exploring-political-sentiments-from-twitter-data\/","title":{"rendered":"Exploring Political Sentiments from Twitter Data"},"content":{"rendered":"<p>By\u00a0<a href=\"https:\/\/www.linkedin.com\/in\/godwin-murithi-847830138\/\" target=\"_blank\" rel=\"noopener\">Godwin Murithi<\/a><\/p>\n<p><span style=\"font-weight: 400;\">Twitter is currently one of the most widely used social networks \u2014 <\/span><span style=\"font-weight: 400;\">It is a real-time microblogging platform, publicly launched in July 2006. <\/span><span style=\"font-weight: 400;\">Twitter has one of the biggest data sets in the world. It is much different from Facebook from the aspect that Twitter is real time.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Twitter datasets are awesome troves of information and provide great insights. It provides a platform where businesses and public persons engage with their audiences, important messages are shared in near-real time and, as the recent events show, even politics is done. Today, Twitter boasts of 450M monthly users globally.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Due to the rich data in the pooled tweets, governments, businesses and researchers have understood the power of twitter data.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">For instance, think of a way to predict the outcome of an election in a country. <\/span><span style=\"font-weight: 400;\">Given the fast-paced and concise format of messages shared, it is a nice tool that could be used to gather signals and sentiments that could affect the outcome of an election. These sentiments could be used to understand the mood of the public and their perceptions towards different candidates.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">In recent studies, researchers have used twitter data to successfully understand, reach, predict and <\/span><a href=\"https:\/\/sci-hub.ru\/https:\/\/www.tandfonline.com\/doi\/abs\/10.1080\/13658816.2020.1719495\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">map violence hotspots during sports events<\/span><\/a><span style=\"font-weight: 400;\">.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">In business, Twitter helps brands to understand, track, and benchmark the conversations and perceptions surrounding the brand. In terms of customer engagement <\/span><span style=\"font-weight: 400;\">it helps directly engage with customers to quickly answer questions, resolve their issues, and provide exceptional service.<\/span><\/p>\n<p><b>Understanding political sentiments<\/b><\/p>\n<p><span style=\"font-weight: 400;\">In this series, we\u2019ll look into the process for extracting, transforming and understanding Twitter data relevant to the Kenya 2022 general election. To access the codes refer to this <\/span><a href=\"https:\/\/github.com\/godwinmurithi\/tweeterapp-\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">Github repository<\/span><\/a><span style=\"font-weight: 400;\">\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">To access tweets we developed scripts in Python 3.7, <\/span><a href=\"https:\/\/www.tweepy.org\/\"><span style=\"font-weight: 400;\">Tweepy<\/span><\/a><span style=\"font-weight: 400;\">\u00a0 and a couple of other libraries. <\/span><span style=\"font-weight: 400;\">Tweepy is an open-source python package that provides a way for developers to communicate with the Twitter API.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Twitter levies a rate limit on the number of requests made to the Twitter API. To be precise, 900 requests\/15 minutes are allowed; Twitter feeds anything above that an error<\/span><\/p>\n<p><span style=\"font-weight: 400;\">But first, you will need to obtain the necessary credentials from Twitter. <\/span><span style=\"font-weight: 400;\">These credentials from Twitter are used to instantiate the API.<\/span><span style=\"font-weight: 400;\">\u00a0<\/span><\/p>\n<p><b>Mapping geographic distribution of tweets<\/b><\/p>\n<p><span style=\"font-weight: 400;\">To carry out spatial analysis on the data, it is necessary to add a location component to the individual tweets. This helps in mapping the distribution of tweets as well as classified sentiments. To achieve this, you can use the google maps API or Nominating API to geocode.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Usually the google maps API has a limit of 2500 addresses per day. Nominatim is preferable since it is open and free. Additionally, there is no limit to the number of addresses you can geocode in a day.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Due to the high number of tweets that you can scrap it is advisable to automate the process. The Tidygeocoder package in R which uses the Nominatim API provides an easy and effective approach to automate.\u00a0<\/span><\/p>\n<p><b>Preliminary results<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Below is a map of the distribution of tweets related to the 2022 Kenya general election from May to August of 2022.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-70\" src=\"https:\/\/geopsyresearch.org\/blogs\/wp-content\/uploads\/2022\/10\/kenya-map-twitter.png\" alt=\"\" width=\"2048\" height=\"1448\" srcset=\"https:\/\/geopsyresearch.org\/blogs\/wp-content\/uploads\/2022\/10\/kenya-map-twitter.png 2048w, https:\/\/geopsyresearch.org\/blogs\/wp-content\/uploads\/2022\/10\/kenya-map-twitter-300x212.png 300w, https:\/\/geopsyresearch.org\/blogs\/wp-content\/uploads\/2022\/10\/kenya-map-twitter-1024x724.png 1024w, https:\/\/geopsyresearch.org\/blogs\/wp-content\/uploads\/2022\/10\/kenya-map-twitter-768x543.png 768w, https:\/\/geopsyresearch.org\/blogs\/wp-content\/uploads\/2022\/10\/kenya-map-twitter-1536x1086.png 1536w\" sizes=\"auto, (max-width: 2048px) 100vw, 2048px\" \/><\/p>\n<p>&nbsp;<\/p>\n<h2><b>Next steps<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">Through this, I was able to extract over a million tweets related to the 2022 Kenya general Election. These tweets were cleaned,\u00a0 geocoded and mapped. From the geocoded tweets it was possible to determine the number of tweets per county, the dominant candidates in each county as well as the dominant candidates in the country from the sentiments of twitter users.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Next we are going to use machine learning to explore and classify the tweets based on their polarity. From the results we will map electoral related\u00a0 violence hotspots and validate using data from the just concluded election.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>By\u00a0Godwin Murithi Twitter is currently one of the most widely used social networks \u2014 It is a real-time microblogging platform, publicly launched in July 2006. Twitter has one&#8230; <\/p>\n","protected":false},"author":3,"featured_media":69,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-68","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-science"],"_links":{"self":[{"href":"https:\/\/geopsyresearch.org\/blogs\/wp-json\/wp\/v2\/posts\/68","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geopsyresearch.org\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geopsyresearch.org\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geopsyresearch.org\/blogs\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/geopsyresearch.org\/blogs\/wp-json\/wp\/v2\/comments?post=68"}],"version-history":[{"count":7,"href":"https:\/\/geopsyresearch.org\/blogs\/wp-json\/wp\/v2\/posts\/68\/revisions"}],"predecessor-version":[{"id":77,"href":"https:\/\/geopsyresearch.org\/blogs\/wp-json\/wp\/v2\/posts\/68\/revisions\/77"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/geopsyresearch.org\/blogs\/wp-json\/wp\/v2\/media\/69"}],"wp:attachment":[{"href":"https:\/\/geopsyresearch.org\/blogs\/wp-json\/wp\/v2\/media?parent=68"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geopsyresearch.org\/blogs\/wp-json\/wp\/v2\/categories?post=68"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geopsyresearch.org\/blogs\/wp-json\/wp\/v2\/tags?post=68"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}