/* Introduction to Time Series Analysis, DG ECFIN Laura Mayoral November 2021 */ ************************************************* *Lecture 1 ************************************************ *O. Prepare your path and data *Replace this path by the path you want to work from cd "/Users/lauramayoral/Dropbox/docum_dropbox/clases/ECFIN_time series/STATA" *Install some data and do files that we will use later on. *Source: Introduction to Time series using Stata, by Sean Becketti net from http://www.stata-press.com/data/itsus/ net get itsus_files net get itsus_data *this additional command installs a goodness of fit statistic to be used later on net install itsus_files ******************************************************************************** *1. Load data (source: World Bank) ******************************************************************************** use wbdata,clear *keep data for a single country (so we don't have a panel of countries): for instance, keep data for the US. keep if code=="USA":code *It's useful to start declaring that our data is a time series: tsset year, yearly ******************************************************************************** *2. Plot time series data ******************************************************************************** *plot single time series twoway line gdp year twoway connected co2 year, graphregion(color(white)) graph export "/Users/lauramayoral/Dropbox/docum_dropbox/clases/ECFIN_time series/Slides/Lecture 1/figures/co2.pdf", as(pdf) name("Graph") *...or combine them in one graph twoway (connected co2 year) (line gdp year) *it looks bad because of the different scales, but we can create a graph with a second axis twoway (connected co2 year) (line gdp year, yaxis(2)), graphregion(color(white)) graph export "/Users/lauramayoral/Dropbox/docum_dropbox/clases/ECFIN_time series/Slides/Lecture 1/figures/twoseries.pdf", as(pdf) name("Graph") *We can add years to the data points two connected co2 year, mlabel(year) graphregion(color(white)) graph export "/Users/lauramayoral/Dropbox/docum_dropbox/clases/ECFIN_time series/Slides/Lecture 1/figures/label.pdf", as(pdf) name("Graph") *Since we've declared that our data is a time series, we can simply time tsline (and stata will plot it against time): tsline gdp *AUTOCOVARIANCE AND AUTOCORRELATION FUNCTION tsline urbpop ac urbpop, lags(17) ******************************************************************************** *SIMULATING A FEW TIME SERIES PROCESS ******************************************************************************** clear all set more off *1) I.I.D sequence set obs 300 //For the 300 observations in every time series gen t = _n tsset t g y1=0 * Creating a time series forvalues i = 1(1)300 { gen e = rnormal(0,1) replace y1=e if t==1 replace y1 = e if t > 1 mkmat t y1 e, matrix(serie) drop e } tsline y1 ac y1 pac y1 *********************************************************** *MA(1) with phi=0.8 clear all set more off set obs 300 //For the 300 observations in every time series gen t = _n tsset t g y1=0 * Creating a time series forvalues i = 1(1)300 { gen e = rnormal(0,1) replace y1=e if t==1 replace y1 = 0.8*L1.e + e if t > 1 mkmat t y1 e, matrix(serie) drop e } tsline y1 ac y1 pac y1 *********************************************************** *AR(1) with phi=0.8 clear all set more off set obs 300 //For the 300 observations in every time series gen t = _n tsset t g y1=0 * Creating a time series forvalues i = 1(1)300 { gen e = rnormal(0,1) replace y1=e if t==1 replace y1 = 0.8*L1.y1 + e if t > 1 mkmat t y1 e, matrix(serie) drop e } tsline y1 , graphregion(color(white)) graph export "/Users/lauramayoral/Dropbox/docum_dropbox/clases/ECFIN_time series/Slides/Lecture 1/figures/ar1.pdf", as(pdf) name("Graph") ac y1, graphregion(color(white)) graph export "/Users/lauramayoral/Dropbox/docum_dropbox/clases/ECFIN_time series/Slides/Lecture 1/figures/ar1_ac.pdf", as(pdf) name("Graph") pac y1 , graphregion(color(white)) graph export "/Users/lauramayoral/Dropbox/docum_dropbox/clases/ECFIN_time series/Slides/Lecture 1/figures/ar1_pac.pdf", as(pdf) name("Graph") *********************************************************** *AR(1) with phi=1--NOT STATIONARY!! clear all set more off set obs 300 //For the 300 observations in every time series gen t = _n tsset t g y1=0 * Creating a time series forvalues i = 1(1)300 { gen e = rnormal(0,1) replace y1=e if t==1 replace y1 = L1.y1 + e if t > 1 mkmat t y1 e, matrix(serie) drop e } tsline y1 ac y1 pac y1 tsline y1 , graphregion(color(white)) graph export "/Users/lauramayoral/Dropbox/docum_dropbox/clases/ECFIN_time series/Slides/Lecture 1/figures/rw.pdf", as(pdf) name("Graph") ac y1, graphregion(color(white)) graph export "/Users/lauramayoral/Dropbox/docum_dropbox/clases/ECFIN_time series/Slides/Lecture 1/figures/rw_ac.pdf", as(pdf) name("Graph") pac y1 , graphregion(color(white)) graph export "/Users/lauramayoral/Dropbox/docum_dropbox/clases/ECFIN_time series/Slides/Lecture 1/figures/rw_pac.pdf", as(pdf) name("Graph") *********************************************************** *ARMA(2,2) clear all set more off set obs 300 //For the 300 observations in every time series gen t = _n tsset t g y1=0 * Creating a time series forvalues i = 1(1)300 { gen e = rnormal(0,1) replace y1=0 if t==1 replace y1 = 0.7*L1.y1 + e +0.6*L1.e if t == 2 replace y1 = 0.7*L1.y1 +0.2*L2.y1 + e + 0.6*L1.e - 0.08*L2.e if t > 2 mkmat t y1 e, matrix(serie) drop e } tsline y1 , graphregion(color(white)) graph export "/Users/lauramayoral/Dropbox/docum_dropbox/clases/ECFIN_time series/Slides/Lecture 1/figures/arma.pdf", as(pdf) name("Graph") ac y1, graphregion(color(white)) graph export "/Users/lauramayoral/Dropbox/docum_dropbox/clases/ECFIN_time series/Slides/Lecture 1/figures/arma_ac.pdf", as(pdf) name("Graph") pac y1 , graphregion(color(white)) graph export "/Users/lauramayoral/Dropbox/docum_dropbox/clases/ECFIN_time series/Slides/Lecture 1/figures/arma_pac.pdf", as(pdf) name("Graph") ***END****************************