Error in as.POSIXct.numeric(index(dfplot)) : 'origin' must be supplied

base-r

#1

Hello, I'm troubleshooting another person's code, where I am the noob. There is an error thrown saying that I did not specify an origin - I thought the origin was automatically 1970-01-01 00:00:00 and that I do not need to specify. Curious too is that code used to work 2 weeks ago, but now does not. How do I correct this error? Below is the partial code (I can post the full script too if that's helpful)

Error in as.POSIXct.numeric(index(dfplot)) : 'origin' must be supplied


    dfplot <- merge(as.xts(InputDataTimeSeries), as.xts(pred$lower))
    dfplot <- merge(dfplot, as.xts(pred$mean))
    dfplot <- merge(dfplot, as.xts(pred$upper))
    dfplot <- merge(dfplot, as.xts(pred$fitted))

    names(dfplot)[1:7] <- c("actuals", "lower80", "lower95", "predicted", "upper80", "upper95", "fitted")

    ggplot(dfplot, aes(x=as.POSIXct(index(dfplot)))) +
    geom_line(aes(y=fitted), col='grey90', size = 2) +
    geom_line(aes(y=predicted), col='orange', size= 1) +
    geom_line(aes(y=actuals), col='orange', size= 1) +

    geom_ribbon(aes(ymin=lower80,ymax=upper80),alpha=0.3, fill="orange") +
    geom_ribbon(aes(ymin=lower95,ymax=upper95),alpha=0.1, fill="orange") +
    theme_bw() +
    geom_vline(xintercept=as.numeric(as.POSIXct(end(InputDataTimeSeries))), linetype="dashed", fill="gray40") +
    labs(title=paste(pred$method, "80/95% PI Bands" ) , x="Time", y="Observed / Fitted") +
    theme(plot.title = element_text(size=18, face="bold"))

#2

The specific as.POSIXct() method for numeric objects does not assume an origin, and requires you to supply one. This is unfortunately less obvious than it could be in the documentation! But we can confirm it for ourselves by examining the code directly, by running

as.POSIXct.numeric

(note lack of parentheses!) which gives the output:

function (x, tz = "", origin, ...) 
{
    if (missing(origin)) 
        stop("'origin' must be supplied")
    .POSIXct(as.POSIXct(origin, tz = "GMT", ...) + x, tz)
}

Reading between the lines in the documentation, I suspect this choice was made with the assumption that if you’ve got a vector with numbers of seconds in it, you are likely converting from some other system that uses a different origin date (see the examples, which include conversions from data using SAS and Stata conventional origins).