Let's say I have the following scenario. I have a data set that is split by groups. I want to fit two different models to each groups, but I want to constrain the coefficient to be the same between both models. Is there a way to do this. E.g. in the following
set.seed(100)
adf <- data.frame(x=runif(100,0,100))
adf$y <- rnorm(100, adf$x, 30)
lm1 <- lm(y ~ x, data=adf[1:50,])
lm2 <- lm(y ~ x, data=adf[51:100,])
Is there a way to constrain the y~x coefficient to be the same in both model fits?
I know, this is a weird problem - the final product is a bit wonkier, and necessitates this approach (I've explored interaction effects, and there are some properties there that aren't practical for what I'm trying to do).
Thanks!
lm1andlm2estimates three parameters: an intercept, a slope, and an error variance. Do you really want to constrain just the slopes to be equal or do you also want the error variances to be equal, too? BTW, this is a fairly common question. See https://stats.stackexchange.com/questions/12797, for instance (which appears to be essentially the same thing). – whuber Oct 28 '17 at 14:57