Linear regression calculator (line of best fit)

Linear regression calculator: least-squares line of best fit with R², standard errors, p-values and predictions, plus polynomial and exponential fits.

Обновлено Проверенные примеры: 5

Weekly study hours (default example).
Exam scores, in the same order as x.
Попробовать
Fitted equation
y = 48.890909 + 3.109091x
Fitted equation: y = 48.890909 + 3.109091x
Intercept b₀
48.890909
Slope / x coefficient b₁
3.109091
Correlation r
0.99662
R²
0.993252
Adjusted R²
0.992408
Residual standard error
0.822966
Standard error of b₀
0.643873
Standard error of b₁
0.090606
Predicted y
72.209091
Points n
10

Each extra unit of x goes with a change of 3.1091 in y on average, and the line accounts for 99.33% of the variation in y. At x = 7.5 the fitted value is 72.2091.

Data and fitted curve

607080246810xyŷ = 72.2091
Datay = 48.890909 + 3.109091x

Residuals

-2-1012246810xObserved − fitted
ResidualZero
Coefficients (t tests with 8 degrees of freedom) Строк: 2
TermCoefficientStandard errortp-value
Intercept48.89090.643975.93251.008 × 10⁻¹²
x3.10910.090634.31465.686 × 10⁻¹⁰
Как выполняется расчёт S
  1. Means

    xˉ=6.5,yˉ=69.1\bar x = 6.5,\quad \bar y = 69.1
  2. Sums of squares

    Sxx=∑(xi−xˉ)2=82.5,Sxy=∑(xi−xˉ)(yi−yˉ)=256.5S_{xx} = \sum (x_i-\bar x)^2 = 82.5,\quad S_{xy} = \sum (x_i-\bar x)(y_i-\bar y) = 256.5
  3. Slope

    b1=SxySxx=256.582.5=3.109091b_1 = \frac{S_{xy}}{S_{xx}} = \frac{256.5}{82.5} = 3.109091
  4. Intercept

    b0=yˉ−b1xˉ=69.1−3.109091×6.5=48.890909b_0 = \bar y - b_1 \bar x = 69.1 - 3.109091 \times 6.5 = 48.890909
  5. Goodness of fit

    R2=1−SSESST=1−5.418182802.9=0.993252,s=SSEn−2=0.822966R^2 = 1 - \frac{SSE}{SST} = 1 - \frac{5.418182}{802.9} = 0.993252,\quad s = \sqrt{\frac{SSE}{n - 2}} = 0.822966
  6. Prediction

    y^(7.5)=72.209091\hat y(7.5) = 72.209091

О калькуляторе: Linear regression calculator (line of best fit)

Least squares picks the line or curve that makes the sum of squared vertical distances from the points as small as possible. For a straight line the slope is b₁ = Sxy/Sxx and the intercept b₀ = ȳ − b₁x̄; polynomial fits of degree 2 to 4 solve the normal equations, and the exponential model fits a straight line to ln y. Each coefficient gets a standard error, a t statistic and a p-value, and R² gives the share of the variation in y that the fit accounts for.

The default relates weekly study hours to exam scores for 10 students. The fitted line is y = 48.89 + 3.109x, so each extra hour goes with about 3.1 more points, R² is 0.9933, and at 7.5 hours the line predicts 72.2.

The standard errors and p-values assume independent residuals with constant spread. A curved or funnel-shaped residual plot means the model is missing something, and predictions outside the range of the x data are flagged as extrapolations.

Примеры с решением

Exact straight line y = 1 + 2x

x values
1, 2, 3
y values
3, 5, 7
Model
Linear
Predict y at x
4
Fitted equation
y = 1 + 2x
Intercept b₀
1
Slope / x coefficient b₁
2
R²
1
Predicted y
9

Источник проверки: Constructed points (1,3), (2,5), (3,7) lie exactly on y = 1 + 2x; at x = 4, y = 9.

Study hours vs score (linear)

x values
2, 3, 4, 5, 6, 7, 8, 9, 10, 11
y values
55, 58, 62, 64, 68, 71, 72, 78, 80, 83
Model
Linear
Predict y at x
7.5
Slope / x coefficient b₁
3.109091
Intercept b₀
48.890909
R²
0.993252
Adjusted R²
0.992408
Residual standard error
0.822966
Standard error of b₁
0.090606
Standard error of b₀
0.643873
Predicted y
72.209091
Correlation r
0.99662

Источник проверки: Python fractions: b₁ = Sxy/Sxx, b₀ = ȳ − b₁x̄, se = √(SSE/(n − 2)) with decimal square roots

Quadratic trend

x values
1, 2, 3, 4, 5, 6, 7, 8
y values
2.1, 3.9, 8.2, 14.8, 24.1, 35.9, 50.2, 66.8
Model
Polynomial
Degree
2 (quadratic)
Intercept b₀
2.85
Slope / x coefficient b₁
-1.980952
x² coefficient b₂
1.247619
R²
0.999994
Residual standard error
0.069007

