tsset
Declares which column is time, so lags and differences know what they mean.
What we have
- a plain whole-number time variable, and
delta() - a panel:
tsset panelvar timevarlags each unit on its own L.F.D.S.in any expression, counting in time, not in rows
Coming later
- Stata's date formats —
daily,weekly,monthly,quarterly,halfyearlyandyearlyare refused xtset, and the panel commands such asxtreg
How you write it
tsset timevar [, delta(#)] | tsset panelvar timevar
Square brackets mean optional. A vertical bar means choose one.
What it does
tsset month tells Virata the rows are a series in time order. After that, L.sales means last period's sales and D.sales means the change since last period, in any expression.
tsset panelvar timevar handles several series at once — one per state, one per firm — and each is lagged on its own, never bleeding into the next.
The important detail: the operators count in units of *time*, not in rows. L.sales at month 5 is month 4's value, and it is missing if month 4 is not in the data — even when the row physically above is month 3. Shifting rows would get that wrong silently, and most real series have a gap somewhere.
Try it
Every example below was run after sysuse vira.sample04, clear.
. tsset month
Time variable: month, 1 to 96
Delta: 1 unit
The same thing in pandas and in R
pandas
# tsset: sorted by the time variable; L. and D. count in time, not rows
R
# no R form for `tsset` yet -- see docs/r-equivalents.md
. generate growth = D.revenue / L.revenue
(1 missing values generated)
The same thing in pandas and in R
pandas
df["growth"] = ((df["revenue"] - df["revenue"].shift(1)) / df["revenue"].shift(1)) # .shift() counts ROWS; Stata's L. counts TIME. They agree only when the data is sorted with no gaps.
R
library(dplyr)
df <- df %>% mutate(growth = (c(rep(NA, 1), diff(revenue,
differences = 1)) / lag(revenue, 1)))
# This R was run in a real R (4.6.1) and gave the same numbers.
. summarize growth
Variable | Obs Mean Std. dev. Min Max
-------------+------------------------------------------------------------
growth | 95 .01867384 .166664 -.3653187 .8135293
The same thing in pandas and in R
pandas
df[['growth']].describe()
R
library(dplyr)
df %>% summarise(growth_n = sum(!is.na(growth)),
growth_mean = mean(growth, na.rm = TRUE), growth_sd = sd(growth,
na.rm = TRUE), growth_min = min(growth, na.rm = TRUE),
growth_max = max(growth, na.rm = TRUE))
# This R was run in a real R (4.6.1) and gave the same numbers.
The full built-in help (help tsset)
tsset timevar [, delta(#)] | tsset panelvar timevar
L.x L2.x F.x D.x D2.x S12.x in any expression
tsline var [var ...]
tssmooth ma newvar = var, window(#l #c #f)
tsdecomp var, period(#) [multiplicative] [generate(stub)]
. tsset month
. generate growth = D.sales / L.sales
. generate yoy = S12.sales / L12.sales
. tsdecomp sales, period(12)
. tsline sales sales_trend
The operators count in units of the time variable, NOT in rows. L.x at
month 5 is x at month 4, and it is missing if there is no month 4 -- even
when the row above is month 3. A plain row shift gets that wrong quietly,
and most real series have a missing month somewhere. In a panel each unit
is lagged on its own.
L. / F. a lag / a lead; L2. is two periods
D. a difference: x - L.x; D2. is a difference of differences
S12. a seasonal difference: x - L12.x
tssmooth ma window(2 1 2) is a 5-term centred average. The edges, where
the window does not fit, are missing
tsdecomp classical decomposition into trend, seasonal and remainder
(created as var_trend, var_seasonal, var_resid). Needs every
period present and two full cycles. Not a Stata command
Not built: date formats (the time variable is a plain number: 1, 2, 3 or
2019, 2020), arima, Holt-Winters, exponential smoothing, dfuller, forecasting.
The decomposition and moving average are checked against R's decompose() and
stats::filter(); the moving average's edge handling has not been compared
with Stata.