Example: air traffic controller

Time Series - Princeton University

time Series (ver. ). Oscar Torres-Reyna Data Consultant 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 " ~otorres/ ", 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.

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: ... The number of lags depend on theory, AIC/BIC process or experience. The output includes autocorrelation coefficient and ...

Tags:

  Series, University, Time, Princeton, Theory, Time series, Princeton university, String

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Transcription of Time Series - Princeton University

1 time Series (ver. ). Oscar Torres-Reyna Data Consultant 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 " ~otorres/ ", 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.

2 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 ( color red): gen week= weekly(stringvar,"wy"). NOTE: Remember to format the date variable accordingly. After creating it type: gen month= monthly(stringvar,"my"). gen quarter= quarterly(stringvar,"qy") format datevar %t? /*Change datevar' with your date variable*/. gen half = halfyearly(stringvar,"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 ( color black): gen daily = mdy(month,day,year).

3 Gen week = yw(year, week) NOTE: Remember to format the date variable accordingly. After creating it type: gen month = ym(year,month) format datevar %t? /*Change datevar' with your date variable*/. gen quarter = yq(year,quarter). 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.

4 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.

5 Type the following: use 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 " ", 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 " ", clear gen datevar = date(date2, "MDY", 2099).

6 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.

7 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 help tsfill for more details. 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 174. 2000q2 175. 2000q3 4. 176. 2000q4 . list datevar unemp if twithin(2000q1,2000q4). datevar unemp 174. 2000q2 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).

8 Regress y x1 x2 if twithin(01jan2000,01jan2001). 8. PU/DSS/OTR. Merge/Append See 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= generate unempL2= list datevar unemp unempL1 unempL2 in 1/5.. generate unempL1= (1 missing value generated).. generate unempL2= (2 missing values generated).. list datevar unemp unempL1 unempL2 in 1/5. datevar unemp unempL1 unempL2. 1. 1957q1 .. 2. 1957q2 . 3. 1957q3 4. 1957q4 5. 1958q1 In a regression you could type: regress y 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= generate unempF2= list datevar unemp unempF1 unempF2 in 1/5.

9 Generate unempF1= (1 missing value generated).. generate unempF2= (2 missing values generated).. list datevar unemp unempF1 unempF2 in 1/5. datevar unemp unempF1 unempF2. 1. 1957q1 2. 1957q2 3. 1957q3 4. 1957q4 5. 1958q1 In a regression you could type: regress y 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 = y t yt-1 */. generate unempD2= /* D2 = (y t yt-1) (yt-1 yt-2) */. list datevar unemp unempD1 unempD2 in 1/5.. generate unempD1= (1 missing value generated).. generate unempD2= (2 missing values generated).. list datevar unemp unempD1 unempD2 in 1/5. datevar unemp unempD1 unempD2. 1. 1957q1 .. 2. 1957q2 .1666665 . 3. 1957q3 .1333332 4. 1957q4.

10 7000003 .5666671. 5. 1958q1 .6666665. In a regression you could type: regress y x 12. PU/DSS/OTR. Lag operators (seasonal). To generate seasonal differences use the S operator generate unempS1= /* S1 = y t yt-1 */. generate unempS2= /* S2 = (y t yt-2) */. list datevar unemp unempS1 unempS2 in 1/5.. generate unempS1= (1 missing value generated).. generate unempS2= (2 missing values generated).. list datevar unemp unempS1 unempS2 in 1/5. datevar unemp unempS1 unempS2. 1. 1957q1 .. 2. 1957q2 .1666665 . 3. 1957q3 .1333332 .2999997. 4. 1957q4 .7000003 .8333335. 5. 1958q1 In a regression you could type: regress y 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.


Related search queries