对于使用fable
软件包进行预测的绘图,我有以下代码。我希望找到一个解决方案,将预报风扇连接到实际的geom_line
,以便两者之间没有空隙。另外,有没有办法改变风扇的颜色?我尝试添加
例如,col = 'maroon'
括号中的autoplot
似乎不起作用。
这是我的数据集cpi
的可复制性
structure(list(Date = structure(c(1356998400, 1359676800, 1362096000,
1364774400, 1367366400, 1370044800, 1372636800, 1375315200, 1377993600,
1380585600, 1383264000, 1385856000, 1388534400, 1391212800, 1393632000,
1396310400, 1398902400, 1401580800, 1404172800, 1406851200, 1409529600,
1412121600, 1414800000, 1417392000, 1420070400, 1422748800, 1425168000,
1427846400, 1430438400, 1433116800, 1435708800, 1438387200, 1441065600,
1443657600, 1446336000, 1448928000, 1451606400, 1454284800, 1456790400,
1459468800, 1462060800, 1464739200, 1467331200, 1470009600, 1472688000,
1475280000, 1477958400, 1480550400, 1483228800, 1485907200, 1488326400,
1491004800, 1493596800, 1496275200, 1498867200, 1501545600, 1504224000,
1506816000, 1509494400, 1512086400, 1514764800, 1517443200, 1519862400,
1522540800, 1525132800, 1527811200, 1530403200, 1533081600, 1535760000,
1538352000, 1541030400, 1543622400, 1546300800, 1548979200, 1551398400,
1554076800, 1556668800, 1559347200, 1561939200, 1564617600, 1567296000,
1569888000, 1572566400, 1575158400, 1577836800, 1580515200, 1583020800,
1585699200, 1588291200, 1590969600, 1593561600), class = c("POSIXct",
"POSIXt"), tzone = "UTC"), CPI = c(100.943613610327, 101.355726290109,
101.920519704091, 102.251765014058, 102.399483334481, 102.654230611209,
103.366370423635, 103.771996583604, 104.069828647932, 104.475897454947,
104.745585890252, 104.9, 105.877675706645, 106.600613244374,
107.25658797107, 108.285287342243, 108.607710827378, 108.935592526775,
109.11670321665, 109.390661099815, 109.563232156331, 109.694215435852,
109.939646273932, 109.754097918499, 110.601049654351, 110.415206179718,
110.905507883552, 111.45837834832, 111.873469766967, 112.253828314821,
112.699336213665, 113.056054221625, 113.204653466884, 113.387164759728,
113.581282843726, 113.810860009533, 116.506784014018, 117.199721025597,
118.107968739773, 118.823678758349, 119.420709143437, 119.808600479962,
120.575551335206, 120.774779709305, 121.014544917053, 121.61732414169,
121.917354377998, 122.116542025261, 126.058371342546, 126.285551233707,
126.43426615261, 126.763103151148, 126.92061331762, 127.095652703716,
127.146439944094, 127.257270861715, 127.754395868046, 127.897364611267,
128.227889139291, 128.426778898969, 130.540032633942, 130.730222134177,
130.87769195147, 131.302356289165, 131.797387843531, 132.126557217198,
132.823218725753, 132.868685232286, 133.870800057958, 134.439906096246,
135.351580975176, 135.040382301698, 136.620612224767, 136.503608878263,
136.763944144826, 137.24925661824, 137.169191683167, 137.331600194512,
137.656945057261, 137.792027588476, 137.792027588476, 138.493686354623,
138.681976535356, 138.535078801086, 139.421769773802, 139.848223614133,
139.983926150073, 139.504431667605, 139.994961370897, 140.280481556844,
140.529583177439)), row.names = c(NA, -91L), class = c("tbl_df",
"tbl", "data.frame"))
这是我的模型+情节的实际代码
library(tidyverse)
library(tsibble)
library(fable)
cpi <- cpi %>%
mutate(Date = yearmonth(Date)) %>%
as_tsibble(index=Date)
fit <- cpi %>%
model(arima = ARIMA(log(CPI)))
fit %>%
forecast(h="1 year") %>%
autoplot(cpi %>% slice(tail(row_number(), 12)), level=seq(10,90,by=10)) +
theme(legend.position="none")
答案 0 :(得分:0)
这可以通过以下方式来实现:通过select(slice(., 1), Date, CPI = .mean))
从预测返回的df中获取长度为1的切片并将其通过dplyr::bind_rows()
绑定到您的数据。为了绑定两个数据集,仅从模型df中选择列Date
和。mean
并将.mean
重命名为CPI
。关于风扇的填充颜色。这可以通过操纵自动绘图返回的ggplot对象来实现。通过反复试验,我设法通过p$layers[[1]]$aes_params$fill <- "red"
更改了填充颜色。试试这个:
library(tidyverse)
library(tsibble)
library(fable)
#> Loading required package: fabletools
cpi <- cpi %>%
mutate(Date = yearmonth(Date)) %>%
as_tsibble(index=Date)
fit <- cpi %>%
model(arima = ARIMA(log(CPI)))
p <- fit %>%
forecast(h="1 year") %>%
autoplot(bind_rows(cpi %>% slice(tail(row_number(), 12)), select(slice(., 1), Date, CPI = .mean)), level=seq(10,90,by=10), show_gap = TRUE) +
theme(legend.position="none")
p$layers[[1]]$aes_params$fill <- "red"
p