0
> MLest<- arima(X, order = c(1,0,0), method = c("ML"))
> MLest

Call:
arima(x = X, order = c(1, 0, 0), method = c("ML"))

>Coefficients:
         ar1  intercept  
      0.2657    -0.0824  
  0.0680     0.1018  
sigma^2 estimated as 1.121:  log likelihood = -295.23,  aic = 596.47

我想将 0.2657 和 1.121 结果写入输出文件。我已经定义了路径和文件名,这是我的代码。

当我使用时,write(MLest, file=filename, append=TRUE, sep="\t")我收到以下错误:

Error in cat(list(...), file, sep, fill, labels, append) : 
  argument 1 (type 'list') cannot be handled by 'cat'

当我使用时,write.table(MLest[1:2], file=filename,sep=" ", col.names = F, row.names = F)

它有效,但我有:

 0.265705946688229 1.12087092992291  
-0.0823543583874666 1.12087092992291

我想得到一个结果:( 0.265705946688229 -0.0823543583874666 1.12087092992291每个值到不同的列)

我应该使用什么?

4

2 回答 2

1

write.table在文件中写入单行有点矫枉过正。我建议您cat直接在矢量上使用。正如您从错误消息中看到的那样,这就是write.table引擎盖下的用途。这有效:

cat(with(MLest, c(coef, sigma2)), "\n", sep = "\t",
    file = filename, append = TRUE)

但我要指出:每次运行此命令时,都会创建一个文件句柄,移动到文件末尾,写入新行,然后关闭文件句柄。这是非常低效的,你最好打开一个文件连接:

fh <- open(filename)
for (...) { 
   MLest <- arima(...)
   cat(with(MLest, c(coef, sigma2)), "\n", sep = "\t", file = fh)
}
close(fh)

这样,只会创建一个文件句柄,并且它始终指向文件的末尾。


或者,您可以等待所有 arima 输出来创建整个 data.frame 或系数矩阵,然后再通过一次调用write.table.

假设您已经建立了一个输出列表llarima您可以通过执行以下操作创建和写入该系数矩阵:

output.mat <- t(sapply(ll, with, c(coef, sigma2 = sigma2)))
write.table(output.mat, file = "test.csv", row.names = FALSE)
于 2013-11-30T12:30:29.197 回答
0

尝试

write.table(unique(as.vector(MLest[1:2])), file=filename,sep=" ", col.names = F, row.names = F)
于 2013-11-30T12:08:51.933 回答