Alex Cauwel
Alex Cauwel

Reputation: 1

How to get last datas from an output in R

Hope you are well.

As I am not familiar with R, I don't know how to get the last datas from a time series output.

Here is the script :

fit_arima<-auto.arima(DATA$CLI,d=1,D=1,stepwise = FALSE,approximation = FALSE, trace = TRUE)
fcst<-forecast(fit_arima,h=36)
print(summary(fcst))

Following that, I get these values as output :

371       99.87327 96.30132 103.4452 94.41044 105.3361
372       99.87327 96.24796 103.4986 94.32884 105.4177
373       99.87327 96.19538 103.5512 94.24842 105.4981

In fact, I would like to get col2&3 of the last line (373) in order to work with datas. I tried to use nail(fcst, n=1) but it does not work...

Thank you in advance for your help.

Alex

Upvotes: 0

Views: 32

Answers (1)

akrun
akrun

Reputation: 887028

The output from forecast is a list (check the str(fcst)). We could extract each of those components and get the tail and create a data.frame

data.frame(Mean = tail(fcst$mean, 1), lower = tail(fcst$lower, 1), 
     upper = tail(fcst$upper, 1), check.names = FALSE)
    Mean lower.80% lower.95% upper.80% upper.95%
1 216.798  115.8223  62.36906  317.7736  371.2269

There is also a data.frame method with as.data.frame

> grep("forecast", methods(as.data.frame), value = TRUE)
[1] "as.data.frame.forecast"  "as.data.frame.mforecast"

to convert the object directly to data.frame and then use the tail

 tail(as.data.frame(fcst), 1)
    Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
136        216.798 115.8223 317.7736 62.36906 371.2269

data

library(forecast)
fit <- auto.arima(WWWusage)
fcst <- forecast(fit,h=20)

Upvotes: 1

Related Questions