英文:
How to print consistent R visualizations of decomposed time series using autoplot()
问题
我有一个R脚本,有时以一种方式生成分解的时间序列图,有时以另一种方式生成。我无法弄清楚它是使用了两个不同的autoplot()函数还是其他什么原因导致这种情况发生。这似乎是随机的:一个星期我得到一种格式,下一个星期我得到另一种格式。
我更喜欢带有标题"Decomposition of additive time series."的图表。有人能告诉我为什么运行相同的脚本会给我不同的结果吗?
谢谢
library(pacman)
p_load(tidyverse, janitor, lubridate, forecast)
# 导入数据
df <- clean_names(read_csv("data.csv")) %>%
mutate(date = mdy(date)) %>%
select(-dpb)
# 创建月度数据框
df_monthly <- df %>%
mutate(month = floor_date(date, unit = "month")) %>%
group_by(month) %>%
summarise(bbl = sum(bbl),
rev = sum(rev)) %>%
mutate(dpb = rev/bbl) %>%
select(month, dpb)
# 分解时间序列
t <- ts(data = df_monthly[,-1], start = c(2014, 1, 1), frequency = 12)
d <- decompose(t, type = "additive")
# 绘制分解的时间序列图
p <- autoplot(d)
print(p)
我尝试过指定ggplot2::autoplot()和forecast::autoplot(),以为这样可以解决我的问题,但没有成功。目前它们都生成了我不想要的图表。
英文:
I have an R script that sometimes produces plots of decomposed time series one way and sometimes another way. I can't figure out if it's using two different autoplot() functions or what else could be making this happen. It is seemingly random: one week I get one format, the next week I get the other one.
I prefer the plot with the title "Decomposition of additive time series." Can anyone tell why running the same script gives me different results?
Thanks
library(pacman)
p_load(tidyverse, janitor, lubridate, forecast)
# import data
df <-
clean_names(
read_csv("data.csv")) %>%
mutate(date = mdy(date)) %>%
select(-dpb)
# create monthly df
df_monthly <- df %>%
mutate(month = floor_date(date, unit = "month")) %>%
group_by(month) %>%
summarise(bbl = sum(bbl),
rev = sum(rev)) %>%
mutate(dpb = rev/bbl) %>%
select(month, dpb)
# Decompose time series
t <- ts(data = df_monthly[,-1], start = c(2014, 1, 1), frequency = 12)
d <- decompose(t, type = "additive")
# Plot decomposed time series
p <- autoplot(d)
print(p)
I have tried specifying ggplot2::autoplot() and forecast::autoplot() thinking that would answer my question, but to no avail. Currently they are both producing the plot I don't want.
答案1
得分: 1
几乎可以肯定,差异是由于你加载的软件包不同造成的。你问题中的代码应该使用 forecast 包中的 autoplot.decomposed.ts()
函数。但是,如果你加载了其他软件包,可能其中一个软件包有一个类似的函数被使用了。特别是,ggfortify 软件包有一个 autoplot.decomposed.ts()
函数,所以如果你在加载 forecast 之后加载 ggfortify,你会得到一个不同的图形。
你可以通过在函数前加上软件包名称来指定你想要使用的函数。例如:
forecast:::autoplot.decomposed.ts(d)
因为具体的方法没有被导出,所以你需要使用 3 个点,而不是 2 个点。
英文:
Almost certainly the differences arise because of the packages you have loaded. The code in your question should use the autoplot.decomposed.ts()
function from the forecast package. But if you have loaded other packages, it is possible one of them has a similar function that is being used instead. In particular, the ggfortify package has an autoplot.decomposed.ts()
function, so if you load ggfortify after you load forecast, you will get a different plot.
You can specify which one you want be prefixing the function with the package name. For example
forecast:::autoplot.decomposed.ts(d)
Because the specific method is not exported, you need 3 dots, not 2 dots.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论