Measure a relationship and fit a line to it

short · 35 min · Objective 3.2

Task

Calculate the correlation between the survey's satisfaction and likelihood-to-recommend scores, fit a least-squares line, and use it to predict -- once inside the data's range and once outside it.

Steps

  1. Read satisfaction (x) and recommend (y) from data/survey.json.
  2. With statistics.correlation and statistics.linear_regression, record in lab/regress/fit.txt the lines r: N, r squared: N, slope: N and intercept: N, each to three decimal places.
  3. Predict recommend for a satisfaction of 3 and of 0, and record predict 3: N and predict 0: N to two decimal places.

Verify

These checks run in a POSIX shell with Python 3: Terminal on macOS or Linux, and on Windows Git Bash (it comes with Git for Windows) or WSL. On Windows, python3 is often a Microsoft Store placeholder that prints “Python was not found”; run alias python3=py in Git Bash first.

grep -E '^(r|r squared|slope|intercept|predict 3|predict 0): ' lab/regress/fit.txt
awk -F': ' '/^predict 0/ {print ($2 < 0) ? "predict 0 is impossible: the scale starts at 0" : "predict 0 is possible"}' lab/regress/fit.txt

r is 0.937 and r squared 0.879: a very strong positive relationship. The line has a slope of 1.912 and an intercept of -0.106, so a satisfaction of 3 predicts 5.63, and a satisfaction of 0 -- off the survey's 1-to-5 scale -- predicts -0.11, a score nobody can give. The same equation is useful inside the data and nonsense outside it.

This is an independent study companion for CompTIA Data+ DA0-002 and is not produced by or endorsed by CompTIA.