ThisCalculator

Everyday

Desmos Linear Regression: How to Find 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.

Desmos graphing calculator can compute a linear regression (a line of best fit) directly from a table of data you enter, giving you the slope, y-intercept, and correlation coefficient. You type or paste your x and y values into a table, then use the ~ (tilde) symbol to tell Desmos to fit a line to them. This article walks through the exact steps, explains the output, and covers common issues like missing r² values or data that doesn’t fit a straight line.

Formula

The linear regression line is written as y = mx + b, where:

  • m (slope) = [n·Σ(xy) − Σx·Σy] / [n·Σ(x²) − (Σx)²]
  • b (y-intercept) = [Σy − m·Σx] / n
  • r (correlation coefficient) = [n·Σ(xy) − Σx·Σy] / √([n·Σ(x²) − (Σx)²]·[n·Σ(y²) − (Σy)²])

Here, n is the number of data points, x and y are your variables, and Σ means “sum of.” R² is simply r squared, telling you how much of the variation in y is explained by x.

Worked Example

Using the default values x = 1,2,3,4,5 and y = 2,4,5,7,8:

  1. n = 5, Σx = 15, Σy = 26, Σ(xy) = 2+8+15+28+40 = 93, Σ(x²) = 1+4+9+16+25 = 55, Σ(y²) = 4+16+25+49+64 = 158.
  2. Slope m = (5·93 − 15·26) / (5·55 − 15²) = (465 − 390) / (275 − 225) = 75 / 50 = 1.5.
  3. Intercept b = (26 − 1.5·15) / 5 = (26 − 22.5) / 5 = 3.5 / 5 = 0.7.
  4. r = (5·93 − 15·26) / √((5·55 − 15²)·(5·158 − 26²)) = 75 / √(50·(790−676)) = 75 / √(50·114) = 75 / √5700 ≈ 75 / 75.498 ≈ 0.9934.
  5. R² = 0.9934² ≈ 0.9868, which matches the calculator output (rounded to 0.987).

So the regression line is y = 1.5x + 0.7.

How to Perform Linear Regression in Desmos

Desmos makes the process visual and interactive. Follow these steps:

  1. Go to desmos.com/calculator (or open the Desmos app).
  2. Click the “+” button and choose “Table.”
  3. Enter your x values in the first column (x1) and y values in the second (y1).
  4. Click on the “y1” header and type y1 ~ mx1 + b (using the tilde key, usually Shift+` on most keyboards).
  5. Desmos will instantly draw the line and show a results box with values for m, b, and r².
  6. If you want the line equation displayed, type y = mx + b in a new expression line, and Desmos will substitute the fitted values.

You can also use y1 ~ ax1 + b or y1 ~ a x1 + b – the variable letters don’t matter as long as they are consistent. For a quadratic fit, use y1 ~ a x1² + b x1 + c.

Reading the Results

The output box gives you three key numbers:

  • m – the slope: how much y changes for each one-unit increase in x.
  • b – the y-intercept: the predicted y value when x = 0.
  • r² – the coefficient of determination, ranging from 0 to 1. A value close to 1 means the line explains most of the variation; close to 0 means the line is a poor fit.

Desmos also shows the correlation coefficient r if you click on the “r²” value in the results box. A positive r means y increases with x; negative means it decreases. The sign of r is the same as the sign of the slope.

Common Problems and Fixes

  • r² doesn’t appear: Desmos only shows r² when you use the tilde (~) with a linear model (y1 ~ mx1 + b). If you use a plain equals sign, you’re just plotting a line, not fitting it.
  • “Cannot fit” error: This happens when your data has no variation in x (all x values identical) or when you have fewer than two distinct points. Add more varied data.
  • Negative r²: In a standard linear regression, r² is always between 0 and 1. If you see a negative value, you’re likely using a different model (like a log fit) or a custom function that isn’t a simple line.
  • Wrong slope due to reversed columns: Make sure x is in the first column and y in the second. If you swap them, you get the regression of x on y, which is a different line.
  • Outliers skewing the line: Desmos uses ordinary least squares, so extreme points pull the line toward them. Consider removing obvious errors or using a robust method if you have outliers.

When to Use Linear Regression (and When Not To)

Linear regression is appropriate when the relationship between two variables appears roughly straight. Check by looking at a scatter plot: if the points cluster around a line, a linear fit is reasonable. However, if the pattern is curved (e.g., exponential growth), a linear model will be misleading. In such cases, try a quadratic, exponential, or power fit in Desmos by changing the model expression (e.g., y1 ~ a·b^x1). Also, remember that correlation does not imply causation – a high r² only means the variables move together, not that one causes the other.

Frequently Asked Questions

How do I get the correlation coefficient r in Desmos?
After you fit a line, click on the “r²” value in the results box. Desmos will toggle to show r. Alternatively, you can type corr(x1,y1) in a new expression line to get r directly.

Can I do linear regression without a table?
No, you need data points. If you have summary statistics (like mean, standard deviation, and covariance), you could manually compute the slope using the formula, but Desmos requires the raw paired values.

Why does Desmos show r² but not r?
By default, Desmos displays r² because it’s more interpretable for goodness-of-fit. Clicking on the r² value toggles to r. If you want both, you can type corr(x1,y1) and corr(x1,y1)^2 in separate lines.

What does the tilde (~) do in Desmos?
The tilde tells Desmos to perform a regression. It means “is modeled by” – so y1 ~ mx1 + b reads as “y1 is modeled by a line with slope m and intercept b.” Without it, Desmos just plots the equation.

Can I use Desmos for multiple linear regression (two or more predictors)?
Yes. Add more columns to your table (x1, x2, y1) and type a model like y1 ~ a x1 + b x2 + c. Desmos will solve for the coefficients a, b, and c.

Is the Desmos regression the same as Excel’s LINEST?
Yes, both use the ordinary least squares method, so they give identical results for the same data (within rounding). The main difference is that Desmos is free, web-based, and more interactive.

Related

Formula used
in x | x values | - | 1, 2, 3, 4, 5 | 0
in y | y values | - | 2, 4, 5, 7, 8 | 0
out slope | Slope (m) | - | (count(x)*sum(x*y)-sum(x)*sum(y))/(count(x)*sum(x^2)-sum(x)^2) | 3
out intercept | Intercept (b) | - | (sum(y)-slope*sum(x))/count(x) | 3
out r2 | R² (coefficient of determination) | - | ( (count(x)*sum(x*y)-sum(x)*sum(y)) / sqrt((count(x)*sum(x^2)-sum(x)^2)*(count(x)*sum(y^2)-sum(y)^2)) )^2 | 3

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.