ARIMA for each row of dataframe

Hi ARIMA experts,

We have data like below:

df = data.frame(
       Jan10 = c(0, 18, 9, 4, 20, 18, 3, 34),
       Feb10 = c(0, 19, 6, 0, 4, 14, 10, 10),
       Mar10 = c(2, 7, 12, 11, 14, 0, 12, 10),
       .......
       .......
       .......
       Oct15 = c(7, 16, 98, 42, 2, 18, 13, 3),
       Nov15 = c(9, 9, 61, 0, 41, 14, 17, 11),
       Dec15 = c(2, 72, 1, 11, 14, 2, 2, 19),
        city = as.factor(c("SD", "BLR", "HAJ", "MEX", "SD", "BLR", "HAJ",
                           "MEX")),
        type = as.factor(c("MER", "BMW", "MER", "BMW", "MER", "BMW", "MER",
                           "BMW")),
        size = as.factor(c("13", "14", "15", "16", "17", "18", "19", "20"))
)

How can we apply ARIMA for each row ? Not Column
For example: each unique combination i.e.each row, Got to apply ARIMA
Idea is to Forecast these values for next 6 months for each combination or rows.
i.e.

# Specific row or combination:
city == "SD"
type == "MER"
size == "13"
Jan 2010: 0, 
Feb 2010: 0,
Mar 2010: 2, 
.........
Oct 2015: 7,
Nov 2015: 9,
Dec 2015: 2 
# What is the value for Jan-Jun 2016 ? 

Idea is to use For loop or map functions !
Feeding the Months data as input to ARIMA and xreg parameters as
the city, type and size

Could you please enlighten me
Abi

If you're trying to forecast this as a time series, you'll need to format it as such. Take a look at the tutorial here:
https://www.datascience.com/blog/introduction-to-forecasting-with-arima-in-r-learn-data-science-tutorials

Note that it might be easier to work with if you tidy up the data to use the month and year values as a variable, so that you can format them as dates:

suppressPackageStartupMessages(library(tidyverse))
df = data.frame(
  Jan10 = c(0, 18, 9, 4, 20, 18, 3, 34),
  Feb10 = c(0, 19, 6, 0, 4, 14, 10, 10),
  Mar10 = c(2, 7, 12, 11, 14, 0, 12, 10),
  Oct15 = c(7, 16, 98, 42, 2, 18, 13, 3),
  Nov15 = c(9, 9, 61, 0, 41, 14, 17, 11),
  Dec15 = c(2, 72, 1, 11, 14, 2, 2, 19),
  city = as.factor(c("SD", "BLR", "HAJ", "MEX", "SD", "BLR", "HAJ",
                     "MEX")),
  type = as.factor(c("MER", "BMW", "MER", "BMW", "MER", "BMW", "MER",
                     "BMW")),
  size = as.factor(c("13", "14", "15", "16", "17", "18", "19", "20"))
)

df %>%
  gather(key = "mon_year", value = "measure", -city, -type, -size) %>%
  mutate(mon_year = zoo::as.yearmon(mon_year, format = "%b%y")) %>%
  select(one_of(c("mon_year", "city", "type", "size", "measure")))  # optional, I just like reordering the variables
#>    mon_year city type size measure
#> 1  Jan 2010   SD  MER   13       0
#> 2  Jan 2010  BLR  BMW   14      18
#> 3  Jan 2010  HAJ  MER   15       9
#> 4  Jan 2010  MEX  BMW   16       4
#> 5  Jan 2010   SD  MER   17      20
#> 6  Jan 2010  BLR  BMW   18      18
#> 7  Jan 2010  HAJ  MER   19       3
#> 8  Jan 2010  MEX  BMW   20      34
#> 9  Feb 2010   SD  MER   13       0
#> 10 Feb 2010  BLR  BMW   14      19
#> 11 Feb 2010  HAJ  MER   15       6
#> 12 Feb 2010  MEX  BMW   16       0
#> 13 Feb 2010   SD  MER   17       4
#> 14 Feb 2010  BLR  BMW   18      14
#> 15 Feb 2010  HAJ  MER   19      10
#> 16 Feb 2010  MEX  BMW   20      10
#> 17 Mar 2010   SD  MER   13       2
#> 18 Mar 2010  BLR  BMW   14       7
#> 19 Mar 2010  HAJ  MER   15      12
#> 20 Mar 2010  MEX  BMW   16      11
#> 21 Mar 2010   SD  MER   17      14
#> 22 Mar 2010  BLR  BMW   18       0
#> 23 Mar 2010  HAJ  MER   19      12
#> 24 Mar 2010  MEX  BMW   20      10
#> 25 Oct 2015   SD  MER   13       7
#> 26 Oct 2015  BLR  BMW   14      16
#> 27 Oct 2015  HAJ  MER   15      98
#> 28 Oct 2015  MEX  BMW   16      42
#> 29 Oct 2015   SD  MER   17       2
#> 30 Oct 2015  BLR  BMW   18      18
#> 31 Oct 2015  HAJ  MER   19      13
#> 32 Oct 2015  MEX  BMW   20       3
#> 33 Nov 2015   SD  MER   13       9
#> 34 Nov 2015  BLR  BMW   14       9
#> 35 Nov 2015  HAJ  MER   15      61
#> 36 Nov 2015  MEX  BMW   16       0
#> 37 Nov 2015   SD  MER   17      41
#> 38 Nov 2015  BLR  BMW   18      14
#> 39 Nov 2015  HAJ  MER   19      17
#> 40 Nov 2015  MEX  BMW   20      11
#> 41 Dec 2015   SD  MER   13       2
#> 42 Dec 2015  BLR  BMW   14      72
#> 43 Dec 2015  HAJ  MER   15       1
#> 44 Dec 2015  MEX  BMW   16      11
#> 45 Dec 2015   SD  MER   17      14
#> 46 Dec 2015  BLR  BMW   18       2
#> 47 Dec 2015  HAJ  MER   19       2
#> 48 Dec 2015  MEX  BMW   20      19

Created on 2019-02-26 by the reprex package (v0.2.1)

3 Likes

Thanks Mara for insights on transforming the data and reference for ARIMA application.
Definitely I will transform the data. but is there a way to apply a model on row wise data ?

You can take a look at this repo/slides (and also webinar) by Jenny Bryan on row-oriented workflows.

The problem I think you'll encounter in this case is that there isn't a good way of indicating that each column value in the row is of the same measure, and that each column represents a different date in a time series. But, that's just my experience working in R. Someone else might have a different suggestion.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.