CloverCeline
CloverCeline

Reputation: 541

tsibble with 2 keys (not hierarchy) having the week of the year as index

Can anyone give some suggestions as to how to construct a tsibble?

I have a dateset that has four original columns: product, market, price, date I want to construct a tsibble object. I have the key=id(product,market) and 'year of the week' as index. then I can predict for each product in each market what should be the base line of price.

if you use nycflights13::weather data set, I can use key=id(origin,year) (here I do not have a market so use year to represent the market).

then have idex=week(year+month+day). I can then combine the year, month, day column as date then calculate the week() then add year and week together as 'weekyear' and set up this as an index, then use median(temp) for that yearweek. After this dataset reform I can have a tsibble able to predict next 2-4 weeks temp.

Upvotes: 0

Views: 1133

Answers (1)

Earo Wang
Earo Wang

Reputation: 789

Looks like you want to convert the date variable to yearweek class first. Can you do

library(tsibble)
data %>%
  mutate(index = yearweek(date)) %>%
  as_tsibble(key = id(product, market), index = index)

Upvotes: 2

Related Questions