<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/sentiment-use-across-the-course-of-pitchfork-music-reviews-a-tidy-text-analysis-with-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://methodmatters.blogspot.com/2018/06/sentiment-use-across-course-of.html"> Method Matters</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In this post, we’ll return to the Kaggle data containing information on Pitchfork music reviews. In a <a rel="nofollow" href="http://methodmatters.blogspot.com/2017/12/clustering-music-genres-with-r.html" target="_blank">previous post</a>, I used this dataset to cluster music genres. In the current post, I will use R and the tidytext package (and philosophy) to examine the text of the music reviews. Specifically, the goal of the analysis described in this post will be to track the course of positive and negative sentiment use across the length of the review texts.</p>
<div><b>The Data</b></div>
<p>The data are available on the <a rel="nofollow" href="https://www.kaggle.com/nolanbconaway/pitchfork-data" target="_blank">Kaggle website</a>. I did an extensive data munging exercise to clean and prepare the data for analysis. As noted in the <a rel="nofollow" href="http://methodmatters.blogspot.com/2017/12/clustering-music-genres-with-r.html" target="_blank">previous post using these data</a>, some of the music reviews are assigned multiple genres. As we will later be interested in examining sentiment use in the different genres, I excluded reviews with more than 1 genre from the scope of this analysis, leaving us with 12,147 review texts. In this post we will focus on 3 columns in our dataset: one column containing a unique review identifier, one column with the review text, and one column that contains the genre of the album being reviewed (with the following options: <i>electronic</i>, <i>experimental</i>, <i>folk/country</i>, <i>global</i>, <i>jazz</i>, <i>metal</i>,<i> pop/rnb</i>, <i>rap </i>and <i>rock</i>).</p>
<p></p>
<div><b>The tidytext Approach</b></div>
<p>In order to prepare our data for analysis, we must turn it into a tidy dataset. The basic idea behind the tidytext framework is that we represent our data with 1 line per token (a sub-division of a longer text, typically but not always a single word), keeping track of important meta-data (e.g. the id number of the review the word appears in) in additional columns.</p>
<p>I was first introduced to this way of thinking about text analysis at <a rel="nofollow" href="https://channel9.msdn.com/events/useR-international-R-User-conferences/useR-International-R-User-2017-Conference/Text-mining-the-tidy-way?term=silge" target="_blank">Julia Silge’s excellent talk</a> at the useR conference in Brussels last June. I have done lots of text analysis (<a rel="nofollow" href="http://methodmatters.blogspot.com/2013/12/rap-text-analysis-watch-throne-kanye_28.html" target="_blank">with</a> <a rel="nofollow" href="http://methodmatters.blogspot.com/2014/06/kanye-westyeezus-part-1.html" target="_blank">R</a> <a rel="nofollow" href="http://methodmatters.blogspot.com/2017/06/analyzing-wine-data-in-python-part-3.html" target="_blank">and</a> <a rel="nofollow" href="http://methodmatters.blogspot.com/2017/11/using-word2vec-to-analyze-word.html" target="_blank">Python</a>), and I have always used the “<a rel="nofollow" href="https://en.wikipedia.org/wiki/Bag-of-words_model" target="_blank">bag of words</a>” framework, in which each text is kept in a single line of the dataset. This traditional approach is very handy when doing, for example, <a rel="nofollow" href="http://methodmatters.blogspot.com/2017/06/analyzing-wine-data-in-python-part-3.html" target="_blank">predictive analysis</a> <a rel="nofollow" href="http://methodmatters.blogspot.com/2018/04/nas-vs-doom-model-based-text-analysis.html" target="_blank">with text</a>. However, the tidytext philosophy lets us think about and analyze our data in a slightly different way. Check out the excellent <a rel="nofollow" href="https://www.tidytextmining.com/" target="_blank">tidytext book</a>, available freely online, for a thorough overview of the approach and its implementation in R.</p>
<p>One of the advantages of the tidytext approach is that it retains information about word order that is lost when using the bag of words approach. A nice illustration of using word order in quantitative text analysis is <a rel="nofollow" href="https://www.tidytextmining.com/sentiment.html" target="_blank">described in the tidytext book</a> and <a rel="nofollow" href="https://cran.r-project.org/web/packages/tidytext/vignettes/tidytext.html" target="_blank">vignette</a>. This analysis examines the balance of emotion words across the course of each of Jane Austen’s novels. I was inspired by this approach, and the current post is an adaptation of this idea, applied to Pitchfork music reviews.</p>
<div><b>Data Preparation </b></div>
<p>The head of our raw dataset (called <i>text_df</i>), which serves as the input for our analysis, looks like this:*</p>
<table>
<thead>
<tr>
<th></th>
<th>line</th>
<th>text</th>
<th>genre</th>
</tr>

