4
$\begingroup$

I have a project I am working on, and while this seems like a simple question, I thought I'd post this as there has been some different opinions I have heard on this. I have a list of the following variables (shown below) from a data set. One part of the project is to "identify each variable as quantitative or categorical (with levels specified for each categorical variable)". Can anyone give me a rule of thumb for this? Our goal is to build a model for selling price of a house. Obviously this help knowing which is which so you can add the categorical variables last for the predictors, but we haven't talked much about this aspect. I know this is probably way easier than I am making it out to be, but I wanted to get some opinions. I have some ideas, for instance, if I make the basement square footage an indicator (Y/N), that could be categorical. Any suggestions? Thanks!

enter image description here

Thanks so much for any suggestions!

$\endgroup$
5
  • $\begingroup$ What do you mean by adding the categorical variables last? $\endgroup$
    – Dave
    Commented Mar 3, 2021 at 12:34
  • $\begingroup$ In our class we are talking about OLS/WS/GLM, we were advised that while, at times, categorical values are good to have in our models, we should add them last (the last ones as predictors) as typically they are just 0 and 1 values. $\endgroup$
    – azdatasci
    Commented Mar 3, 2021 at 14:29
  • $\begingroup$ Bat what does that mean? The OLS equation $\hat{beta} = (X^TX)^{-1}X^Ty$ does not care about variable order. $\endgroup$
    – Dave
    Commented Mar 3, 2021 at 14:31
  • $\begingroup$ azdatasci - two points. The ORDER that you put variables in a regression model doesn't matter at all. Secondly, if you are talking about whether it's OK to put "categorical" variables in a regression model, it depends on on the type (see my answer below). Binary categorical variables are perfectly fine. Ordinal categorical variables are controversial and it depends on the context. Nominal categorical variables will get you garbage unless you transform them into a set of binary "dummy" variables. $\endgroup$ Commented Mar 3, 2021 at 17:15
  • $\begingroup$ @GrahamWright I think what she is getting at with this case study is the "dummy variables" since that was the last section of lectures we went over. $\endgroup$
    – azdatasci
    Commented Mar 3, 2021 at 20:37

2 Answers 2

6
$\begingroup$

