# Convert a categorical variable to a numerical variable prior to regression

I am doing a project to estimate students' final graduation GPAs based on several variables. I have students' first year GPAs, high school GPAs, their race, where they come from, and their ACT score, and so on.

I have two questions:

1. How to convert race into numbers, I know I can just assign white to be 1, Black to be 2, Asian to be 3, but it may cause some problem that make my result not significant, so how do I convert the race into numbers to make my model more accurate?

2. How do I find which factor make the most contribution to estimate students final GPA, so I can put more weight on it?

• Is this a school project? What do you mean by "put more weight"? – Dimitriy V. Masterov Mar 10 '14 at 23:44
• Race is categorical. Why would you not use dummies for Race? – Glen_b Mar 10 '14 at 23:59

## 2 Answers

1) Why do you want to convert race into numbers? I'm assuming you want to do something like a regression model, is that correct? I'm going to assume you're asking how to handle "categorical data" (categories like different races) in regression.

So, you want numerical variables, and you could just assign a number to each race. But, if you choose White=1, Black=2, Asian=3 then does it really make sense that the distance between White's and Black's is exactly half the distance between White's and Asian's? And, is that ordering even correct? Probably not.

Instead, what you do is create dummy variables. Let's say you have just those three races. Then, you create two dummy variables: White, Black. You could also use White, Asian or Black, Asian; the key is that you always create one fewer dummy variables then categories. Now, the White variable is 1 if the individual is white and is 0 otherwise, and the Black variable is 1 if the individual is black and is 0 otherwise. If you now fit a regression model, the coefficient for White tells you the average difference between asians and whites (note that the Asian dummy variable was not used, so asians become the baseline we compare to). The coefficient for Black tells you the average difference between asians and blacks.

Note: If you're using software to fit your regression model, you probably don't have to worry about all this. You just tell your software that the variable is categorical, and it handles all these details.

2) You don't need to worry about this, at least if you're doing a regression. Running the regression model will tell you coefficients for each variable as well as their standard errors, and that information tells you which variables are most important. If you want help interpreting those coefficients, that's a whole new topic.

• it'd be great if you answered the question if you can rather than only explaining why this isn't the approach the user should take. Other people will be directed here from similar searches that may need to know how to accomplish it! – yo.ian.g Jul 7 '15 at 18:08
• My response answers question 1 (in paragraph 3). Question 2 doesn't make sense: if you're running a regression model, fitting the model will tell you the weight. There is no need to "put weight on" any particular variable as the model does precisely that. – random_forest_fanatic Jul 10 '16 at 3:56
• I agree that you are advising a practical solution to his particulat problem in 1), but not really answering the question. Say you have White Irish, Native, White Polish, White Italian, Black, Middle-Eastern, and tens more. How do you convert them meaningfully into numbers? Seem to me in this case converting to dummy would not be ideal, due to the large number of additional columns. – famargar Feb 14 '17 at 16:08
• @famargar What's the problem with having tens or even hundreds of dummies? As long as you have enough data to estimate coefficients reasonably, then this approach is perfectly valid. If you don't have enough data, you could still take this approach but use a penalised regression model like lasso. You could also just group categories together (ie European decent, Asian descent, ...). Or, if you absolutely refuse to use dummies, you could use a mean of some covariate within groups (say average income per ethnicity) and use that variable in your model. – random_forest_fanatic Feb 15 '17 at 7:48
• @famargar Yeah, using alot of dummies can create huge datasets, but in those cases the huge dataset is zero almost everywhere. So, you can store it in sparse format, and there are many machine learning algorithms for fitting on sparse matrices too. – random_forest_fanatic Feb 15 '17 at 16:31

Answer for your questions:

1) how do I convert the race into numbers to make my model more accurate?

-> I think answer lies in which tool you are using for analysis. Most of the tool have facility to convert attributes/factor in appropriate inputs. To explain your first question you can refer following link:

You can find your answer precisely here: http://www.ats.ucla.edu/stat/r/dae/logit.htm

It's self-explanatory article on admission based on GPA and ranks.

I am just recreating example from there. Tool used in this blog is R, freeware statistical analysis tool.

Data would look like this:

##   admit gre  gpa rank
## 1     0 380 3.61    3
## 2     1 660 3.67    3
## 3     1 800 4.00    1
## 4     1 640 3.19    4
## 5     0 520 2.93    4
## 6     1 760 3.00    2


Admit is output, 1 means student got admission. Now lets make rank as category:

mydata$rank <- factor(mydata$rank)


You can use other input into factor/category using above method. Now we will prepare a regression model for above table.

mylogit <- glm(admit ~ gre + gpa + rank, data = mydata, family = "binomial")


Above function will prepare a logistic regression model where we are checking whether admission depends on GRE,GPA or rank. Using summary function you get see the results.

summary(mylogit)


2) How do I find which factor make the most contribution to estimate students final GPA, so I can put more weight on it?

-> You don't have to give weight before hand, regression table will give you weight (co-efficient) for each input along with its statistical significance.

I hope I have cleared your answer.