如何将多个Forecast :: accuracy()结果的输出合并到表中以进行比较和绘图

时间:2018-12-24 20:33:57

标签: r ggplot2 tidyverse forecasting

我在4对时间序列上调用了forecast::accuracy(),每个时间序列的输出都是2x8的精度测量矩阵。

> acc1
                        ME      RMSE       MAE      MPE     MAPE      MASE        
ACF1 Theil's U
Training set -3.918303e-05 0.5315159 0.3574662 86.62683 164.6431 0.6785569 
-0.00042333        NA
Test set      6.959079e-02 0.6452657 0.4388499 61.94763 105.3055 0.8330428  
0.45847534 0.9572293

实际上,我想要{strong>仅RMSE列,两行每个系列的结果ggplot >。该图可能应为每个系列包括不同的颜色,以及与火车/测试行相对应的2个点符号。但是为此,我需要将此信息放入整洁的数据框中。

从这四个矩阵到整洁的数据帧需要执行哪些步骤?我已经加载了tidyverse,但是我也愿意接受R的基础。

可复制的数据:

library(forecast)

x <- NULL
x$obs <- runif(3652)
x <- as.data.frame(df)
y <- NULL
y$obs <- runif(365)
y <- as.data.frame(df)

x1 <- ts(x$obs, start=c(2007, 10), end=c(2017, 9), frequency = 365)
y1 <- ts(y$obs, start=c(2016, 9), end=c(2017, 9), frequency = 365)
x2 <- ts(x$obs, start=c(2007, 10), end=c(2017, 9), frequency = 365)
y2 <- ts(y$obs, start=c(2016, 9), end=c(2017, 9), frequency = 365)
x3 <- ts(x$obs, start=c(2007, 10), end=c(2017, 9), frequency = 365)
y3 <- ts(y$obs, start=c(2016, 9), end=c(2017, 9), frequency = 365)
x4 <- ts(x$obs, start=c(2007, 10), end=c(2017, 9), frequency = 365)
y4 <- ts(y$obs, start=c(2016, 9), end=c(2017, 9), frequency = 365)

acc1 <- accuracy(x1,y1)
acc2 <- accuracy(x2,y2)
acc3 <- accuracy(x3,y3)
acc4 <- accuracy(x4,y4)

1 个答案:

答案 0 :(得分:2)

我用不同的颜色和不同的符号绘制了训练和测试数据的RMSE。

acc1 = data.frame(matrix(data = 
c(-3.918303e-05, 0.5315159, 0.3574662, 86.62683, 164.6431, 0.6785569, -0.00042333, NA,
  6.959079e-02, 0.6452657, 0.4388499, 61.94763, 105.3055, 0.8330428, 0.45847534, 0.9572293), nrow = 2, byrow = TRUE) )
rownames(acc1) = c("Training set", "Test set")      
colnames(acc1) = c("ME", "RMSE", "MAE", "MPE", "MAPE", "MASE", "ACF1", "Theil's U")      

acc2 = acc1 + 1
acc3 = acc1 + 2
acc4 = acc1 + 3

lsAcc = list(acc1, acc2, acc3, acc4)
dfAcc = do.call(what = rbind, args = lsAcc)
dfAcc$rowPair = ceiling(1:8 * 0.5)

plot(0, cex=0, xlim=c(0,5), ylim=c(0,4))
points(x = dfAcc$rowPair[grepl(pattern = "Training", rownames(dfAcc))], 
       y = dfAcc$RMSE[   grepl(pattern = "Training", rownames(dfAcc))],
       pch = 2, col="blue")
points(x = dfAcc$rowPair[grepl(pattern = "Test", rownames(dfAcc))], 
       y = dfAcc$RMSE[   grepl(pattern = "Test", rownames(dfAcc))],
       pch = 3, col="red")

enter image description here

Z.Lin问您,您的代码不可复制。您缺少定义对象x和y的信息。 我使用您的部分数据重新创建数据集。