我是 tsibble 包的新手。我有每月数据,我将其强制转换为 tsibble 以使用寓言包。我遇到的一些问题
library(dplyr)
library(fable)
library(lubridate)
library(tsibble)
test <- data.frame(
YearMonth = c(20160101, 20160201, 20160301, 20160401, 20160501, 20160601,
20160701, 20160801, 20160901, 20161001, 20161101, 20161201),
Claims = c(13032647, 1668005, 24473616, 13640769, 17891432, 11596556,
23176360, 7885872, 11948461, 16194792, 4971310, 18032363),
Revenue = c(12603367, 18733242, 5862766, 3861877, 15407158, 24534258,
15633646, 13720258, 24944078, 13375742, 4537475, 22988443)
)
test_ts <- test %>%
mutate(YearMonth = ymd(YearMonth)) %>%
as_tsibble(
index = YearMonth,
regular = FALSE #because …Run Code Online (Sandbox Code Playgroud) 这个问题是关于包 GRATIS 中的 generate_msts() 函数。
我添加了一些新东西(使函数可以选择将其输出转换为可爱的 tsibble 格式或保留原始的“列表”格式)并准备更新到 CRAN。
新代码添加如下(代码的详细信息以及问题底部显示的示例)
我想知道我应该得到 tsibble 索引吗?但是生成的数据好像没有索引?
output <- if (output_format == "list") {
res #this is output name defined before
} else if (output_format == "tsibble") {
as_tsibble(res)
}
return(output)
}
Run Code Online (Sandbox Code Playgroud)
作为指导,我在Vignette 中更新了此函数的相应示例。然后事情变得连贯起来。
如果我没有保存生成的时间序列输出(例如 x <- my_function()),则小插图 无法编织。(不过我可以在独立的普通RMD文件中直接使用这个功能)
直接使用这段代码可以在RStudio里面显示输出,但是不能编出来。
my_function(seasonal.periods = c(7, 365), n = 800, nComp = 2,output_format="tsibble")
Run Code Online (Sandbox Code Playgroud)
Error in Fun(X[[i]],...): 'list' object cannot be coerced to type 'integer' Calls: <Anonymous>...
as.data.frame -> head -> head.data.frame -> lappy -> …Run Code Online (Sandbox Code Playgroud) 我想预测在服务时间内进入商店的顾客数量。我有每小时的数据
因此,我认为我的时间序列实际上是有规律的,但在某种意义上是非典型的,因为我每天有 10 个小时,每周有 5 天。
我可以通过将非服务时间设置为零来对这个常规的 24/7 时间序列进行建模,但我发现这样做效率低下而且也不正确,因为时间并没有丢失。相反,它们并不存在。
使用旧的ts框架我能够明确指定
myTS <- ts(x, frequency = 10)
Run Code Online (Sandbox Code Playgroud)
然而,在新的tsibble/fable框架内这是不可能的。它检测每小时的数据,预计每天 24 小时,而不是 10 小时。每个后续函数都会提醒我隐含的时间间隙。手动覆盖interval-Attribute 有效:
> attr(ts, "interval") <- new_interval(hour = 10)
> has_gaps(ts)
# A tibble: 1 x 1
.gaps
<lgl>
1 FALSE
Run Code Online (Sandbox Code Playgroud)
但对建模没有影响:
model(ts,
snaive = SNAIVE(customers ~ lag("week")))
Run Code Online (Sandbox Code Playgroud)
我仍然收到相同的错误消息:
snaive [1] 遇到 1 个错误。数据包含隐式时间间隙。您应该检查数据并
tsibble::fill_gaps()根据需要使用将隐式间隙转换为显式缺失值。
任何帮助,将不胜感激。
我一直在使用该tsibble包,但我不知道如何从聚合结果中删除时间分量的正确方法。因此,在下面的数据集中,我想要按地区和州划分的平均行程。tsibble将 转换为 a 的正确方法是tibble(可能是,我只是不确定)还是我缺少一些选项来实现聚合?
library(tsibble)
library(dplyr)
tourism %>% group_by(Region, State) %>% summarise(Mean_trips = mean(Trips))
# A tsibble: 6,080 x 4 [1Q]
# Key: Region, State [76]
# Groups: Region [76]
Region State Quarter Mean_trips
<chr> <chr> <qtr> <dbl>
1 Adelaide South Australia 1998 Q1 165.
2 Adelaide South Australia 1998 Q2 112.
3 Adelaide South Australia 1998 Q3 148.
## This is not what I want, this is what I want:
tourism %>% as_tibble …Run Code Online (Sandbox Code Playgroud) 我目前正在阅读《预测:原理与实践》,第 3 版,该书可在网上免费获取。要复制我的问题,您需要安装然后加载包fpp3。然后,您需要在 R(或 RStudio)中执行以下操作:
PBS %>%\n filter(ATC2 == "A10") %>%\n select(Month, Concession, Type, Cost) %>%\n summarise(TotalC = sum(Cost)) %>%\n mutate(Cost = TotalC / 1e6) -> a10\nRun Code Online (Sandbox Code Playgroud)\n定义 后a10,需要绘制tsibble,如下所示:
autoplot(a10, Cost) +\n labs(y = "$ (millions)",\n title = "Australian antidiabetic drug sales")\nRun Code Online (Sandbox Code Playgroud)\n然后你应该得到这样的情节:
\n\n大多数情况下都很好,但我希望 x 轴上的刻度数至少是两倍,如果它们可以旋转 45\xc2\xb0 左右,可能会更多。我尝试了一些看起来很有希望的东西,它添加scale_x_date(date_labels = "%m-%Y")到绘图对象中,但只适用于类对象Date,而这里我们正在处理yearmonths. 在这种情况下如何获得更详细的x轴?
我如何才能从 mable 中单独报告每个模型。
示例代码(来自https://otexts.com/fpp3/holt-winters.html)
library(fabletools)
library(fable)
library(forecast)
library(tsibble)
library(feasts)
aus_holidays <- tourism %>%
filter(Purpose == "Holiday") %>%
summarise(Trips = sum(Trips))
fit <- aus_holidays %>%
model(
additive = ETS(Trips ~ error("A") + trend("A") + season("A")),
multiplicative = ETS(Trips ~ error("M") + trend("A") + season("M"))
)
fc <- fit %>% forecast(h = "3 years")
fc %>%
autoplot(aus_holidays, level = NULL) + xlab("Year") +
ylab("Overnight trips (millions)") +
scale_color_brewer(type = "qual", palette = "Dark2")
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,我想分别报告加法模型和乘法模型。我尝试过report(fc$additive),但这不起作用。或者,我可以一次安装一个模型,并且report(fc).
| 日期 |
|---|
| 1960年第一季度 |
| 1960年第二季度 |
| 1960年第三季度 |
| 1960年第四季度 |
| 1961年第一季度 |
| 1961年第二季度 |
我有以下数据框。我正在尝试将第一列放入 tsibble 中。现在我有一个问题。我如何切换到日期以便可以将其读取为季度。
我尝试zoo使用
DATA.QTR <- DATA.QTR %>% mutate(QUARTER = as.Date(as.yearqtr(Date, "%Y %Q")))
Run Code Online (Sandbox Code Playgroud)
但它没有读它。