# Statistical test to check if data come from two different lines or just one

I have a very basic problem, can you give me some insights on how to solve it, or just some keywords to search online?

I have two sets of points in $$\mathbb{R}^2$$, let say $$S_1 = \{(x_i, y_i)\}_{i=1}^{N}$$ and $$S_2 = \{(x_i, y_i)\}_{i=1}^{M}$$, the points of both sets seem to have a linear relation:

$$y_i = mx_i + q$$

But my hypotesis is that they "come" from two different lines, so for example the line with parameters $$(m_1, q_1)$$ for $$S_1$$ and parameters $$(m_2, q_2)$$ for $$S_2$$.

How can I test if they actually come from two different lines (and there is statistical significance) or from the same one?

• Do you know which point $(x_i,y_i)$ belongs to which set? Moreover, are the slopes of the lines arbitrary and can be, e.g., vertical? May 19 at 16:07
• Yes I know to which set they belong. The slopes are not vertical. I have real data points, and visually on the scatter plot it seems the come from two slightly different lines. I would like to know if I can use a statistical tool to check if there is statistical significance in this observation May 19 at 16:39
• In this case the answer by @EdM solves this problem: if you use "treatment coding" (default in R) for the group label, then you can check whether the coefficients related to the group difference are different from zero; these are reported in the R summary of your model as SS2 for the intercept and x:S2 for the slope (look for stars at the end). May 20 at 13:15

This is a standard application of an interaction term in a model.

With x as continuous-valued predictors and y as your outcomes, let S represent which set a data point belongs to, with S1 specified as the reference value. In a standard symbolic representation of a linear model, you would write:

y ~ x + S + x:S


That model returns 4 coefficients:* an Intercept (your q for S1), a coefficient for x that represents the change in y per unit change in x for S1, a coefficient for S that represents the difference for set S2 from the Intercept when x = 0, and an interaction coefficient that is the extra difference for S2 (again, beyond what you predict for S1) for non-zero values of x.

Standard tests then indicate whether there is evidence that distinguishing S1 from S2 matters. This might be done by also fitting the simple model without considering set:

y ~ x


and comparing the two models with analysis of variance.

*This assumes that S1 is represented numerically as 0 in your model and S2 as 1. That's a typical coding of set, often done silently for you by statistical software.