Simple linear regression is a statistical method you can use to quantify the relationship between a predictor variable and a response variable.
This tutorial explains how to perform simple linear regression by hand.
Example: Simple Linear Regression by Hand
Suppose we have the following dataset that shows the weight and height of seven individuals:

Use the following steps to fit a linear regression model to this dataset, using weight as the predictor variable and height as the response variable.
Step 1: Calculate X*Y, X2, and Y2

Step 2: Calculate ΣX, ΣY, ΣX*Y, ΣX2, and ΣY2

Step 3: Calculate b0
The formula to calculate b0 is: [(ΣY)(ΣX2) – (ΣX)(ΣXY)] / [n(ΣX2) – (ΣX)2]
In this example, b0 = [(477)(222755) – (1237)(85125)] / [7(222755) – (1237)2] = 32.783
Step 4: Calculate b1
The formula to calculate b1 is: [n(ΣXY) – (ΣX)(ΣY)] / [n(ΣX2) – (ΣX)2]
In this example, b1 = [7(85125) – (1237)(477)] / [7(222755) – (1237)2] = 0.2001
Step 5: Place b0 and b1 in the estimated linear regression equation.
The estimated linear regression equation is: ŷ = b0 + b1*x
In our example, it is ŷ = 0.32783 + (0.2001)*x
How to Interpret a Simple Linear Regression Equation
Here is how to interpret this estimated linear regression equation: ŷ = 32.783 + 0.2001x
b0 = 32.7830. When weight is zero pounds, the predicted height is 32.783 inches. Sometimes the value for b0 can be useful to know, but in this example it doesn’t actually make sense to interpret b0 since a person can’t weigh zero pounds.
b1 = 0.2001. A one pound increase in weight is associated with a 0.2001 inch increase in height.
Simple Linear Regression Calculator
We can double check our results by inputting our data into the simple linear regression calculator:

This equation matches the one that we calculated by hand.
thanks for your example…
I am trying to code something in C and needed it… to understand what I was doing..
What does b0 and b1 signify in this equation
How do I calculate and interpret linear regression
Hi Idjawe…### Calculating Linear Regression
Linear regression is a statistical method to model the relationship between a dependent variable \( y \) and one or more independent variables \( x \). The simplest form is a **simple linear regression**, which involves one independent variable.
The linear regression equation is:
\[ y = \beta_0 + \beta_1 x + \epsilon \]
– \( y \): Dependent variable
– \( x \): Independent variable
– \( \beta_0 \): Intercept (the value of \( y \) when \( x \) is 0)
– \( \beta_1 \): Slope (the change in \( y \) for a one-unit change in \( x \))
– \( \epsilon \): Error term (the difference between the observed and predicted values)
### Steps to Calculate Linear Regression
1. **Collect Data**: Gather the data for the dependent and independent variables.
2. **Plot Data**: Visualize the data to understand the relationship between variables.
3. **Calculate Coefficients**: Use the formulas for the intercept (\( \beta_0 \)) and slope (\( \beta_1 \)):
\[
\beta_1 = \frac{n(\sum xy) – (\sum x)(\sum y)}{n(\sum x^2) – (\sum x)^2}
\]
\[
\beta_0 = \frac{\sum y – \beta_1 \sum x}{n}
\]
Where \( n \) is the number of data points.
4. **Make Predictions**: Use the regression equation to predict values of \( y \) for given \( x \) values.
### Example Calculation
Suppose you have the following data:
| x | y |
|—|—-|
| 1 | 2 |
| 2 | 3 |
| 3 | 5 |
| 4 | 4 |
| 5 | 6 |
Calculate sums needed for coefficients:
– \( \sum x = 1 + 2 + 3 + 4 + 5 = 15 \)
– \( \sum y = 2 + 3 + 5 + 4 + 6 = 20 \)
– \( \sum xy = (1 \cdot 2) + (2 \cdot 3) + (3 \cdot 5) + (4 \cdot 4) + (5 \cdot 6) = 60 \)
– \( \sum x^2 = 1^2 + 2^2 + 3^2 + 4^2 + 5^2 = 55 \)
Now calculate \( \beta_1 \) and \( \beta_0 \):
\[
\beta_1 = \frac{5(60) – 15(20)}{5(55) – 15^2} = \frac{300 – 300}{275 – 225} = \frac{0}{50} = 0
\]
\[
\beta_0 = \frac{20 – 0(15)}{5} = \frac{20}{5} = 4
\]
So, the regression equation is:
\[ y = 0 \cdot x + 4 \]
In this simplified example, the slope is 0, indicating no relationship between \( x \) and \( y \).
### Interpreting Linear Regression
1. **Coefficients**:
– **Intercept (\( \beta_0 \))**: The expected value of \( y \) when \( x \) is 0.
– **Slope (\( \beta_1 \))**: The expected change in \( y \) for a one-unit change in \( x \).
2. **R-squared (\( R^2 \))**: Indicates how well the data fit the regression model. It ranges from 0 to 1.
– \( R^2 = 1 – \frac{\text{Sum of Squares of Residuals (SSR)}}{\text{Total Sum of Squares (TSS)}} \)
– A higher \( R^2 \) indicates a better fit.
3. **P-value**: Tests the hypothesis that the coefficient is different from zero.
– A low p-value (typically < 0.05) indicates that the coefficient is statistically significant. 4. **Residuals**: The difference between observed and predicted values. - Plotting residuals helps check the assumptions of linear regression (e.g., homoscedasticity, normality). ### Example in Python You can use Python's `scikit-learn` library to perform linear regression: ```python import numpy as np import matplotlib.pyplot as plt from sklearn.linear_model import LinearRegression # Sample data x = np.array([1, 2, 3, 4, 5]).reshape(-1, 1) y = np.array([2, 3, 5, 4, 6]) # Create linear regression model model = LinearRegression() model.fit(x, y) # Coefficients beta_0 = model.intercept_ beta_1 = model.coef_[0] print(f"Intercept: {beta_0}") print(f"Slope: {beta_1}") # Make predictions y_pred = model.predict(x) # Plot plt.scatter(x, y, color='blue') plt.plot(x, y_pred, color='red') plt.xlabel('x') plt.ylabel('y') plt.title('Linear Regression') plt.show() ``` This code will fit a linear regression model to the data, calculate the coefficients, make predictions, and plot the results. Understanding and interpreting linear regression involves assessing these components to determine the relationship between variables and the strength of the model.