How can I export a Time Series model in R?
Asked Answered
B

1

8

Is there a standard (or available) way to export a Time Series model in R? PMML would work, but when I I try to use the pmml library, perhaps incorrectly, I get an error:

For example, my code looks similar to this:

require(fpp)
library(forecast)
library(pmml)
data <- ts(livestock, start = 1970, end = 2000,frequency=3)
model <- ses(data , h=10 )
export <- pmml(model)

And the error I get is:

Error in UseMethod("pmml") :   no applicable method for 'pmml' applied to an object of class "forecast"
Berglund answered 4/3, 2015 at 5:16 Comment(6)
There is no pmml.forecast listed in ?pmml. Probably the reason why you get this error.Baggott
Is there any option to create pmml for time series?Berglund
Maybe directly ask the package maintainer maintainer("pmml").Baggott
You can save it, like anything else, as an .RData or RDS object - that will do if you would like to interact with another person who is using R. Code example: for a model forecast, you would use saveRDS(model, "model.rds") and load it later as model <– loadRDS("model.rds").Commons
Thanks for your help, But I have written a UDF on my own to export the forecast model as PMML.Berglund
@yoganathank if you wrote a UDF to export it as PMML and this is the solution then please provide the code to the community as an answer and select your answer as the solution so that others may benefit (you can also get more points this way)Disabled
K
2

Here is what I can tell:

When you use ses(), you're not creating a model; you're using a model to find a prediction (in particular, making a forecast via exponential smoothing for a time series). Your result is not a predictive model, but rather a particular prediction of a model for a particular data set. While I'm not that familiar with PMML, from what I can tell, it's not meant for the job you are trying to use it for.

If you want to export the time series and the result, I would say your best bet would be to just export a .csv file with the data; just about anything can read .csv's. A ts object is nothing more than a glorified vector, so you can export the data and the times. Additionally, model is just a table with data. So try this:

write.csv(model, file="forecast.csv")

If you want to write the ts object, try one of the following:

write.csv(data, file="ts1.csv") # No dates for index
write.csv(cbind("time" = time(data), "val" = data), file = "ts2.csv") # Adds dates
Knowing answered 2/12, 2016 at 7:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.