Skip to main content

Chapter 7 Relationships Between Variables

So far we have only looked at one variable at a time. In this chapter we start looking at relationships between variables. Two variables are related if knowing one gives you information about the other. For example, height and weight are related -- people who are taller tend to be heavier. Of course, it is not a perfect relationship: there are short heavy people and tall light ones. But if you are trying to guess someone’s weight, you will be more accurate if you know their height than if you don’t.
This chapter presents several ways to visualize relationships between variables, and one way to quantify the strength of a relationship, correlation.
Listing 7.0.1. Python Code
from os.path import basename, exists


def download(url):
    filename = basename(url)
    if not exists(filename):
        from urllib.request import urlretrieve

        local, _ = urlretrieve(url, filename)
        print("Downloaded " + local)


download("https://github.com/AllenDowney/ThinkStats/raw/v3/nb/thinkstats.py")
Listing 7.0.2. Python Code
try:
    import empiricaldist
except ImportError:
    %pip install empiricaldist
Listing 7.0.3. Python Code
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

from thinkstats import decorate