Follow ______ on twitter.

6.3.2 Logistic Regression

Code

Vega-Lite Chart

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "hconcat": [
    {
      "title": [
        "Log-Odds of Event",
        "(Linear Relationship)"
      ],
      "width": 200,
      "height": 200,
      "data": {
        "values": [
          {
            "GDP": 9,
            "UnemploymentRate": 1,
            "Event": 0.05
          },
          {
            "GDP": 8,
            "UnemploymentRate": 2,
            "Event": 0.1
          },
          {
            "GDP": 7,
            "UnemploymentRate": 3,
            "Event": 0.2
          },
          {
            "GDP": 6,
            "UnemploymentRate": 4,
            "Event": 0.35
          },
          {
            "GDP": 5,
            "UnemploymentRate": 5,
            "Event": 0.5
          },
          {
            "GDP": 4,
            "UnemploymentRate": 6,
            "Event": 0.7
          },
          {
            "GDP": 3,
            "UnemploymentRate": 7,
            "Event": 0.85
          },
          {
            "GDP": 2,
            "UnemploymentRate": 8,
            "Event": 0.95
          }
        ]
      },
      "transform": [
        {
          "calculate": "clamp(datum. Event, 0.001, 0.999)",
          "as": "adjusted_Event"
        },
        {
          "calculate": "log(datum.adjusted_Event / (1 - datum.adjusted_Event))",
          "as": "logit_Event"
        }
      ],
      "layer": [
        {
          "mark": "point",
          "encoding": {
            "x": {
              "field": "GDP",
              "type": "quantitative",
              "title": "GDP (%)"
            },
            "y": {
              "field": "logit_Event",
              "type": "quantitative",
              "title": "Log-Odds of Event"
            }
          }
        },
        {
          "mark": "point",
          "encoding": {
            "x": {
              "field": "UnemploymentRate",
              "type": "quantitative",
              "title": "Unemployment Rate (%)"
            },
            "y": {
              "field": "logit_Event",
              "type": "quantitative"
            }
          }
        },
        {
          "transform": [
            {
              "regression": "logit_Event",
              "on": "GDP",
              "method": "linear",
              "as": [
                "GDP",
                "predicted_logit_Event"
              ]
            }
          ],
          "mark": {
            "type": "line",
            "color": "blue"
          },
          "encoding": {
            "x": {
              "field": "GDP",
              "type": "quantitative"
            },
            "y": {
              "field": "predicted_logit_Event",
              "type": "quantitative"
            }
          }
        },
        {
          "transform": [
            {
              "regression": "logit_Event",
              "on": "UnemploymentRate",
              "method": "linear",
              "as": [
                "UnemploymentRate",
                "predicted_logit_Event"
              ]
            }
          ],
          "mark": {
            "type": "line",
            "color": "red"
          },
          "encoding": {
            "x": {
              "field": "UnemploymentRate",
              "type": "quantitative"
            },
            "y": {
              "field": "predicted_logit_Event",
              "type": "quantitative"
            }
          }
        }
      ]
    },
    {
      "title": [
        "Probability of Event",
        "(Non-Linear Polynomial Fit)"
      ],
      "width": 200,
      "height": 200,
      "data": {
        "values": [
          {
            "GDP": 9,
            "UnemploymentRate": 1,
            "Event": 0.05
          },
          {
            "GDP": 8,
            "UnemploymentRate": 2,
            "Event": 0.1
          },
          {
            "GDP": 7,
            "UnemploymentRate": 3,
            "Event": 0.2
          },
          {
            "GDP": 6,
            "UnemploymentRate": 4,
            "Event": 0.35
          },
          {
            "GDP": 5,
            "UnemploymentRate": 5,
            "Event": 0.5
          },
          {
            "GDP": 4,
            "UnemploymentRate": 6,
            "Event": 0.7
          },
          {
            "GDP": 3,
            "UnemploymentRate": 7,
            "Event": 0.85
          },
          {
            "GDP": 2,
            "UnemploymentRate": 8,
            "Event": 0.95
          }
        ]
      },
      "transform": [
        {
          "calculate": "clamp(datum. Event, 0.001, 0.999)",
          "as": "adjusted_Event"
        },
        {
          "calculate": "log(datum.adjusted_Event / (1 - datum.adjusted_Event))",
          "as": "logit_Event"
        },
        {
          "calculate": "1 / (1 + exp(-datum.logit_Event))",
          "as": "predicted_Event"
        }
      ],
      "layer": [
        {
          "mark": "point",
          "encoding": {
            "x": {
              "field": "GDP",
              "type": "quantitative",
              "title": "GDP (%)"
            },
            "y": {
              "field": "predicted_Event",
              "type": "quantitative",
              "title": "Probability of Event"
            }
          }
        },
        {
          "mark": "point",
          "encoding": {
            "x": {
              "field": "UnemploymentRate",
              "type": "quantitative",
              "title": "Unemployment Rate (%)"
            },
            "y": {
              "field": "predicted_Event",
              "type": "quantitative"
            }
          }
        },
        {
          "transform": [
            {
              "regression": "predicted_Event",
              "on": "GDP",
              "method": "poly",
              "order": 4,
              "as": [
                "GDP",
                "predicted_Probability"
              ]
            }
          ],
          "mark": {
            "type": "line",
            "color": "green"
          },
          "encoding": {
            "x": {
              "field": "GDP",
              "type": "quantitative"
            },
            "y": {
              "field": "predicted_Probability",
              "type": "quantitative"
            }
          }
        },
        {
          "transform": [
            {
              "regression": "predicted_Event",
              "on": "UnemploymentRate",
              "method": "poly",
              "order": 4,
              "as": [
                "UnemploymentRate",
                "predicted_Probability"
              ]
            }
          ],
          "mark": {
            "type": "line",
            "color": "purple"
          },
          "encoding": {
            "x": {
              "field": "UnemploymentRate",
              "type": "quantitative"
            },
            "y": {
              "field": "predicted_Probability",
              "type": "quantitative"
            }
          }
        }
      ]
    }
  ]
}