# Is there a name for the following coding scheme?

Suppose, that we have a categorical variable say color which can take one of three values: Red, Green and Yellow.

We can code then using dummy coding or effects coding. But, suppose, we were to code it as follows:

Red:     1
Green:   2
Yellow:  3


Leaving aside issues of estimation and interpretation, is there a name for coding categorical variables as above?

• Ordinal? I can't see why you would ever want to do this, unless you have a very strong belief that $R<G<Y$ (or $>$) and that is is exactly linear. Jul 14, 2014 at 20:26
• This is not a coding scheme because it is unable to distinguish clearly among the colors. For instance, it would equate the average of Red and Yellow with Green. It amounts to a kind of (arbitrary) valuation of the colors. Therefore it does not have any standard name.
– whuber
Jul 14, 2014 at 20:56
• @whuber 'It does not have any standard name.' Perhaps, add your comment as an answer? Jul 14, 2014 at 21:36

That would fall under the category of numerical coding.

In fact, take a look at this piece of R code:

colors <- c("Red", "Green", "Yellow")
colors.df <- data.frame(color=sample(colors, size=25, replace=TRUE))
str(colors.df)


If you run these lines you'll see that R actually internally encodes factors in the very same way you've described. Crazy!

As suggested in the Comments, there is one important caveat, however: Just because you're using numbers to efficiently represent the values of your categorical variable does not mean that you can treat them as numbers. In other words, your data remains categorical regardless of any name changes you may have applied.

Nevertheless, there's nothing inherently wrong with this particular coding scheme. In fact, it's entirely reasonable.

Hope this helps!

### Clarification:

Today, it seems as though (at least to me) that the concept of coding arises most often in the context of regression (for example, as mentioned in the original post, recoding categorical variables as dummy variables). However, the notion of coding systems is actually much more general than this current usage may suggest, as indicated by its entry in Dover's Outline of Basic Statistics: Dictionary and Formulas:

Coding System - In information theory, any consistent scheme used to represent
a given set of data. A coding system is usually employed either to reduce
error or increase efficiency in the transmission of information.


According to this standard, it is clear that the simple mapping of colors to integers as described in the original post would, indeed, qualify as a coding scheme. Other than merely reducing the size of the data being stored (since, for example, the vector c("Green", "Green", "Yellow", "Red") could now be stored more compactly as c(2,2,3,1)), there does not appear to be much benefit to this specific scheme, however. Nevertheless, it still qualifies as a coding system.

Now, to elaborate on the "important caveat" mentioned above (in my original answer), this particular coding scheme is very misleading within the regression setting. In fact, in its entry for "Dummy Variable", the Cambridge Dictionary of Statistics indicates the importance of re-coding (via Dummy Variables) such numerical codes when in the regression setting:

"Such recoding is used before polychotomous variables are used as
explanatory variables in a regression analysis to avoid the unreasonable
assumption that the original numerical codes for the categories, i.e. the
values 1,2,...,k, correspond to an interval scale."


Thus, it's not that this system of coding is meaningless, per se, within the regression setting; rather, the misspecification is due to attributing more meaning to these numerical codes than would otherwise be warranted. Make sense?

Hope this clarifies my original answer!

• You are confusing encoding with naming of factors. The encoding for a trivariate factor requires two-dimensional vectors and this is what R uses under the hood. It definitely does not use the numbers $1,2,3$ to encode these three strings.
– whuber
Jul 15, 2014 at 16:39
• From CRAN: Factors are currently implemented using an integer array to specify the actual levels and a second array of names that are mapped to the integers. It's the coding system represented here--the mapping of the names to integers--that I am referring to. Jul 15, 2014 at 22:44
• I believe you misunderstand the meaning of "integer array." It does not refer to the array c(1,2,3) as your answer suggests. It instead refers to using a vector for each distinct factor. These vectors appear within the rows of the model matrix. You can see them with x <- factor(c("red","green","yellow")); model.matrix(~x). The factor names are "1", "2", and "3" while their encodings are $(1,1,0)$, $(1,0,0)$, and $(1,0,1)$, respectively. The map is from names to vectors rather than integers. The closest Swerdlin gets to this is his fertilizer example which distinguishes N, P, and K.
– whuber
Jul 16, 2014 at 0:27
• No, that's just it--by creating a design matrix you are actually re-coding the coded numerical data in order to make it suitable for regression. As described in the Cambridge Dictionary of Statistics: "Such recoding is used before *polychotomous* variables are used as explanatory variables in a regression analysis to avoid the unreasonable assumption that the original numerical codes for the categories, i.e. the values 1,2,...,k, correspond to an *interval* scale." Jul 16, 2014 at 9:55
• In other words, you have the categories (e.g. "Red", "Green", "Yellow"), you have the original numerical codes for the categories (e.g. 1,2,3), and then (assuming you want to be able to run a regression) you re-code with the k-1 dummy variables, etc. Jul 16, 2014 at 10:23