Sentiment analysis is a text analysis technique that allows companies to make sense of qualitative data. By detecting positive and negative sentiment in text data, such as tweets, product reviews, and support tickets, you can understand how customers feel about your brand, product, or service, and gain insights that lead to data-driven decisions.
In this short guide, we’ll introduce you to an R package, created by data scientists Maëlle Salmon and Amanda Dobbyn, which works seamlessly with MonkeyLearn’s API and makes it easy to start performing sentiment analysis in R.
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.
Now that the introductions are out of the way, let’s get started!
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 an R package for MonkeyLearn’s API.
To perform sentiment analysis in R using this package and MonkeyLearn, just follow these five simple steps:
First you’ll need to install the MonkeyLearn R Package using the following command:
Then, upload the package in your environment:
Now, save your API key as an environment variable:
Sys.setenv(MONKEYLEARN_KEY = "ADD YOUR API KEY HERE")
All functions of the package will conveniently look for your API key using:
In this step, you’ll need to define the texts you want to analyze with MonkeyLearn:
text1 <- "this is really bad" text2 <- "i love this" texts <- c(text1, text2)
Finally, you’ll need to send a request through MonkeyLearn’s API to MonkeyLearn's pre-trained sentiment analysis model using the monkey_classify function:
library(tidyverse) monkey_classify(texts, classifier_id = "cl_pi3C7JiL") %>% unnest_wider(res)
This will tell the pre-trained sentiment analysis model to analyze the texts defined in step 4. This will return:
# A tibble: 2 x 5 # req confidence category_id probability label # <chr> <dbl> <int> <dbl> <chr> # 1 this is really bad 0.997 60333049 0.997 Negative # 2 i love this 0.98 60333048 0.98 Positive
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.
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:
Go to the dashboard and click on ‘create model’. This action will prompt you to choose a model type. Choose “Classifier”:
Next, you’ll see different options to train a classifier. Choose “Sentiment Analysis”:
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:
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:
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.
You can call your new sentiment analysis model using the MonkeyLearn R package in the previous tutorial::
monkey_classify(texts, classifier_id = "ADD THE MODEL ID HERE", unnest = FALSE))
And there you have it. A quick and easy way to analyze text with R using a custom machine learning model!
Sentiment analysis has become a game-changer for companies that want to gain valuable insights from analyzing customer data.
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 sentiment analysis model and start analyzing data right away. Alternatively, you can build your own custom model for even more accurate results.
Want to learn more about how MonkeyLearn can help you. Schedule a demo and we’ll be happy to show you.
December 27th, 2019