Follow ______ on twitter.

4.4.2 Constructing Confidence Intervals

Code

Vega-Lite Chart

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "hconcat": [
    {
      "data": {
        "sequence": {
          "start": 1,
          "stop": 1001,
          "step": 1,
          "as": "index"
        }
      },
      "transform": [
        {
          "calculate": "clamp(620 + 50 * sampleNormal(0, 1), 200, 900)",
          "as": "credit_score"
        }
      ],
      "layer": [
        {
          "mark": "point",
          "encoding": {
            "x": {
              "field": "index",
              "type": "quantitative",
              "title": "Index"
            },
            "y": {
              "field": "credit_score",
              "type": "quantitative",
              "title": "Credit Score",
              "scale": {
                "domain": [
                  400,
                  900
                ]
              }
            },
            "color": {
              "value": "grey"
            },
            "size": {
              "value": 10
            }
          }
        },
        {
          "transform": [
            {
              "aggregate": [
                {
                  "op": "mean",
                  "field": "credit_score",
                  "as": "mean_credit_score"
                },
                {
                  "op": "stdev",
                  "field": "credit_score",
                  "as": "stdev_credit_score"
                },
                {
                  "op": "count",
                  "field": "credit_score",
                  "as": "sample_size"
                }
              ]
            },
            {
              "calculate": "datum.mean_credit_score + 1.96 * datum.stdev_credit_score / sqrt(datum.sample_size)",
              "as": "mean_plus_ci"
            },
            {
              "calculate": "datum.mean_credit_score - 1.96 * datum.stdev_credit_score / sqrt(datum.sample_size)",
              "as": "mean_minus_ci"
            }
          ],
          "layer": [
            {
              "mark": {
                "type": "rule",
                "color": "blue",
                "strokeWidth": 2
              },
              "encoding": {
                "y": {
                  "field": "mean_credit_score",
                  "type": "quantitative"
                },
                "size": {
                  "value": 2
                }
              }
            },
            {
              "mark": {
                "type": "rule",
                "color": "green",
                "strokeDash": [
                  4,
                  2
                ]
              },
              "encoding": {
                "y": {
                  "field": "mean_plus_ci",
                  "type": "quantitative"
                }
              }
            },
            {
              "mark": {
                "type": "rule",
                "color": "green",
                "strokeDash": [
                  4,
                  2
                ]
              },
              "encoding": {
                "y": {
                  "field": "mean_minus_ci",
                  "type": "quantitative"
                }
              }
            }
          ]
        }
      ],
      "title": "Population Data with Confidence Intervals"
    },
    {
      "data": {
        "sequence": {
          "start": 1,
          "stop": 1001,
          "step": 1,
          "as": "index"
        }
      },
      "transform": [
        {
          "calculate": "clamp(620 + 50 * sampleNormal(0, 1), 200, 900)",
          "as": "credit_score"
        },
        {
          "aggregate": [
            {
              "op": "mean",
              "field": "credit_score",
              "as": "mean_credit_score"
            },
            {
              "op": "stdev",
              "field": "credit_score",
              "as": "stdev_credit_score"
            },
            {
              "op": "count",
              "field": "credit_score",
              "as": "sample_size"
            }
          ]
        },
        {
          "calculate": "datum.mean_credit_score + 1.96 * datum.stdev_credit_score / sqrt(datum.sample_size)",
          "as": "mean_plus_ci"
        },
        {
          "calculate": "datum.mean_credit_score - 1.96 * datum.stdev_credit_score / sqrt(datum.sample_size)",
          "as": "mean_minus_ci"
        },
        {
          "calculate": "datum.mean_credit_score + 1.1 * 1.96 * datum.stdev_credit_score / sqrt(datum.sample_size)",
          "as": "mean_plus_ci_zoom"
        },
        {
          "calculate": "datum.mean_credit_score - 1.1 * 1.96 * datum.stdev_credit_score / sqrt(datum.sample_size)",
          "as": "mean_minus_ci_zoom"
        }
      ],
      "layer": [
        {
          "mark": "point",
          "encoding": {
            "x": {
              "field": "index",
              "type": "quantitative",
              "title": "Index"
            },
            "y": {
              "field": "credit_score",
              "type": "quantitative",
              "title": "Credit Score",
              "scale": {
                "domain": [
                  610,
                  630
                ]
              }
            },
            "color": {
              "value": "grey"
            },
            "size": {
              "value": 10
            }
          }
        },
        {
          "mark": {
            "type": "rule",
            "color": "blue",
            "strokeWidth": 2
          },
          "encoding": {
            "y": {
              "field": "mean_credit_score",
              "type": "quantitative"
            },
            "size": {
              "value": 2
            }
          }
        },
        {
          "mark": {
            "type": "rule",
            "color": "green",
            "strokeDash": [
              4,
              2
            ]
          },
          "encoding": {
            "y": {
              "field": "mean_plus_ci",
              "type": "quantitative"
            }
          }
        },
        {
          "mark": {
            "type": "rule",
            "color": "green",
            "strokeDash": [
              4,
              2
            ]
          },
          "encoding": {
            "y": {
              "field": "mean_minus_ci",
              "type": "quantitative"
            }
          }
        }
      ],
      "resolve": {
        "scale": {
          "y": "shared"
        }
      },
      "title": "Zoomed-In View - Mean and Confidence Intervals"
    }
  ]
}