4
$\begingroup$

I am trying to perform multiple regression. One of the feature variables is time of the day, represented by 0 to 23. I am confused as to whether I need to use dummy coding or not. Is this a categorical variable or continuous variable?

$\endgroup$
  • 3
    $\begingroup$ How circular variables are to be included in a model depends on the setting. You will need to provide much more information to get specific answers. $\endgroup$ – Michael M Aug 3 '15 at 20:31
  • 1
    $\begingroup$ here is an example of using time of the day. @Paul had a good answer. $\endgroup$ – Haitao Du Jan 31 '18 at 16:56
18
$\begingroup$

It is neither. Actually, it is what you make it to be in your model formula, there are more than two possibilities, and there is not necessarily one correct answer among them!

If you make it categorical, then your model will have a separate, independent coefficient (or more precisely, degree of freedom) for each hour of the day. This could be too many variables to fit with your limited available data, in which case you could divide the day into halves or quarters instead of 24ths, which is what hours do.

If you make the hours variable numeric, your model will have an effect with magnitude proportional to the hour. You might want to think twice about that: it will cause a discontinuity between 11pm and midnight (23 and 0), which is not realistic for most situations (unless you have a process that is accumulating through the day and getting reset every midnight). Consider instead fitting a periodic formula like $$y \sim A \sin(2\pi h/24) + B \cos(2\pi h/24)$$ where h is the hour (numeric not categorical) and $A$,$B$ are the fit coefficients. This is just one of many possible periodic functions, all of which will have no discontinuity.

If a smooth, periodic function $f(h)$ is desired, one especially appealing option could be to find the best such curve using Generalized Additive Modeling (GAM) and cyclic regression splines. GAM is fully nonparametric for univariate functionals, automatically searching a (potentially) infinite-dimensional space of smooth, periodic functions for the one that best describes your data.

The key takeaway here is that numeric vs categorical is better thought of as a modeling choice, not a property of the data, and there are many modeling choices besides just those two. You have to consider your situation and try to find the most appropriate one.

$\endgroup$
  • 1
    $\begingroup$ Thanks,Never thought that type of variable selection can be a modelling choice $\endgroup$ – mc20 Aug 3 '15 at 22:18
  • $\begingroup$ In a marketing setting, maybe (hourly) time would be most naturally a categorical variable. The pattern of sales through the day doesn't need to vary continuously. In some natural science setting, maybe time as continuous is better choice. Depends on the setting, and on the goal of the modelling exercise. $\endgroup$ – kjetil b halvorsen Jan 12 '17 at 15:21
1
$\begingroup$

Well if you include it in levels, $0, ..., 23$ then what would be the interpretation of the $\hat{\beta}_{\text{time of day}}$?

You are including ordinal information, the coding is esstially arbitrary. You could change the value of 23 (11 PM) to 512, and it would still hold the same meaning. This is unlike (say) height, where 23cm is implies something very different from 510cm. Therefore dummuies are the way to go. You need some form of coding scheme. Most software programs have a very easy way of dealing with dummuies, for instance as.factor in R.

$\endgroup$
  • 7
    $\begingroup$ Time of day is not "ordinal" in any standard sense, because 0 comes right after 23! It clearly is a truncated circular variable; this observation alone indicates there are more parsimonious solutions to consider before one uses 24 dummy variables. $\endgroup$ – whuber Aug 3 '15 at 21:15
0
$\begingroup$

It depends on how you interpret the variable but I would be inclined to say continuous, since it is ordered and there is a natural, consistent separation between the values that can be assumed (1 hr between consecutive values). A continuous example would be if your response is the location of an object in freefall and your predictor is time and/or the square on time. A somewhat contrived categorical example would be optical character recognition, where each time point really corresponds to the character representation of that time point.

$\endgroup$
  • $\begingroup$ I agree - and would suggest you consider whether 0 and 23 should be considered as "nearby" values (as 11pm and midnight) or not. $\endgroup$ – dcorney Aug 3 '15 at 19:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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