</thead>
<tbody>
<tr>
<td>1</td>
<td>1</td>
<td>“Trip-hop” eventually became a ’90s punchline, a music-press shorthand for “overhyped hotel lounge music.”… </td>
<td>electronic</td>
</tr>
<tr>
<td>2</td>
<td>2</td>
<td>Eight years, five albums, and two EPs in, the New York-based outfit Krallice have long since shut up purists about their “hipster black metal.” …</td>
<td>metal</td>
</tr>
<tr>
<td>3</td>
<td>3</td>
<td>Minneapolis’ Uranium Club seem to revel in being aggressively obtuse… </td>
<td>rock</td>
</tr>
<tr>
<td>4</td>
<td>4</td>
<td>Kleenex began with a crash. It transpired one night not long after they’d formed, in Zurich of 1978… </td>
<td>rock</td>
</tr>
<tr>
<td>5</td>
<td>5</td>
<td>It is impossible to consider a given release by a footwork artist without confronting the long shadow cast by DJ Rashad’s catalog…</td>
<td>electronic</td>
</tr>
<tr>
<td>6</td>
<td>6</td>
<td>Rapper Simbi Ajikawo, who records as Little Simz, is by all measures on an upward trajectory…</td>
<td>rap</td>
</tr>
</tbody>
</table>
<p>The column “line” serves as an indicator of the review id. The column “text” contains the text of the review and the “genre” column indicates the genre.</p>
<p>We will first turn our raw data into a tidy text dataframe (as explained in the <a rel="nofollow" href="https://cran.r-project.org/web/packages/tidytext/vignettes/tidytext.html" target="_blank">tidytext vignette</a>):</p>
<pre><span># load the packages we'll be using</span><br>library(<span>plyr</span>); library(<span>dplyr</span>)<br>library(<span>tidytext</span>)<br>library(<span>tidyr</span>)<br>library(<span>ggplot2</span>)<br><span># unnest to one line </span><br><span># https://cran.r-project.org/web/packages/tidytext/vignettes/tidytext.html</span><br><span>tidy_reviews</span> <span>&lt;-</span> <span>text_df</span> %<span>></span>% unnest_tokens(<span>word</span>, <span>text</span>)<br><br></pre>
<p>Our data have now been transformed into a tidy format (only first 10 rows shown):</p>
<table>
<thead>
<tr>
<th></th>
<th>line</th>
<th>genre</th>
<th>word</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>1</td>
<td>electronic</td>
<td>trip</td>
</tr>
<tr>
<td>2</td>
<td>1</td>
<td>electronic</td>
<td>hop</td>
</tr>
<tr>
<td>3</td>
<td>1</td>
<td>electronic</td>
<td>eventually</td>
</tr>
<tr>
<td>4</td>
<td>1</td>
<td>electronic</td>
<td>became</td>
</tr>
<tr>
<td>5</td>
<td>1</td>
<td>electronic</td>
<td>a</td>
</tr>
<tr>
<td>6</td>
<td>1</td>
<td>electronic</td>
<td>90s</td>
</tr>
<tr>
<td>7</td>
<td>1</td>
<td>electronic</td>
<td>punchline</td>
</tr>
<tr>
<td>8</td>
<td>1</td>
<td>electronic</td>
<td>a</td>
</tr>
<tr>
<td>9</td>
<td>1</td>
<td>electronic</td>
<td>music</td>
</tr>
<tr>
<td>10</td>
<td>1</td>
<td>electronic</td>
<td>press</td>
</tr>
</tbody>
</table>
<p>As shown above, our data now contains one word per line, and our meta-data (review id and genre) are contained in two additional columns. Note that, by default, the <i>unnest_tokens</i> function <a rel="nofollow" href="https://www.tidytextmining.com/tidytext.html" target="_blank">removes punctuation and converts all letters to lower case</a>.</p>
<p>Unnesting our text data gives us a narrow but extremely long dataframe. Specifically, our dataframe contains as many rows as there are words in the 12,147 reviews: 8,182,882 to be precise!</p>
<p>As we are interested in understanding the course of emotional valence throughout the texts, we will <a rel="nofollow" href="https://stackoverflow.com/questions/26784361/dplyr-put-count-occurrences-into-new-variable" target="_blank">add a column indicating the order of the words within each review</a>, which I will call “<i>position_in_review_0</i>.”</p>
<pre><span># add position within review text</span><br><span>tidy_reviews</span> <span>&lt;-</span> <span>tidy_reviews</span> %<span>></span>% group_by(<span>line</span>) %<span>></span>% <br>  mutate(<span>position_in_review_0</span> <span>=</span> <span>1</span><span>:</span>n())<br><br></pre>
<p>Our dataset, called <i>tidy_reviews</i>, now looks like this (only first 10 rows shown):</p>
<table>
<thead>
<tr>
<th></th>
<th>line</th>
<th>genre</th>
<th>word</th>
<th>position_in_review_0</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>1</td>
<td>electronic</td>
<td>trip</td>
<td>1</td>
</tr>
<tr>
<td>2</td>
<td>1</td>
<td>electronic</td>
<td>hop</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>1</td>
<td>electronic</td>
<td>eventually</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>1</td>
<td>electronic</td>
<td>became</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
<td>1</td>
<td>electronic</td>
<td>a</td>
<td>5</td>
</tr>
<tr>
<td>6</td>
<td>1</td>
<td>electronic</td>
<td>90s</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>1</td>
<td>electronic</td>
<td>punchline</td>
<td>7</td>
</tr>
<tr>
<td>8</td>
<td>1</td>
<td>electronic</td>
<td>a</td>
<td>8</td>
</tr>
<tr>
<td>9</td>
<td>1</td>
<td>electronic</td>
<td>music</td>
<td>9</td>
</tr>
<tr>
<td>10</td>
<td>1</td>
<td>electronic</td>
<td>press</td>
<td>10</td>
</tr>
</tbody>
</table>
<p>We can see that the data contain stopwords (words which occur frequently but contain no meaningful content such as “the”, “a” etc.). Before we continue, let’s remove these stopwords. In the tidytext approach, this is done with an anti-join of our tidy text dataframe against a tidy dataframe containing a list of stopwords. We then order the dataset by the review id (called <i>line</i>) and by the word order position (<i>position_in_review_0</i>) in the raw data. We create a new word order variable, called <i>position_in_review</i>, which gives the position of each word in the cleaned data (without stopwords), while removing the <i>position_in_review_0</i> variable created above.</p>
<p></p>
<pre><span># remove stop words</span><br><span># order the dataset by review id</span><br><span># and the order of the remaining words</span><br><span># in the original dataset</span><br><span>cleaned_reviews</span> <span>&lt;-</span> <span>tidy_reviews</span> %<span>></span>%<br>  anti_join(<span>stop_words</span>) %<span>></span>% arrange(<span>line</span>, <span>position_in_review_0</span>)<br><br><span># add position of word within cleaned review</span><br><span># we also remove the first word order column</span><br><span># (position_in_review_0) created above</span><br><span>cleaned_reviews</span> <span>&lt;-</span> <span>cleaned_reviews</span> %<span>></span>% group_by(<span>line</span>) %<span>></span>% <br>  mutate(<span>position_in_review</span> <span>=</span> <span>1</span><span>:</span>n())  %<span>></span>% select(<span>-</span><span>position_in_review_0</span>)<br><br></pre>
<p>The data (called <i>cleaned_reviews</i>) now look like this (only first 10 rows shown):</p>
<table>
<thead>
<tr>
<th></th>
<th>line</th>
<th>genre</th>
<th>word</th>
<th>position_in_review</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>1</td>
<td>electronic</td>
<td>trip</td>
<td>1</td>
</tr>
<tr>
<td>2</td>
<td>1</td>
<td>electronic</td>
<td>hop</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>1</td>
<td>electronic</td>
<td>eventually</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>1</td>
<td>electronic</td>
<td>90s</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
<td>1</td>
<td>electronic</td>
<td>punchline</td>
<td>5</td>
</tr>
<tr>
<td>6</td>
<td>1</td>
<td>electronic</td>
<td>music</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>1</td>
<td>electronic</td>
<td>press</td>
<td>7</td>
</tr>
<tr>
<td>8</td>
<td>1</td>
<td>electronic</td>
<td>shorthand</td>
<td>8</td>
</tr>
<tr>
<td>9</td>
<td>1</td>
<td>electronic</td>
<td>overhyped</td>
<td>9</td>
</tr>
<tr>
<td>10</td>
<td>1</td>
<td>electronic</td>
<td>hotel</td>
<td>10</td>
</tr>
</tbody>
</table>
<p>Our goal was to preserve the word order so that we can track the use of emotion words across the course of the review texts. The steps necessary to achieve this were somewhat involved, but we have reached our goal. We have retained the important words in the texts and, for each word, we have created a record of its position in the review.</p>
<p>As the reviews have differing numbers of words, we cannot simply compare the evolution of sentiment use across word number. Therefore, we will count the number of words in each review, and for each word, calculate its position in terms of its percentage in the words of the text. This will result in 101 different levels representing word position for each text (because we go from 0 to 1 in increments of .01). We will eventually aggregate the data to this level, making it possible to visualize the use of emotion words across the different percentages of the review texts.</p>
<pre><span># count the number of remaining words for each review</span><br><span># and calculate the percentage within each review</span><br><span># that each word falls in</span><br><span>cleaned_reviews</span> <span>&lt;-</span> <span>cleaned_reviews</span> %<span>></span>% group_by(<span>line</span>) %<span>></span>% <br>      mutate(<span>wordcount_review</span> <span>=</span> n(), <br>      <span>percentage_in_review</span> <span>=</span> round(<span>position_in_review</span><span>/</span><span>wordcount_review</span>,<span>2</span>))<br><br></pre>
<p>Our data now look like this (only first 10 rows shown):</p>
<table>
<thead>
<tr>
<th></th>
<th>line</th>
<th>genre</th>
<th>word</th>
<th>position_in_review</th>
<th>wordcount_review</th>
<th>percentage_in_review</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>1</td>
<td>electronic</td>
<td>trip</td>
<td>1</td>
<td>746</td>
<td>0</td>
</tr>
<tr>
<td>2</td>
<td>1</td>
<td>electronic</td>
<td>hop</td>
<td>2</td>
<td>746</td>
<td>0</td>
</tr>
<tr>
<td>3</td>
<td>1</td>
<td>electronic</td>
<td>eventually</td>
<td>3</td>
<td>746</td>
<td>0</td>
</tr>
<tr>
<td>4</td>
<td>1</td>
<td>electronic</td>
<td>90s</td>
<td>4</td>
<td>746</td>
<td>0.01</td>
</tr>
<tr>
<td>5</td>
<td>1</td>
<td>electronic</td>
<td>punchline</td>
<td>5</td>
<td>746</td>
<td>0.01</td>
</tr>
<tr>
<td>6</td>
<td>1</td>
<td>electronic</td>
<td>music</td>
<td>6</td>
<td>746</td>
<td>0.01</td>
</tr>
<tr>
<td>7</td>
<td>1</td>
<td>electronic</td>
<td>press</td>
<td>7</td>
<td>746</td>
<td>0.01</td>
</tr>
<tr>
<td>8</td>
<td>1</td>
<td>electronic</td>
<td>shorthand</td>
<td>8</td>
<td>746</td>
<td>0.01</td>
</tr>
<tr>
<td>9</td>
<td>1</td>
<td>electronic</td>
<td>overhyped</td>
<td>9</td>
<td>746</td>
<td>0.01</td>
</tr>
<tr>
<td>10</td>
<td>1</td>
<td>electronic</td>
<td>hotel</td>
<td>10</td>
<td>746</td>
<td>0.01</td>
</tr>
</tbody>
</table>
<p></p>
<div></div>
<div><i>Intermezzo: Coding Sentiment With Dictionaries </i></div>
<p>There are a number of different ways of analyzing sentiment in text. One common approach is to use dictionaries, which contain pre-defined lists of words which are categorized as belonging to a particular type of higher-level characteristic we wish to understand (e.g. in the case of sentiment – positive, negative, or more fine-grained such as excitement, anxiety, etc.). </p>
<p>In this post, we will use a dictionary set included in the tidytext package and which is described in the tidytext book and vignette. Specifically, we will use the positive and negative sentiment words contained in the “bing” dictionaries. For more information about the bing dictionaries, you can check out these <a rel="nofollow" href="http://jacobsimmering.com/2016/11/15/tidytext/" target="_blank">excellent</a> <a rel="nofollow" href="https://rstudio-pubs-static.s3.amazonaws.com/236096_2ef4566f995e48c1964013310bf197f1.html" target="_blank">blog posts</a>, consult the tidytext book or vignette, or type <i>?sentiments</i> in the R console (with the tidytext package loaded). One thing to keep in mind is that the bing dictionaries contain many more negative words than positive words:</p>
<pre><span># count of positive/negative sentiment</span><br><span># in bing dictionaries</span><br>get_sentiments(<span>&quot;bing&quot;</span>) %<span>></span>% <br>  count(<span>sentiment</span>) <br><br></pre>
<p>Which returns:  </p>
<table>
<thead>
<tr>
<th></th>
<th>sentiment</th>
<th>n</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>negative</td>
<td>4782</td>
</tr>
<tr>
<td>2</td>
<td>positive</td>
<td>2006</td>
</tr>
</tbody>
</table>
<p>Indeed, there are more than twice as many negative than positive words in the bing dictionaries.</p>
<p>Let’s look at the most frequent positive and negative words from the bing dictionaries in our review data. We can make a word cloud with code directly adapted from the <a rel="nofollow" href="https://cran.r-project.org/web/packages/tidytext/vignettes/tidytext.html" target="_blank">tidytext vignette</a> to do this:</p>
<pre><span># plot most frequent positive/negative words</span><br><span># with wordcloud</span><br>library(<span>reshape2</span>)<br>library(<span>wordcloud</span>)<br><br><span>cleaned_reviews</span> %<span>></span>%<br>  inner_join(get_sentiments(<span>&quot;bing&quot;</span>), <span>by</span> <span>=</span> <span>&quot;word&quot;</span>) %<span>></span>% <br>  count(<span>word</span>, <span>sentiment</span>, <span>sort</span> <span>=</span> <span>TRUE</span>) %<span>></span>% <br>  acast(<span>word</span> <span>~</span> <span>sentiment</span>, <span>value.var</span> <span>=</span> <span>&quot;n&quot;</span>, <span>fill</span> <span>=</span> <span>0</span>, <span>fun.aggregate</span> <span>=</span> <span>length</span>) %<span>></span>% <br>  comparison.cloud(<span>colors</span> <span>=</span> c(<span>&quot;#F8766D&quot;</span>, <span>&quot;#00BFC4&quot;</span>),<br>                   <span>max.words</span> <span>=</span> <span>150</span>, <span>title.size</span> <span>=</span> <span>1</span>, <span>scale</span>=c(<span>3.5</span>,<span>1</span>))<br><br></pre>
<p>Which gives us the following plot:</p>
<div><a rel="nofollow" href="https://i2.wp.com/3.bp.blogspot.com/-NglqNf5yEmw/Wmzt7ycOoUI/AAAAAAAAAZw/gKREAkemKCkShw2dzPH8-cGrSZ-Ja4SxQCLcBGAs/s1600/comp_cloud_pos_neg.png?ssl=1" target="_blank"><img border="0" data-original-height="1067" src="https://i2.wp.com/3.bp.blogspot.com/-NglqNf5yEmw/Wmzt7ycOoUI/AAAAAAAAAZw/gKREAkemKCkShw2dzPH8-cGrSZ-Ja4SxQCLcBGAs/s1600/comp_cloud_pos_neg.png?resize=450%2C1067&ssl=1" data-recalc-dims="1" data-original-width="450"></a></div>
<p>The vast majority of the words make sense to me, and seem to capture positive and negative things one could say about music in the context of an album review. It is amusing to see that “punk” is classified as negative, which makes sense in many contexts but is a bit off here. This is the downside of using general-purpose text classification dictionaries; overall they can perform quite well but they are by design not adapted for the specifics of every corpus. Despite these small imperfections, the above visualization makes clear that the bing dictionaries are picking up on meaningful indicators of sentiment in the Pitchfork reviews.</p>
<p>Note that, in the analysis below, I will treat positive and negative sentiment separately. The analysis presented in the <a rel="nofollow" href="https://cran.r-project.org/web/packages/tidytext/vignettes/tidytext.html" target="_blank">tidytext vignette</a> analyzes an overall sentiment score (e.g. sentiment = positive – negative). However, this seems strange to me for 2 reasons. First, as we saw above, there are twice as many negative (vs. positive) words in the bing corpus. Second, there is a large literature in psychology that suggests that positive and negative affect are orthogonal (independent); this logic underpins the measurement of affect in the widely used <a rel="nofollow" href="https://en.wikipedia.org/wiki/Positive_and_Negative_Affect_Schedule" target="_blank">PANAS questionnaire</a>, for example.  </p>
<p>Let’s then apply these dictionaries to our data, in order to extract both positive and negative sentiment at each percentage of our review texts.</p>
<p><i>Finishing the Munging </i></p>
<p>We will conduct all of the necessary steps in a single dplyr chain. First, we merge in the sentiment dictionaries, retaining only the words classified as positive or negative. We then count the number of positive and negative words at each percentage in the reviews; this counting is done separately for each percentage of each review. We then aggregate the data by genre and percentage of the review text. Specifically, for each genre, we calculate the average number of positive and negative words that occur at each percentage in the review text.</p>
<pre><span>pitchfork_sentiment</span> <span>&lt;-</span> <span>cleaned_reviews</span> %<span>></span>%<br>  <span># code the words according to the positive/negative bing sentiment dictionaries</span><br>  inner_join(get_sentiments(<span>&quot;bing&quot;</span>), <span>by</span> <span>=</span> <span>&quot;word&quot;</span>) %<span>></span>%<br>  <span># count the number of pos/neg words at each percentage of each review</span><br>  count(<span>genre</span>, <span>index</span> <span>=</span> <span>percentage_in_review</span>, <span>sentiment</span>)  %<span>></span>%<br>  <span># put the pos/neg counts into their own columns</span><br>  spread(<span>sentiment</span>, <span>n</span>, <span>fill</span> <span>=</span> <span>0</span>)   %<span>></span>%<br>  <span># for each genre, compute the average number of pos/neg words</span><br>  <span># used at each percentage of the review texts</span><br>  group_by(<span>genre</span>, <span>index</span>)  %<span>></span>%<br>  summarize(<span>mean_negative</span> <span>=</span> round(mean(<span>negative</span>, <span>na.rm</span> <span>=</span> <span>TRUE</span>),<span>2</span>),<br>            <span>mean_positive</span> <span>=</span> round(mean(<span>positive</span>, <span>na.rm</span> <span>=</span> <span>TRUE</span>),<span>2</span>))<br><br></pre>
<p>The resulting dataset, called <i>pitchfork_sentiment</i>, looks like this (only first 10 rows shown):</p>
<table>
<thead>
<tr>
<th></th>
<th>genre</th>
<th>index</th>
<th>mean_negative</th>
<th>mean_positive</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>electronic</td>
<td>0</td>
<td>0.67</td>
<td>0.35</td>
</tr>
<tr>
<td>2</td>
<td>electronic</td>
<td>0.01</td>
<td>0.61</td>
<td>0.54</td>
</tr>
<tr>
<td>3</td>
<td>electronic</td>
<td>0.02</td>
<td>0.61</td>
<td>0.59</td>
</tr>
<tr>
<td>4</td>
<td>electronic</td>
<td>0.03</td>
<td>0.62</td>
<td>0.54</td>
</tr>
<tr>
<td>5</td>
<td>electronic</td>
<td>0.04</td>
<td>0.64</td>
<td>0.53</td>
</tr>
<tr>
<td>6</td>
<td>electronic</td>
<td>0.05</td>
<td>0.56</td>
<td>0.59</td>
</tr>
<tr>
<td>7</td>
<td>electronic</td>
<td>0.06</td>
<td>0.62</td>
<td>0.55</td>
</tr>
<tr>
<td>8</td>
<td>electronic</td>
<td>0.07</td>
<td>0.64</td>
<td>0.56</td>
</tr>
<tr>
<td>9</td>
<td>electronic</td>
<td>0.08</td>
<td>0.63</td>
<td>0.56</td>
</tr>
<tr>
<td>10</td>
<td>electronic</td>
<td>0.09</td>
<td>0.58</td>
<td>0.58</td>
</tr>
</tbody>
</table>
<p>Our data contains 9 genres, with 101 rows per genre (because we go from 0 to 1 in increments of .01), resulting in 909 total rows.</p>
<div><b>Visualizing Emotional Valence Across the Album Reviews</b>  </div>
<p>We will produce our plots using the excellent <a rel="nofollow" href="http://ggplot2.org/" target="_blank"><b>ggplot2</b></a> package, which also is built according to the tidy data philosophy. From the tidy perspective, our data are problematic in that the values we want to visualize (<i>mean_positive</i> and <i>mean_negative</i>) are contained in two different columns. Therefore, in order to plot using ggplot2, we must transform our data from a wide to a long format, putting our observations in a single column, with an additional column containing the sentiment type (positive or negative).</p>
<p>We can achieve this using code taken directly from the <i>gather </i>help page from the <a rel="nofollow" href="https://cran.r-project.org/web/packages/tidyr/index.html" target="_blank">tidyr</a> package (check out <a rel="nofollow" href="https://www.rdocumentation.org/packages/tidyr/versions/0.7.2/topics/gather?tap_a=5644-dce66f&tap_s=10907-287229" target="_blank">this webpage</a> or type <i>?gather</i> with the <i>tidyr </i>package loaded):</p>
<pre><span># make the wide to long data</span><br><span># (from the &quot;gather&quot; help page in tidyr)</span><br><span>long_sentiment_by_genre</span> <span>&lt;-</span> gather(<span>pitchfork_sentiment</span>, <span>key</span>,<br>                                  <span>value</span>, <span>-</span><span>genre</span>, <span>-</span><span>index</span>)<br><br></pre>
<p>Which gives us (only first 10 rows shown):</p>
<table>
<thead>
<tr>
<th></th>
<th>genre</th>
<th>index</th>
<th>key</th>
<th>value</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>electronic</td>
<td>0</td>
<td>mean_negative</td>
<td>0.67</td>
</tr>
<tr>
<td>2</td>
<td>electronic</td>
<td>0.01</td>
<td>mean_negative</td>
<td>0.61</td>
</tr>
<tr>
<td>3</td>
<td>electronic</td>
<td>0.02</td>
<td>mean_negative</td>
<td>0.61</td>
</tr>
<tr>
<td>4</td>
<td>electronic</td>
<td>0.03</td>
<td>mean_negative</td>
<td>0.62</td>
</tr>
<tr>
<td>5</td>
<td>electronic</td>
<td>0.04</td>
<td>mean_negative</td>
<td>0.64</td>
</tr>
<tr>
<td>6</td>
<td>electronic</td>
<td>0.05</td>
<td>mean_negative</td>
<td>0.56</td>
</tr>
<tr>
<td>7</td>
<td>electronic</td>
<td>0.06</td>
<td>mean_negative</td>
<td>0.62</td>
</tr>
<tr>
<td>8</td>
<td>electronic</td>
<td>0.07</td>
<td>mean_negative</td>
<td>0.64</td>
</tr>
<tr>
<td>9</td>
<td>electronic</td>
<td>0.08</td>
<td>mean_negative</td>
<td>0.63</td>
</tr>
<tr>
<td>10</td>
<td>electronic</td>
<td>0.09</td>
<td>mean_negative</td>
<td>0.58</td>
</tr>
</tbody>
</table>
<p>Our numeric data on both positive and negative sentiment are now contained in a single column (called <i>value</i>) while the sentiment type (called <i>key</i>) is contained in a separate column.</p>
<p>Our input dataframe (<i>pitchfork_sentiment</i>) had 909 rows, and because each row had a value for both positive and negative sentiment, our long dataframe (called <i>long_sentiment_by_genre</i>) has 1,818 rows.</p>
<p><i>Overall Trends</i></p>
<p>We are now ready to plot the average number of positive and negative sentiment words across the course of the reviews. We’ll first produce a plot of the overall data (not split by genre), using <a rel="nofollow" href="https://en.wikipedia.org/wiki/Local_regression" target="_blank">loess regression</a> to visualize the overall trends per sentiment type. Note that I set the limits of the y axis to focus on the loess regression lines:</p>
<pre><span># plot the positive vs. negative sentiment across review percentage</span><br>ggplot(<span>long_sentiment_by_genre</span>, aes(<span>index</span> <span>*</span> <span>100</span>, <span>value</span>, <span>color</span> <span>=</span> <span>key</span>)) <span>+</span><br>  geom_point() <span>+</span><br>  geom_smooth(<span>method</span>=<span>&quot;loess&quot;</span>) <span>+</span> <br>  coord_cartesian(<span>ylim</span> <span>=</span> c(.<span>5</span>, .<span>7</span>)) <span>+</span><br>  labs(<span>x</span> <span>=</span> <span>&quot;Percentage of Review Text&quot;</span>, <br>       <span>y</span> <span>=</span> <span>&quot;Mean Number of Positive &amp; Negative Words&quot;</span> ) <span>+</span><br>  scale_color_manual(<span>name</span>=<span>&quot;Sentiment&quot;</span>,<br>                     <span>breaks</span>=c(<span>&quot;mean_negative&quot;</span>, <span>&quot;mean_positive&quot;</span>),<br>                     <span>labels</span>=c(<span>&quot;Negative&quot;</span>, <span>&quot;Positive&quot;</span>),<br>                     <span>values</span> <span>=</span> c(<span>&quot;#F8766D&quot;</span>, <span>&quot;#00BFC4&quot;</span>))<br><br></pre>
<p>Which produces the following plot:</p>
<div><a rel="nofollow" href="https://i1.wp.com/4.bp.blogspot.com/-XDGO8DCZzB0/WnIhvLU3haI/AAAAAAAAAaA/eYRLQ9r_7KAzTG13jLES4r-1isFJMOsJACLcBGAs/s1600/overall_pos_neg.png?ssl=1" target="_blank"><img border="0" data-original-height="1067" src="https://i1.wp.com/4.bp.blogspot.com/-XDGO8DCZzB0/WnIhvLU3haI/AAAAAAAAAaA/eYRLQ9r_7KAzTG13jLES4r-1isFJMOsJACLcBGAs/s1600/overall_pos_neg.png?resize=450%2C1067&ssl=1" data-recalc-dims="1" data-original-width="450"></a></div>
<p>This is quite interesting. There is a clear pattern of positive and negative sentiment use across the album reviews. Negative sentiment use is flat for around the first 45 percent of the review text, after which it increases, peaking just shy of the 75th percentile of the reviews. After peaking, the use of negative sentiment plummets sharply, ending lower than its starting point.</p>
<p>Positive sentiment, meanwhile, increases slightly at around 1/4 of the review text. It then decreases, reaching a low point at around 70%. From around the 75th percentile of the review texts, positive sentiment use increases sharply and ends far above its starting point.</p>
<p>When comparing the trends of positive and negative sentiment, there is a clear divergence just short of the 75th percentile of the album review texts. At this point, negative sentiment increases, while positive sentiment decreases. After the 75th percentile, these trends reverse, and the review texts end with more positive than negative sentiment.</p>
<p><i>Trends by Genre</i></p>
<p>We can also examine the course of positive and negative sentiment across the reviews for the different genres. This requires just a slight modification of the above code to use genre as a <a rel="nofollow" href="http://ggplot2.tidyverse.org/reference/facet_grid.html" target="_blank">facet</a> (note I again specify the range of the y-axis in the plot to highlight the trends):</p>
<pre><span># separate plots per genre</span><br>ggplot(<span>long_sentiment_by_genre</span>, aes(<span>index</span>, <span>value</span>, <span>color</span> <span>=</span> <span>key</span>)) <span>+</span><br>  geom_point() <span>+</span><br>  geom_smooth(<span>method</span>=<span>&quot;loess&quot;</span>) <span>+</span> <br>  coord_cartesian(<span>ylim</span> <span>=</span> c(.<span>45</span>, .<span>8</span>)) <span>+</span><br>  labs(<span>x</span> <span>=</span> <span>&quot;Percentage of Review Text&quot;</span>, <span>y</span> <span>=</span> <span>&quot;Mean Count Positive &amp; Negative Sentiment&quot;</span> ) <span>+</span><br>  scale_color_manual(<span>name</span>=<span>&quot;Emotional<span>\n</span>Valence&quot;</span>,<br>                     <span>breaks</span>=c(<span>&quot;mean_negative&quot;</span>, <span>&quot;mean_positive&quot;</span>),<br>                     <span>labels</span>=c(<span>&quot;Negative&quot;</span>, <span>&quot;Positive&quot;</span>),<br>                     <span>values</span> <span>=</span> c(<span>&quot;#F8766D&quot;</span>, <span>&quot;#00BFC4&quot;</span>)) <span>+</span><br>  facet_wrap(<span>~</span><span>genre</span>, <span>ncol</span> <span>=</span> <span>3</span>, <span>scales</span> <span>=</span> <span>&quot;free_x&quot;</span>)<br><br></pre>
<p>Which yields the following plot:</p>
<div><a rel="nofollow" href="https://i0.wp.com/4.bp.blogspot.com/-TZJydoWsEsA/WnNzZ3sDzmI/AAAAAAAAAac/v7Adnz1dWGUc6XzTncTuzpbcJCcHdIDzgCLcBGAs/s1600/pos_neg_by_genre.png?ssl=1" target="_blank"><img border="0" data-original-height="1067" src="https://i0.wp.com/4.bp.blogspot.com/-TZJydoWsEsA/WnNzZ3sDzmI/AAAAAAAAAac/v7Adnz1dWGUc6XzTncTuzpbcJCcHdIDzgCLcBGAs/s1600/pos_neg_by_genre.png?resize=450%2C1067&ssl=1" data-recalc-dims="1" data-original-width="450"></a></div>
<div></div>
<p>The increase in negative sentiment and the decrease in positive sentiment just shy of 75% (and subsequent reversal of this trend) is evident across genres. The relative levels of positive and negative sentiment, however, differ across genres. Interestingly, jazz is the only genre for which positive sentiment use is consistently higher than negative sentiment use. </p>
<div><b>Caveats and Limitations</b></div>
<p><i>Dictionary Considerations</i></p>
<p>One striking pattern in the data was that negative sentiment use was consistently higher than positive sentiment use. Do the Pitchfork reviews really contain more negative than positive sentiment? I think that it’s important to keep in mind that the bing sentiment dictionaries contain twice as many negative as positive words. One alternative interpretation of the difference in mean levels of positive vs. negative sentiment, therefore, is that we have an easier time detecting negative sentiment (because we have many more negative words in our dictionary) than positive sentiment (which has half as many words). In sum, it’s hard to say from these data whether the Pitchfork music reviews are really more negative than positive overall. </p>
<p><i>Effect Size</i></p>
<p>We saw in the above figures that positive and negative sentiment dipped and peaked across the review texts. How large are these decreases and increases in sentiment use? This question relates to the <i>effect size</i> of the observed trends in emotion use across the review texts.</p>
<p>One way to interpret observed effect sizes is by using domain knowledge, e.g. expertise accumulated through previous work in the domain. Unfortunately, we’re using a very specific metric here (mean number of words across percentages of a text), and I don’t know of any existing studies using this type of coding. This is the first time I myself have used this approach!</p>
<p>For classical statistical models, there are statistical definitions of effect size, but these do not apply to the type of local regression (loess) models we are using here.</p>
<p>One principal that I’ve heard mentioned a number of times is that an effect worth considering should be visible with the naked eye. The trends are quite striking in the above plots, but I’ve set the axes in such a way that the differences are highlighted. What happens if we plot the data, but allow the y-axis to be defined by the minimum and maximum observed values of mean positive and negative sentiment? (Note that this approach parallels the underlying logic of most measures of effect size, which compare observed differences scaled to some measure of variation in the data.) </p>
<p>The plot (obtained by using the above code with the <i>coord_cartesian</i> command removed) looks like this:</p>
<p></p>
<div><a rel="nofollow" href="https://i2.wp.com/3.bp.blogspot.com/-6SfWXYw04oM/WnXnnRPhxbI/AAAAAAAAAaw/U2nTiGB74CU8KbRj86rcGFyg1Xja9dHZQCLcBGAs/s1600/pos_neg_full_y_axis.png?ssl=1" target="_blank"><img border="0" data-original-height="1067" src="https://i2.wp.com/3.bp.blogspot.com/-6SfWXYw04oM/WnXnnRPhxbI/AAAAAAAAAaw/U2nTiGB74CU8KbRj86rcGFyg1Xja9dHZQCLcBGAs/s1600/pos_neg_full_y_axis.png?resize=450%2C1067&ssl=1" data-recalc-dims="1" data-original-width="450"></a></div>
<p>The patterns still looks clear to me. Even when shown across the observed values of mean sentiment, the divergence and subsequent reversal in sentiment use is easily observable.  </p>
<div><b>Conclusion</b></div>
<p>In summary, in this post we examined the use of positive and negative sentiment words across the course of Pitchfork album reviews. We first turned our raw data (with one review per line) into a tidy text dataframe (with one word per line). We then removed stopwords and calculated the position of each word in terms of its percentage in the review text. Next, we calculated the mean positive and negative sentiment words at each percentage of the review texts for each genre separately. Finally, we visualized the overall trends of positive and negative sentiment across the review texts, and also examined these trends separately across genres.</p>
<p>There were clear patterns for positive and negative sentiment use. Positive sentiment reached its lowest point just short of the 75th percentile of the review texts, after which it sharply increased. Negative sentiment peaked just shy of the 75th percentile, after which it sharply decreased. In sum, it appears that Pitchfork music reviews increase in negativity and decrease in positivity around 75the percentile of the review texts, after which they become much less negative and much more positive (in essence ending on a positive note).</p>
<p><i>Coming Up Next</i></p>
<p>In the next post, I will continue exploring the Pitchfork music reviews with the tidyext package. Specifically, we will examine how word usage differs according to the genre of the album being reviewed. <i>Stay tuned!</i> </p>
<p>—</p>
<p>* All the tables in this post were created with the <a rel="nofollow" href="https://cran.r-project.org/web/packages/htmlTable/index.html" target="_blank">htmlTable package</a> in R, which made it wonderfully easy to directly create the tables from the data in R. Plus, they look great! </p>
<p></p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://methodmatters.blogspot.com/2018/06/sentiment-use-across-course-of.html"> Method Matters</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/hg05kq9SS_Y" height="1"></a></div>