Donny P Donny P - 1 month ago 6
R Question

Get the forecasted values when using forecast() in R

I did a forecast() in R, and I only want the forecasted values. How can I do this? If I use

forecast$means
I get extra information instead of a usable data object like a vector.

suppressMessages(require('forecast'))
suppressMessages(require('rjson'))

# Load data. Let user choose what file they want to input.
my_json = "[1,2,3,4,5]"

my_data = fromJSON(my_json)

my_df = data.frame(my_data)

# Plot the decomposed time series. Let user choose their season length.
forecasted_data = forecast(ts(my_df[,1]), h=5)

print(forecasted_data$mean)


Current Output:

Time Series:
Start = 6
End = 10
Frequency = 1
[1] 4.9999 4.9999 4.9999 4.9999 4.9999


Desired Output:

[1] 4.9999 4.9999 4.9999 4.9999 4.9999

Answer

Try stripping the ts attributes by coercing it into a simple numeric vector:

as.numeric(forecasted_data$mean)