Reputation: 33
We have two data set (X1,Y1) and (X2,Y2). If they seem to have different intercepts and different slopes, how can I use a single linear model to draw two fitted lines? At the same time, what's the difference between using the same model to fit two different lines and using two different linear models?
Upvotes: 1
Views: 4309
Reputation: 4807
You simply need to add a "dummy" variable that distinguishes between the two data sets. A dummy variable is something that is either 1 or 0. Multiple regressions w/ predictor variables are categorical often use dummy variables – look at the model.matrix
the next time you do one, you'll see. Any way, this should do what you want:
x1 <- rnorm(100)
y1 <- x1*0.5 + 2 + rnorm(100, sd=0.001)
x2 <- rnorm(100)
y2 <- x2*0.25 + 2.5 + rnorm(100, sd=0.001)
x3 <- c(rep(0, length(x1)), rep(1, length(x2))) # dummy variable indicating origin of data
data <- data.frame(dummy=x3, x=c(x1,x2), y=c(y1,y2))
(model.out1 <- lm(y1~x1))
(model.out2 <- lm(y2~x2))
(model.out3 <- lm(y~x*dummy, data=data))
pred.model3.1 <- predict(model.out3, newdata=data[x3==0,]) # using the "combined" model, but providing "newdata" for the regression line corresponding to x1 an y1
pred.model3.2 <- predict(model.out3, newdata=data[x3==1,]) # same as above, but for x2 and y2
plot(data[,c("x","y")])
lines(data[x3==0,"x"], pred.model3.1, col="red")
lines(data[x3==1,"x"], pred.model3.2, col="blue")
legend("topleft", title="All from combined model", legend=c("x1 y1", "x2 y2"), col=c("red","blue"), lty=1)
Edit: I forgot to answer the statistical question. If you want a more detailed answer for this aspect, I suggest checking crossvalidated. But for completeness, the main difference between doing the two separate regressions and doing the combined regression with a dummy variable is that in the second case there is only 1 error term (so there is only 1 residual variance), and there are also parameters describing the differences between the relationships. So they're different because of the interaction and only having 1 error. However, with regard to the slopes and intercepts, those are pretty much identical.
Upvotes: 0