简体   繁体   English

用于矩阵运算的 for 循环

[英]For loop for matrix operations

Trying to use "for loop" in R.尝试在 R 中使用“for 循环”。 I have a vector of length 44 with 4401 observations read from data file "data.csv".我有一个长度为 44 的向量,其中包含从数据文件“data.csv”中读取的 4401 个观察值。

I am converting it to a matrix for working on each column as a time series data.我将其转换为矩阵,以便将每一列作为时间序列数据处理。

I want to extract each column, do forecasting and then make a matrix for that.我想提取每一列,进行预测,然后为此制作一个矩阵。

What is the easiest way to do that?最简单的方法是什么?

library(forecast)
data<-read.table(file="data.csv",sep=",",row.names=NULL,header=FALSE)
x <- matrix(1:47, ncol = 1, byrow = FALSE)
for (i in 1:4401)
{
y <- data[i]
y_ts <- ts(y, start=c(2016,1), end=c(2019,8), frequency=12)
AutoArimaModel=auto.arima(y_ts)
forecast=predict(AutoArimaModel, 3)
output <- matrix(forecast$pred, ncol = 1, byrow = FALSE)
ym = data.matrix(y)
z = rbind(ym,output)
x = cbind(x,z)}

It is just running for i = 1 and giving me error as below:它只是为i = 1运行并给我如下错误:

Error in array(x, c(length(x), 1L), if (.is,null(names(x))) list(names(x): , 'data' must be of a vector type, was 'NULL'数组中的错误(x,c(长度(x),1L),如果(.is,null(names(x)))list(names(x):,'data'必须是向量类型,是'NULL '

Use the tibbletime package: https://www.business-science.io/code-tools/2017/09/07/tibbletime-0-0-1.html使用 tibbletime package: https://www.business-science.io/code-tools/2017/09/07/tibbletime-0-0-1.ZFC35FDC70D5FC67D2368C

Read the data with readr::read_csv such that it's a tibble.使用readr::read_csv读取数据,使其成为一个小标题。 Turn it into a tibbletime with your date vector.用你的日期向量把它变成一个tibbletime。 Use tmap_* functions as described in the article to encapsulate your forecasting code and map them to the columns of the tibbletime.使用文章中描述的tmap_*函数将您的预测代码和 map 封装到 tibbletime 的列中。 The article should have all the info you need to implement this.这篇文章应该包含实现这一点所需的所有信息。

The problem seems to be your data source.问题似乎是您的数据源。 This works:这有效:

n_col <- 5
n_rows <- 44

#generate data
data <- data.frame(replicate(n_col, rnorm(n_rows)))

x <- matrix(1:47, ncol = 1, byrow = FALSE)

for (i in seq_len(n_col)) {
  y <- data[i]
  y_ts <- ts(y, start=c(2016,1), end=c(2019,8), frequency=12)
  AutoArimaModel=auto.arima(y_ts)
  forecast=predict(AutoArimaModel, 3)
  output <- matrix(forecast$pred, ncol = 1, byrow = FALSE)
  ym = data.matrix(y)
  z = rbind(ym,output)
  x = cbind(x,z)}
x

As an aside, I think I would approach it like this, especially if you have 4,401 fields to perform an auto.arima on:顺便说一句,我想我会这样处理它,特别是如果你有 4,401 个字段来执行 auto.arima:

y_ts <- ts(data, start = c(2016, 1), end = c(2019, 8), frequency = 12)

library(future.apply)
plan(multiprocess)

do.call(
  cbind,
  future_lapply(y_ts,
       function(y_t) {
         AutoArimaModel = auto.arima(y_t)
         forecast = predict(AutoArimaModel, 3)
         output = matrix(forecast$pred, ncol = 1, byrow = F)
         ym = data.matrix(y_t)
         z = rbind(ym, output)
       }
  )
)

So, your code needed a partial re-write!因此,您的代码需要部分重写!

If I understand, you want to get 3 forecasts for every 44 time-series data.如果我理解,您希望每 44 个时间序列数据获得 3 个预测。 I used the.xlsx data that you provided.我使用了您提供的.xlsx 数据。

library(forecast)
library(readxl)

data<-read_excel("data.xlsx",col_names = F)

z <- NULL
data <- t(data)
forecast_horizon <- 3

for (i in 1:ncol(data)){
  y <- data[,i]

  y_ts <- ts(y, start=c(2016,1), end=c(2019,8), frequency=12)
  AutoArimaModel <- auto.arima(y_ts)
  forecast <- tryCatch(predict(AutoArimaModel, forecast_horizon),
                       error = function(e) data.frame(pred = rep(NA,forecast_horizon))) 

  output <- matrix(forecast$pred, ncol = 1, byrow = FALSE)

  z = cbind(z,output)

}

Pay attention to the usage of tryCatch which is used because there is one time series that produces errors when accessing the predictions (you can investigate further why this is the case.)注意使用tryCatch的用法,因为有一个时间序列在访问预测时会产生错误(您可以进一步调查为什么会这样。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM