I would like to simulate data for a regression analysis, which involves a dummary variable. While a regression recovers the slopes, it does not recover the intercept:
beta <- c(2,3,4)
x1 <- rnorm(100,50,5)
x2 <- sample(c(0,1), replace=T,100)
eps <- rnorm(100, 0, 5)
y <- beta[1] + beta[2]*x1 + beta[3]*x2 + eps
summary(lm(y~x1 + x2))
Call:
lm(formula = y ~ x1 + x2)
Residuals:
Min 1Q Median 3Q Max
-12.6598 -2.7433 -0.2873 2.4616 13.2250
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -8.2858 5.3470 -1.550 0.124
x1 3.2216 0.1070 30.109 < 2e-16 ***
x2 3.9209 0.9065 4.325 3.7e-05 ***
I know the dummy variable shifts the intercept up or down, but I'm confused about what adjustments to make to create a data set that can recover the intercept. Any suggestions are greatly appreciated, thank you.