How to Do Sentiment Analysis in R

Sentiment analysis enables companies to make sense of qualitative data such as tweets, product reviews, and support tickets, and extract insights to respond to their clients’ needs. By detecting positive, neutral, and negative opinions within text, you can understand how customers feel about a brand, product, or service, and make data-driven decisions.

Luckily, there is a neat R package, created by data scientists Maëlle Salmon and Amanda Dobbyn, which works seamlessly with MonkeyLearn’s API and makes it super easy to start using sentiment analysis in R. 

In this short guide, we are going to tell you a bit more about this package and show you how to use it for sentiment analysis. 

Let’s get started!

R Package for MonkeyLearn’s API

Amanda and Maëlle are evangelists within the R community, making noteworthy contributions on a regular basis. To make it easy to analyze text with machine learning, they put together this package – an R interface for MonkeyLearn’s API

By the way, MonkeyLearn is a machine learning platform that makes it easy to build text classifiers for detecting sentiment, intent, topic, urgency and more. You can also build text extractors to identify specific data within text, including keywords, entities, features, and more.

For detecting emotions within text, we’ve already built a pre-trained sentiment analysis model that is ready for you to use and start analyzing data right away.

How to Use MonkeyLearn R Package for Sentiment Analysis

To use the MonkeyLearn R package for doing sentiment analysis, just follow these five simple steps:

1. Install the MonkeyLearn R package

First you’ll need to install the MonkeyLearn R Package using the following command: 

2. Load The Packages

Then, upload the package in your environment:

3. Set Your API Key

To use MonkeyLearn’s models through its API, you will need to provide an API key. You can get your API key by signing up to MonkeyLearn for free, then head to My account > API Key

Now, save your API key as an environment variable:

All functions of the package will conveniently look for your API key using:

4. Setting Up The Texts 

In this step, you’ll need to define the texts you want to analyze with MonkeyLearn: 

5. Making The Request via The API

Finally, you’ll need to send a request through MonkeyLearn’s API to the sentiment analysis model using the monkey_classify function:

This will tell our pre-trained sentiment analysis model to analyze the texts defined in step 4. This will return:

And that’s it! You’ve just learned how to do sentiment analysis in R! You can now use sentiment to analyze data at scale, get insights and make data-driven decisions.

Create Your Own Sentiment Analysis Model!

Pre-trained models for sentiment analysis are great because you can start analyzing data right away. But, if you want maximum accuracy, you should train a custom model that uses your own data and criteria for sentiment analysis. This will enable the machine learning model to learn from industry-specific expressions but also understand your criteria and what you consider positive or negative. 

You can use MonkeyLearn to create a custom model for sentiment analysis in a few simple steps:

1. Choose A Model

Go to the dashboard and click on ‘create model’. This action will prompt you to choose a model type. Choose “Classifier”:

2. Select The Classification Type

Next, you’ll see different options to train a classifier. Choose “Sentiment Analysis”:

3. Upload Your Data

You need to upload the data you want to use to train your sentiment analysis. Either upload it in an Excel or CSV file, or you use one of our many integrations to import your data: 

4. Train Your Model

Here comes the most important step in creating your model: training. You’ll need to assign each text a sentiment tag (Positive, Neutral, or Negative). Once you’ve tagged several samples manually, you’ll notice the model will start making predictions on its own:

5. Test Your Model

In this step, you need to make sure the model delivers accurate results. You can type text manually into the text box or upload a batch of reviews to see the confidence level of your model when making predictions:

You can continue to tag new data if the results are not accurate enough. The more data you feed your machine learning model, and the more data you tag, the better it will be at making predictions.

6. Call the model API with R

You can call your new sentiment analysis model by using the MonkeyLearn R package:

And there you have it. A quick and easy way to analyze text with R using a custom machine learning model!

Wrapping up

Sentiment analysis has become a game-changer for companies that want to gain valuable insights from analyzing customer data and extracting sentiment within opinions.

The MonkeyLearn R package makes sentiment analysis in R simple and straightforward. Instead of creating machine learning models yourself, you can use MonkeyLearn’s pre-trained models and start analyzing data right away with sentiment analysis. Alternatively, you can build your own custom model for even more accurate results.

Sign up to MonkeyLearn for free and start using sentiment analysis in R in next to no time. 

Federico Pascual

Federico Pascual

COO & Co-Founder @MonkeyLearn. Machine Learning. @500startups B14. @Galvanize SoMa. TEDxDurazno Speaker. Wannabe musician and traveler.

Notification

Have something to say?

Text Analysis with Machine Learning

Turn tweets, emails, documents, webpages and more into actionable data. Automate
business processes and save hours of manual data processing.