Follow ______ on twitter.

3.4.2 Understanding Confidence Intervals for Risk Analysis

Code

Vega-Lite Chart

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "description": "Regression with error bands.",
  "data": {
    "sequence": {
      "start": 1,
      "stop": 1001,
      "step": 1,
      "as": "x"
    }
  },
  "transform": [
    {
      "calculate": "50 + 0.05 * datum.x + sampleNormal(0, 10)",
      "as": "y"
    }
  ],
  "hconcat": [
    {
      "layer": [
        {
          "mark": "point",
          "encoding": {
            "x": {
              "field": "x",
              "type": "quantitative",
              "title": "Variable X"
            },
            "y": {
              "field": "y",
              "type": "quantitative",
              "title": "Variable Y"
            }
          }
        }
      ],
      "title": "Stochastic Process with Drift"
    },
    {
      "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": "Linear Regression Line"
    },
    {
      "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_hat",
                "y_hat"
              ]
            },
            {
              "calculate": "datum.y_hat - datum.y",
              "as": "errors"
            },
            {
              "calculate": "datum.y_hat + 1.96 * 5",
              "as": "ci_upper"
            },
            {
              "calculate": "datum.y_hat - 1.96 * 5",
              "as": "ci_lower"
            }
          ],
          "layer": [
            {
              "mark": {
                "type": "line",
                "color": "blue"
              },
              "encoding": {
                "x": {
                  "field": "x_hat",
                  "type": "quantitative"
                },
                "y": {
                  "field": "y_hat",
                  "type": "quantitative"
                }
              }
            },
            {
              "mark": {
                "type": "errorband",
                "opacity": 0.6
              },
              "encoding": {
                "x": {
                  "field": "x_hat",
                  "type": "quantitative"
                },
                "y": {
                  "field": "ci_upper",
                  "type": "quantitative"
                },
                "y2": {
                  "field": "ci_lower"
                },
                "color": {
                  "value": "green"
                }
              }
            }
          ]
        }
      ],
      "title": "80%-ile Confident Intervals"
    }
  ]
}