ThisCalculator

Everyday

Desmos Linear Regression: How to Use It

Updated · Standard formula, shown below

The calculator for this page is switched off while it is checked. The formula is below, and the worked example in the article shows how to use it by hand.

This calculator performs linear regression on a set of (x, y) data points you provide, returning the slope, intercept, correlation coefficient (r), and coefficient of determination (R²). You enter your data as two comma-separated lists (x-values and y-values), and the calculator uses the least-squares method to fit the line y = mx + b. The result gives you the best-fit equation and a measure of how well the line explains the data.

Formula

The linear regression line is written as y = mx + b, where m is the slope and b is the y-intercept. The slope is calculated using the least-squares method:

m = [n·Σ(xy) − Σx·Σy] / [n·Σ(x²) − (Σx)²]

b = [Σy − m·Σx] / n

The correlation coefficient r measures the strength and direction of the linear relationship, ranging from −1 to +1. Its formula is:

r = [n·Σ(xy) − Σx·Σy] / √{[n·Σ(x²) − (Σx)²]·[n·Σ(y²) − (Σy)²]}

Here, n is the number of data points, Σ means “sum of,” x and y are your data values. The coefficient of determination R² is simply r², and it tells you what fraction of the variance in y is explained by x.

Worked Example

Using the default data: x = 1, 2, 3, 4, 5 and y = 2, 4, 5, 4, 5.

  1. Calculate the sums: Σx = 15, Σy = 20, Σxy = (1·2)+(2·4)+(3·5)+(4·4)+(5·5) = 2+8+15+16+25 = 66
  2. Σx² = 1+4+9+16+25 = 55, Σy² = 4+16+25+16+25 = 86
  3. Slope: m = [5·66 − 15·20] / [5·55 − 15²] = (330 − 300) / (275 − 225) = 30 / 50 = 0.6
  4. Intercept: b = (20 − 0.6·15) / 5 = (20 − 9) / 5 = 11 / 5 = 2.2
  5. Correlation: r = [5·66 − 15·20] / √[(5·55 − 15²)·(5·86 − 20²)] = 30 / √(50·30) = 30 / √1500 ≈ 0.7746
  6. R² = 0.7746² ≈ 0.6

The regression line is y = 0.6x + 2.2, with r ≈ 0.77 and R² = 0.60.

How to Use the Calculator

Enter your x-values and y-values as comma-separated lists in the two input fields. The lists must have the same length. The calculator then computes the slope, intercept, r, and R². For example, to fit a line to the points (0,1), (2,3), (4,5), enter x = 0,2,4 and y = 1,3,5. The result will be a perfect line with slope 1 and intercept 1.

This tool is ideal for quick checks when you don’t have a graphing calculator handy. It works for any set of numerical data, whether you’re analyzing lab measurements, survey results, or stock prices.

When to Use Linear Regression

Use linear regression when you suspect a straight-line relationship between two variables. Common examples include:

  • Predicting sales based on advertising spend
  • Estimating a person’s weight from height
  • Trending temperature over time

Before applying linear regression, plot your data to see if a straight line is reasonable. If the scatter plot shows a curve, a polynomial or exponential model might be better. Also, be aware that outliers can heavily influence the least-squares line, so check for extreme points.

Common Mistakes and Misconceptions

One frequent error is mixing up x and y. The independent variable (the one you control or measure first) goes in x, and the dependent variable goes in y. Swapping them changes the slope and intercept.

Another misconception is that a high R² means the line is a good predictor. R² only measures how well the line fits the data; it doesn’t prove causation. Also, correlation does not imply causation—two variables can be strongly correlated by coincidence or due to a third factor.

Rounding too early can also cause inaccuracies. Keep several decimal places during intermediate steps and round only the final answer.

Limitations of Linear Regression

Linear regression assumes a linear relationship, independence of errors, constant variance of errors, and normal distribution of errors. If these assumptions are violated, the results may be misleading. For example, if your data has a curved pattern, a straight line will underfit and give poor predictions.

Extrapolation—predicting beyond the range of your data—is risky. The line may not hold outside the observed x-values. Also, if your sample size is very small (e.g., 3 points), the regression line is highly sensitive to each point.

Comparison with Other Regression Types

Linear regression is the simplest form. If your data shows a curve, you might try polynomial regression (e.g., quadratic y = ax² + bx + c), exponential (y = a·e^(bx)), or logarithmic (y = a + b·ln x). The Desmos graphing calculator supports these through its built-in regression feature, but this tool focuses on linear only.

For multiple independent variables, you would need multiple linear regression, which this calculator does not handle. That requires matrix operations or specialized software.

Frequently Asked Questions

How do I perform linear regression on Desmos?

In Desmos, you can type a table of points and then add a line like y1 ~ mx1 + b. Desmos will automatically calculate m and b using least squares. This calculator replicates that functionality in a simpler form.

What does the correlation coefficient r tell me?

r ranges from −1 to 1. A value close to 1 or −1 indicates a strong linear relationship; 0 means no linear relationship. The sign tells you the direction: positive means y increases as x increases, negative means the opposite.

What is R² and why is it useful?

R² is the square of r, ranging from 0 to 1. It represents the proportion of variance in y explained by x. For example, R² = 0.60 means 60% of the variation in y is explained by the linear relationship with x.

Can I use this calculator for non-linear data?

No, it only fits a straight line. If your data is curved, the line will be a poor fit. Consider transforming your data (e.g., taking logs) or using a different model.

What if my x and y lists have different lengths?

The calculator will not produce a result. Make sure both lists have the same number of values. If you have missing data, you may need to omit those pairs.

How do I interpret the slope and intercept?

The slope (m) tells you how much y changes for a one-unit increase in x. The intercept (b) is the predicted y value when x = 0. In the example, slope 0.6 means y increases by 0.6 for each unit increase in x, and when x=0, y is 2.2.

Related

Formula used
in x | X values (comma-separated) | - | 1,2,3,4,5 | 0
in y | Y values (comma-separated) | - | 2,4,5,4,5 | 0
out slope | Slope (m) | - | (n*sum(x*y)-sum(x)*sum(y))/(n*sum(x^2)-sum(x)^2) | 4
out intercept | Intercept (b) | - | (sum(y)-slope*sum(x))/n | 4
out r | Correlation coefficient (r) | - | (n*sum(x*y)-sum(x)*sum(y))/sqrt((n*sum(x^2)-sum(x)^2)*(n*sum(y^2)-sum(y)^2)) | 4
out r2 | R-squared (R²) | - | r^2 | 4

Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.