[R] Transposing forecasts results from nnetar function and turn them into a data frame

Jim Lemon drjimlemon at gmail.com
Fri Mar 17 00:23:53 CET 2017


Hi Paul,
It looks like the information that is printed is in:

TSModelForecast$mean

If str(TSModelForecast$mean) returns something like a list with two
components, you can probably use something like this:

paste(format(TSModelForecast$mean$Date,"%b-%Y"),
 TSModelForecast$mean$Forecast,sep="-",collapse="\n")

It also might be in TSModelForecast$fitted

Jim


On Fri, Mar 17, 2017 at 5:34 AM, Paul Bernal <paulbernal07 at gmail.com> wrote:
> Dear friends,
>
> I am currently using R version 3.3.3 (64-bit) and used the following code
> to generate forecasts:
>
>> library(forecast)
>>
>> library(tseries)
>
>     ‘tseries’ version: 0.10-35
>
>     ‘tseries’ is a package for time series analysis and computational
> finance.
>
>     See ‘library(help="tseries")’ for details.
>
>
>> DAT<-read.csv("TrainingData.csv")
>>
>> TSdata<-ts(DAT[,1], start=c(1994,10), frequency=12)
>>
>> TSmodel<-nnetar(TSdata)
>>
>> TSmodelForecast<-forecast(TSmodel, h=24)
>>
>> TSmodelForecast
>
> The problem is that the output comes in this fashion:
>
>                 Jan    Feb    Mar    Apr    May    Jun     Jul    Aug
> Sep   Oct
>  2017        10      20      15      40     9         8         21     21
>     19     18
>  2018        34      15       7        6      10      11
>
> The format I would like to have is the following:
>
> Date                 Forecast
> Jan-2017               10
> Feb-2017               20
> Mar-2017               15
> Apr-2017                40
> May-2017               9
> Jun-2017                8
> Jul-2017                 21
> Aug-2017               21
> Sep-2017               19
> etc                          etc
>
> Is there a way to make the results look like this?
>
> Attached is a dataset as a reference.
>
> Best regards,
>
> Paul
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list