从预测对象中提取值并绘制箱形图 [英] Extracting values and plot Box-plot from forecast objects

查看:109
本文介绍了从预测对象中提取值并绘制箱形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用带有几种模型的预测程序包进行了一些预测,您可以在下面看到该模型的示例:

I made some forecast with forecast package with several models.Example of this models you can see below:

    # CODE

library(fpp2) # required for the data
library(dplyr)
library(forecast)
#HOLT WINTER
fc <- hw(subset(hyndsight,end=length(hyndsight)-35),
         damped = TRUE, seasonal="multiplicative", h=35)
autoplot(hyndsight) +
  autolayer(fc, series="HW multi damped", PI=FALSE)+
  guides(colour=guide_legend(title="Daily forecasts"))
#ETS
ets_f <- forecast(subset(hyndsight,end=length(hyndsight)-35),
        , h=35)
autoplot(hyndsight) +
  autolayer(ets_f, series="ETS", PI=FALSE)+
  guides(colour=guide_legend(title="Daily forecasts"))

因此,下一个比较困难的是模型之间的比较与RMSE。也就是说,该软件包可以自动绘制所有这些结果(RMSE以及Rsquared和MAE)。您可以通过以下代码看到它:

So next steep is comparation between models with RMSE. Namely this package can automatically plot all this results RMSE and also (Rsquared and MAE).You can see that with code below:

#CARET
library(caret)
library(caretEnsemble)

MY_DATA111<-data.frame(uschange[,1:2])

trainControl1 <- trainControl(method="repeatedcv", number=10, repeats=3,
                             savePredictions=TRUE, classProbs=TRUE)
algorithmList1 <- c('lm', 'rpart')
set.seed(7)
models1 <- caretList(Consumption ~., data=MY_DATA111, trControl=trainControl1, methodList=algorithmList1)
results1 <- resamples(models1)
summary(results1)
dotplot(results1)

该模型对于30的RMSE的输出-resamples如下:

Output of this model for RMSE with 30-resamples is as fallow:

Call:
summary.resamples(object = results1)

Models: lm, rpart 
Number of resamples: 30 

RMSE 
           Min.   1st Qu.    Median      Mean   3rd Qu.      Max. NA's
lm    0.3624163 0.4959485 0.5582943 0.5940259 0.6851297 0.9484492    0
rpart 0.4514924 0.5046020 0.6208663 0.6332712 0.7370780 0.9759921    0

所以有人可以帮助我如何解决此问题并从预测的前两个模型中提取值包以绘制类似于RMSE的Caret包示例的箱线图?

So can anybody help me how to resolve this problem and extract values from first two model from forecast package in order to plot box-plot like example of Caret package for RMSE ?

推荐答案

您可以使用<$ c获得预测均值$ c> ets_f $ mean 并手动计算均方根。

You can get forecast mean with ets_f$mean and calculate rmse by hand.

# Extract forecasted values
forcasted_values <- ets_f$mean
actual_values <- subset(hyndsight,start=length(hyndsight)-36)

# Example ar model
fc2 <- ar(subset(hyndsight,end=length(hyndsight)-35))
for_values2 <- forecast(fc2, h=35)$mean

# Prepare output data.frame
result <- data.frame(
  model = c("hw",  
            "ar"),
  rmse = c(sqrt(mean((forcasted_values - actual_values)^2)), 
           sqrt(mean((for_values2 - actual_values)^2)))
)

#Box plot
boxplot(result$rmse ~ result$model)

这篇关于从预测对象中提取值并绘制箱形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