Follow ______ on twitter.

6.3.1 Linear Regression

Code

Vega-Lite Chart

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "hconcat": [
    {
      "title": "Simple Linear Regression",
      "width": 200,
      "height": 200,
      "data": {
        "values": [
          {
            "x": 1,
            "y": 1.1
          },
          {
            "x": 2,
            "y": 2.3
          },
          {
            "x": 3,
            "y": 4.7
          },
          {
            "x": 4,
            "y": 9
          },
          {
            "x": 5,
            "y": 16.1
          },
          {
            "x": 6,
            "y": 27.9
          },
          {
            "x": 7,
            "y": 44.2
          },
          {
            "x": 8,
            "y": 69.1
          },
          {
            "x": 9,
            "y": 106.4
          },
          {
            "x": 10,
            "y": 159.3
          }
        ]
      },
      "layer": [
        {
          "mark": "point",
          "encoding": {
            "x": {
              "field": "x",
              "type": "quantitative",
              "title": "Variable X"
            },
            "y": {
              "field": "y",
              "type": "quantitative",
              "title": "Variable Y"
            }
          }
        },
        {
          "transform": [
            {
              "regression": "y",
              "on": "x",
              "method": "linear",
              "as": [
                "x",
                "y"
              ]
            }
          ],
          "mark": {
            "type": "line",
            "color": "blue"
          },
          "encoding": {
            "x": {
              "field": "x",
              "type": "quantitative"
            },
            "y": {
              "field": "y",
              "type": "quantitative"
            }
          }
        }
      ]
    },
    {
      "title": "Multiple Linear Regression",
      "width": 200,
      "height": 200,
      "data": {
        "values": [
          {
            "x1": 1,
            "x2": 2,
            "y": 3.1
          },
          {
            "x1": 2,
            "x2": 3,
            "y": 4.3
          },
          {
            "x1": 3,
            "x2": 4,
            "y": 6.8
          },
          {
            "x1": 4,
            "x2": 5,
            "y": 11.5
          },
          {
            "x1": 5,
            "x2": 6,
            "y": 18.4
          },
          {
            "x1": 6,
            "x2": 7,
            "y": 28.3
          },
          {
            "x1": 7,
            "x2": 8,
            "y": 42.5
          },
          {
            "x1": 8,
            "x2": 9,
            "y": 61.2
          },
          {
            "x1": 9,
            "x2": 10,
            "y": 85
          },
          {
            "x1": 10,
            "x2": 11,
            "y": 115.3
          }
        ]
      },
      "layer": [
        {
          "mark": "point",
          "encoding": {
            "x": {
              "field": "x1",
              "type": "quantitative",
              "title": "Variable X1"
            },
            "y": {
              "field": "y",
              "type": "quantitative",
              "title": "Variable Y"
            },
            "color": {
              "value": "red"
            }
          }
        },
        {
          "mark": "point",
          "encoding": {
            "x": {
              "field": "x2",
              "type": "quantitative",
              "title": "Variable X2"
            },
            "y": {
              "field": "y",
              "type": "quantitative"
            },
            "color": {
              "value": "green"
            }
          }
        },
        {
          "transform": [
            {
              "regression": "y",
              "on": "x1",
              "method": "linear",
              "as": [
                "x1",
                "y"
              ]
            }
          ],
          "mark": {
            "type": "line",
            "color": "red"
          },
          "encoding": {
            "x": {
              "field": "x1",
              "type": "quantitative"
            },
            "y": {
              "field": "y",
              "type": "quantitative"
            }
          }
        },
        {
          "transform": [
            {
              "regression": "y",
              "on": "x2",
              "method": "linear",
              "as": [
                "x2",
                "y"
              ]
            }
          ],
          "mark": {
            "type": "line",
            "color": "green"
          },
          "encoding": {
            "x": {
              "field": "x2",
              "type": "quantitative"
            },
            "y": {
              "field": "y",
              "type": "quantitative"
            }
          }
        }
      ]
    }
  ]
}