# Questions tagged [categorical-encoding]

Representing categorical variables as sets of numerical variables. Necessary in many types of analysis for them to process categorical data. A common example is using a categorical predictor in regression/ANOVA via dummy coding, effect coding, Helmert coding, user-defined contrasts, etc.

303 questions
11k views

### What is a contrast matrix?

What exactly is contrast matrix (a term, pertaining to an analysis with categorical predictors) and how exactly is contrast matrix specified? I.e. what are columns, what are rows, what are the ...
35k views

### Why is gender typically coded 0/1 rather than 1/2, for example?

I understand the logic of coding for data analysis. My question below is on the use of a specific code. Is there a reason why gender is often coded as 0 for female and 1 for male? Why is this coding ...
25k views

### When should one use multiple regression with dummy coding vs. ANCOVA?

I recently analyzed an experiment that manipulated 2 categorical variables and one continuous variable using ANCOVA. However, a reviewer suggested that multiple regression with the categorical ...
40k views

### Significance of categorical predictor in logistic regression

I am having trouble interpreting the z values for categorical variables in logistic regression. In the example below I have a categorical variable with 3 classes and according to the z value, CLASS2 ...
21k views

### How to recode categorical variable into numerical variable when using SVM or Neural Network

To use SVM or Neural Network it needs to transform (encode) categorical variables into numeric variables, the normal method in this case is to use 0-1 binary values with the k-th categorical value ...
24k views

### Why do we need to dummy code categorical variables

I am not sure why we need to dummy code categorical variables. For instance, if I have a categorical variable with four possible values 0,1,2,3 I can replace it by two dimensions. If the variable had ...
3k views

### How to treat categorical predictors in LASSO

I am running a LASSO that has some categorical variable predictors and some continuous ones. I have a question about the categorical variables. The first step I understand is to break each of them ...
19k views

### Qualitative variable coding in regression leads to “singularities”

I have an independent variable called "quality"; this variable has 3 modalities of response (bad quality; medium quality; high quality). I want to introduce this independent variable into my multiple ...
16k views

### How to do regression with effect coding instead of dummy coding in R?

I am currently working on a regression model where I have only categorical/factor variables as independent variables. My dependent variable is a logit transformed ratio. It is fairly easy just to run ...
6k views

### “Dummy variable” versus “indicator variable” for nominal/categorical data

"Dummy variable" and "indicator variable" are labels frequently used terms to describe membership in a category with 0/1 coding; usually 0: Not a member of category, 1: Member of category. On 11/26/...
904 views

### What are the different types of codings available for categorical variables (in R) and when would you use them?

If you fit a linear model or a mixed model there are different types of codings available to transform a categorical or nominal varibale into a number of variables for which paramaters are estimated, ...
15k views

### Regression based for example on days of week

I need a little help to move in the right direction. It's a long time since I studied any stats and the jargon seems to have changed. Imagine that I have a set of car-related data such as Journey ...
6k views

### Dropping one of the columns when using one-hot encoding

My understanding is that in machine learning it can be a problem if your dataset has highly correlated features, as they effectively encode the same information. Recently someone pointed out that ...
4k views

### What is “one-hot” encoding called in scientific literature?

What is the name of the operator that takes a categorical vector and transforms it to the binary representation using one-hot encoding? I am wondering since I am writing a scientific paper and need a ...
48k views

### Understanding dummy (manual or automated) variable creation in GLM

