Dummifying Variables- Hello there, Gretl !!
By: Sonali Pandey
Tags: Machine Learning, Gretl, Categorical Variable, Data Science, Data Preprocessing

Photo by Kevin Ku on Unsplash

The word “dummy” means the act of replication. In the field of data science, it holds the same meaning. The whole art of dummifying variables in data science is the process of transforming the variables into a numerical representation.

Data scientists, analysts, or programmers often come across such datasets that include a categorical variable. This variable is of utmost importance in scenarios where the dependent variable is bound to consider the categorical variable for analysis or prediction.

In this article, we will learn how we can dummify the variables easily in less than a minute! As the title suggests, I will be using Gretl for transforming the variables. For those who are new to Gretl, can understand it as a tool that performs machine learning operations without involving coding from the user side. If you are interested in learning a bit more about it, please check this link out.

Having said this, let’s start with the process of creating dummy variables. Excited to dive deeper? Let’s get started!!

Installing Gretl

I am assuming all of us do not have Gretl installed on our machines, so we will begin with the installation first. The installation is very simple. All you have to do is, google how to install it. But, if you don’t want to waste your time looking for a credible source, follow the link given here.

The installation begins with a simple setup. Just follow the default settings and you’ll be good to go. Now, let’s move on to the next section where we select a dataset for the operation.

Choosing The Dataset

You can take any dataset which includes a categorical variable. I have used a simple one which can be downloaded from here. A sample screenshot of the dataset is attached below:

As you can see in the image above, Gender is a categorical variable with two distinct categories: Men and Women. We will be transforming this variable in the next step.

Creating dummy variables

Welcome to the last section of this article which talks about dummy variables. We will use a step by step procedure for creating the dummy variables. Just follow the steps given below and the task will be done in no time:

Step 1: Open Gretl

Initially, the Gretl screen looks like this. If you have installed it successfully, then you should have a similar screen like the image above. Now, let’s import our data.

Step 2: Importing the data

Just follow the screenshot given below to import your existing data. Also, if you have imported a file before and, want to use it again, Gretl shows a list of those files when you are trying to import it. In the image below, I have drawn a purple box to highlight them.

After you have successfully imported the file, the screen will look like the image given below:

You can see the columns of the dataset are listed here. Now, we will move to our main goal and dummy the “Gender” variable.

Step 3: Dummy Variables on the way!

To create the dummy variables, go to the desired variable, right-click on it, and, choose dummify. If you are confused about how to do it, refer the screenshot below:

After you are done with this, a pop up appears that asks you to choose the option of whether encoding all of them of skipping the highest or lowest.

I have chosen the “encode all” option in the following image. Feel free to choose the one that suits your problem requirements.

After you are done with this, Gretl creates some default names for the variables and encodes them. The encoding by Gretl is shown below:

Step 4: Renaming the dummy variables

This section is optional and, feel free to skip it. Here, we will learn how we can rename the default variables chosen by Gretl. Normally, I prefer to rename the default values so that it is easier for me to interpret.

All you have to do is, right-click on the name you want to change -> Edit attributes and follow the image shown below:

Change the field “Name” of the variable and you’ll be good to go. I have changed the name for both the variables. And, the final thing looks something like:

Hurrah! We have successfully changed the variable names after creating the dummy variables. I hope this was an exciting journey for all the data science enthusiasts.

I look forward to meeting you all again. Until then, Happy Reading!!