如何在R中绘制预测的子集?

问题描述 投票:1回答:1

我有一个简单的R脚本来创建基于文件的预测。数据自2014年以来已被记录,但我无法完成以下两个目标:

  • 仅绘制预测信息的一部分(从11月开始)。
  • 以特定格式包括月份和年份(即Jun 17)。

这里是dataset的链接,下面你会找到我到目前为止的代码。

# Load required libraries
library(forecast)
library(ggplot2)

# Load dataset
emea <- read.csv(file="C:/Users/nsoria/Downloads/AMS Globales/EMEA_Depuy_Finanzas.csv", header=TRUE, sep=';', dec=",")

# Create time series object
ts_fin <- ts(emea$Value, frequency = 26, start = c(2014,11))

# Pull out the seasonal, trend, and irregular components from the time series 
model <- stl(ts_fin, s.window = "periodic")

# Predict the next 3 bi weeks of tickets
pred <- forecast(model, h = 5)

# Plot the results
plot(pred, include = 5, showgap = FALSE, main = "Ticket amount", xlab = "Timeframe", ylab = "Quantity")

我感谢对我的两点和一个干净的情节的任何帮助和建议。

提前致谢。

编辑01/10 - 问题1:我添加了建议代码的屏幕截图输出。 Plot1

编辑01/10 - 问题2:一旦用下面的代码转换,它就会错过日期计数并弄乱结果。请参阅两个屏幕截图并比较最后一个值。

Screenshot 1 Screenshot 2

r plot data-science
1个回答
0
投票

绘图使用ggplot2 w / ggfortifytidyverselubridatescales

    library(lubridate)
    library(tidyverse)
    library(scales)
    library(ggfortify)

    # Convert pred from list to data frame object
    df1 <- fortify(pred) %>% as_tibble()

    # Convert ts decimal time to Date class
    df1$Date <- as.Date(date_decimal(df1$Index), "%Y-%m-%d")
    str(df1)

    # Remove Index column and rename other columns
    # Select only data pts after 2017
    df1 <- df1 %>% 
      select(-Index) %>% 
      filter(Date >= as.Date("2017-01-01")) %>% 
      rename("Low95" = "Lo 95",
             "Low80" = "Lo 80",
             "High95" = "Hi 95",
             "High80" = "Hi 80",
             "Forecast" = "Point Forecast")
    df1

    ### Updated: To connect the gap between the Data & Forecast, 
    # assign the last non-NA row of Data column to the corresponding row of other columns
    lastNonNAinData <- max(which(complete.cases(df1$Data)))
    df1[lastNonNAinData, !(colnames(df1) %in% c("Data", "Fitted", "Date"))] <- df1$Data[lastNonNAinData]

    # Or: use [geom_segment](http://ggplot2.tidyverse.org/reference/geom_segment.html)

    plt1 <- ggplot(df1, aes(x = Date)) +   
      ggtitle("Ticket amount") +
      xlab("Time frame") + ylab("Quantity") +
      geom_ribbon(aes(ymin = Low95, ymax = High95, fill = "95%")) +
      geom_ribbon(aes(ymin = Low80, ymax = High80, fill = "80%")) +
      geom_point(aes(y = Data, colour = "Data"), size = 4) +
      geom_line(aes(y = Data, group = 1, colour = "Data"), 
                linetype = "dotted", size = 0.75) +
      geom_line(aes(y = Fitted, group = 2, colour = "Fitted"), size = 0.75) +
      geom_line(aes(y = Forecast, group = 3, colour = "Forecast"), size = 0.75) +
      scale_x_date(breaks = scales::pretty_breaks(), date_labels = "%b %y") +
      scale_colour_brewer(name = "Legend", type = "qual", palette = "Dark2") +
      scale_fill_brewer(name = "Intervals") +
      guides(colour = guide_legend(order = 1), fill = guide_legend(order = 2)) +
      theme_bw(base_size = 14)
    plt1

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.