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?
-
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 MCommented Aug 3, 2015 at 20:31
-
1$\begingroup$ here is an example of using time of the day. @Paul had a good answer. $\endgroup$– Haitao DuCommented Jan 31, 2018 at 16:56
3 Answers
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.
-
1$\begingroup$ Thanks,Never thought that type of variable selection can be a modelling choice $\endgroup$– mc20Commented Aug 3, 2015 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$ Commented Jan 12, 2017 at 15:21
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.
-
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 ♦Commented Aug 3, 2015 at 21:15
-
$\begingroup$ @whuber ok so if I am predicting number of car accidents per hour given categorical weather conditions and datetime (month(1-12), weekday (1-7), hour 0-23), creating dummy variables for all 23 hours is not what we should do. What are your suggestions if we want to consider rush hour times etc? Make categorical variables i.e. morning_rush, evening_rush (only for Mon-Fri)? What's the convention for accident datasets? Thanks. $\endgroup$ Commented Dec 17, 2020 at 11:16
-
$\begingroup$ @ShibuyaShoto Some common options include seasonal ARIMA models and circular splines as well as indicators of predictable, known factors such as day of week or a holiday indicator. $\endgroup$– whuber ♦Commented Dec 17, 2020 at 15:07
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.
-
$\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$– dcorneyCommented Aug 3, 2015 at 19:57