Источник проверки: Python fractions solving the 3×3 normal equations exactly (b₀ = 57/20, b₁ = −208/105, b₂ = 131/105); agrees with numpy.polyfit(x, y, 2)

Exact quartic y = 1 + x + x² + x³ + x⁴

x values
0 1 2 3 4 5 6 7 8 9 10
y values
1 5 31 121 341 781 1555 2801 4681 7381 11111
Model
Polynomial
Degree
4 (quartic)
Predict y at x
11
Intercept b₀
1
Slope / x coefficient b₁
1
x² coefficient b₂
1
x³ coefficient b₃
1
x⁴ coefficient b₄
1
R²
1
Predicted y
16,105

Источник проверки: Constructed data: y = 1 + x + x² + x³ + x⁴ exactly, so every coefficient is 1 and R² = 1; 1 + 11 + 121 + 1331 + 14641 = 16105

Вопросы

How do you calculate the line of best fit?

Find the means x̄ and ȳ, then the slope b₁ = Σ(x − x̄)(y − ȳ) / Σ(x − x̄)² and the intercept b₀ = ȳ − b₁x̄. For the default study-hours data, Sxy = 256.5 and Sxx = 82.5, so b₁ = 3.109 and b₀ = 48.89. This least-squares line minimizes the sum of squared vertical distances, as set out in the NIST/SEMATECH e-Handbook (§4.1.4.1).

What does R² tell you in regression?

R² is the fraction of the variation in y that the model accounts for: R² = 1 − SSE/SST. The default fit has R² = 0.9933, so study hours account for 99.3% of the spread in exam scores. For a straight line R² equals the square of the correlation r. A high R² does not show that x causes y, that the model has the right shape, or that predictions beyond the data will hold.

What is the difference between R² and adjusted R²?

Adjusted R² charges for each extra coefficient: 1 − (1 − R²)(n − 1)/(n − k), where k counts the coefficients including the intercept. R² never falls when a term is added, so a quartic always fits at least as well as a quadratic; adjusted R² falls when the new term explains less than it costs. For the default data, n = 10 and k = 2 turn R² = 0.99325 into 0.99241.

What does the p-value of the slope mean?

It tests the null hypothesis that the true slope is 0, using t = b₁/SE(b₁) with n − 2 degrees of freedom. For the default data t = 3.109/0.0906 = 34.3 with 8 df, giving p = 5.7 × 10⁻¹⁰: a slope this steep would be very unlikely if hours and scores had no linear relation. The p-value says nothing about how large or important the effect is.

When should I use an exponential fit instead of a straight line?

When y grows or shrinks by a roughly constant percentage for each unit of x, so that ln y against x is straight. The exponential example, with y rising from 3.0 to 22.1 as x goes from 0 to 5, fits y = 2.98·e^(0.4007x), an increase of 49.3% per unit of x. The fit is made on ln y, as Excel's LOGEST function does, so R² describes ln y rather than y.

Насколько точен «Linear regression calculator (line of best fit)»?

Точность зависит от введённых данных и допущений метода. Десятичная арифметика использует 50 значащих цифр, но оценки, численные методы и исходные данные могут быть менее точными; округление на экране не устраняет эти ограничения. Решённые примеры, проверенные по независимым источникам: 5. Например, «Exact straight line y = 1 + 2x» проверяется по источнику Constructed points (1,3), (2,5), (3,7) lie exactly on y = 1 + 2x; at x = 4, y = 9.

Откуда взята методика?

NIST/SEMATECH e-Handbook of Statistical Methods, §4.1.4.1 Linear least squares regression; NIST Statistical Reference Datasets — linear regression; Draper & Smith, Applied Regression Analysis, 3rd ed. (Wiley, 1998), ch. 1 and 12.

Об этом калькуляторе

b1=∑(xi−xˉ)(yi−yˉ)∑(xi−xˉ)2,b0=yˉ−b1xˉ,R2=1−∑(yi−y^i)2∑(yi−yˉ)2b_1 = \frac{\sum (x_i-\bar x)(y_i-\bar y)}{\sum (x_i-\bar x)^2},\quad b_0 = \bar y - b_1\bar x,\quad R^2 = 1 - \frac{\sum (y_i-\hat y_i)^2}{\sum (y_i-\bar y)^2}

Источники

  1. NIST/SEMATECH e-Handbook of Statistical Methods, §4.1.4.1 Linear least squares regression
  2. NIST Statistical Reference Datasets — linear regression
  3. Draper & Smith, Applied Regression Analysis, 3rd ed. (Wiley, 1998), ch. 1 and 12

Проверено по источникам

В калькулятор включены решённые примеры с ответами из независимых источников. Их количество: 5. Они входят в набор тестов, и вы также можете запустить их здесь.

Похожие калькуляторы