问题
Do you ever look back a your old questions and feel a bit embarrassed? I just did, and now I do. And I'll probably feel the same about this one at some point.
I'm trying to move my forecasting work to fable
. In the process I'm trying to use a tsibble
. Previously with a ts
object I just set the start year and frequency. Now the tsibble
is looking for a date object. However I have data which is biannual (fall and spring semester). And the variable is irregular (which I would like to keep). Forecast
did a great job of accurately "forecasting" it. My uni names the terms with a 3 digit year and a term. So fall of the 2019-2020 school year is 2204, where the 4 represents fall. The spring is 2207.
Basically, I can't find an example online of a situation where the index is irregular in the sense of not a date object? Any hints? Thanks.
Alright, gonna try and solve this one if it kills me. I see that they added an ordered factor as a possible index. So I will try that.
Here is a reproducible example of where I am stuck.
enroll <- data.frame(term = c(2194L, 2197L, 2204L, 2207L),
ECO110 = c(518, 410, 537, 386),
ECO120 = c(315, 405, 419, 401))
enroll.tb <- enroll %>%
mutate(term = ordered(term)) %>%
select(term, starts_with("ECO")) %>%
pivot_longer(-term, names_to = "class", values_to = "enroll")
enroll.tb <- as_tsibble(enroll.tb, key = class, index = term)
fc <- enroll.tb %>%
model(arima = ARIMA()) %>%
forecast(h = 2)
Now it lets me make the tsibble, but the fable produces the error:
Error: Unsupported index type: logical
Excellent answer by Mitchell below.
However it seems factor throw more problems, turns out all is not quite fixed. ARIMA model works well, buy ETS doesn't.
fc <- enroll.tb %>%
model(ets = ETS()) %>%
forecast(new_data = enroll.future)
Throws the error Error: A model specification is trained to a dataset using the
model()function.
回答1:
The issue here is that your index variable is an ordered factor, and forecast()
does not know how to generate future values of that index.
I've added a more informative error (02fb2a), so it should now say:
fc <- enroll.tb %>%
model(arima = ARIMA()) %>%
forecast(h = 2)
#> Model not specified, defaulting to automatic modelling of the `enroll` variable.
#> Override this using the model formula.
#> Error: Cannot automatically create `new_data` from an factor/ordered time index. Please provide `new_data` directly.
Created on 2020-01-23 by the reprex package (v0.3.0)
As the error now suggests, to produce forecasts you will need to specify the appropriate factor levels in new_data
.
enroll.future <- tsibble(
term = rep(ordered(c(2214L, 2217L)), 2),
class = rep(c("ECO110", "ECO120"), each = 2),
index = term, key = class)
fc <- enroll.tb %>%
model(arima = ARIMA()) %>%
forecast(new_data = enroll.future)
#> Model not specified, defaulting to automatic modelling of the `enroll` variable.
#> Override this using the model formula.
fc
#> # A fable: 4 x 5 [1]
#> # Key: class, .model [2]
#> class .model term enroll .distribution
#> <chr> <chr> <ord> <dbl> <dist>
#> 1 ECO110 arima 2214 532. N(532, 1380)
#> 2 ECO110 arima 2217 385. N(385, 1380)
#> 3 ECO120 arima 2214 385 N(385, 2237)
#> 4 ECO120 arima 2217 385 N(385, 2237)
Created on 2020-01-23 by the reprex package (v0.3.0)
来源:https://stackoverflow.com/questions/59721148/setting-index-in-a-tsibble