【发布时间】:2022-01-03 19:31:57
【问题描述】:
我正在复习otext Forecasting: Principles and Practice,在7.1一章中,作者写了一个简单的代码:
us_change %>%
model(TSLM(Consumption ~ Income)) %>%
report()
us_change数据可以通过fpp3包here找到。
我正在尝试使用retail sales 和disposable income 上的圣路易斯经济数据将其应用于我自己的数据集。我有一个关于 2013 年 1 月 - 2021 年 6 月的系列的窗口(以防有人想复制 完全我所拥有的)。这是我的代码:
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()
最后一行产生“FUN 中的错误(X[[i]], ...) : object 'Retail Sales' not found”。于是我跑了
head(z)
class(us_change)
class(z)
class(us_change$Consumption)
class(z$`Retail Sales`)
"Retail Sales" %in% names(z)
产生:
> 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
它们是同一个类,我验证了z 中存在零售销售列。我错过了什么导致这个错误?我的额头上出现皱纹的时间比我想承认的要长,所以我很感激任何帮助,谢谢!
【问题讨论】:
标签: r time-series linear-regression