Here's a simple example that illustrates my question:
df <- data.frame(y = rnorm(10), x = rnorm(10), z = sample(c("a","b"), size = 10, replace = TRUE))
Using the *
operator gives me a regression of y on 1, 1[z = b], x, 1[z=b]x.
> lm(data = df, y ~ as.factor(z)*x)
Call:
lm(formula = y ~ as.factor(z) * x, data = df)
Coefficients:
(Intercept) as.factor(z)b x as.factor(z)b:x
-0.2351 0.1524 0.2309 -0.2699
I would like to regress y on 1[z = a], 1[z=a]x, 1[z=b], 1[z=b]x (with no constant term). This regression will produce the same fitted values as the one above, but the interpretation of the coefficients is different, and preferable in some cases. How can I specify the formula to do this in a single regression?