如何在R中导出arima模型?这样,另一个应用程序可以使用此模型来预测时间序列

时间:2017-03-22 12:33:03

标签: python r machine-learning microservices forecasting

我正在研究微服务应用程序。所以我必须编写R和Python脚本。我的R脚本需要创建arima模型。之后我的Python脚本需要使用创建的arima模型来预测时间序列。

在我的R脚本中,我实现了创建模型并进行比较。但是,我不知道如何导出它们以使python脚本使用模型。

我尝试了 pmml :: savePMML(arimaModel1)函数来创建pmml文件。

但我收到了错误:

(函数(classes,fdef,mtable)中的错误:   无法找到函数的继承方法' saveXML'签名'"列表"'

我认为此错误是由对象类型差异引起的。

有没有办法导出我创建的arima模型,以便我的python脚本使用它们来预测时间序列?

我的R脚本:

if (!require("xts")) {
  # install.packages("xts", repos="http://cran.rstudio.com/") # for r terminal, docker
  install.packages("xts") 
  library("xts")
}
if (!require("astsa")) {
  # install.packages("astsa", repos="http://cran.rstudio.com/") # for r terminal, docker
  install.packages("astsa") 
  library("astsa")
}

if (!require("forecast")) {
  # install.packages("forecast", repos="http://cran.rstudio.com/") # for r terminal, docker
  install.packages("forecast") 
  library("forecast")
}

elecTurkeyData <- read.csv(file="data.csv", header = TRUE, sep = ";", stringsAsFactors = FALSE) # original data

head(elecTurkeyData)
tail(elecTurkeyData)
date <- as.Date(as.yearmon(as.character(elecTurkeyData$Date..Montly.)), format="%d-%m-%Y")
consumption <- as.numeric(elecTurkeyData$T.C..Elektricity.consumption..Gross.Demand..Kwh) # original data

tcElektricityConsumption <- data.frame(Date=date,
                                       consumption=consumption, 
                                       stringsAsFactors=FALSE)
plot(tcElektricityConsumption, main = "TC Electiricity Consumption", type= "o") # buggy plot
xtsTcElektricityConsumption <- xts(tcElektricityConsumption[, -1], order.by=date)
periodicity(xtsTcElektricityConsumption)
plot.xts(xtsTcElektricityConsumption, main = "TC Electiricity Consumption")
plot(diff(xtsTcElektricityConsumption), main = "Diff TC Electiricity Consumption")
plot(log(xtsTcElektricityConsumption), main = "Log TC Electiricity Consumption")
plot(diff(log(xtsTcElektricityConsumption)), main = "Diff Log TC Electiricity Consumption")


timeseries <- ts(data=xtsTcElektricityConsumption, frequency=12)

arimaModel1 <- sarima(log(timeseries), p = 0, d = 1, q = 1, P = 0, D = 1, Q = 0, S = 12) # dataiku model, AIC = -5.649985
arimaModel2 <- sarima(log(timeseries), p = 1, d = 1, q=1, P = 0, D = 1, Q = 1, S = 12) # AIC = -5.804879
arimaModel3 <- sarima(log(timeseries), 0, 1, 1, 0, 1, 1, 12) # AIC = -5.810673
arimaModel4 <- sarima(log(timeseries), p = 3, d = 0, q=1, P = 0, D = 1, Q = 2, S = 12) # AIC = -5.836181
arimaModel5 <- sarima(log(timeseries), p = 3, d = 0, q=1, P = 1, D = 1, Q = 1, S = 12) # AIC = -5.844795


barplot(c(ARIMA1=arimaModel1$AIC, ARIMA2=arimaModel2$AIC, ARIMA3=arimaModel3$AIC, ARIMA4=arimaModel4$AIC, ARIMA5=arimaModel5$AIC),
        col='light blue',
        ylab='AIC')

0 个答案:

没有答案