Error: “Can’t obtain the interval due to the mismatched indexclass.”
I have monthly data and coerce it to a tsibble. Why does tsibblegive one-day interval
[1D]instead of one-month[1M]?
mth<-make_date("2018")+months(0:3)tsibble(mth = mth,index = mth)#> # A tsibble: 4 x 1 [1D]#> mth#> <date>#> 1 2018-01-01#> 2 2018-02-01#> 3 2018-03-01#> 4 2018-04-01The interval depends on the index class. It is unclear in thissituation to tell if it’s daily data with implicit missingness or it’smonthly data. If usingDate underlying monthly data, eachmonth could range from 28 to 31 days, which isn’t regularly spaced. Butclassyearmonth puts emphasis on 12 months per year, whichis clearly regularly spaced and the accurate representation foraggregations over months. This applies toPOSIXct forsub-daily data,Date for daily,yearquarterfor quarterly, and etc. If you encounter this error “Can’t obtain theinterval due to mismatched index class.”, it’s the same underlyingissue.
tsibble(mth =yearmonth(mth),index = mth)#> # A tsibble: 4 x 1 [1M]#> mth#> <mth>#> 1 2018 Jan#> 2 2018 Feb#> 3 2018 Mar#> 4 2018 AprDoes tsibble respect time zones?
Yes, tsibble respects time zones throughout the package. All indexfunctions includingyearweek(),yearmonth(),yearquarter(), andtime_in() take care of timezones, and will NOT convert to “UTC”. The interval obtained from thedata also respects the time zone, by converting to seconds. Thefollowing example demonstrates how tsibble handles daylight savings.
x<-ymd_h("2015-04-05 01",tz ="Australia/Melbourne")# base arithmetic respect tztsibble(time = x+ (c(0,3,6,9))*60*60,index = time)#> # A tsibble: 4 x 1 [3h] <Australia/Melbourne>#> time#> <dttm>#> 1 2015-04-05 01:00:00#> 2 2015-04-05 03:00:00#> 3 2015-04-05 06:00:00#> 4 2015-04-05 09:00:00# lubridate arithmetic doesn't respect tztsibble(time = x+hours(c(0,3,6,9)),index = time)#> # A tsibble: 4 x 1 [1h] <Australia/Melbourne>#> time#> <dttm>#> 1 2015-04-05 01:00:00#> 2 2015-04-05 04:00:00#> 3 2015-04-05 07:00:00#> 4 2015-04-05 10:00:00I would say both are correct. The displayed interval may suggest theactual time is different from what you think it is.
I have multiple units measured at different time intervals. Can Iput them into one tsibble?
tsbl1<-tsibble(time =make_datetime(2018)+hours(0:3),station ="A",index = time,key = station)%>%print()#> # A tsibble: 4 x 2 [1h] <UTC>#> # Key: station [1]#> time station#> <dttm> <chr>#> 1 2018-01-01 00:00:00 A#> 2 2018-01-01 01:00:00 A#> 3 2018-01-01 02:00:00 A#> 4 2018-01-01 03:00:00 Atsbl2<-tsibble(time =make_datetime(2018)+minutes(seq(0,90,by =30)),station ="B",index = time,key = station)%>%print()#> # A tsibble: 4 x 2 [30m] <UTC>#> # Key: station [1]#> time station#> <dttm> <chr>#> 1 2018-01-01 00:00:00 B#> 2 2018-01-01 00:30:00 B#> 3 2018-01-01 01:00:00 B#> 4 2018-01-01 01:30:00 Bbind_rows(tsbl1, tsbl2)#> # A tsibble: 8 x 2 [30m] <UTC>#> # Key: station [2]#> time station#> <dttm> <chr>#> 1 2018-01-01 00:00:00 A#> 2 2018-01-01 01:00:00 A#> 3 2018-01-01 02:00:00 A#> 4 2018-01-01 03:00:00 A#> 5 2018-01-01 00:00:00 B#> 6 2018-01-01 00:30:00 B#> 7 2018-01-01 01:00:00 B#> 8 2018-01-01 01:30:00 BCertainly you can. But tsibble only allows for one interval, becausestationA is thought of as time gaps involved. If you wantto analyse them differently, it is recommended to have separate tsibblesinstead.
I have multiple units measured at the same time interval. But thetsibble interval doesn’t look correct.
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)#> # A tsibble: 4 x 2 [1m] <UTC>#> # Key: station [2]#> time station#> <dttm> <chr>#> 1 2018-01-01 00:00:00 A#> 2 2018-01-01 00:15:00 A#> 3 2018-01-01 00:01:00 B#> 4 2018-01-01 00:16:00 BEach station shares the common 15-minute interval, but the date-timesdon’t align. Rounding them is a quick way to fix it, if binning timedoesn’t matter to the analysis. If it does, please organise them indifferent tables.
tbl%>%mutate(time =floor_date(time,unit ="15 mins"))%>%as_tsibble(index = time,key = station)#> # A tsibble: 4 x 2 [15m] <UTC>#> # Key: station [2]#> time station#> <dttm> <chr>#> 1 2018-01-01 00:00:00 A#> 2 2018-01-01 00:15:00 A#> 3 2018-01-01 00:00:00 B#> 4 2018-01-01 00:15:00 BIf it’s event data, each event couples with a precise time stamp, andmost likely you needregular = FALSE for anirregularly-spaced tsibble.