I think whoever wrote the question is causing you additional confusion by conflating two concepts. The term "quantitative variable" (which is not a term I've ever really heard before) is especially confusing/ambiguous, since it's not clear whether it's referring to the kind of data stored in the variable or what that represents.

Firstly, in a given dataset a variable can store "numeric" data meaning that is only contains numbers, or it can store "string" data, meaning that it can contain words or symbols or whatever.

String variables are, in general, useless for ANY statistical analysis - if you want to do anything with them you usually need to transform them into numeric variables that only contain numbers. Unless otherwise specified we always assume variables are numeric.

Now, we can classify (numeric) variables by what the numbers they store actually represent.

In a "continuous" variable the numbers actually represents "numbers of things." So in the variable "age" a value of "35" means "35 years old," and in a variable "income" a value of "30,213" mean "30,213 dollars (per year)." This is important to know because it tells us that a "one unit" increase in these variables actually means something - it means "one more dollar" or "one more year of age." There is also an additional sub-type of continuous variable called an "interval level" variable, in which the zero value is actually meaningful (so age and income are both technically "interval level continuous variables" whereas "degrees Fahrenheit" is continuous but NOT interval level).

In a "categorical" variable by contrast, the numbers don't actually represent numbers, but are simply "codes" to represent different categories (a good statistical program will usually allow you to see the codes when looking at the variables, even though the values are still stored as numbers). So you might have a variable "gender" where a value of 0 represents "male" and 1 represents "female." In particular, this gender variable is a binary categorical variable because there are only two options (but you could also have a gender variable with more than two options). Some categorical variables are "ordinal" where higher numbers represent "more" of something (like a "how happy are you" question with values of 1=not very happy 2=kind of happy 3=very happy, but note that we have no idea whether the "distance" between 1 and 2 is the same as between 3 and 4), while others are "nominal," where the order of the categories is arbitrary (e.g. a "race" variable where 1="white" 2="Black" and 3="other").

Categorical variables require special treatment in statistical analyses because the numbers they store aren't "really" numbers. For binary categorical variables you can often get away with treating them like continuous variables because a "one unit increase" (say from 0 to 1) is still a meaningful concept. But treating ordinal variables like this is dangerous and treating nominal variables like this will get you garbage.

$\endgroup$
6
  • 2
    $\begingroup$ +1 I agree that it is important to decouple the statistics concept of variable type and the software concept of data type. Both matter but not in the same way. $\endgroup$
    – Dave
    Commented Mar 3, 2021 at 14:51
  • 2
    $\begingroup$ Overall good answer, but I don't really agree with the notion that string variables are useless and should always be converted to numbers. Lots of string variables like house color are categorical, so converting to numbers doesn't achieve much except opening the door to erroneously treating them as numeric/ordinal variables. Keeping them as strings (or in R, as factors which can take a limited number of fixed values) seems preferable. $\endgroup$ Commented Mar 3, 2021 at 15:05
  • $\begingroup$ While this is super great information and very detailed, I think it’s getting a bit off the main topic of my original question. I’ll ask the professor about this. As far as this data set goes, would I be correct in stating that ID and waterfront are categorical? Grade and condition are continuous scales, so even though in my head they sound like categories, should I consider them to be quantitative? Other than the model stuff, we have also been asked to identify if each variable is quantitative or categorical and to list the levels for the categorical ones. $\endgroup$
    – azdatasci
    Commented Mar 3, 2021 at 15:42
  • 1
    $\begingroup$ Again I'm not really sure what your professor means by "quantitative" (and I teach stats myself) but in general a categorical variable is any variable where the values don't represent actual numbers. So for for the variable "year:" if one house was built in 1992 and another was built in 1982 we can figure out how many years apart they were built by doing subtraction. But we can't do that for "condition," even though it is represented by a scale of numbers, because a value of 3 doesn't mean the house has "3" of anything. $\endgroup$ Commented Mar 3, 2021 at 17:11
  • 1
    $\begingroup$ You write "gender is a binary categorical variable because there are only two options". There are many other possible examples you could use to make your point more simply and without a real risk of upsetting some people who would contest this on non-statistical grounds (and I tend to agree). $\endgroup$
    – Nick Cox
    Commented Mar 3, 2021 at 18:06
1
$\begingroup$

A quantitative variable is one whose values can be measured on some numeric scale. Some examples in your dataset are price, bedrooms and bathrooms. Note that all these share numeric relationships to one another e.g. $10 > 6 > 4$ and $10 = 6 + 4$.

A categorical variable is one who just indicates categories. It's values don't have any numeric relationships to one another. For example, id is a categorical value. If you add the IDs $2+1$, you won't get ID $3$, because they are just an arbitrary enumeration.


Note: the distinction does not have to do with the data type of the variable, rather what that variable represents! It is possible to have a quantitative variable with string values, as well as a categorical one with numeric ones.

Some other examples:

  • Zip code: this is a categorical variable with numeric values. The relationship z1 < z2 has no meaning in zip codes.
  • House color (e.g. 'red', 'blue'): this is a categorical variable with string values. The relationship 'red' < 'blue' has no meaning.
  • Age: this is a quantitative variable with numeric values. It is pretty obvious that you can say 20 < 30, in this context.
  • Rating (e.g. 'bad', 'good', 'excellent'): this is a quantitative variable with string values. You can absolutely say 'bad' < 'good' < 'excellent'
$\endgroup$
5
  • 2
    $\begingroup$ That last one would be an ordinal variable, which is kind of a hybrid of numerical and categorical. $\endgroup$
    – Dave
    Commented Mar 3, 2021 at 12:55
  • 1
    $\begingroup$ @Dave aren't ordinal variables considered quantitative? $\endgroup$
    – Djib2011
    Commented Mar 3, 2021 at 12:56
  • 1
    $\begingroup$ Ordinal variables are hybrids, and they can be tricky. For instance, I would say that a “star” rating for a movie is an ordinal variable. $\endgroup$
    – Dave
    Commented Mar 3, 2021 at 13:15
  • $\begingroup$ @Djib2011 So for the context of the list above, I get that ID is categorical. Waterfront is just a 0 or 1 variable noting if it has a water front or not. Would this be categorical? What about condition and grade? Those are just values given to assess that condition and grade, so to me it puts the homes into a category on those respective variables. Finally, I see an association with the dates sold and the date renovated, so using the dates, would these be considered quantitative? I would assume so since its just a time period. Thanks! $\endgroup$
    – azdatasci
    Commented Mar 3, 2021 at 14:32
  • 1
    $\begingroup$ Well I'm not sure if binary variables are considered categorical or not... condition and grade are ordinal which is kind of ambiguous (I'd put these more close to quantitative than categorical). Depending on their format dates can be considered quantitative, yes. $\endgroup$
    – Djib2011
    Commented Mar 3, 2021 at 16:51

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.