Loess Regression Example
Locally-estimated regression produces a trend line by performing weighted regressions over a sliding window of points. The loess
method (for locally-estimated scatterplot smoothing) computes a sequence of local linear regressions to estimate smoothed points. The bandwidth parameter determines the size of the sliding window of nearest-neighbor points, expressed as a fraction of the total number of points included. Alternatively, see the regression example for regression results using parametric functions.
Vega JSON Specification <>
{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"description": "A scatter plot with trend line calculated via locally-weighted (loess) regression.",
"padding": 5,
"width": 500,
"height": 500,
"autosize": "pad",
"signals": [
{
"name": "loessBandwidth", "value": 0.3,
"bind": {"input": "range", "min": 0.05, "max": 1}
},
{
"name": "groupby", "value": "none",
"bind": {"input": "select", "options": ["none", "genre"]}
}
],
"data": [
{
"name": "movies",
"url": "data/movies.json",
"transform": [
{
"type": "filter",
"expr": "datum['Rotten Tomatoes Rating'] != null && datum['IMDB Rating'] != null"
}
]
},
{
"name": "trend",
"source": "movies",
"transform": [
{
"type": "loess",
"groupby": [{"signal": "groupby === 'genre' ? 'Major Genre' : 'foo'"}],
"bandwidth": {"signal": "loessBandwidth"},
"x": "Rotten Tomatoes Rating",
"y": "IMDB Rating",
"as": ["u", "v"]
}
]
}
],
"scales": [
{
"name": "x",
"type": "linear",
"domain": {"data": "movies", "field": "Rotten Tomatoes Rating"},
"range": "width"
},
{
"name": "y",
"type": "linear",
"domain": {"data": "movies", "field": "IMDB Rating"},
"range": "height"
}
],
"marks": [
{
"type": "symbol",
"from": {"data": "movies"},
"encode": {
"enter": {
"x": {"scale": "x", "field": "Rotten Tomatoes Rating"},
"y": {"scale": "y", "field": "IMDB Rating"},
"fillOpacity": {"value": 0.5},
"size": {"value": 16}
}
}
},
{
"type": "group",
"from": {
"facet": {
"data": "trend",
"name": "curve",
"groupby": "Major Genre"
}
},
"marks": [
{
"type": "line",
"from": {"data": "curve"},
"encode": {
"enter": {
"x": {"scale": "x", "field": "u"},
"y": {"scale": "y", "field": "v"},
"stroke": {"value": "firebrick"}
}
}
}
]
}
]
}