Solved – Time Series Cross Sectional Analysis and Forecasting With R

cross-sectiontime series

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.

Best Answer

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]

Related Question