![]() |
|
Programs
Courses
Course Schedule
![]() ![]() |
Time Series in StataOne of Stata's greatest strengths is its ability to work with time-series data. Time-series data are any data in which time (hours, days, years) is a significant component of the analysis. An extension of a simple time series is panel, or cross-sectional time series, in which you have time-sereies data from several observations (people, countries, companies). Here we will provide a short introduction to time-series analysis in Stata.
Time-series DataTo use Stata's time-series functions and analyses, you must first make sure that your data are, indeed, time-series. First, you must have a date variable that is in a date format. Secondly, you must make sure that your data are sorted by this date variable. If you have panel data, then your data must be sorted by the date variable within the variable that identifies the panel. Finally, you must use the tsset command to tell Stata that your data are time-series:
. tsset panelvar datevar
DatesTime-series dates in Stata have their own formats similar to regular date formats. The main difference is that for a regular date format a "unit" or single "time period" is one day. For time series formats, a unit or single time period can be a day, week, month, quarter, half-year or year. There is a format for each of these time periods:
You should note that in the weekly format, the year is divided into 52 weeks. The first week is defined as the first seven days, regardless of what day of the week it may be. Also, the last week, week 52, may have 8 or 9 days. For the quarterly format, the first quarter is January through March. For the half-yearly format, the first half of the year is January through June. It's even more important to note that you cannot jump from one format to another by simply re-issuing the format command because the units are different in each format. Here are the corresponding results for January 1, 1999, which is an elapsed date of 14245:
These dates are so different because the elapsed date is actually the number of weeks, quarters, etc., from the first week, quarter, etc of 1960. The value for %ty is missing because it would be equal to the year 14,245 which is beyond what Stata can accept. So what if you need to convert from one time unit to another? Look in the Stata User's Guide (version 6), page 296.
Inputting Time-Series DatesOften we will have separate variables for the various components of the date; we need to put them together before we can designate them as proper time-series dates. Stata provides as easy way to do this, wether the variables are numeric or string. First we will discuss numeric variables.Let's say you have two variables, "year" and "quarter" and you need to combine them so that you can make a time-series date. To do this, you would use the "yq()" function:
. gen qtr=yq(1990,3)
Sometimes, our data will have the dates in string format. Stata also provides a way to convert these to time-series dates. The first thing you need to know is, like converting strings to regular dates, the string must be easily separated into its components. In other words, strings like "01feb1990" "February 1, 1990" "02/01/90" are acceptable, but "020190" is not. For example, let's say that you have a string variable "sdate" with values like "01feb1990" and you need to convert it to a daily time-series date:
monthly(stringvar,"my") quarterly(stringvar,"qy") halfyearly(stringvar,"hy") yearly(stringvar,"y") Specifying DatesOften we need to consuct a particular analysis only on observations that fall on a certain date. To do this, we have to use something called a date literal. A date literal is simply a way of entering a date in words and have Stata automatically convert it to an elapsed date. As with the d() literal to specify a regular date, there are the w(), m(), q(), h(), and y() literals for entering weekly, monthly, quarterly, half-yearly, and yearly dates, respectively. Here are some examples:
. sum income if q(1988-3) . tab gender if y(1999)
. sum income if twithin(1988-3,1998-3) Time Series Variable ListsOften in time-series analyses we need to "lag" or "lead" the values of a variable from one observation to the next. If we have many variables, this can be cumbersome, especially if we need to lag a variable more than once. In Stata, we can specify which variables are to be lagged and how many times without having to create new variables, thus saving alot of disk space and memory. You should note that the tsset command must have been issued before any of the "tricks" in this section will work. Also, if you have defined your data as panel data, Stata will automatically re-start the calculations as it comes to the beginning of a panel so you need not worry about values from one panel being carried over to the next.L.varname and F.varnameIf you need to lag or lead a variable for an analysis, you can do so by using the L.varname (to lag) and F.varname (to lead). Both work the same way, so we'll just show some examples with L.varname. Let's say you want to regress this year's income on last year's income:
. reg income L(1/3).income D.varnameAnother useful shortcut is D.varname, which takes the difference of income in time 1 and income in time 2. For example, let's say a person earned $20 yesterday and $30 today.
So, you can see that D.=(income-incomet-1) and D2=(income-incomet-1)-(incomet-1-incomet-2) S.varnameS.varname refers to seasonal differences and works like D.varname, except that the difference is always taken from the current observation to the nth observation:
In other words: S.=income-incomet-1 and S2.=income-incomet-2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| top | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||