0

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!

whuber
  • 281,159
  • 54
  • 637
  • 1,101
jebyrnes
  • 835
  • 6
  • 16
  • Why not just fit a model with x and an indicator for which half of the data-set the observations are in? – mdewey Oct 28 '17 at 13:43
  • 1
    You want to fit two different models, but want to constrain the coefficient to be the same between both models. So what are you allowing to be different between the models (i.e., makes them different models)? – Mark L. Stone Oct 28 '17 at 14:22
  • I know, I told you this is an odd question. So, there might be no answer. What I would like to do is fit the same model to two different data sets but constrain the coefficient to be the same between them. Might not be possible... – jebyrnes Oct 28 '17 at 14:57
  • Just to make explicit what @Mark is getting at: each of these models `lm1` and `lm2` estimates *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
  • e.g., looking at the r^2 for both models fit to different data sets. – jebyrnes Oct 28 '17 at 14:58

2 Answers2

0

Sorry if I didn't understood the idea, but to me this looks exactly like a simple linear regression (eg. y ~ x). You can have a column, or indicator, showing in which group the data is, but this doesn't seem to change anything.

Bruna w
  • 513
  • 2
  • 9
0

Thanks for the discussion. For the above example, in my own work, I've been convinced that the answer is to approach this using gls in nlme in order to accommodate differences in error.

library(nlme)

adf$group <- c(rep("a", 50), rep("b", 50))
uncons <- gls(y ~ x*group, data=adf,
              weights = varIdent(form = ~ 1| group))

cons <- gls(y ~ x, data=adf,
            weights = varIdent(form = ~ 1| group))

the results of uncons are the same as the linear regressions above. It's a bit of a pain to look at the different residual standard deviations (you have to multiple a scaling factor by a residual), but doing it by hand, it all works out.

Thanks!

jebyrnes
  • 835
  • 6
  • 16