Follow ______ on twitter.

6.5.1 Advanced understanding of correlation

Code

Vega-Lite Chart

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "description": "Scatterplots showing correlation with different residuals.",
  "hconcat": [
    {
      "width": 200,
      "height": 200,
      "data": {
        "name": "table1",
        "values": [
          {
            "x": 1,
            "y": 1
          },
          {
            "x": 2,
            "y": 2
          },
          {
            "x": 3,
            "y": 3
          },
          {
            "x": 4,
            "y": 4
          },
          {
            "x": 5,
            "y": 5
          },
          {
            "x": 6,
            "y": 6
          }
        ]
      },
      "transform": [
        {
          "calculate": "correl([1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6])",
          "as": "corr_value"
        },
        {
          "calculate": "datum.x !== 0 ? (datum.y - datum.x) / datum.x : 0",
          "as": "normalized_residual"
        }
      ],
      "layer": [
        {
          "mark": "point",
          "encoding": {
            "x": {
              "field": "x",
              "type": "quantitative",
              "title": "X (Variable)"
            },
            "y": {
              "field": "normalized_residual",
              "type": "quantitative",
              "title": "Normalized Residuals"
            }
          }
        },
        {
          "mark": {
            "type": "text",
            "align": "right",
            "baseline": "top",
            "fontSize": 12
          },
          "encoding": {
            "text": {
              "field": "corr_value",
              "type": "quantitative",
              "format": ".2f"
            },
            "x": {
              "value": 200
            },
            "y": {
              "value": 10
            },
            "color": {
              "value": "black"
            }
          }
        }
      ],
      "title": "Constant Norm Residuals"
    },
    {
      "width": 200,
      "height": 200,
      "data": {
        "name": "table2",
        "values": [
          {
            "x": 1,
            "y": 2
          },
          {
            "x": 2,
            "y": 6
          },
          {
            "x": 3,
            "y": 12
          },
          {
            "x": 4,
            "y": 20
          },
          {
            "x": 5,
            "y": 30
          },
          {
            "x": 6,
            "y": 42
          }
        ]
      },
      "transform": [
        {
          "calculate": "correl([1, 2, 3, 4, 5, 6], [2, 6, 12, 20, 30, 42])",
          "as": "corr_value"
        },
        {
          "calculate": "datum.x !== 0 ? (datum.y - datum.x) / datum.x : 0",
          "as": "normalized_residual"
        }
      ],
      "layer": [
        {
          "mark": "point",
          "encoding": {
            "x": {
              "field": "x",
              "type": "quantitative",
              "title": "X (Variable)"
            },
            "y": {
              "field": "normalized_residual",
              "type": "quantitative",
              "title": "Normalized Residuals"
            }
          }
        },
        {
          "mark": {
            "type": "text",
            "align": "right",
            "baseline": "top",
            "fontSize": 12
          },
          "encoding": {
            "text": {
              "field": "corr_value",
              "type": "quantitative",
              "format": ".2f"
            },
            "x": {
              "value": 200
            },
            "y": {
              "value": 10
            },
            "color": {
              "value": "black"
            }
          }
        }
      ],
      "title": "Increasing Norm Residuals"
    },
    {
      "width": 200,
      "height": 200,
      "data": {
        "name": "table3",
        "values": [
          {
            "x": 1,
            "y": 2.5
          },
          {
            "x": 2,
            "y": 1
          },
          {
            "x": 3,
            "y": 4
          },
          {
            "x": 4,
            "y": 5.5
          },
          {
            "x": 5,
            "y": 3
          },
          {
            "x": 6,
            "y": 7
          }
        ]
      },
      "transform": [
        {
          "calculate": "correl([1, 2, 3, 4, 5, 6], [2.5, 1, 4, 5.5, 3, 7])",
          "as": "corr_value"
        },
        {
          "calculate": "datum.x !== 0 ? (datum.y - datum.x) / datum.x : 0",
          "as": "normalized_residual"
        }
      ],
      "layer": [
        {
          "mark": "point",
          "encoding": {
            "x": {
              "field": "x",
              "type": "quantitative",
              "title": "X (Variable)"
            },
            "y": {
              "field": "normalized_residual",
              "type": "quantitative",
              "title": "Normalized Residuals"
            }
          }
        },
        {
          "mark": {
            "type": "text",
            "align": "right",
            "baseline": "top",
            "fontSize": 12
          },
          "encoding": {
            "text": {
              "field": "corr_value",
              "type": "quantitative",
              "format": ".2f"
            },
            "x": {
              "value": 200
            },
            "y": {
              "value": 10
            },
            "color": {
              "value": "black"
            }
          }
        }
      ],
      "title": "Random Norm Residuals"
    }
  ]
}