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

Rui Barradas ruipbarradas at sapo.pt
Fri Mar 17 22:45:23 CET 2017


Hello,

By running the code of the OP, I've come to the conclusion that 
print.forecast calls print.data.frame:

forecast:::print.forecast
function (x, ...)
{
     print(as.data.frame(x))
}
<bytecode: 0x0000000007ac9cd0>
<environment: namespace:forecast>

But I'm unable to get what the OP wants by call as.data.frame(...).
Here is the code with some made up data.

library(forecast)
library(tseries)

DAT <- data.frame(x = sample(100, 100, TRUE))

TSdata <- ts(DAT[,1], start=c(1994,10), frequency=12)
TSmodel <- nnetar(TSdata)
TSmodelForecast <- forecast(TSmodel, h=24)
TSmodelForecast

str(as.data.frame(TSmodelForecast))

Hope this helps,

Rui Barradas

Em 17-03-2017 20:48, Jim Lemon escreveu:
> Hi Paul,
> When manipulating any R object, the first thing to ascertain is what it is:
>
> class(TSmodelForecast)
>
> should give you useful information.
>
> str(TSmodelForecast)
>
> should give you more. Because of the wealth of defined data structures
> in R, it is difficult to manipulate them without this information. I
> suspect that your output is something like a time series object, and
> once that is known, it should not be too hard to display its contents
> in the way you want.
>
> Jim
>
>
> On Sat, Mar 18, 2017 at 7:12 AM, Paul Bernal <paulbernal07 at gmail.com> wrote:
>> Dear Jim,
>>
>> Hope you are doing great. I tried to do what you suggested but R send an
>> error message saying that $ operator is invalid for atomic vectors.
>>
>> The format of the forecasts are as follows: forecasted years are as rows,
>> and forecasted months are in columns what I want to do is to have two
>> colums, one with the forecasted dates in (MMM-YYYY format) and the second
>> column with the actual forecast results.
>>
>> The output that is giving me hard time is the forecast output from nnetar
>> model.
>>
>> Thanks for your valuable support,
>>
>> Best of regards,
>>
>> Paul
>>
>>
>> 2017-03-16 18:23 GMT-05:00 Jim Lemon <drjimlemon at gmail.com>:
>>>
>>> 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.
>>
>>
>
> ______________________________________________
> 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