Paul Bernal
2017-Mar-16 18:34 UTC
[R] Transposing forecasts results from nnetar function and turn them into a data frame
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) > > TSmodelForecastThe 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
Jim Lemon
2017-Mar-16 23:23 UTC
[R] Transposing forecasts results from nnetar function and turn them into a data frame
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.
Jim Lemon
2017-Mar-17 20:48 UTC
[R] Transposing forecasts results from nnetar function and turn them into a data frame
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. > >
Paul Bernal
2017-Mar-17 21:23 UTC
[R] Transposing forecasts results from nnetar function and turn them into a data frame
Dear Jim, Thank you for your valuable replies. So variable mf5 has the forecasts for a fitted nnetar model. Below is the class and the str(mf5) output. Maybe with this information I am sharing with you, hopefully you can give me some more guidance. Again, thank you so much!> class(mf5)[1] "forecast"> > str(mf5)List of 16 $ x : Time-Series [1:377] from 1986 to 2017: 48 40 44 35 44 42 39 37 41 36 ... $ m : num 12 $ p : num 25 $ P : num 1 $ scalex :List of 2 ..$ center: num 38.1 ..$ scale : num 10.9 $ size : num 13 $ subset : int [1:377] 1 2 3 4 5 6 7 8 9 10 ... $ model :List of 20 ..$ :List of 15 .. ..$ n : num [1:3] 25 13 1 .. ..$ nunits : int 40 .. ..$ nconn : num [1:41] 0 0 0 0 0 0 0 0 0 0 ... .. ..$ conn : num [1:352] 0 1 2 3 4 5 6 7 8 9 ... .. ..$ nsunits : num 39 .. ..$ decay : num 0 .. ..$ entropy : logi FALSE .. ..$ softmax : logi FALSE .. ..$ censored : logi FALSE .. ..$ value : num 5.83 .. ..$ wts : num [1:352] -1.081 2.199 0.734 1.358 1.115 ... .. ..$ convergence : int 1 .. ..$ fitted.values: num [1:352, 1] 0.246 0.439 -0.357 1.325 -0.195 ... .. .. ..- attr(*, "dimnames")=List of 2 .. .. .. ..$ : NULL .. .. .. ..$ : NULL .. ..$ residuals : num [1:352, 1] -0.0754 0.00853 -0.0242 -0.05026 0.0898 ... .. .. ..- attr(*, "dimnames")=List of 2 .. .. .. ..$ : NULL .. .. .. ..$ : NULL .. ..$ call : language nnet.default(x = x, y = y, size = ..1, linout = linout, trace = trace) .. ..- attr(*, "class")= chr "nnet" ..$ :List of 15 .. ..$ n : num [1:3] 25 13 1 .. ..$ nunits : int 40 .. ..$ nconn : num [1:41] 0 0 0 0 0 0 0 0 0 0 ... .. ..$ conn : num [1:352] 0 1 2 3 4 5 6 7 8 9 ... .. ..$ nsunits : num 39 .. ..$ decay : num 0 .. ..$ entropy : logi FALSE .. ..$ softmax : logi FALSE .. ..$ censored : logi FALSE .. ..$ value : num 2.54 .. ..$ wts : num [1:352] -7.113 4.668 -0.801 -5.487 2.751 ... .. ..$ convergence : int 1 .. ..$ fitted.values: num [1:352, 1] 0.145 0.443 -0.401 1.286 -0.158 ... .. .. ..- attr(*, "dimnames")=List of 2 .. .. .. ..$ : NULL .. .. .. ..$ : NULL .. ..$ residuals : num [1:352, 1] 0.0258 0.0037 0.02 -0.0107 0.0528 ... .. .. ..- attr(*, "dimnames")=List of 2 .. .. .. ..$ : NULL .. .. .. ..$ : NULL .. ..$ call : language nnet.default(x = x, y = y, size = ..1, linout = linout, trace = trace) .. ..- attr(*, "class")= chr "nnet" ..$ :List of 15 .. ..$ n : num [1:3] 25 13 1 .. ..$ nunits : int 40 .. ..$ nconn : num [1:41] 0 0 0 0 0 0 0 0 0 0 ... .. ..$ conn : num [1:352] 0 1 2 3 4 5 6 7 8 9 ... .. ..$ nsunits : num 39 .. ..$ decay : num 0 .. ..$ entropy : logi FALSE .. ..$ softmax : logi FALSE .. ..$ censored : logi FALSE .. ..$ value : num 1.1 .. ..$ wts : num [1:352] -2.24 -0.698 -0.243 -2.575 4.073 ... .. ..$ convergence : int 1 .. ..$ fitted.values: num [1:352, 1] 0.2466 0.2986 -0.4369 1.3053 -0.0965 ... .. .. ..- attr(*, "dimnames")=List of 2 .. .. .. ..$ : NULL .. .. .. ..$ : NULL .. ..$ residuals : num [1:352, 1] -0.07551 0.14845 0.056 -0.03027 -0.00844 ... .. .. ..- attr(*, "dimnames")=List of 2 .. .. .. ..$ : NULL .. .. .. ..$ : NULL .. ..$ call : language nnet.default(x = x, y = y, size = ..1, linout = linout, trace = trace) .. ..- attr(*, "class")= chr "nnet" ..$ :List of 15 .. ..$ n : num [1:3] 25 13 1 .. ..$ nunits : int 40 .. ..$ nconn : num [1:41] 0 0 0 0 0 0 0 0 0 0 ... .. ..$ conn : num [1:352] 0 1 2 3 4 5 6 7 8 9 ... .. ..$ nsunits : num 39 .. ..$ decay : num 0 .. ..$ entropy : logi FALSE .. ..$ softmax : logi FALSE .. ..$ censored : logi FALSE .. ..$ value : num 1.8 .. ..$ wts : num [1:352] 0.427 1.89 -0.705 -3.121 -1.134 ... .. ..$ convergence : int 1 .. ..$ fitted.values: num [1:352, 1] 0.11 0.37 -0.38 1.25 -0.15 ... .. .. ..- attr(*, "dimnames")=List of 2 .. .. .. ..$ : NULL .. .. .. ..$ : NULL .. ..$ residuals : num [1:352, 1] 0.06113 0.07689 -0.00135 0.02279 0.04491 ... .. .. ..- attr(*, "dimnames")=List of 2 .. .. .. ..$ : NULL .. .. .. ..$ : NULL .. ..$ call : language nnet.default(x = x, y = y, size = ..1, linout = linout, trace = trace) .. ..- attr(*, "class")= chr "nnet" ..$ :List of 15 .. ..$ n : num [1:3] 25 13 1 .. ..$ nunits : int 40 .. ..$ nconn : num [1:41] 0 0 0 0 0 0 0 0 0 0 ... .. ..$ conn : num [1:352] 0 1 2 3 4 5 6 7 8 9 ... .. ..$ nsunits : num 39 .. ..$ decay : num 0 .. ..$ entropy : logi FALSE .. ..$ softmax : logi FALSE .. ..$ censored : logi FALSE .. ..$ value : num 2.67 .. ..$ wts : num [1:352] -0.632 2.121 6.144 1.697 0.953 ... .. ..$ convergence : int 1 .. ..$ fitted.values: num [1:352, 1] 0.2228 0.4483 -0.3944 1.236 -0.0856 ... .. .. ..- attr(*, "dimnames")=List of 2 .. .. .. ..$ : NULL .. .. .. ..$ : NULL .. ..$ residuals : num [1:352, 1] -0.05171 -0.00123 0.01351 0.03908 -0.01928 ... .. .. ..- attr(*, "dimnames")=List of 2 .. .. .. ..$ : NULL .. .. .. ..$ : NULL .. ..$ call : language nnet.default(x = x, y = y, size = ..1, linout = linout, trace = trace) .. ..- attr(*, "class")= chr "nnet" ..$ :List of 15 .. ..$ n : num [1:3] 25 13 1 .. ..$ nunits : int 40 .. ..$ nconn : num [1:41] 0 0 0 0 0 0 0 0 0 0 ... .. ..$ conn : num [1:352] 0 1 2 3 4 5 6 7 8 9 ... .. ..$ nsunits : num 39 .. ..$ decay : num 0 .. ..$ entropy : logi FALSE .. ..$ softmax : logi FALSE .. ..$ censored : logi FALSE .. ..$ value : num 1.8 .. ..$ wts : num [1:352] -0.919 -0.769 1.105 -0.444 4.516 ... .. ..$ convergence : int 1 .. ..$ fitted.values: num [1:352, 1] 0.0833 0.4158 -0.3748 1.3173 -0.082 ... .. .. ..- attr(*, "dimnames")=List of 2 .. .. .. ..$ : NULL .. .. .. ..$ : NULL .. ..$ residuals : num [1:352, 1] 0.08773 0.03123 -0.00616 -0.04231 -0.02296 ... .. .. ..- attr(*, "dimnames")=List of 2 .. .. .. ..$ : NULL .. .. .. ..$ : NULL .. ..$ call : language nnet.default(x = x, y = y, size = ..1, linout = linout, trace = trace) .. ..- attr(*, "class")= chr "nnet" ..$ :List of 15 .. ..$ n : num [1:3] 25 13 1 .. ..$ nunits : int 40 .. ..$ nconn : num [1:41] 0 0 0 0 0 0 0 0 0 0 ... .. ..$ conn : num [1:352] 0 1 2 3 4 5 6 7 8 9 ... .. ..$ nsunits : num 39 .. ..$ decay : num 0 .. ..$ entropy : logi FALSE .. ..$ softmax : logi FALSE .. ..$ censored : logi FALSE .. ..$ value : num 2.65 .. ..$ wts : num [1:352] 2.03 -1.6 -1.91 -4.65 -3.03 ... .. ..$ convergence : int 1 .. ..$ fitted.values: num [1:352, 1] 0.199 0.447 -0.443 1.263 -0.138 ... .. .. ..- attr(*, "dimnames")=List of 2 .. .. .. ..$ : NULL .. .. .. ..$ : NULL .. ..$ residuals : num [1:352, 1] -0.02808 0.000236 0.061884 0.012432 0.033544 ... .. .. ..- attr(*, "dimnames")=List of 2 .. .. .. ..$ : NULL .. .. .. ..$ : NULL .. ..$ call : language nnet.default(x = x, y = y, size = ..1, linout = linout, trace = trace) .. ..- attr(*, "class")= chr "nnet" ..$ :List of 15 .. ..$ n : num [1:3] 25 13 1 .. ..$ nunits : int 40 .. ..$ nconn : num [1:41] 0 0 0 0 0 0 0 0 0 0 ... .. ..$ conn : num [1:352] 0 1 2 3 4 5 6 7 8 9 ... .. ..$ nsunits : num 39 .. ..$ decay : num 0 .. ..$ entropy : logi FALSE .. ..$ softmax : logi FALSE .. ..$ censored : logi FALSE .. ..$ value : num 7.79 .. ..$ wts : num [1:352] -3.314 3.89 -6.355 0.842 4.491 ... .. ..$ convergence : int 1 .. ..$ fitted.values: num [1:352, 1] 0.171 0.462 -0.337 1.24 -0.128 ... .. .. ..- attr(*, "dimnames")=List of 2 .. .. .. ..$ : NULL .. .. .. ..$ : NULL .. ..$ residuals : num [1:352, 1] 0.000172 -0.014804 -0.044404 0.03475 0.023508 ... .. .. ..- attr(*, "dimnames")=List of 2 .. .. .. ..$ : NULL .. .. .. ..$ : NULL .. ..$ call : language nnet.default(x = x, y = y, size = ..1, linout = linout, trace = trace) .. ..- attr(*, "class")= chr "nnet" ..$ :List of 15 .. ..$ n : num [1:3] 25 13 1 .. ..$ nunits : int 40 .. ..$ nconn : num [1:41] 0 0 0 0 0 0 0 0 0 0 ... .. ..$ conn : num [1:352] 0 1 2 3 4 5 6 7 8 9 ... .. ..$ nsunits : num 39 .. ..$ decay : num 0 .. ..$ entropy : logi FALSE .. ..$ softmax : logi FALSE .. ..$ censored : logi FALSE .. ..$ value : num 2.3 .. ..$ wts : num [1:352] 5.12 -2.27 2.25 4.02 -6.78 ... .. ..$ convergence : int 1 .. ..$ fitted.values: num [1:352, 1] 0.1137 0.4326 -0.4635 1.2762 -0.0999 ... .. .. ..- attr(*, "dimnames")=List of 2 .. .. .. ..$ : NULL .. .. .. ..$ : NULL .. ..$ residuals : num [1:352, 1] 0.05741 0.01451 0.08259 -0.00115 -0.00503 ... .. .. ..- attr(*, "dimnames")=List of 2 .. .. .. ..$ : NULL .. .. .. ..$ : NULL .. ..$ call : language nnet.default(x = x, y = y, size = ..1, linout = linout, trace = trace) .. ..- attr(*, "class")= chr "nnet" ..$ :List of 15 .. ..$ n : num [1:3] 25 13 1 .. ..$ nunits : int 40 .. ..$ nconn : num [1:41] 0 0 0 0 0 0 0 0 0 0 ... .. ..$ conn : num [1:352] 0 1 2 3 4 5 6 7 8 9 ... .. ..$ nsunits : num 39 .. ..$ decay : num 0 .. ..$ entropy : logi FALSE .. ..$ softmax : logi FALSE .. ..$ censored : logi FALSE .. ..$ value : num 2.42 .. ..$ wts : num [1:352] 4.12 -1.78 2.89 4.28 2.57 ... .. ..$ convergence : int 1 .. ..$ fitted.values: num [1:352, 1] 0.0986 0.4599 -0.4425 1.2061 -0.1931 ... .. .. ..- attr(*, "dimnames")=List of 2 .. .. .. ..$ : NULL .. .. .. ..$ : NULL .. ..$ residuals : num [1:352, 1] 0.0724 -0.0128 0.0615 0.069 0.0882 ... .. .. ..- attr(*, "dimnames")=List of 2 .. .. .. ..$ : NULL .. .. .. ..$ : NULL .. ..$ call : language nnet.default(x = x, y = y, size = ..1, linout = linout, trace = trace) .. ..- attr(*, "class")= chr "nnet" ..$ :List of 15 .. ..$ n : num [1:3] 25 13 1 .. ..$ nunits : int 40 .. ..$ nconn : num [1:41] 0 0 0 0 0 0 0 0 0 0 ... .. ..$ conn : num [1:352] 0 1 2 3 4 5 6 7 8 9 ... .. ..$ nsunits : num 39 .. ..$ decay : num 0 .. ..$ entropy : logi FALSE .. ..$ softmax : logi FALSE .. ..$ censored : logi FALSE .. ..$ value : num 2.81 .. ..$ wts : num [1:352] -1.207 -1.817 -3.409 -4.643 0.775 ... .. ..$ convergence : int 1 .. ..$ fitted.values: num [1:352, 1] 0.0905 0.5451 -0.4395 1.1706 -0.0989 ... .. .. ..- attr(*, "dimnames")=List of 2 .. .. .. ..$ : NULL .. .. .. ..$ : NULL .. ..$ residuals : num [1:352, 1] 0.08053 -0.09806 0.05853 0.10442 -0.00604 ... .. .. ..- attr(*, "dimnames")=List of 2 .. .. .. ..$ : NULL .. .. .. ..$ : NULL .. ..$ call : language nnet.default(x = x, y = y, size = ..1, linout = linout, trace = trace) .. ..- attr(*, "class")= chr "nnet" ..$ :List of 15 .. ..$ n : num [1:3] 25 13 1 .. ..$ nunits : int 40 .. ..$ nconn : num [1:41] 0 0 0 0 0 0 0 0 0 0 ... .. ..$ conn : num [1:352] 0 1 2 3 4 5 6 7 8 9 ... .. ..$ nsunits : num 39 .. ..$ decay : num 0 .. ..$ entropy : logi FALSE .. ..$ softmax : logi FALSE .. ..$ censored : logi FALSE .. ..$ value : num 6.06 .. ..$ wts : num [1:352] 1.092 4.676 -0.514 1.13 1.276 ... .. ..$ convergence : int 1 .. ..$ fitted.values: num [1:352, 1] 0.1781 0.4221 -0.4852 1.2227 -0.0774 ... .. .. ..- attr(*, "dimnames")=List of 2 .. .. .. ..$ : NULL .. .. .. ..$ : NULL .. ..$ residuals : num [1:352, 1] -0.007 0.025 0.1043 0.0523 -0.0275 ... .. .. ..- attr(*, "dimnames")=List of 2 .. .. .. ..$ : NULL .. .. .. ..$ : NULL .. ..$ call : language nnet.default(x = x, y = y, size = ..1, linout = linout, trace = trace) .. ..- attr(*, "class")= chr "nnet" ..$ :List of 15 .. ..$ n : num [1:3] 25 13 1 .. ..$ nunits : int 40 .. ..$ nconn : num [1:41] 0 0 0 0 0 0 0 0 0 0 ... .. ..$ conn : num [1:352] 0 1 2 3 4 5 6 7 8 9 ... .. ..$ nsunits : num 39 .. ..$ decay : num 0 .. ..$ entropy : logi FALSE .. ..$ softmax : logi FALSE .. ..$ censored : logi FALSE .. ..$ value : num 4.1 .. ..$ wts : num [1:352] -4.1 1.95 -5.39 6.06 -3.65 ... .. ..$ convergence : int 1 .. ..$ fitted.values: num [1:352, 1] 0.1674 0.4017 -0.3968 1.2474 -0.0835 ... .. .. ..- attr(*, "dimnames")=List of 2 .. .. .. ..$ : NULL .. .. .. ..$ : NULL .. ..$ residuals : num [1:352, 1] 0.00363 0.0454 0.01586 0.02764 -0.02148 ... .. .. ..- attr(*, "dimnames")=List of 2 .. .. .. ..$ : NULL .. .. .. ..$ : NULL .. ..$ call : language nnet.default(x = x, y = y, size = ..1, linout = linout, trace = trace) .. ..- attr(*, "class")= chr "nnet" ..$ :List of 15 .. ..$ n : num [1:3] 25 13 1 .. ..$ nunits : int 40 .. ..$ nconn : num [1:41] 0 0 0 0 0 0 0 0 0 0 ... .. ..$ conn : num [1:352] 0 1 2 3 4 5 6 7 8 9 ... .. ..$ nsunits : num 39 .. ..$ decay : num 0 .. ..$ entropy : logi FALSE .. ..$ softmax : logi FALSE .. ..$ censored : logi FALSE .. ..$ value : num 1.22 .. ..$ wts : num [1:352] -2.5 1.43 2.96 -2.31 -1.18 ... .. ..$ convergence : int 1 .. ..$ fitted.values: num [1:352, 1] 0.1436 0.446 -0.4246 1.2081 -0.0853 ... .. .. ..- attr(*, "dimnames")=List of 2 .. .. .. ..$ : NULL .. .. .. ..$ : NULL .. ..$ residuals : num [1:352, 1] 0.02742 0.00101 0.04371 0.06697 -0.01964 ... .. .. ..- attr(*, "dimnames")=List of 2 .. .. .. ..$ : NULL .. .. .. ..$ : NULL .. ..$ call : language nnet.default(x = x, y = y, size = ..1, linout = linout, trace = trace) .. ..- attr(*, "class")= chr "nnet" ..$ :List of 15 .. ..$ n : num [1:3] 25 13 1 .. ..$ nunits : int 40 .. ..$ nconn : num [1:41] 0 0 0 0 0 0 0 0 0 0 ... .. ..$ conn : num [1:352] 0 1 2 3 4 5 6 7 8 9 ... .. ..$ nsunits : num 39 .. ..$ decay : num 0 .. ..$ entropy : logi FALSE .. ..$ softmax : logi FALSE .. ..$ censored : logi FALSE .. ..$ value : num 0.858 .. ..$ wts : num [1:352] 2.6535 4.398 0.0444 4.9804 -0.6771 ... .. ..$ convergence : int 1 .. ..$ fitted.values: num [1:352, 1] 0.089 0.4388 -0.283 1.1308 -0.0592 ... .. .. ..- attr(*, "dimnames")=List of 2 .. .. .. ..$ : NULL .. .. .. ..$ : NULL .. ..$ residuals : num [1:352, 1] 0.0821 0.0083 -0.0979 0.1443 -0.0457 ... .. .. ..- attr(*, "dimnames")=List of 2 .. .. .. ..$ : NULL .. .. .. ..$ : NULL .. ..$ call : language nnet.default(x = x, y = y, size = ..1, linout = linout, trace = trace) .. ..- attr(*, "class")= chr "nnet" ..$ :List of 15 .. ..$ n : num [1:3] 25 13 1 .. ..$ nunits : int 40 .. ..$ nconn : num [1:41] 0 0 0 0 0 0 0 0 0 0 ... .. ..$ conn : num [1:352] 0 1 2 3 4 5 6 7 8 9 ... .. ..$ nsunits : num 39 .. ..$ decay : num 0 .. ..$ entropy : logi FALSE .. ..$ softmax : logi FALSE .. ..$ censored : logi FALSE .. ..$ value : num 2.19 .. ..$ wts : num [1:352] 1.216 -4.28 -2.531 0.987 1.311 ... .. ..$ convergence : int 1 .. ..$ fitted.values: num [1:352, 1] 0.1548 0.4733 -0.3074 1.2065 -0.0909 ... .. .. ..- attr(*, "dimnames")=List of 2 .. .. .. ..$ : NULL .. .. .. ..$ : NULL .. ..$ residuals : num [1:352, 1] 0.0163 -0.0263 -0.0735 0.0685 -0.014 ... .. .. ..- attr(*, "dimnames")=List of 2 .. .. .. ..$ : NULL .. .. .. ..$ : NULL .. ..$ call : language nnet.default(x = x, y = y, size = ..1, linout = linout, trace = trace) .. ..- attr(*, "class")= chr "nnet" ..$ :List of 15 .. ..$ n : num [1:3] 25 13 1 .. ..$ nunits : int 40 .. ..$ nconn : num [1:41] 0 0 0 0 0 0 0 0 0 0 ... .. ..$ conn : num [1:352] 0 1 2 3 4 5 6 7 8 9 ... .. ..$ nsunits : num 39 .. ..$ decay : num 0 .. ..$ entropy : logi FALSE .. ..$ softmax : logi FALSE .. ..$ censored : logi FALSE .. ..$ value : num 2.51 .. ..$ wts : num [1:352] -0.862 1.953 1.304 -0.273 1.033 ... .. ..$ convergence : int 1 .. ..$ fitted.values: num [1:352, 1] 0.141 0.492 -0.376 1.244 -0.101 ... .. .. ..- attr(*, "dimnames")=List of 2 .. .. .. ..$ : NULL .. .. .. ..$ : NULL .. ..$ residuals : num [1:352, 1] 0.02978 -0.04529 -0.00509 0.03066 -0.00362 ... .. .. ..- attr(*, "dimnames")=List of 2 .. .. .. ..$ : NULL .. .. .. ..$ : NULL .. ..$ call : language nnet.default(x = x, y = y, size = ..1, linout = linout, trace = trace) .. ..- attr(*, "class")= chr "nnet" ..$ :List of 15 .. ..$ n : num [1:3] 25 13 1 .. ..$ nunits : int 40 .. ..$ nconn : num [1:41] 0 0 0 0 0 0 0 0 0 0 ... .. ..$ conn : num [1:352] 0 1 2 3 4 5 6 7 8 9 ... .. ..$ nsunits : num 39 .. ..$ decay : num 0 .. ..$ entropy : logi FALSE .. ..$ softmax : logi FALSE .. ..$ censored : logi FALSE .. ..$ value : num 2.02 .. ..$ wts : num [1:352] 6.78 -0.31 -1.04 -7.21 2.37 ... .. ..$ convergence : int 1 .. ..$ fitted.values: num [1:352, 1] 0.0302 0.4157 -0.3828 1.134 -0.1247 ... .. .. ..- attr(*, "dimnames")=List of 2 .. .. .. ..$ : NULL .. .. .. ..$ : NULL .. ..$ residuals : num [1:352, 1] 0.14087 0.03136 0.00183 0.14107 0.01977 ... .. .. ..- attr(*, "dimnames")=List of 2 .. .. .. ..$ : NULL .. .. .. ..$ : NULL .. ..$ call : language nnet.default(x = x, y = y, size = ..1, linout = linout, trace = trace) .. ..- attr(*, "class")= chr "nnet" ..$ :List of 15 .. ..$ n : num [1:3] 25 13 1 .. ..$ nunits : int 40 .. ..$ nconn : num [1:41] 0 0 0 0 0 0 0 0 0 0 ... .. ..$ conn : num [1:352] 0 1 2 3 4 5 6 7 8 9 ... .. ..$ nsunits : num 39 .. ..$ decay : num 0 .. ..$ entropy : logi FALSE .. ..$ softmax : logi FALSE .. ..$ censored : logi FALSE .. ..$ value : num 1.81 .. ..$ wts : num [1:352] 0.385 1.215 -2.217 -1.288 -1.172 ... .. ..$ convergence : int 1 .. ..$ fitted.values: num [1:352, 1] 0.26023 0.47306 -0.31769 1.29285 -0.00974 ... .. .. ..- attr(*, "dimnames")=List of 2 .. .. .. ..$ : NULL .. .. .. ..$ : NULL .. ..$ residuals : num [1:352, 1] -0.0892 -0.026 -0.0632 -0.0178 -0.0952 ... .. .. ..- attr(*, "dimnames")=List of 2 .. .. .. ..$ : NULL .. .. .. ..$ : NULL .. ..$ call : language nnet.default(x = x, y = y, size = ..1, linout = linout, trace = trace) .. ..- attr(*, "class")= chr "nnet" ..$ :List of 15 .. ..$ n : num [1:3] 25 13 1 .. ..$ nunits : int 40 .. ..$ nconn : num [1:41] 0 0 0 0 0 0 0 0 0 0 ... .. ..$ conn : num [1:352] 0 1 2 3 4 5 6 7 8 9 ... .. ..$ nsunits : num 39 .. ..$ decay : num 0 .. ..$ entropy : logi FALSE .. ..$ softmax : logi FALSE .. ..$ censored : logi FALSE .. ..$ value : num 2.11 .. ..$ wts : num [1:352] 3.38 -2.75 -3.5 -1.13 -8.48 ... .. ..$ convergence : int 1 .. ..$ fitted.values: num [1:352, 1] -0.0863 0.4472 -0.3007 1.2226 -0.1176 ... .. .. ..- attr(*, "dimnames")=List of 2 .. .. .. ..$ : NULL .. .. .. ..$ : NULL .. ..$ residuals : num [1:352, 1] 0.257314 -0.000178 -0.080274 0.052434 0.012663 ... .. .. ..- attr(*, "dimnames")=List of 2 .. .. .. ..$ : NULL .. .. .. ..$ : NULL .. ..$ call : language nnet.default(x = x, y = y, size = ..1, linout = linout, trace = trace) .. ..- attr(*, "class")= chr "nnet" ..- attr(*, "class")= chr "nnetarmodels" $ nnetargs : list() $ fitted : Time-Series [1:377] from 1986 to 2017: NA NA NA NA NA NA NA NA NA NA ... $ residuals: Time-Series [1:377] from 1986 to 2017: NA NA NA NA NA NA NA NA NA NA ... $ lags : num [1:25] 1 2 3 4 5 6 7 8 9 10 ... $ series : chr "TSDat" $ method : chr "NNAR(25,1,13)[12]" $ call : language nnetar(y = TSDat) $ mean : Time-Series [1:24] from 2017 to 2019: 54.7 50.3 55.4 62.6 59.7 ... - attr(*, "class")= chr "forecast">2017-03-17 15:48 GMT-05:00 Jim Lemon <drjimlemon at gmail.com>:> 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. > > > > >[[alternative HTML version deleted]]
Rui Barradas
2017-Mar-17 21:45 UTC
[R] Transposing forecasts results from nnetar function and turn them into a data frame
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. >