Follow ______ on twitter.

7.2.2 Time Series Decomposition Techniques

Code

Vega-Lite Chart

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "data": {
    "values": [
      {
        "month": "2020-01",
        "sales": 1000
      },
      {
        "month": "2020-02",
        "sales": 1100
      },
      {
        "month": "2020-03",
        "sales": 1300
      },
      {
        "month": "2020-04",
        "sales": 1200
      },
      {
        "month": "2020-05",
        "sales": 1400
      },
      {
        "month": "2020-06",
        "sales": 1350
      },
      {
        "month": "2020-07",
        "sales": 1500
      },
      {
        "month": "2020-08",
        "sales": 1450
      },
      {
        "month": "2020-09",
        "sales": 1600
      },
      {
        "month": "2020-10",
        "sales": 1550
      },
      {
        "month": "2020-11",
        "sales": 1700
      },
      {
        "month": "2020-12",
        "sales": 1800
      },
      {
        "month": "2021-01",
        "sales": 1650
      },
      {
        "month": "2021-02",
        "sales": 1750
      },
      {
        "month": "2021-03",
        "sales": 1950
      },
      {
        "month": "2021-04",
        "sales": 1850
      },
      {
        "month": "2021-05",
        "sales": 2050
      },
      {
        "month": "2021-06",
        "sales": 2000
      }
    ]
  },
  "transform": [
    {
      "window": [
        {
          "op": "mean",
          "field": "sales",
          "as": "trend"
        }
      ],
      "frame": [
        -5,
        5
      ]
    },
    {
      "window": [
        {
          "op": "mean",
          "field": "sales",
          "as": "yearly_avg"
        }
      ],
      "frame": [
        -6,
        5
      ]
    },
    {
      "calculate": "datum.sales - datum.yearly_avg",
      "as": "seasonal"
    },
    {
      "calculate": "datum.sales - datum.trend - datum.seasonal",
      "as": "residual"
    },
    {
      "calculate": "datum.sales / datum.trend",
      "as": "seasonal_mult"
    },
    {
      "calculate": "datum.sales / (datum.trend * datum.seasonal_mult)",
      "as": "residual_mult"
    }
  ],
  "hconcat": [
    {
      "title": "Additive Decomposition",
      "vconcat": [
        {
          "width": 300,
          "height": 100,
          "title": "Original Sales Data",
          "mark": "line",
          "encoding": {
            "x": {
              "field": "month",
              "type": "temporal",
              "axis": {
                "labels": false
              }
            },
            "y": {
              "field": "sales",
              "type": "quantitative"
            }
          }
        },
        {
          "width": 300,
          "height": 100,
          "title": "Trend",
          "mark": "line",
          "encoding": {
            "x": {
              "field": "month",
              "type": "temporal",
              "axis": {
                "labels": false
              }
            },
            "y": {
              "field": "trend",
              "type": "quantitative"
            },
            "color": {
              "value": "blue"
            }
          }
        },
        {
          "width": 300,
          "height": 100,
          "title": "Seasonal",
          "mark": "line",
          "encoding": {
            "x": {
              "field": "month",
              "type": "temporal",
              "axis": {
                "labels": false
              }
            },
            "y": {
              "field": "seasonal",
              "type": "quantitative"
            },
            "color": {
              "value": "green"
            }
          }
        },
        {
          "width": 300,
          "height": 100,
          "title": "Residual",
          "mark": "line",
          "encoding": {
            "x": {
              "field": "month",
              "type": "temporal"
            },
            "y": {
              "field": "residual",
              "type": "quantitative"
            },
            "color": {
              "value": "red"
            }
          }
        }
      ]
    },
    {
      "title": "Multiplicative Decomposition",
      "vconcat": [
        {
          "width": 300,
          "height": 100,
          "title": "Original Sales Data",
          "mark": "line",
          "encoding": {
            "x": {
              "field": "month",
              "type": "temporal",
              "axis": {
                "labels": false
              }
            },
            "y": {
              "field": "sales",
              "type": "quantitative"
            }
          }
        },
        {
          "width": 300,
          "height": 100,
          "title": "Trend",
          "mark": "line",
          "encoding": {
            "x": {
              "field": "month",
              "type": "temporal",
              "axis": {
                "labels": false
              }
            },
            "y": {
              "field": "trend",
              "type": "quantitative"
            },
            "color": {
              "value": "blue"
            }
          }
        },
        {
          "width": 300,
          "height": 100,
          "title": "Seasonal",
          "mark": "line",
          "encoding": {
            "x": {
              "field": "month",
              "type": "temporal",
              "axis": {
                "labels": false
              }
            },
            "y": {
              "field": "seasonal_mult",
              "type": "quantitative"
            },
            "color": {
              "value": "green"
            }
          }
        },
        {
          "width": 300,
          "height": 100,
          "title": "Residual",
          "mark": "line",
          "encoding": {
            "x": {
              "field": "month",
              "type": "temporal"
            },
            "y": {
              "field": "residual_mult",
              "type": "quantitative"
            },
            "color": {
              "value": "red"
            }
          }
        }
      ]
    }
  ]
}