Since you asked for it, here is how I understand your problem.
You have two groups of y values corresponding to identical x values. Here I assume that the relationship between y and x is linear. If it isn't you could transform your variables, use a non-linear model, an additive model, ...
First let's simulate some data since you don't provide any:
set.seed(42)
x <- 1:20
y1 <- 2.5 + 3 * x +rnorm(20)
y2 <- 4 + 2.5 * x +rnorm(20)
plot(y1~x, col="blue", ylab="y")
points(y2~x, col="red")
legend("topleft", legend=c("y1", "y2"), col=c("blue", "red"), pch=1)
Now, we want to know if the two samples differ. We can find out by fitting a model:
DF <- cbind(stack(cbind.data.frame(y1, y2)), x)
names(DF) <- c("y", "group", "x")
fit <- lm(y~x*group, data=DF)
summary(fit)
Call:
lm(formula = y ~ x * group, data = DF)
Residuals:
Min 1Q Median 3Q Max
-2.2585 -0.4603 -0.1899 0.9008 2.2127
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 3.51769 0.55148 6.379 2.17e-07 ***
x 2.92136 0.04604 63.457 < 2e-16 ***
groupy2 0.67218 0.77991 0.862 0.394
x:groupy2 -0.46525 0.06511 -7.146 2.11e-08 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 1.187 on 36 degrees of freedom
Multiple R-squared: 0.9949, Adjusted R-squared: 0.9945
F-statistic: 2333 on 3 and 36 DF, p-value: < 2.2e-16
The intercepts are not significantly different, but the slopes are. If group
is a significant effect, we can test best by comparing with a model that doesn't consider group
:
fit0 <- lm(y~x, data=DF)
anova(fit0, fit)
Analysis of Variance Table
Model 1: y ~ x
Model 2: y ~ x * group
Res.Df RSS Df Sum of Sq F Pr(>F)
1 38 300.196
2 36 50.738 2 249.46 88.498 1.267e-14 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
As you see, the samples are different.