# Calculating uncertainty in gradient of a slope

I have a collection of datapoints, each with different and known errors. I then fitted a line of best fit for the data and calculated its gradient. How can I use these to calculate the error in the gradient for the fitted slope?

My intuition told me that since the datapoints each make an equal contribution to the overall error, that I should average their percentage errors for the gradient percentage error, but I'm not sure if this is correct.

It's a common misconception that the contribution of every datapoint is the same. It's simply not true. That's why we look at things like the "leverage of a fit" or "cook's distance".

There are mathematical formulas how to write the standard deviation of the fit parameters. These formulas are not complicated and you can google them by looking for "uncertainty of linear regression". Maybe use "design matrix" as well. However, (1) they are not self-explanatory, if I would post them here, and (2) nobody does these calculations by hand, so don't waist your time. Instead use some software to help you.

Here is an example in R, which is a free software specialised on statistics:

## Generate the dataset: You should past your data here
x = seq(1, 10)
y = x + rnorm(length(x))

## plot the data:
plot(x,y)

## fit the data:
df = data.frame(x=x, y=y)
lm.out = lm(y ~ x , df)
# print the result of the fit:
summary(lm.out)
# check the fit assumptions:
plot(lm.out)

## fancy way for plotting the result:
library(ggplot2)
ggplot(df, aes(x,y)) +
geom_point(color="blue") +
geom_smooth(method='lm', formula= y~x)


The data and the fit look like this:

The result of the fit is as follows:

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.46300    0.59173  -0.782    0.456
x            1.00680    0.09537  10.557 5.65e-06 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.8662 on 8 degrees of freedom
Multiple R-squared:  0.933, Adjusted R-squared:  0.9247
F-statistic: 111.5 on 1 and 8 DF,  p-value: 5.654e-06


which states that the slope is 1.00680 and its uncertainty (=standard deviation) is 0.09537.

For a two parameter (linear) fit of a data set $$(x_i, y_i, \sigma_i)$$:

$$y = m x + b$$

you compute the total chi-squared:

$$\chi^2(m, b)= \sum_i{\frac{[y_i - (mx_i +b)]^2}{\sigma_i^2}}$$

The best fit parameters, $$(\bar m,\bar b)$$, minimize chi-squared:

$$\chi^2_{min} = \chi^2(\bar m, \bar b)$$

From there, you can define a region where in $$(m, b)$$ space where:

$$\chi^2(m, b) \le \chi^2_{min} + 1$$

That region is you 1-sigma error region. If it's a proper ellipse with major/minor axes aligned with the $$m$$ and $$b$$ axes, then the fit parameters are uncorrelated and $$\sigma_m$$ and $$\sigma_b$$ are the semi-axes.

If it is tilted, you have correlated fit parameters. This technique works for any fit function, it's just that with a linear function you can compute the solution using linear regression.

take the max of all points , do the best fit, then take the min of all points, do the best fit. Now you have 3 slopes, the measured, the max and the min. The max and min slopes are the errors of the measured slope

• This is a horrible technique and will give wrong results. Such methods used to be used as a shortcut in the days before calculators, let alone computers. There are standard statistical formulae and software as described in the other answers. Commented Apr 28, 2021 at 19:22