---
title: "Frequently Asked Questions and Answers"
author: "Earo Wang"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{FAQ}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r initial, echo = FALSE, cache = FALSE, results = 'hide'}
knitr::opts_chunk$set(
  warning = FALSE, message = FALSE, echo = TRUE, collapse = TRUE,
  fig.width = 7, fig.height = 6, fig.align = 'centre',
  comment = "#>"
)
options(tibble.print_min = 5)
```

```{r load}
library(dplyr)
library(tsibble)
library(lubridate)
```

> *Error: "Can't obtain the interval due to the mismatched index class."*

> *I have monthly data and coerce it to a tsibble. Why does tsibble give one-day interval `[1D]` instead of one-month `[1M]`?*

```{r}
mth <- make_date("2018") + months(0:3)
tsibble(mth = mth, index = mth)
```

The interval depends on the index class. It is unclear in this situation to tell if it's daily data with implicit missingness or it's monthly data. If using `Date` underlying monthly data, each month could range from 28 to 31 days, which isn't regularly spaced. But class `yearmonth` puts emphasis on 12 months per year, which is clearly regularly spaced and the accurate representation for aggregations over months. This applies to `POSIXct` for sub-daily data, `Date` for daily, `yearquarter` for quarterly, and etc. If you encounter this error "Can't obtain the interval due to mismatched index class.", it's the same underlying issue.

```{r}
tsibble(mth = yearmonth(mth), index = mth)
```

---

> *Does tsibble respect time zones?*

Yes, tsibble respects time zones throughout the package. All index functions including `yearweek()`, `yearmonth()`, `yearquarter()`, and `time_in()` take care of time zones, and will NOT convert to "UTC". The interval obtained from the data also respects the time zone, by converting to seconds. The following example demonstrates how tsibble handles daylight savings.

```{r}
x <- ymd_h("2015-04-05 01", tz = "Australia/Melbourne")
# base arithmetic respect tz
tsibble(time = x + (c(0, 3, 6, 9)) * 60 * 60, index = time)
# lubridate arithmetic doesn't respect tz
tsibble(time = x + hours(c(0, 3, 6, 9)), index = time)
```

I would say both are correct. The displayed interval may suggest the actual time is different from what you think it is.

---

> *I have multiple units measured at different time intervals. Can I put them into one tsibble?*

```{r}
tsbl1 <- tsibble(
  time = make_datetime(2018) + hours(0:3),
  station = "A",
  index = time, key = station
) %>% print()
tsbl2 <- tsibble(
  time = make_datetime(2018) + minutes(seq(0, 90, by = 30)),
  station = "B",
  index = time, key = station
) %>% print()
bind_rows(tsbl1, tsbl2)
```

Certainly you can. But tsibble only allows for one interval, because station `A` is thought of as time gaps involved. If you want to analyse them differently, it is recommended to have separate tsibbles instead.

---

> *I have multiple units measured at the same time interval. But the tsibble interval doesn't look correct.*

```{r}
x <- make_datetime(2018) + minutes(0:1)
tbl <- tibble(  
  time = c(x, x + minutes(15)),
  station = rep(c("A", "B"), 2)
)
as_tsibble(tbl, index = time, key = station)
```

Each station shares the common 15-minute interval, but the date-times don't align. Rounding them is a quick way to fix it, if binning time doesn't matter to the analysis. If it does, please organise them in different tables.

```{r}
tbl %>% 
  mutate(time = floor_date(time, unit = "15 mins")) %>% 
  as_tsibble(index = time, key = station)
```

If it's event data, each event couples with a precise time stamp, and most likely you need `regular = FALSE` for an irregularly-spaced tsibble.
