我正在研究ARIMA模型,我在R控制台上得到了以下结果:
ARIMA(0,1,0) with drift : 124.185
ARIMA(1,1,0) with drift : 127.0279
ARIMA(0,1,1) with drift : 126.831
ARIMA(0,1,0) : 121.4817
ARIMA(1,1,1) with drift : Inf
Best model: ARIMA(0,1,0)
ARIMA(2,1,2) with drift : Inf
ARIMA(0,1,0) with drift : 125.6857
ARIMA(1,1,0) with drift : 128.5824
ARIMA(0,1,1) with drift : 128.4456但我希望结果以表格的形式自动存储在外部文件夹中,如下所示:
[![Model AIC
ARIMA(0,1,0) with drift 124.185
ARIMA(1,1,0) with drift 127.0279
ARIMA(0,1,1) with drift 126.831
ARIMA(0,1,0) Inf
Best model: ARIMA(0,1,0)
ARIMA(2,1,2) with drift : Inf][1]][1]我用来获得结果的代码;
data<-read.table(file.choose(), header=T,sep="",quote="")
library(forecast)
dat<-c()
Error<- NULL
for(a in 1:11){
for(b in 1:5){
for (c in 1:5){
Data1<-data[(nrow(data)-9-2*(a-1)+(b-1)):(nrow(data)-10+(b-1)),]
}
#This selects test sets 1,2,3,4, and 5 years beyond the training set
Data2<-data[(nrow(data)-10+(b+c)):(nrow(data)-10+(b+c)),]
Data3<-data[(nrow(data)-11+(b+c)):(nrow(data)-10+(b+c)),]
#Fitting autoarima model
fitarima<-auto.arima(Data1$abun,trace = TRUE)
muhat<-predict(fitarima,data=Data2$abun)
muhat$pred<-as.numeric(muhat$pred)
for (i in nrow(Data2)){
Error[i]<-abs(muhat$pred-Data2$abun)
dat<-rbind(dat,Error)
}
}
}
}发布于 2019-09-28 03:58:00
您可以从控制台获取输出,并使用read.table()创建数据帧。然后,您可以使用gridExtra包来创建数据框表。然后使用` pdf ()将表导出为pdf。
library(grid)
library(gridExtra)
rawdata <- 'ARIMA(0,1,0) with drift : 124.185
ARIMA(1,1,0) with drift : 127.0279
ARIMA(0,1,1) with drift : 126.831
ARIMA(0,1,0) : 121.4817
ARIMA(1,1,1) with drift : Inf
Best model: ARIMA(0,1,0)
ARIMA(2,1,2) with drift : Inf
ARIMA(0,1,0) with drift : 125.6857
ARIMA(1,1,0) with drift : 128.5824
ARIMA(0,1,1) with drift : 128.4456'
df <- read.table(text = rawdata, header = F, sep = ":", col.names = c('Model', 'AIC'))
best <- df[6,]
df <- df[-6,]
hj <- matrix(c(0, 0.5), ncol=2, nrow=nrow(df), byrow=TRUE)
x <- matrix(c(0, 0.5), ncol=2, nrow=nrow(df), byrow=TRUE)
mytheme <- ttheme_default(core = list(fg_params = list(hjust=0,
x=0.1,
fontsize=10)),
colhead = list(fg_params = list(fontsize=10,
fontface="bold"))
)
pdf("arima_models.pdf", height=6, width=8.5, onefile = TRUE)
grid.table(df, theme = mytheme, rows = NULL)
pushViewport(viewport(y=.25,height=.5))
grid.table(best, rows = NULL, cols = NULL, theme = mytheme)
dev.off()以下是输出的图像

发布于 2019-09-30 16:12:22
我们要做的是将打印到控制台的输出保存为文本文件,然后将文本文件读入R。我们将使用sink()函数将输出保存到控制台。由于每次迭代都会打印输出,因此我们需要在for循环中包含sink()。您可以在下面的for循环中看到,我在auto.arima()调用周围放置了两个sink()调用。这是因为这是将输出打印到控制台的函数。我已经使用paste0()为每次迭代的输出创建了一个新名称。输出将保存为"output 1.txt“格式的文本文件。
library(forecast)
library(grid)
library(gridExtra)
dat<-c()
Error<- NULL
x <- 1
for(f in 1:11){
for(b in 1:5){
for (c in 1:5){
Data1<-data[(nrow(data)-9-2*(f-1)+(b-1)):(nrow(data)-10+(b-1)),]
}
#This selects test sets 1,2,3,4, and 5 years beyond the training set
Data2<-data[(nrow(data)-10+(b+c)):(nrow(data)-10+(b+c)),]
Data3<-data[(nrow(data)-11+(b+c)):(nrow(data)-10+(b+c)),]
#Fitting autoarima model
sink(paste0("output", " ", x, ".txt"))
fitarima<-auto.arima(Data1$abun,trace = TRUE)
sink()
x <- x + 1
muhat<-predict(fitarima,data=Data2$abun)
muhat$pred<-as.numeric(muhat$pred)
for (i in nrow(Data2)){
Error[i]<-abs(muhat$pred-Data2$abun)
dat<-rbind(dat,Error)
}
}
}for循环完成后,我们将文件导入R。我们使用list.files()获得文件列表,它在工作目录中搜索文件扩展名为“.txt”的文件。我们在read.table()上使用lapply()创建一个数据帧列表,其中包含每次迭代的arima模型。然后,我们只需循环遍历列表中的每个对象,并创建数据框的绘图,然后保存它。这和之前一样,我只是为每个数据帧创建了一个循环来做这件事。最佳模型保存在每个数据帧的最后一行,因此可以方便地使用nrow(mod)进行提取。
files <- list.files(getwd(), pattern = ".txt")
arima_mods <- lapply(files, read.table, sep = ":", header = FALSE)
j <- 1
for (mod in arima_mods) {
df <- mod[-nrow(mod), , drop = FALSE]
best <- mod[nrow(mod), , drop = FALSE]
colnames(df) <- c('Model', 'AIC')
hj <- matrix(c(0, 0.5), ncol=2, nrow=nrow(df), byrow=TRUE)
x <- matrix(c(0, 0.5), ncol=2, nrow=nrow(df), byrow=TRUE)
mytheme <- ttheme_default(core = list(fg_params = list(hjust=hj,
x=x,
fontsize=10)),
colhead = list(fg_params = list(fontsize=10,
fontface="bold"))
)
mod_num <- paste0('arima_fit', j, ".pdf")
pdf(mod_num, height=6, width=8.5, onefile = TRUE)
grid.table(df, theme = mytheme, rows = NULL)
pushViewport(viewport(y=.25,height=.5))
grid.table(best, rows = NULL, cols = NULL, theme = mytheme)
dev.off()
j <- j + 1
}这是其中一张桌子的图片

https://stackoverflow.com/questions/58138460
复制相似问题