R – adding group value into result of auto.arima

If you want to use arima with data that are divided into groups, you can do something like this.

data <- read.csv("some_input.txt", header=TRUE, sep="\t")
split_data <- split( data, data$group )

for (name in names(split_data)) {
  pred <- c(spt1[[name]][['value']])
  indata <- stats::ts( pred )
  result<-forecast(auto.arima(indata,seasonal=FALSE),seasonal=FALSE,h=12)
  print(result)
}

Result will be grouped in sections, like this

[1] "1"
   Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
[1] "2"
   Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
[1] "3"
   Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
[1] "4"
   Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
[1] "5"
   Point Forecast Lo 80 Hi 80 Lo 95 Hi 95

If you want to add group No. into result, you can easily achieve that with awk

cat result | awk 'BEGIN{regex="^\\[";} \ 
{ if (match($0, regex)) { split($0,arr,"\""); number = arr[2]} \
else \
{ if ($0 ~ /Point/) {} \
else \
{ print $0,number } }}'