tsline
Draws one or more series against time.
What we have
- one or more series against the time variable
Coming later
- a second axis, and shaded recession-style bands
You may shorten it to tsli.
How you write it
tsline var [var ...]
Square brackets mean optional. A vertical bar means choose one.
What it does
The chart you want the moment you have a series: the line, the trend you smoothed out of it, and the original, all on one set of axes so they can be compared.
Needs tsset first, because the horizontal axis is the time variable.
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
. tsline revenue
(graph saved to graph_tsline.html)
The same thing in pandas and in R
pandas
# rendered with D3; see graph_tsline.html
R
# no R form for `tsline` yet -- see docs/r-equivalents.md
The full built-in help (help tsline)
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.