reconhub / incidence

☣:chart_with_upwards_trend::chart_with_downwards_trend:☣ Compute and visualise incidence

Home Page:https://reconhub.github.io/incidence

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

problems with plotting POSIXt incidence + fit objects

zkamvar opened this issue Β· comments

This was going to be a separate issue for an error brought up due to changes made in #92, but I realize now that something has been screwed up in general (and I thought I tested it 😞).

Basically, the POSIXt plotting is shifted forward half a day (as opposed to having the left-hand side of the bin representing the day. Moreover, because the fit is now converted to Date, it no longer can be plotted with the POSIXt data.

Here's an example:

library("incidence")
days <- 1:14
dat_cases <- round(exp(.2*(days)))
dat_dates_Date <- rep(as.Date(Sys.Date() + days), dat_cases)
dat_dates_POSIXct <- as.POSIXct(dat_dates_Date)

iD <- incidence(dat_dates_Date)
iP <- incidence(dat_dates_POSIXct)

plot(iD, fit = fit(iD))

plot(iP)

plot(fit(iP))

plot(iP, fit = fit(iP))
#> Error: Invalid input: time_trans works with objects of class POSIXct only
plot(iD, fit = fit(iP))

Created on 2019-01-08 by the reprex package (v0.2.1)

Wait.... I think I know why the data are being shifted...

library("incidence")
days <- 1:14
dat_cases <- round(exp(.2*(days)))
dat_dates_Date <- rep(as.Date(Sys.Date() + days), dat_cases)
dat_dates_POSIXct <- as.POSIXct(dat_dates_Date)

iD <- incidence(dat_dates_Date)
iP <- incidence(dat_dates_POSIXct)

as.data.frame(iP, long = TRUE)$dates + (86400/2)
#>  [1] "2019-01-09 21:00:00 KST" "2019-01-10 21:00:00 KST"
#>  [3] "2019-01-11 21:00:00 KST" "2019-01-12 21:00:00 KST"
#>  [5] "2019-01-13 21:00:00 KST" "2019-01-14 21:00:00 KST"
#>  [7] "2019-01-15 21:00:00 KST" "2019-01-16 21:00:00 KST"
#>  [9] "2019-01-17 21:00:00 KST" "2019-01-18 21:00:00 KST"
#> [11] "2019-01-19 21:00:00 KST" "2019-01-20 21:00:00 KST"
#> [13] "2019-01-21 21:00:00 KST" "2019-01-22 21:00:00 KST"
as.data.frame(iD, long = TRUE)$dates + (1/2)
#>  [1] "2019-01-09" "2019-01-10" "2019-01-11" "2019-01-12" "2019-01-13"
#>  [6] "2019-01-14" "2019-01-15" "2019-01-16" "2019-01-17" "2019-01-18"
#> [11] "2019-01-19" "2019-01-20" "2019-01-21" "2019-01-22"

Created on 2019-01-08 by the reprex package (v0.2.1)

The data are being shifted because I'm in a time zone that's 9 hours ahead and ggplot2 is considering that when plotting.

I'm going to stop here today and pick this up tomorrow

Damn. Locals are gonna be horrible on this one.

There should be a way to deal with it.

The solution, it turns out, was to use the timezone = "UTC" argument in ggplot2::scale_datetime_x() and not use as.POSIXct() with a Date object πŸ™€.

The solution to the mis-match of classes was a bit trickier. I thought I was able to do a simple as.POSIXlt(df$dates), but it turns out that operation truncates the decimal date, which is bad for us since the prediction points are represented by the middle of the interval, thus, I add half a day to the output:

df$dates <- as.POSIXlt(df$dates) + 43200 # adding half a day

And now it works :)

library("incidence")
days <- 1:14
dat_cases <- round(exp(.2*(days)))
dat_dates_Date <- rep(as.Date(Sys.Date() + days), dat_cases)
dat_dates_POSIXct <- as.POSIXct(dat_dates_Date)

iD <- incidence(dat_dates_Date)
iP <- incidence(dat_dates_POSIXct)

plot(iD, fit = fit(iD))

plot(iP, fit = fit(iP))

Created on 2019-01-09 by the reprex package (v0.2.1)

Wikid! :)