33
$\begingroup$

Is "hour of the day" where the value can be 0, 1, 2, ..., 23 a categorical variable? I would be tempted to say no, since 5, for example, is 'closer' to 4 or 6 than it is to 3 or 7.

On the other hand, there is the discontinuity between 23 and 0.

So is it generally considered categorical or not? Note that 'hour' is one of the independent variables, not the variable I'm trying to predict.

$\endgroup$
3
  • 8
    $\begingroup$ What are you trying to accomplish? If you are fitting a model, is hour a covariate or the response, eg? $\endgroup$ Nov 14, 2016 at 17:05
  • 2
    $\begingroup$ You could use a dummy variable for each hour if you have enough degrees of freedom to spare (i.e. treat as categorical), or use the first few Fourier terms if you haven't. In general think how to best represent a potential relation with the response - a single dummy variable flagging when the shops are open might serve. $\endgroup$ Nov 14, 2016 at 17:42
  • $\begingroup$ Something like hour can be treated as either "categorical" or "numeric" depending on what works best. There isn't a right or wrong answer in general - it depends on what works best. I'd recommend trying different things and seeing what works best in your situation. $\endgroup$ Oct 10, 2019 at 18:51

3 Answers 3

41
$\begingroup$

Depending on what you want to model, hours (and many other attributes like seasons) are actually ordinal cyclic variables. In case of seasons you can consider them to be more or less categorical, and in case of hours you can model them as continuous as well.

However, using hours in your model in a form that does not take care of cyclicity for you will not be fruitful. Instead try to come up with some kind of transformation. Using hours you could use a trigonometric approach by

xhr = sin(2*pi*hr/24)
yhr = cos(2*pi*hr/24)

Thus you would instead use xhr and yhr for modelling. See this post for example: Use of circular predictors in linear regression.

$\endgroup$
4
  • 2
    $\begingroup$ (+1) Could you elaborate on the difference between seasons & hours? $\endgroup$ Nov 14, 2016 at 17:55
  • $\begingroup$ Hmm, I think seasons have a similar meaning like morning, noon, and evening when speaking about hours during day. Imho when only vague information is available and the resolution is poor (like 4 values in seasons) considering them categorical and using dummy variables for encoding seems reasonable. :-) $\endgroup$
    – Drey
    Nov 14, 2016 at 18:04
  • 2
    $\begingroup$ I think the key point is that, because there are only 4 seasons, using the trig. approach compared to a categorical representation you spare only 1 degree of freedom - with hours of the day you spare 21 degrees of freedom. (And if you don't need to spare them, then xhr = sin(4*pi*hr/24), yhr = cos(4*pi*hr/24), & so on can be added, up to the point where with enough observations you may as well treat hours of the day as categorical.) $\endgroup$ Nov 15, 2016 at 10:06
  • 1
    $\begingroup$ Or look into cyclic splines. $\endgroup$ Aug 1, 2019 at 7:29
1
$\begingroup$

Hour of the day isn't best represented as a categorical variable, because there is a natural ordering of the values. Hair color, for example, is categorical, because the ordering of the categories has no meaning - {red, brown, blonde} is as valid as {blonde, brown, red}. Hour of the day, on the other hand, has a natural ordering - 9am is closer to 10am or 8am than it is to 6pm. It is best thought of as a discrete ordinal variable. It has an added characteristic of being cyclic, since 12am follows 11pm and precedes 1am.

$\endgroup$
3
  • $\begingroup$ Isn't there a natural ordering to the values of certain categorical variables? $\endgroup$
    – dsaxton
    Nov 14, 2016 at 17:19
  • $\begingroup$ Yes, but they're better described as ordinal in that case. Ordinal variables are categorical variables that have a natural sequence. $\endgroup$ Nov 14, 2016 at 17:20
  • 4
    $\begingroup$ So how would you represent a discrete, ordinal, cyclic variable as a predictor in a regression model? $\endgroup$ Nov 14, 2016 at 17:47
0
$\begingroup$

Theoretically, it depends on how you format the variable i.e. it can be "continuous" (modeled with a single coefficient) or categorical (a coefficient per "hour" of day). You could also do a mix of both e.g. piece-wise functions.

Practically, because 0 and 23 is essentially the same "hour" of day, I would consider grouping periods of the day into larger, more homogenous and credible groupings. For example, in 8 hour increments - 8am-4pm, 4pm-12am, and 12-8am.

$\endgroup$
8
  • 5
    $\begingroup$ 0 and 23 are distinct hours. 0 and 24 would be the same hour. $\endgroup$ Nov 14, 2016 at 17:05
  • $\begingroup$ BTW, I am assuming per Gung's comment that hour of day is an independent variable, not the modeled dependent variable. My point is that 0 and 23 is not that different in reality - would you say that there is a statistical difference between your modeled event occurring at 23:59 versus 0:01? $\endgroup$
    – Frank H.
    Nov 14, 2016 at 17:07
  • 1
    $\begingroup$ Not sure what problem throwing away information is supposed to solve. See What is the benefit of breaking up a continuous predictor variable?. $\endgroup$ Nov 14, 2016 at 17:45
  • $\begingroup$ @Scortchi - like the post says, you are assuming a continuous relationship such that binning would "throw away" information. But if that is not the case, then binning is the more appropriate transformation. And this assumes you have enough data to begin with, which the OP has not mentioned. $\endgroup$
    – Frank H.
    Nov 14, 2016 at 20:53
  • $\begingroup$ Imposing constraints on the relation between a predictor & the response isn't in itself a bad thing - as you're the first in this post to bring up, how many observations are available is an important consideration - , but the one imposed by this representation of the hour of the day - flat from the eight to the fifteenth hours, with a jump or drop at the sixteenth, & so on - seems a strange suggestion for a generally suitable approach. $\endgroup$ Nov 15, 2016 at 9:58

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.