Error in FUN(X[[i]], ...) : object 'Retail Sales' not found | otext - Forecasting: Principles and Practice

Viewed 69

I'm going over the otext Forecasting: Principles and Practice, and in chapter 7.1, the author writes a simple code:

us_change %>%
  model(TSLM(Consumption ~ Income)) %>%
  report()

us_change data can be found through the fpp3 package here.

I'm trying to apply this to my own data set using St. Louis economic data on retail sales and disposable income. I have a window on the series from Jan2013 - Jun2021 (in case anyone wants to reproduce exactly what I have). Here is my code:

library(tidyverse)
library(readxl)
library(dplyr)
library(report)
library(tsibble)
library(fpp3)
library(fable)

data("us_change")

rs <- read_excel("Retail Sales.xlsx",
                 sheet = "Retail Sales")

rdpi <- read_excel("RDPI.xlsx",
                   sheet = "RDPI - Edit")

us_change %>%
  model(TSLM(Consumption ~ Income)) %>%
  report()

z <- cbind(rs,rdpi)

# both data sets had a "Date" column
z <- z[, !duplicated(colnames(z))]

z <- z %>%
  as_tsibble(
    key = c(`Retail Sales`,RDPI),
    index = Date)

z <- z[order(z$Date),]

z %>%
  model(TSLM(`Retail Sales` ~ RDPI)) %>%
  report()

The last line produces "Error in FUN(X[[i]], ...) : object 'Retail Sales' not found". So I ran

head(z)
class(us_change)
class(z)
class(us_change$Consumption)
class(z$`Retail Sales`)
"Retail Sales" %in% names(z)

which produces:

> head(z)
# A tsibble: 6 x 3 [?] <UTC>
# Key:       Retail Sales, RDPI [6]
  Date                `Retail Sales`   RDPI
  <dttm>                       <dbl>  <dbl>
1 2013-01-01 00:00:00         333663 12259.
2 2013-02-01 00:00:00         332471 12225.
3 2013-03-01 00:00:00         374171 12262.
4 2013-04-01 00:00:00         362978 12299 
5 2013-05-01 00:00:00         389709 12359.
6 2013-06-01 00:00:00         369355 12362.
> class(us_change)
[1] "tbl_ts"     "tbl_df"     "tbl"        "data.frame"
> class(z)
[1] "tbl_ts"     "tbl_df"     "tbl"        "data.frame"
> class(us_change$Consumption)
[1] "numeric"
> class(z$`Retail Sales`)
[1] "numeric"
> "Retail Sales" %in% names(z)
[1] TRUE

They're the same class, and I verified that the retail sales column exists in z. What am I missing that's causing this error? I've been putting wrinkles on my forehead at this longer than I'd like to admit, so I would appreciate any help, thank you!

1 Answers

When you made Retail Sales the key, it was no longer an option. If you set no parameters with as_tsibble() it will work fine.

z <- z %>% as_tsibble()

z %>%
  model(TSLM(`Retail Sales` ~ RDPI)) %>%
  report()
Related