Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

problems with plotting POSIXt incidence + fit objects #99

Closed
zkamvar opened this issue Jan 8, 2019 · 8 comments · Fixed by #100
Closed

problems with plotting POSIXt incidence + fit objects #99

zkamvar opened this issue Jan 8, 2019 · 8 comments · Fixed by #100

Comments

@zkamvar
Copy link
Member

zkamvar commented Jan 8, 2019

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
Copy link
Member Author

zkamvar commented Jan 8, 2019

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
Copy link
Member Author

zkamvar commented Jan 8, 2019

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
Copy link
Member Author

zkamvar commented Jan 8, 2019

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

@thibautjombart
Copy link
Contributor

Damn. Locals are gonna be horrible on this one.

@zkamvar
Copy link
Member Author

zkamvar commented Jan 9, 2019

There should be a way to deal with it.

@zkamvar
Copy link
Member Author

zkamvar commented Jan 9, 2019

This might be the solution:

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

zkamvar added a commit that referenced this issue Jan 9, 2019
This will fix #99

 - ggplot2 timezone set to UTC for POSIXt plotting
 - match the data type in `add_incidence_fit()` of the predictors and
   the incoming ggplot2 date column. This is necessary because of the
   change in #92 where fit data were changed to Date to give more
   sensible coefficients.
 - update POSIXt test plot that was accidentally saved from the KST time
   zone
@zkamvar
Copy link
Member Author

zkamvar commented Jan 9, 2019

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)

@thibautjombart
Copy link
Contributor

Wikid! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants