Simple Linear Regression
Calculator
Learn More
Simple Linear Regression
Definition
Simple Linear Regression models the relationship between a predictor variable (X) and a response variable (Y) using a linear equation. It finds the line that minimizes the sum of squared residuals.
Key Formulas
Regression Line:
Slope:
Intercept:
R-squared:
Key Assumptions
Linearity: Relationship between X and Y is linear
Independence: Observations are independent
Homoscedasticity: Constant variance of residuals
Normality: Residuals are normally distributed
Practical Example
Step 1: Data
1 | 2.1 | -2 | -3.82 | 4 | 7.64 |
2 | 3.8 | -1 | -2.12 | 1 | 2.12 |
3 | 6.2 | 0 | 0.28 | 0 | 0 |
4 | 7.8 | 1 | 1.88 | 1 | 1.88 |
5 | 9.3 | 2 | 3.38 | 4 | 6.76 |
Means: ,
Step 2: Calculate Slope ()
Step 3: Calculate Intercept ()
Step 4: Regression Equation
Step 5: Calculate
(98.6% of variation in Y explained by X)
Code Examples
R
1library(tidyverse)
2
3# Example data
4data <- tibble(x = c(1, 2, 3, 4, 5),
5 y = c(2.1, 3.8, 6.2, 7.8, 9.3))
6
7# Fit linear model
8model <- lm(y ~ x, data=data)
9
10# Print summary
11summary(model)
12
13# Get confidence intervals
14confint(model)
15
16# plot with ggplot2
17ggplot(data, aes(x = x, y = y)) +
18 geom_point() +
19 geom_smooth(method = "lm", se = FALSE) +
20 theme_minimal()
Python
1import numpy as np
2import pandas as pd
3import statsmodels.api as sm
4
5# Example data
6X = [1, 2, 3, 4, 5] # predictor variable
7y = [2.1, 3.8, 6.2, 7.8, 9.3] # response variable
8
9# Add constant to X for intercept
10X = sm.add_constant(X)
11
12# Fit model
13model = sm.OLS(y, X).fit()
14
15# Print summary
16print(model.summary())
17
18# Get coefficients
19print(f'Intercept: {model.params[0]:.4f}')
20print(f'Slope: {model.params[1]:.4f}')
21print(f'R-squared: {model.rsquared:.4f}')
Alternative Methods
- Robust Regression: When outliers are present
- Polynomial Regression: For non-linear relationships
- Quantile Regression: For heteroscedastic data
Related Links
Correlation Coefficient Calculator
F Distribution Calculator
Two Sample T-Test Calculator
One-Way ANOVA Calculator
Help us improve
Found an error or have a suggestion? Let us know!