简体   繁体   中英

For loop for matrix operations

Trying to use "for loop" in R. I have a vector of length 44 with 4401 observations read from data file "data.csv".

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:

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'

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

Read the data with readr::read_csv such that it's a tibble. Turn it into a tibbletime with your date vector. Use tmap_* functions as described in the article to encapsulate your forecasting code and map them to the columns of the 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:

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. I used the.xlsx data that you provided.

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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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