# Time Series Cross Sectional Analysis and Forecasting With R

cty time tl

Argentina 2009_Q4 3

Argentina 2010_Q1 2

Argentina 2010_Q2 7

Argentina 2010_Q3 7

Argentina 2010_Q4 10

Argentina 2011_Q1 7

Argentina 2011_Q2 7

Argentina 2011_Q3 1

Argentina 2011_Q4 7

Argentina 2012_Q1 5

The data set has around 40 countries with each country having quarterly data for 5 years. As I am new to R, can someone assist me with how to perform a cross sectional time series ARIMA/ARMA models etc. and forecast in R? Some basic codes to prepare the data, to perform analysis and forecast for this series as an example would be helpful.

• I would say that 20 observations is not so much for time series analysis. How about using some form of panel regression? – Analyst Aug 26 '14 at 9:21
• I think you understood it wrong. I have in total (40 countries * 5years * 4 quartes) observations. But still can you please elaborate on the Panel Regression? I only have column "tl" as the only data row. I mean what will I regress on? – Anjali Aug 28 '14 at 6:34

I had a similar question and the answers there were quite helpful. You might want to read this.

I would summarise it here for you. If your dataset is called df and you want to forecast for each of the 40 countries separately, then you can do so by using either of the packages- dplyr or data.table along with forecast:

library(dplyr) library(forecast) model_fits <- group_by(df, cty) %>% do(fit=auto.arima(.\$tl))

OR

library(data.table) library(forecast) temp <- setDT(temp)[, list(AR = list(auto.arima(tl))), by = cty]

• Thank you for you kind response Shraddha! What if I dont want to use auto.arima? I want to fit model by each country and then forecast also by each country. Do I have to write any loop for that? – Anjali Aug 28 '14 at 6:34
• May be I don't completely understand what you asked, but if you do not want to use auto.arima and want to be able to manually play around with the AR, I and MA terms and do it for each country, I don't really see a way to fit it in for loop..moreover, auto.arima does that for you anyway by checking AIC levels and selecting the best model of all.. – Shraddha Aug 28 '14 at 8:15
• have you looked into the hts package in R? If your dataset has hierarchical structure then that might be useful.. – Shraddha Aug 28 '14 at 8:22