If a factor variable (e.g. gender with levels M and F) is used in the glm formula, dummy variable(s) are created, and can be found in the glm model summary along with their associated coefficients (e....
635 views

### Indicator variable for binary data: {-1,1} vs {0,1}

I am interested in treatment-covariate interactions in the context of experiments/randomized controlled trials, with a binary treatment assignment indicator $T$. Depending on the specific method/ ...
102k views

### How to deal with non-binary categorical variables in logistic regression (SPSS)

I have to do binary logistic regression with a lot of independent variables. Most of them are binary, but a few of the categorical variables have more than two levels. What is the best way to deal ...
14k views

### How to implement dummy variable using n-1 variables?

If I have a variable with 4 levels, in theory I need to use 3 dummy variables. In practice, how is this actually carried out? Do I use 0-3, do I use 1-3 and leave the 4's blank? Any suggestions? ...
7k views

### R linear regression categorical variable “hidden” value

This is just an example that I have come across several times, so I don't have any sample data. Running a linear regression model in R: a.lm = lm(Y ~ x1 + x2) <...
2k views

### How to statistically prove if a column has categorical data or not using Python

I have a data frame in python where I need to find all categorical variables. Checking the type of the column doesn't always work because int type can also be ...
537 views

### Why does treatment coding result in a correlation between random slope and intercept?

Consider a within-subject and within-item factorial design where the experimental treatment variable has two levels (conditions). Let m1 be the maximal model and <...
6k views

### Dummy coding for contrasts: 0,1 vs. 1,-1

I'm seeking your help in understanding the difference between two different contrasts for dichotomous variables. On this page: http://www.psychstat.missouristate.edu/multibook/mlt08.htm under "...
4k views

### Linearity Assumption in OLS with Dummy Variables

Let's say that I have a continuous response variable and have constructed a regression model with multiple predictors. Most of my predictors are continuous but I have one which is a dummy variable. ...
8k views

### How to choose number of dummy variables when encoding several categorical variables?

I'm building a logistic regression, and two of my variables are categorical with three levels each. (Say one variable is male, female, or unknown, and the other is single, married, or unknown.) How ...
668 views

### Coding as a categorical or continuous variable?

I have a question/IV in my study which has been answered: 1- No 2- Do not know 3- Sometimes 4- Yes I was advised to remove the answer 2 (all do not know ...
2k views

### Coding categorical variables for regression

I'm not sure of the best way to code my categorical predictor variable for use in a hierarchical regression in order to test my specific hypothesis. This categorical variable has 3 levels representing ...
136 views

### With two related variables, eg, religion and religiosity, how do I transform them into one variable for regression?

Say I have the nominal variables of religion (0=Athiest; 1=Christian; 2=Jewish; 3=Muslim; 4=Other) And then a scale variable of religiosity from 1-10 If I want just one scale variable, so that I can ...
688 views

### How does coding matter for categories?

So say the predictor variable is coded 1,2,3,4 for 4 different cities. Is this bad? I've heard that it only makes sense for things that have a natural ordering. Like number of stars for a movie or ...
145 views

69 views

### “Joint” dummy variables for two different variables

I am supposed to show the hazard ratio (HR) stratified by gender (1= female vs. 2= male) and age groups (quartiles, 1-4)*. The combination "female" and "first quartile of age" is supposed to be the ...
440 views

### Too many dummy variable in regression model

we have about 50000 models of mobile phone (like Galaxy S7, iPhone 9) in database and the size of data is about 3 million. We want to find the mobile phones that have the least call success rate ( ...
914 views

### How to handle too many categorical features with too many categories for XGBoost?

In my data I have 35 features and 14 of them are categorical. Half of them have 3 to 4 categories but others have 14 to 28 categories. One Hot Encoding them would only lead to a sparse matrix with ...
49 views

### Fitted values of a simple regression with intercept and dummy

Why are the fitted values of a simple regression with intercept and dummy, estimated by OLS, just the group means belonging to the two groups of observations? I.e., why do we have that the fitted ...
89 views

### Feature representation for feature set clustering

I'm studying customer requirements clustering. Each customer's requirements are collected as a set of application features. I'd like to cluster those set of features, so that I can know what are the ...
299 views

### Can a dummy variable take on more than 2 values?

I am doing a research on foreign direct investment in the EU countries. I came across an article in which the authors assign 4 values to a dummy variable, to be more specific, they assign the value 0 ...
37 views

### WLS vs Dummy variable coding for heteroscedasticity

I am a beginner level stat learner (with graduate training in Applied Math) I have just read a sage book which states the following: "Dummy variables help address the issue of heteroscedasticity in ...