Reputation: 1764
Is there a way to export the output from a summary procedure in R (similar to ods output in SAS)?
I have this time series:
summary(ets(ts(c(100, 110, 120, 200, 300, 100, 200, 100, 120, 300), start = c(2009, 1), frequency = 12)))
And when you run this you get all kinds of great information like MAPE and RMSE. I want to selectively assign some of these metrics to variables for use in other processes further down the line.
MAPE.ets <- [some bit of code that takes the MAPE from the output and pops it right here]
Is there a way in R to do this?
EDIT: I realized after some answers that stlf ets behaves differently than ets, and I need a solution to work for stlf ets as well.
summary(stlf(ts(c(100, 110, 120, 200, 300, 100, 200, 100, 120, 300), start = c(2009, 1), frequency = 4)))
Upvotes: 0
Views: 347
Reputation: 16277
The summary gives you a vector. You can get MAPE like this if you want colnames and rownames.
out <-summary(ets(ts(c(100, 110, 120, 200, 300, 100, 200, 100, 120, 300),
start = c(2009, 1), frequency = 12)))
out[,5,drop=FALSE]
MAPE
Training set 44.51514
I you only want the number, remove drop=FALSE
out[5]
[1] 44.51514
EDIT Using stfl
, it is better to use the accuracy
function instead of summary
. To get MAPE, you can do:
res <- stlf(ts(c(100, 110, 120, 200, 300, 100, 200, 100, 120, 300),
start = c(2009, 1), frequency = 4))
accuracy(res)[5]
[1] 44.5994
Upvotes: 1
Reputation: 2939
simply assign the summary to a new object. This creates a matrix in which the metrics you mentioned can be accessed:
summ <- summary(ets(ts(c(100, 110, 120, 200, 300, 100, 200, 100, 120, 300), start = c(2009, 1), frequency = 12)))
summ[,"MAPE"]
[1] 44.51514
EDIT:
The intended way in the forecast package appears to be the use of the accuracy
method (the summary
method for the ets
-class actually calls accuracy
); and this will work for both ets
and stlf
:
res2 <- stlf(ts(c(100, 110, 120, 200, 300, 100, 200, 100, 120, 300), start = c(2009, 1), frequency = 4))
acc <- accuracy(res2)
acc[,"MAPE"]
Upvotes: 1