# Given a set of data, how can I calculate the equation of the mean line that passes through the data?

I have a set of data that I gathered doing some tests. I plotted it and created a scatter chart. I would like to find the equation of the mean line that goes through these points. How can I do this? I remember going over this in my college stats class but have forgotten. Also it would be great if you could refer me to a tool that I could just copy and paste my data out of google docs and have it do it for me.

• Could you please tell us a little bit about these data and the tests? Your answer will determine whether the responses you are getting will be appropriate and useful or inappropriate and misleading.
– whuber
Commented Aug 14, 2012 at 11:53

If you plotted a scatter chart, you have two numeric (not categorical) variables. That means you can (try to) fit a linear regression to these data. Designate one of the variables as the response, and one as the predictor (or dependent and independent, respectively). The intercept and coefficient of the predictor in the regression will be the intercept and slope of the "mean line" that you're looking for.

If your response is $y$ and your predictor is $x$, then this means you're fitting $y=b_0+b_1*x$. $b_0$ is the intercept and $b_1$ is the slope.

For example, in R:

x <- rnorm(100)             # predictor: 100 random numbers, mean=0 and SD=1
y <- 5 + x / 3 + rnorm(100) # response: transformation of x with some noise
lm(y ~ x)                   # fit the regression

Coefficients:
(Intercept)            x
5.0015       0.2969


This means your line has an intercept 5.0015 and a slope 0.2969.

The danger with this approach is it makes all the standard regression assumptions, including that your data are IID and normally distributed.

• The assumption of normallity is made on the errors, not on the data (as illustrated in your simulation).
– user10525
Commented Aug 13, 2012 at 10:44
• +1, this is a nice answer: simple, clean & provides some guidance on how to accomplish this task. NB, in addition to @Procrastinator's comment, the normality of the errors only matter for the validity of the statistical tests. It appears the OP isn't interested in that, & you haven't shown it in your code (appropriately), so it seems to be wholly irrelevant here. Even homoscedasticity probably doesn't matter, as that pertains to efficiency / the variance of the sampling distributions of the betas, but they are unbiased nonetheless. Commented Aug 13, 2012 at 13:19
• How should the OP decide which variable is $x$ and which is $y$? It's an important decision, because the regression line depends on it.
– whuber
Commented Aug 13, 2012 at 21:14
• @whuber Important, but perhaps outside of the scope of this question. OP, please open a new question if deciding what is $x$ and what is $y$ is unclear. Commented Aug 14, 2012 at 5:12
• Jack, I am trying to suggest this is not outside the scope: indeed, it the crux of the matter. If the data collected are just ordered pairs of observations, then choosing either one as a dependent variable is not a good idea and a different technique (such as PCA or errors in variables regression) is appropriate, whereas OLS is not. This doesn't need a new question: it needs clarification of the current question.
– whuber
Commented Aug 14, 2012 at 11:52

The approach is called linear regression or ordinary linear regression. It can involve one or more predictors and a single response. It is one of the most extensively studied and taught subject is statistics. The most common estimation method is called ordinary least squares. When the model residuals are very different from the normal distribution, robust estimation procedures have been proposed.