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!