[Solved] Get access to the elements of a list in R [closed]


as.numeric(res$mean)

Will do the job.

As per your comment, so Forecast is the actual forecast and it is not always just the mean.

Consider the following example using a data set taken from here

library(forecast)
births <- scan("http://robjhyndman.com/tsdldata/data/nybirths.dat")
birthstimeseries <- ts(births, frequency=12, start=c(1946,1))
fit <- auto.arima(birthstimeseries)
res <- forecast(fit, 12)
plot(res)

enter image description here

As you can see from this plot, the forecast isn’t just the mean, and the auto.arima fitted a seasonal model

If you’ll inspect res$mean, you will see that the numbers are different

          Jan      Feb      Mar      Apr      May      Jun      Jul      Aug      Sep      Oct
1960 27.69056 26.07680 29.26544 27.59444 28.93193 28.55379 29.84713 29.45347 29.16388 29.21343
          Nov      Dec
1960 27.26221 28.06863

As per your last comment, in order to inspect the “guts” of an unknown (to you) R object it is always preferable to start with class(res), str(res) and finally attributes(res). The later will reveal you all the attributes that this object contains:

attributes(res)
## v$names
##  [1] "method"    "model"     "level"     "mean"      "lower"     "upper"     "x"         "xname"    
##  [9] "fitted"    "residuals"

## $class
##  [1] "forecast"

So now you can investigate res$method or res$model and finally, the one you were looking for res$mean

3

solved Get access to the elements of a list in R [closed]