This guide explains how to calculate R-squared (R²), the coefficient of determination, for a simple linear regression. You input your observed data points (x, y), and the calculation tells you the proportion of variance in the dependent variable (y) that is explained by the independent variable (x) in your model. The result is a value between 0 and 1, often expressed as a percentage; higher values indicate a better fit.
Formula
R-squared is calculated from the sum of squares of residuals and the total sum of squares:
R² = 1 - (SS_res / SS_tot)
Where:
- SS_res (residual sum of squares) = Σ(yᵢ - ŷᵢ)², the sum of squared differences between each observed yᵢ and its predicted value ŷᵢ from the regression line.
- SS_tot (total sum of squares) = Σ(yᵢ - ȳ)², the sum of squared differences between each observed yᵢ and the mean of all y values (ȳ).
For simple linear regression, the predicted value ŷᵢ is computed as ŷᵢ = a + b·xᵢ, where:
- b (slope) = Σ((xᵢ - x̄)(yᵢ - ȳ)) / Σ((xᵢ - x̄)²)
- a (intercept) = ȳ - b·x̄
All variables are unitless in the final R² value; x and y can be in any units (e.g., meters, dollars, temperature), but the calculation treats them as numbers.
Worked Example
Using the default data: x = 1, 2, 3, 4, 5 and y = 2, 4, 5, 4, 5. There are n = 5 points.
Step 1: Calculate the means.
x̄ = (1+2+3+4+5)/5 = 15/5 = 3
ȳ = (2+4+5+4+5)/5 = 20/5 = 4
Step 2: Compute the slope (b).
First, the numerator: Σ((xᵢ - x̄)(yᵢ - ȳ))
(1-3)(2-4) = (-2)(-2) = 4
(2-3)(4-4) = (-1)(0) = 0
(3-3)(5-4) = (0)(1) = 0
(4-3)(4-4) = (1)(0) = 0
(5-3)(5-4) = (2)(1) = 2
Sum = 4 + 0 + 0 + 0 + 2 = 6
Denominator: Σ((xᵢ - x̄)²)
(1-3)² = 4
(2-3)² = 1
(3-3)² = 0
(4-3)² = 1
(5-3)² = 4
Sum = 4+1+0+1+4 = 10
b = 6 / 10 = 0.6
Step 3: Compute the intercept (a).
a = ȳ - b·x̄ = 4 - 0.6·3 = 4 - 1.8 = 2.2
Step 4: Compute predicted values (ŷᵢ = a + b·xᵢ).
For x=1: ŷ = 2.2 + 0.6·1 = 2.8
For x=2: ŷ = 2.2 + 0.6·2 = 3.4
For x=3: ŷ = 2.2 + 0.6·3 = 4.0
For x=4: ŷ = 2.2 + 0.6·4 = 4.6
For x=5: ŷ = 2.2 + 0.6·5 = 5.2
Step 5: Compute SS_res.
(2-2.8)² = 0.64
(4-3.4)² = 0.36
(5-4.0)² = 1.00
(4-4.6)² = 0.36
(5-5.2)² = 0.04
SS_res = 0.64+0.36+1.00+0.36+0.04 = 2.40
Step 6: Compute SS_tot.
(2-4)² = 4
(4-4)² = 0
(5-4)² = 1
(4-4)² = 0
(5-4)² = 1
SS_tot = 4+0+1+0+1 = 6
Step 7: Calculate R².
R² = 1 - (2.40 / 6) = 1 - 0.4 = 0.6
The calculator gives R² = 0.6, meaning 60% of the variance in y is explained by x in this linear model.
When to Use R-Squared
Use R² when you want to assess how well a regression line fits your data. It is most meaningful for simple linear regression (one predictor). For multiple regression, use adjusted R² instead, because adding more predictors always inflates R² even if they are not useful. R² is commonly used in fields like economics, biology, and engineering to report model fit.
How to Read the Result
R² ranges from 0 to 1. A value of 0 means the model explains none of the variance; 1 means it explains all of it. In practice, values above 0.7 are often considered good for observational data, but the context matters—some fields accept lower values. A high R² does not guarantee causation, nor does a low R² mean the relationship is nonexistent; it only measures linear fit.
Common Mistakes
- Using R² for non-linear relationships: R² only measures linear fit. If the true relationship is curved, R² may be low even if a strong pattern exists.
- Confusing R² with correlation (r): R² is the square of the Pearson correlation coefficient for simple regression. It is always non-negative, while r can be negative.
- Forgetting to sort data: When entering x and y as comma-separated lists, the calculator pairs them by position. If your lists are not aligned, the result is meaningless.
- Rounding too early: Rounding intermediate values (like b or a) to few decimals can change R² noticeably. Keep full precision until the final step.
- Using R² to compare models with different numbers of predictors: For multiple regression, use adjusted R².
Tips
- Always plot your data first. A scatterplot can reveal outliers or non-linear patterns that R² alone will not show.
- Check the residual plot (predicted vs. residual) to see if a linear model is appropriate. Random scatter suggests a good fit.
- When reporting R², also state the sample size and the range of x values, because R² is sensitive to these.
- Use the same units for x and y? No, R² is unitless, but be consistent in how you enter data.
- If you have only two points, R² will always be 1 (or undefined if x values are identical). This does not mean the model is reliable.
Troubleshooting
Result shows "Cannot compute": This usually happens when the lists of x and y have different lengths, or when the denominator (SS_tot) is zero (all y values are identical). Check that you entered the same number of values for both lists.
R² is negative: In a well-fitted linear regression, R² should be between 0 and 1. A negative value can occur if you are using a different formula (e.g., for nonlinear models) or if the model is forced to have no intercept. For simple linear regression with an intercept, R² is always non-negative. If you see a negative value, check your data entry.
R² is exactly 1: This happens when all points lie exactly on a straight line. It could be a perfect fit, but also check for duplicate or fabricated data. In real-world data, perfect fits are rare.
R² is very low (e.g., 0.1): This is not an error; it simply means the linear relationship is weak. Consider transforming the data (e.g., log) or using a non-linear model.
Frequently Asked Questions
What is the difference between R² and adjusted R²?
Adjusted R² penalizes adding extra predictors that do not improve the model. It is always lower than or equal to R² for multiple regression.
Can R² be greater than 1?
No, for a linear regression with an intercept, R² is always between 0 and 1. Values outside this range indicate an error in calculation or a non-standard model.
What does an R² of 0.8 mean?
It means 80% of the variance in the dependent variable is explained by the independent variable(s) in the model. The remaining 20% is due to other factors or random variation.
Is a high R² always good?
Not necessarily. A high R² can result from overfitting (too many predictors) or from data that is artificially constrained. Always consider the context and other diagnostics.
How do I calculate R² in Excel?
Use the RSQ function: =RSQ(known_y's, known_x's). Or, after adding a trendline to a scatter plot, check "Display R-squared value on chart."
What is the relationship between R² and correlation coefficient r?
For simple linear regression, R² = r². The correlation r measures the strength and direction of the linear relationship, while R² measures the proportion of variance explained.