I'm estimating a basic OLS model, y ~ x
, and I want to know the difference in the slope between two groups. Here is a simulated example in R:
set.seed(123)
x <- runif(50, 0, 10)
slope_a <- 1
slope_b <- 3
df <- data.frame(
x = x,
y = c(slope_a*x + rnorm(50), slope_b*x + rnorm(50)),
group = rep(c('a', ''), each = 50)
)
m1 <- lm(y ~ x*group, data = df)
coef(m1)
#> (Intercept) x groupa x:groupa
#> -0.15881145 3.03108969 0.01645874 -1.99291523
This correctly recovers the slope parameters for each group, and the intercept is indeed close to 0.
Since the true intercepts are indeed 0, an alternative approach is to compute the slope at each point and regress a model on the slopes as the y variable. In my simulation I am again able to recover the slope parameters:
df$slope <- df$y / df$x
m2 <- lm(slope ~ group, data = df)
coef(m2)
#> (Intercept) groupa
#> 3.069613 -2.091512
My question is when (if ever) is this approach valid? The second approach is essentially forcing the intercept to go through 0, whereas the first is not. So obviously if the true intercept is not 0, then the second approach would be invalid. Am I missing something else important here?
x
. And because the two models are different, as you note, then how would one determine which is "valid"? At most one can be and assessing its validity would depend on the circumstances. $\endgroup$