4
$\begingroup$

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 something. But in R, if you do factor(city), it basically splits it up into 3 different dummy variables. So why does the distinction matter then?

$\endgroup$

3 Answers 3

10
$\begingroup$

If your factor is indeed encoded as a factor, things will be fine statistically.

Problems will crop up if R interprets your coding as a numerical predictor. So you need to be extra careful in reading numerically encoded categorical data into R. Or any other statistical package, for that matter - any package will interpret numbers as numbers by default (what else should it do?), so any package will require special handling and human intervention in such cases. This makes errors much more likely, especially if the factor in question is not the main focus of your analysis, so you might not catch the error in looking at results.

Plus, of course, "New York", "Chicago", "San Francisco" and "Los Angeles" are much more human-readable than "1", "2", "3" and "4".

$\endgroup$
3
$\begingroup$

It's important to distinguish how you've coded this variable for your own purposes, with how it actually enters into statistical computations. You can call the levels of your variable however you want. You can use names, numbers, whatever. As long as, when you do the actual stats (or when you have a software package do this for you), you convert your data to the correct format. When you use R to convert your own coding scheme (1,2,3,4) to categorical dummy variables (with 0s and 1s), that is what you're doing.

If your variable were counting things rather than indicating a category, you wouldn't have to do this conversion, because in that case the numbers are directly meaningful in a statistical/mathematical sense, and not just meaningful to you in an arbitrary labelling scheme.

$\endgroup$
2
$\begingroup$

The difference lies in the interpretation. If you encode the variable as numbers, then by default most of the packages will interpret that as numbers. In most of the cases, you have to signal that there is no ordering, either by factoring or something similar.

Why it matters? Just because of the additional information which you might induce into the structure of the problem. If your encoding is treated as a number, then you tell also to the learner multiple implications of that.

Suppose you have a variable for the country name, and you factor that as a number. Then a country encoded with 1 is considered similar to a country encoded with 2, and distant than a country encoded with 40. So you start with a randomly assigned the encoding, which is interpreted as information by a learner.

On the other hand, if your encoding is not random, but with purpose, then you can make use of it. For example, you might encode temperature factors "low", "medium", "high" with increasing integer value. Pay attention, however, since you might need to establish if the difference between "low" and "medium" is the same as the difference between "medium" and "high".

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.