SqueakyBeak
SqueakyBeak

Reputation: 434

Question with using time series in R for forecasting via example

Im working through this example. However, when I begin investigating the tk_ts output I don't think it is taking the start/end data Im entering correctly, but am unsure as to what the proper input is if I want it to start at 12-31-2019 and end at 7-17-2020:

daily_cases2 <- as_tibble(countrydatescases) %>%
    mutate(Date = as_date(date)) %>%
    group_by(country, Date) %>%
    summarise(total_cases = sum(total_cases))

  daily_cases2$total_cases <- as.double(daily_cases2$total_cases)
  
  # Nest
  daily_cases2_nest <- daily_cases2 %>%
    group_by(country) %>%
    tidyr::nest()
  
# TS
  daily_cases2_ts <- daily_cases2_nest %>%
    mutate(data.ts = purrr::map(.x  = data, 
                         .f       = tk_ts, 
                         select   = -Date,
                         start    = 2019-12-31,
                         freq     = 1))

Here is what I get when I examine it closely:

enter image description here

enter image description here

When I go through the example steps with these parameters the issue is also then seen in the subsequent graph:

enter image description here

I've tried varying the frequency and start parameters and its just not making sense. Any suggestions?

Upvotes: 0

Views: 75

Answers (1)

Edward
Edward

Reputation: 19394

You've given the start and end dates, but you haven't said what frequency you want. Given that you want the series to start at the end of 2019 and end in the middle of July, 2020, I'm guessing you want a daily time series. In that case, the code should be:

daily_cases2_ts <- daily_cases2_nest %>%
    mutate(data.ts = purrr::map(.x  = data, 
                         .f       = tk_ts, 
                         select   = -Date,
                         start    = c(2019, 365),  # day 365 of year 2019
                         freq     = 365))          # daily series

Upvotes: 2

Related Questions