I am trying to fit a negative binomial regression to a time series using the trending package. When I try to calculate the confidence intervals and prediction intervals, I get the error message:
Error in
cbind(): ! Not supported for tsibble. ℹ Please usedplyr::bind_cols()instead.
Can someone please help?
Thanks
library(surveillance)
library(tidyverse)
library(tsibble)
library(forecast)
library(trending)
data(campyDE)
countsdf <- campyDE%>%
select(date, case)
countsdf$date <- as.Date(countsdf$date)
countsdf <- countsdf %>%
mutate(epiweek = yearweek(date, week_start = 1))
countsdf <- tsibble(countsdf, index = epiweek)
countsdf$fourier <- select(countsdf, epiweek, case) %>%
fourier(K = 1)
head(countsdf)
model <- glm_nb_model(
case ~
epiweek +
fourier)
fitted_model <- trending::fit(model, countsdf)
observed <- predict(fitted_model, simulate_pi = FALSE)
The problem is you are mixing multiple packages that are not designed to work together, and eventually that causes a clash. In particular, the
tsibblepackage is designed to work withfablefor modelling, not withtrendingor withforecast.The only part of the code that relies on
tsibblehere is in the creation ofepiweekusingyearweek(), and in obtaining the weekly frequency of the data insidefourier(). The only part that relies onforecastis in the creation of the Fourier terms.Here is a modification of your code that avoids the problems by not creating a tsibble object, and computing the two fourier columns directly, rather than via the
forecast::fourier()function.Created on 2022-04-27 by the reprex package (v2.0.1)