from edumath.statistics import predict, residuals, simple_linear_regression
line = simple_linear_regression([1, 2, 3], [3, 5, 7])
lineRegressionLine(slope=2.0, intercept=1.0, correlation=1.0)
Regression models relationships between variables. In simple linear regression, we use one quantitative explanatory variable x to predict one quantitative response variable y.
After this lesson you should be able to:
Before fitting a line, look at the data. A scatterplot can show:
A line is appropriate only when the relationship is roughly linear.
Pearson correlation r measures the strength and direction of a linear relationship.
Properties:
r is between -1 and 1;r means y tends to increase as x increases;r means y tends to decrease as x increases;r near 0 means little linear association;A simple linear regression line has the form:
predicted y = intercept + slope * x
The slope is the predicted change in y for a one-unit increase in x. The intercept is the predicted y value when x=0, if that value is meaningful in context.
A residual is the prediction error for one point:
residual = observed y - predicted y
Positive residual: observed value is above the line. Negative residual: observed value is below the line.
Residual plots help check whether a linear model is appropriate. A curved pattern in residuals suggests the line is missing structure.
Interpolation means predicting within the range of observed x values. Extrapolation means predicting outside that range. Extrapolation can be risky because the relationship may not continue.
Regression describes association. It does not automatically prove that changing x causes a change in y. Causal conclusions require careful design, such as random assignment or strong observational methods.
A regression line predicts exam score from study hours:
predicted score = 62 + 4.5 * hours
Question: Interpret the slope.
Answer: For each additional hour studied, the model predicts an average increase of 4.5 exam points.
This is a prediction statement. It does not automatically prove that forcing one more hour of study causes exactly 4.5 more points for every student.
Use the same line:
predicted score = 62 + 4.5 * hours
Predict score for 6 hours.
Step 1: Substitute.
predicted score = 62 + 4.5(6)
Step 2: Compute.
62 + 27 = 89
The predicted score is 89.
A student studied 6 hours and scored 84. The model predicted 89.
residual = observed - predicted = 84 - 89 = -5
The residual is -5, meaning the student scored 5 points below the model’s prediction.
Data:
x: 1, 2, 3
y: 3, 5, 7
Each time x increases by 1, y increases by 2, so slope is 2. Using point (1,3):
3 = intercept + 2(1)
intercept = 1
The line is:
predicted y = 1 + 2x
For x=4, predicted y=9.
predicted y = 10 + 3x.y when x=5 for predicted y = 10 + 3x.y=22 and predicted y=25, find the residual.-1 mean?x, predicted y increases by 3 units.10 + 3(5) = 25.22 - 25 = -3.Guess first. If taller plants tend to have larger leaves, is the association positive or negative?
Guided exercise.
As height increases, leaf size tends to increase, so the association is positive.
Checkpoint. If price increases and demand decreases, what is the direction?
Guess first. In y = 5 + 2x, what is the predicted change in y when x increases by 1?
Guided exercise.
The slope is 2, so predicted y increases by 2.
Checkpoint. Predict y for x=4 in y=5+2x.
Guess first. If a point is above the regression line, is the residual positive or negative?
Guided exercise.
Above the line means observed y is greater than predicted y, so the residual is positive.
Checkpoint. If observed is 12 and predicted is 15, find the residual.
Guess first. Can an observational regression prove causation by itself?
Guided exercise.
No. It can show association, but causal claims require design and reasoning about confounders.
Checkpoint. Name one reason association may not be causation.
5+2(4)=13.12-15=-3.Use edumath to fit a simple regression line and compute residuals.
RegressionLine(slope=2.0, intercept=1.0, correlation=1.0)
Use SymPy to solve for an intercept from one point and a known slope.