Time Series (Ver

Time Series (Ver

Time Series (ver. 1.5) Oscar Torres-Reyna Data Consultant [email protected] http://dss.princeton.edu/training/ PU/DSS/OTR Date variable For ‘date1’ type: gen datevar = date(date1,"DMY", 2099) format datevar %td /*For daily data*/ For ‘date2’ type: gen datevar = date(date2,"MDY", 2099) format datevar %td /*For daily data*/ For ‘date3’ type: tostring date3, gen(date3a) gen datevar=date(date3a,"YMD") format datevar %td /*For daily data*/ For ‘date4’ see next page Date in integers and unbalanced use "http://www.princeton.edu/~otorres/Stata/date.dta", clear drop date1 date2 rename date3 date4 *Year, month, day tostring date4, gen(date4a) gen len = length(date4a) gen year = substr(date4a,1,4) *When len=6, month is in 5th position and day in 6th gen month = substr(date4a,5,1) if len == 6 gen day = substr(date4a,6,1) if len == 6 *When len=7 is hard to distinguish month/day, we skip *When len=8, month is in 5th/6th positon and day in 7th/8th replace month = substr(date4a,5,2) if len == 8 replace day = substr(date4a,7,2) if len == 8 destring month day year, replace *Creating datevar gen datevar = mdy(month,day,year) format datevar %td *Filling in the missing dates replace datevar = datevar[_n-1] + 1 if datevar == . Date variable (cont.) If the original date variable is string (i.e. color red): gen week= weekly(stringvar,"wy") stringvar NOTE: Remember to format the date variable accordingly. After creating it type: gen month= monthly( ,"my") gen quarter= quarterly(stringvar,"qy") format datevar %t? /*Change ‘datevar’ with your date variable*/ stringvar gen half = halfyearly( ,"hy") Change “?” with the correct format: w (week), m (monthly), q (quarterly), h (half), y (yearly). gen year= yearly(stringvar,"y") If the components of the original date are in different numeric variables (i.e. color black): gen daily = mdy(month,day,year) gen week = yw(year, week) NOTE: Remember to format the date variable accordingly. After creating it type: year,month gen month = ym( ) format datevar %t? /*Change ‘datevar’ with your date variable*/ year,quarter gen quarter = yq( ) Change “?” with the correct format: w (week), m (monthly), q (quarterly), h (half), y (yearly). gen half = yh(year,half-year) To extract days of the week (Monday, Tuesday, etc.) use the function dow() gen dayofweek= dow(date) Replace “date” with the date variable in your dataset. This will create the variable ‘dayofweek’ where 0 is ‘Sunday’, 1 is ‘Monday’, etc. (type help dow for more details) To specify a range of dates (or integers in general) you can use the tin() and twithin() functions. tin() includes the first and last date, twithin() does not. Use the format of the date variable in your dataset. /* Make sure to set your data as time series before using tin/twithin */ tsset date regress y x1 x2 if tin(01jan1995,01jun1995) regress y x1 x2 if twithin(01jan2000,01jan2001) 3 PU/DSS/OTR Date variable (example) Time series data is data collected over time for a single or a group of variables. For this kind of data the first thing to do is to check the variable that contains the time or date range and make sure is the one you need: yearly, monthly, quarterly, daily, etc. The next step is to verify it is in the correct format. In the example below the time variable is stored in “date” but it is a string variable not a date variable. In Stata you need to convert this string variable to a date variable.* A closer inspection of the variable, for the years 2000 the format changes, we need to create a new variable with a uniform format. Type the following: use http://dss.princeton.edu/training/tsdata.dta gen date1=substr(date,1,7) gen datevar=quarterly(date1,"yq") format datevar %tq browse date date1 datevar For more details type help date *Data source: Stock & Watson’s companion materials From daily/monthly date variable to quarterly use "http://dss.princeton.edu/training/date.dta", clear *Quarterly date from daily date gen datevar=date(date2,"MDY", 2099) /*Date2 is a string date variable*/ format datevar %td gen quarterly = qofd(datevar) format quarterly %tq *Quarterly date from monthly date gen month = month(datevar) gen day=day(datevar) gen year=year(datevar) gen monthly = ym(year,month) format monthly %tm gen quarterly1 = qofd(dofm(monthly)) format quarterly1 %tq browse date2 datevar quarterly monthly quarterly1 From daily to weekly and getting yearly use "http://dss.princeton.edu/training/date.dta", clear gen datevar = date(date2, "MDY", 2099) format datevar %td gen year= year(datevar) gen w = week(datevar) gen weekly = yw(year,w) format weekly %tw browse ******************************************************** * From daily to yearly gen year1 = year(datevar) * From quarterly to yearly gen year2 = yofd(dofq(quarterly)) * From weekly to yearly gen year3 = yofd(dofw(weekly)) Setting as time series: tsset Once you have the date variable in a ‘date format’ you need to declare your data as time series in order to use the time series operators. In Stata type: tsset datevar . tsset datevar time variable: datevar, 1957q1 to 2005q1 delta: 1 quarter If you have gaps in your time series, for example there may not be data available for weekends. This complicates the analysis using lags for those missing dates. In this case you may want to create a continuous time trend as follows: gen time = _n Then use it to set the time series: tsset time In the case of cross-sectional time series type: sort panel date by panel: gen time = _n xtset panel time 6 PU/DSS/OTR Filling gaps in time variables Use the command tsfill to fill in the gap in the time series. You need to tset, tsset or xtset the data before using tsfill. In the example below: tset quarters tsfill Type for more details. help tsfill 7 PU/DSS/OTR Subsetting tin/twithin With tsset (time series set) you can use two time series commands: tin (‘times in’, from a to b) and twithin (‘times within’, between a and b, it excludes a and b). If you have yearly data just include the years. list datevar unemp if tin(2000q1,2000q4) datevar unemp 173. 2000q1 4.033333 174. 2000q2 3.933333 175. 2000q3 4 176. 2000q4 3.9 . list datevar unemp if twithin(2000q1,2000q4) datevar unemp 174. 2000q2 3.933333 175. 2000q3 4 /* Make sure to set your data as time series before using tin/twithin */ tsset date regress y x1 x2 if tin(01jan1995,01jun1995) regress y x1 x2 if twithin(01jan2000,01jan2001) 8 PU/DSS/OTR Merge/Append See http://dss.princeton.edu/training/Merge101.pdf PU/DSS/OTR Lag operators (lag) Another set of time series commands are the lags, leads, differences and seasonal operators. It is common to analyzed the impact of previous values on current ones. To generate values with past values use the “L” operator generate unempL1=L1.unemp generate unempL2=L2.unemp list datevar unemp unempL1 unempL2 in 1/5 . generate unempL1=L1.unemp (1 missing value generated) . generate unempL2=L2.unemp (2 missing values generated) . list datevar unemp unempL1 unempL2 in 1/5 datevar unemp unempL1 unempL2 1. 1957q1 3.933333 . 2. 1957q2 4.1 3.933333 . 3. 1957q3 4.233333 4.1 3.933333 4. 1957q4 4.933333 4.233333 4.1 5. 1958q1 6.3 4.933333 4.233333 In a regression you could type: regress y x L1.x L2.x or regress y x L(1/5).x 10 PU/DSS/OTR Lag operators (forward) To generate forward or lead values use the “F” operator generate unempF1=F1.unemp generate unempF2=F2.unemp list datevar unemp unempF1 unempF2 in 1/5 . generate unempF1=F1.unemp (1 missing value generated) . generate unempF2=F2.unemp (2 missing values generated) . list datevar unemp unempF1 unempF2 in 1/5 datevar unemp unempF1 unempF2 1. 1957q1 3.933333 4.1 4.233333 2. 1957q2 4.1 4.233333 4.933333 3. 1957q3 4.233333 4.933333 6.3 4. 1957q4 4.933333 6.3 7.366667 5. 1958q1 6.3 7.366667 7.333333 In a regression you could type: regress y x F1.x F2.x or regress y x F(1/5).x 11 PU/DSS/OTR Lag operators (difference) To generate the difference between current a previous values use the “D” operator generate unempD1=D1.unemp /* D1 = y t – yt-1 */ generate unempD2=D2.unemp /* D2 = (y t – yt-1) – (yt-1 – yt-2) */ list datevar unemp unempD1 unempD2 in 1/5 . generate unempD1=D1.unemp (1 missing value generated) . generate unempD2=D2.unemp (2 missing values generated) . list datevar unemp unempD1 unempD2 in 1/5 datevar unemp unempD1 unempD2 1. 1957q1 3.933333 . 2. 1957q2 4.1 .1666665 . 3. 1957q3 4.233333 .1333332 -.0333333 4. 1957q4 4.933333 .7000003 .5666671 5. 1958q1 6.3 1.366667 .6666665 In a regression you could type: regress y x D1.x 12 PU/DSS/OTR Lag operators (seasonal) To generate seasonal differences use the “S” operator generate unempS1=S1.unemp /* S1 = y t – yt-1 */ generate unempS2=S2.unemp /* S2 = (y t – yt-2) */ list datevar unemp unempS1 unempS2 in 1/5 . generate unempS1=S1.unemp (1 missing value generated) . generate unempS2=S2.unemp (2 missing values generated) . list datevar unemp unempS1 unempS2 in 1/5 datevar unemp unempS1 unempS2 1. 1957q1 3.933333 . 2. 1957q2 4.1 .1666665 . 3. 1957q3 4.233333 .1333332 .2999997 4. 1957q4 4.933333 .7000003 .8333335 5. 1958q1 6.3 1.366667 2.066667 In a regression you could type: regress y x S1.x 13 PU/DSS/OTR Correlograms: autocorrelation To explore autocorrelation, which is the correlation between a variable and its previous values, use the command corrgram.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    33 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us