reconhub / incidence

☣:chart_with_upwards_trend::chart_with_downwards_trend:☣ Compute and visualise incidence
https://reconhub.github.io/incidence
Other
58 stars 13 forks source link

problems with plotting POSIXt incidence + fit objects #99

Closed zkamvar closed 5 years ago

zkamvar commented 5 years ago

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)

zkamvar commented 5 years ago

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)

zkamvar commented 5 years ago

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

zkamvar commented 5 years ago

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

thibautjombart commented 5 years ago

Damn. Locals are gonna be horrible on this one.

zkamvar commented 5 years ago

There should be a way to deal with it.

zkamvar commented 5 years ago

This might be the solution:

https://stackoverflow.com/a/35850474/2752888

zkamvar commented 5 years ago

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:

https://github.com/reconhub/incidence/blob/d9bc202e3287a354bd5c4adfa72c9adab5cd413a/R/plot.R#L341

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)

thibautjombart commented 5 years ago

Wikid! :)