POSIXct in a regression as continuous variable?

Viewed 2729

I'm trying to fit a regression with time of day as a continuous predictor, and a binary TRUE/FALSE outcome.

My time of day variable looks like this:

> class(sched_SMS_time)
[1] "POSIXct" "POSIXt" 
> head(sched_SMS_time)
[1] NA    "2014-01-01 11:15:00 EST" "2014-01-01 11:30:00 EST" 

My issue is that R keeps treating it in a categorical sense (i.e. as a factor), and throwing my regression models waaaaay out.

The only approach I can think of (and have found elsewhere on the stack exchange site) appears to be converting the POSIXct object to a decimal numeric counterpart, i.e.

as.numeric(str_sub(gsub(":", ".", bob_os_ten$sched_SMS),1,-4))
head(sched_SMS_time_conv)
[1]    NA 11.15 11.30 11.45 12.15 13.00

Plugging this back into the models I hope to run, this seems to give sensible results...

However, I realise this looses finer grained information. (i.e., there's no way to distinguish between 9.00 on a Monday, and 9.00 on a Tuesday).

My questions are therefore:

1) Is there an approach that allows POSIXct objects to be used directly in regressions in a continuous sense (both basic stuff, and in lme4 for multilevel data)

2) If the answer is "no", is the workaround described above the best alternative?

3 Answers
Related