Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 36 ยท Panel and Time Series

Time Series Basics: tsset, Lags and Plots

Prepare time-series data in Stata with tsset and %t date formats, use lag and difference operators, plot with tsline and test for autocorrelation.

A time series is a single sequence of observations ordered in time: monthly sales, daily temperature, quarterly GDP. The order matters because today's value usually depends on yesterday's, which breaks the independence assumption behind ordinary regression. Stata handles this with one declaration, tsset, which unlocks lag operators, plots, autocorrelation tools and the time-series estimators.

Stata cannot run on this site. Commands are shown as read-only blocks, and the runnable examples are Python (pandas and NumPy) stand-ins that perform the same calculations on simulated data.

Dates in Stata

Stata stores dates as integers counting from 1 January 1960 (day 0). The display format decides how they look: %td daily, %tw weekly, %tm monthly, %tq quarterly, %ty yearly. If your file has a string date, convert it:

STATA
generate date = date(datestr, "YMD")
format date %td

generate month = ym(year, mon)
format month %tm

generate qtr = yq(year, quarter)
format qtr %tq

Functions such as date(), monthly(), quarterly(), mdy() and ym() convert strings or components. Always format the result; an unformatted date looks like an unexplained number such as 22371.

Declaring the series

STATA
tsset month

Stata checks that the time variable is unique and sorted, and reports the range and the delta (the step). If observations are missing from the calendar, say a holiday in daily data, either keep the gaps (lags will be missing there) or use tsfill to insert rows, or define a business calendar with bcal. With panel data use xtset instead; the same operators then work within each unit.

Time-series operators

After tsset, you can write operators in front of a variable name instead of generating new variables:

Operator Meaning Example
L. Lag 1 L.sales
L2. Lag 2 L2.sales
F. Lead 1 F.sales
D. First difference D.sales = sales - L.sales
S12. Seasonal difference S12.sales for monthly data

They work directly inside commands:

STATA
regress sales L.sales L2.sales
regress D.sales L.D.sales
summarize D.sales

Operators can be combined: LD.sales is the lag of the first difference. A lag of a variable when the previous period is missing from the data is itself missing, so gaps do not cause silent mistakes.

Plotting

STATA
tsline sales
tsline sales, tline(2020m3)
ac sales, lags(24)     // autocorrelation function
pac sales, lags(24)    // partial autocorrelation

A first plot tells you more than any test: look for trend, seasonality, breaks and outliers. ac draws the autocorrelation function with confidence bands; pac the partial version. Slowly decaying autocorrelation suggests a trend or a unit root; spikes at lag 12 in monthly data suggest annual seasonality.

Stand-in: building lags and autocorrelations

The block simulates an AR(1) series, forms the lag and difference with pandas (the analogue of L. and D.), and computes the autocorrelation function by hand.

Python Lags, differences and ACF (Python stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The sample ACF decays toward zero in the shape of the theoretical 0.7^k (finite-sample values run lower than theory). That decay is the fingerprint of an AR(1) process, and ac in Stata would draw it with confidence bands.

Regression with time series and autocorrelation

Fit a dynamic regression, then test its errors:

STATA
regress sales L.sales advertising
estat bgodfrey, lags(1 2)   // Breusch-Godfrey test for serial correlation
estat dwatson               // Durbin-Watson (only without lagged outcome)
newey sales advertising, lag(4)   // Newey-West standard errors

Residual autocorrelation leaves coefficients unbiased in static models but makes usual standard errors too small. newey gives heteroskedasticity- and autocorrelation-consistent (HAC) standard errors; regress ..., vce(hac nwest 4) is equivalent. With a lagged outcome on the right side, autocorrelation also biases the coefficients, so add enough lags until estat bgodfrey stops rejecting.

Moving averages and smoothing

STATA
tssmooth ma sales_ma = sales, window(2 1 2)   // centered 5-period average
tssmooth exponential sales_es = sales

Smoothing is for looking at the signal; do not regress on smoothed variables as if they were data. The next lesson covers ARIMA models and unit-root tests.

Python Newey-West vs classical SE (Python stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

With positively autocorrelated errors and a persistent regressor, the HAC standard error is clearly larger than the classical one. Trusting the classical value would make results look more certain than they are.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which format displays monthly dates?

    %tm is monthly; %td daily, %tq quarterly, %ty yearly.
  2. 2. What does D.sales give you?

    D. is the first-difference operator, equal to sales - L.sales.
  3. 3. Which command declares a time series?

    tsset declares the time variable; xtset is for panels.
  4. 4. Newey-West standard errors correct for

    HAC standard errors remain valid with serial correlation and non-constant variance.

Summary

  • Stata dates are integers with %td, %tm, %tq, %ty formats; convert strings with date(), ym(), yq().
  • tsset unlocks L., F., D., S. operators; plot with tsline, ac, pac.
  • Test residual autocorrelation with estat bgodfrey and use newey for HAC errors.
  • All runnable code here is Python stand-in code.

Next: ARIMA models and unit-root tests