Virata manual

tssmooth

Smooths a bumpy series by averaging each point with its neighbours.

What we have

  • ma, a moving average, with window(#l #c #f)

Coming later

  • exponential, hwinters and shwinters, which are refused
  • the edge behaviour has not been compared with Stata

You may shorten it to tssmoo.

How you write it

tssmooth ma newvar = var, window(#l #c #f)

Square brackets mean optional. A vertical bar means choose one.

What it does

tssmooth ma trend = sales, window(2 1 2) replaces each month with the average of itself, the two before and the two after — a five-term centred moving average. The month-to-month noise flattens and the underlying movement becomes visible.

The three numbers in window() are how many periods before, the point itself, and how many after.

Watch out. The first and last few points have no full window around them, so they come out missing. That is honest rather than helpful: a smoothed value at the end of a series is a guess.

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
. tssmooth ma smooth = revenue, window(2 1 2)
(4 missing values generated)
The same thing in pandas and in R

pandas

df["smooth"] = df["revenue"].rolling(5, center=True).mean()

R

# no R form for `tssmooth` yet -- see docs/r-equivalents.md
. summarize revenue smooth
    Variable |         Obs        Mean   Std. dev.         Min         Max
-------------+------------------------------------------------------------
     revenue |          96    1160.346    350.5008       555.6      2012.2
      smooth |          92    1167.145    289.2817      708.44     1845.84
The same thing in pandas and in R

pandas

df[['revenue', 'smooth']].describe()

R

library(dplyr)
df %>% summarise(revenue_n = sum(!is.na(revenue)),
    revenue_mean = mean(revenue, na.rm = TRUE),
    revenue_sd = sd(revenue, na.rm = TRUE), revenue_min = min(revenue,
    na.rm = TRUE), revenue_max = max(revenue, na.rm = TRUE),
    smooth_n = sum(!is.na(smooth)), smooth_mean = mean(smooth,
    na.rm = TRUE), smooth_sd = sd(smooth, na.rm = TRUE),
    smooth_min = min(smooth, na.rm = TRUE), smooth_max = max(smooth,
    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 tssmooth)
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.

Nearby

tsset tsline tsdecomp